From 4452d0a1bf806dac4150e3a3b9f966d9782f6432 Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Fri, 23 Oct 2026 12:55:19 +0100 Subject: [PATCH] fix(app): schedules page follows the connection and its list reads --- .../application/schedules_destination.dart | 29 +++++++- .../schedules_destination_test.dart | 71 +++++++++++++++++++ 2 files changed, 97 insertions(+), 3 deletions(-) create mode 100644 app/test/application/schedules_destination_test.dart diff --git a/app/lib/application/schedules_destination.dart b/app/lib/application/schedules_destination.dart index 4ced0e0e..c92bf8c3 100644 --- a/app/lib/application/schedules_destination.dart +++ b/app/lib/application/schedules_destination.dart @@ -38,18 +38,38 @@ class _SchedulesDestinationState extends State { final TextEditingController _search = TextEditingController(); final ScrollController _scroll = ScrollController(); + int _epoch = 0; + @override void initState() { super.initState(); _controller = SchedulesController( repository: SchedulesRepository(widget.session.gateway), ); + _epoch = widget.session.connectionEpoch; + widget.session.addListener(_sessionChanged); unawaited(_controller.refresh()); } + /// A reconnect re-reads the list once. A read still in flight (the first + /// one, queued until the socket opened) already covers the new connection. + void _sessionChanged() { + final epoch = widget.session.connectionEpoch; + if (epoch == _epoch) return; + _epoch = epoch; + if (widget.session.connected && !_controller.isLoading) { + unawaited(_controller.refresh()); + } + } + @override void didUpdateWidget(SchedulesDestination oldWidget) { super.didUpdateWidget(oldWidget); + if (oldWidget.session != widget.session) { + oldWidget.session.removeListener(_sessionChanged); + widget.session.addListener(_sessionChanged); + _epoch = widget.session.connectionEpoch; + } if (oldWidget.session.gateway != widget.session.gateway) { // A different server lists different schedules; re-read through the same // controller so the screen is never shown with the old server's rows. @@ -59,6 +79,7 @@ class _SchedulesDestinationState extends State { @override void dispose() { + widget.session.removeListener(_sessionChanged); _controller.dispose(); _search.dispose(); _scroll.dispose(); @@ -68,10 +89,12 @@ class _SchedulesDestinationState extends State { @override Widget build(BuildContext context) { final cupertino = usesCupertinoHost(context); - final page = _page(cupertino); + // The page is built inside the builder from the session's connection and + // the list controller's state; the list view itself does not listen, so a + // finished read would otherwise stay on `Loading schedules…`. return AnimatedBuilder( - animation: widget.session, - builder: (context, _) => page, + animation: Listenable.merge([widget.session, _controller]), + builder: (context, _) => _page(cupertino), ); } diff --git a/app/test/application/schedules_destination_test.dart b/app/test/application/schedules_destination_test.dart new file mode 100644 index 00000000..b919db30 --- /dev/null +++ b/app/test/application/schedules_destination_test.dart @@ -0,0 +1,71 @@ +import 'dart:convert'; + +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/application/app_session.dart'; +import 'package:pipelines_app/application/schedules_destination.dart'; +import 'package:pipelines_app/application/server_endpoint.dart'; +import 'package:pipelines_app/core/transport/pipelines_client.dart'; + +import '../core/transport/pipelines_client_test.dart' show FakeChannel; + +void main() { + testWidgets('a page opened before the connection lists once connected', ( + tester, + ) async { + final channel = FakeChannel(); + final client = PipelinesClient(channelFactory: (_) => channel); + final server = ServerEndpoint.resolve( + base: Uri.parse('http://127.0.0.1:8096/'), + web: true, + ); + final session = AppSession(endpoint: server.uri, client: client); + addTearDown(session.dispose); + + await tester.pumpWidget( + MaterialApp( + home: SchedulesDestination( + session: session, + server: server, + onBack: () {}, + onOpenPipeline: (_) {}, + ), + ), + ); + await tester.pump(); + expect( + find.text('Connect to the server to see schedules.'), + findsOneWidget, + ); + + final starting = session.start(); + channel.readyCompleter.complete(); + await starting; + await tester.pump(); + await tester.pump(); + expect(find.text('Connect to the server to see schedules.'), findsNothing); + + final request = channel.outgoing + .map((frame) => jsonDecode(frame as String) as Map) + .lastWhere((frame) => frame['type'] == 'list_schedules'); + channel.incoming.add( + jsonEncode({ + 'id': request['id'], + 'type': 'schedules', + 'schedules': [ + { + 'id': 1, + 'pipeline_id': 4, + 'version': 1, + 'name': 'Nightly review', + 'enabled': true, + 'rule': {'kind': 'interval', 'every_seconds': 3600}, + }, + ], + }), + ); + await tester.pump(); + await tester.pump(); + expect(find.text('Nightly review'), findsOneWidget); + }); +}