diff --git a/app/lib/features/chat/ui/sessions/chat_sessions_controller.dart b/app/lib/features/chat/ui/sessions/chat_sessions_controller.dart index d5dba163..f4de453a 100644 --- a/app/lib/features/chat/ui/sessions/chat_sessions_controller.dart +++ b/app/lib/features/chat/ui/sessions/chat_sessions_controller.dart @@ -137,14 +137,23 @@ class ChatSessionsController extends ChangeNotifier { } } - Future star(String sessionId, bool starred) => - _mutate(sessionId, () => repository.star(sessionId, starred)); + Future star(String sessionId, bool starred) => _mutate( + sessionId, + () => repository.star(sessionId, starred), + patch: {'starred': starred}, + ); - Future close(String sessionId) => - _mutate(sessionId, () => repository.close(sessionId)); + Future close(String sessionId) => _mutate( + sessionId, + () => repository.close(sessionId), + patch: {'closed_at': DateTime.now().toUtc().toIso8601String()}, + ); - Future reopen(String sessionId) => - _mutate(sessionId, () => repository.reopen(sessionId)); + Future reopen(String sessionId) => _mutate( + sessionId, + () => repository.reopen(sessionId), + patch: {'closed_at': null}, + ); /// Renames through the shared server field. Throws the server's refusal so the /// open dialog can keep it in view; success refreshes the list from the server. @@ -241,15 +250,29 @@ class ChatSessionsController extends ChangeNotifier { _notify(); } + /// Applies [patch] to the row at once, so the list answers the tap + /// immediately, then saves it. The server's refusal puts the row back and + /// shows the reason; success re-reads the list in the background. Future _mutate( String sessionId, - Future Function() action, - ) async { + Future Function() action, { + JsonMap? patch, + }) async { + final before = _sessions; + if (patch != null) { + _sessions = [ + for (final row in _sessions) + if ('${row['id'] ?? ''}' == sessionId) {...row, ...patch} else row, + ]; + _actionErrors.remove(sessionId); + _notify(); + } try { await action(); _actionErrors.remove(sessionId); - await refresh(); + unawaited(refresh()); } catch (error) { + if (patch != null) _sessions = before; _actionErrors[sessionId] = '$error'; _notify(); } diff --git a/app/lib/shell/detail_page.dart b/app/lib/shell/detail_page.dart index 710dd30d..8f69f615 100644 --- a/app/lib/shell/detail_page.dart +++ b/app/lib/shell/detail_page.dart @@ -54,7 +54,9 @@ class DetailPageHost { Navigator.of(context).push(route).then((_) { if (_route != route) return; _route = null; - onClosed(); + // Rebuild what lies underneath (route, tab bar, list) once the + // page has finished sliding away, not during its animation. + _afterExit(route, onClosed); }); }); } else if (!selected && _route != null) { @@ -66,6 +68,21 @@ class DetailPageHost { } } + static void _afterExit(Route route, VoidCallback onClosed) { + final animation = route is ModalRoute ? route.animation : null; + if (animation == null || animation.status == AnimationStatus.dismissed) { + onClosed(); + return; + } + late final AnimationStatusListener listener; + listener = (status) { + if (status != AnimationStatus.dismissed) return; + animation.removeStatusListener(listener); + onClosed(); + }; + animation.addStatusListener(listener); + } + /// Removes the page with its owner. void dispose() { // The notifier is left to the page: a page still animating out keeps diff --git a/app/lib/ui/theme/app_theme.dart b/app/lib/ui/theme/app_theme.dart index 73d9b3b0..b881c859 100644 --- a/app/lib/ui/theme/app_theme.dart +++ b/app/lib/ui/theme/app_theme.dart @@ -25,8 +25,58 @@ abstract final class AppColors { const _seed = Color(0xFF6750A4); -ThemeData appLightTheme() => - ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: _seed)); +ThemeData appLightTheme() => ThemeData( + colorScheme: ColorScheme.fromSeed(seedColor: _seed), + pageTransitionsTheme: appPageTransitions, +); + +/// Pages slide in and out quickly. Android's default fades the whole page, +/// which is costly for long pages (a chat) and made Back feel slow; iOS keeps +/// its native transition and swipe-back. +const appPageTransitions = PageTransitionsTheme( + builders: { + TargetPlatform.android: FastSlidePageTransitionsBuilder(), + TargetPlatform.fuchsia: FastSlidePageTransitionsBuilder(), + TargetPlatform.linux: FastSlidePageTransitionsBuilder(), + TargetPlatform.windows: FastSlidePageTransitionsBuilder(), + TargetPlatform.iOS: CupertinoPageTransitionsBuilder(), + TargetPlatform.macOS: CupertinoPageTransitionsBuilder(), + }, +); + +/// A 200 ms horizontal slide with no fade: only the page moves, so nothing +/// has to be redrawn into an offscreen layer. +class FastSlidePageTransitionsBuilder extends PageTransitionsBuilder { + const FastSlidePageTransitionsBuilder(); + + @override + Duration get transitionDuration => const Duration(milliseconds: 200); + + @override + Duration get reverseTransitionDuration => const Duration(milliseconds: 180); + + @override + Widget buildTransitions( + PageRoute route, + BuildContext context, + Animation animation, + Animation secondaryAnimation, + Widget child, + ) { + final curved = CurvedAnimation( + parent: animation, + curve: Curves.easeOutCubic, + reverseCurve: Curves.easeInCubic, + ); + return SlideTransition( + position: Tween( + begin: const Offset(1, 0), + end: Offset.zero, + ).animate(curved), + child: child, + ); + } +} /// Material's dark scheme with gray surfaces stepping up from the page. ThemeData appDarkTheme() { @@ -47,6 +97,7 @@ ThemeData appDarkTheme() { return ThemeData( colorScheme: scheme, scaffoldBackgroundColor: scheme.surface, + pageTransitionsTheme: appPageTransitions, ); } diff --git a/app/test/features/chat/ui/sessions/chat_star_optimistic_test.dart b/app/test/features/chat/ui/sessions/chat_star_optimistic_test.dart new file mode 100644 index 00000000..bbfd3308 --- /dev/null +++ b/app/test/features/chat/ui/sessions/chat_star_optimistic_test.dart @@ -0,0 +1,53 @@ +import 'dart:async'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/core/api_gateway.dart'; +import 'package:pipelines_app/features/chat/data/chat_repository.dart'; +import 'chat_test_harness.dart'; +import 'package:pipelines_app/features/chat/ui/sessions/chat_sessions_controller.dart'; + +import 'fake_chat_gateway.dart'; + +void main() { + late FakeChatGateway gateway; + late ChatSessionsController controller; + late Completer reply; + + setUp(() async { + gateway = FakeChatGateway() + ..sessionsReply = { + 'chat_sessions': [ + {'id': 's1', 'title': 'One', 'directory': '/w', 'starred': false}, + ], + }; + reply = Completer(); + gateway.actionHandler = (_, _) => reply.future; + controller = ChatSessionsController( + ChatRepository(gateway), + localStore: MemoryChatSessionLocalStore(), + ); + await controller.refresh(); + }); + tearDown(() => controller.dispose()); + + bool starred() => controller.sessions.single['starred'] == true; + + test('a star shows at once, before the server answers', () async { + final pending = controller.star('s1', true); + expect(starred(), isTrue); + reply.complete({ + 'chat_session': {'id': 's1', 'starred': true}, + }); + await pending; + expect(controller.errorFor('s1'), isNull); + }); + + test('a refused star is undone and the reason is shown', () async { + final pending = controller.star('s1', true); + expect(starred(), isTrue); + reply.completeError(StateError('star refused')); + await pending; + expect(starred(), isFalse); + expect(controller.errorFor('s1'), contains('star refused')); + }); +} diff --git a/app/test/ui/theme/app_theme_test.dart b/app/test/ui/theme/app_theme_test.dart index f714102c..40e787a4 100644 --- a/app/test/ui/theme/app_theme_test.dart +++ b/app/test/ui/theme/app_theme_test.dart @@ -25,4 +25,19 @@ void main() { expect(appCupertinoTheme.scaffoldBackgroundColor, AppColors.background); expect(AppColors.groupedBackground.darkColor, isNot(CupertinoColors.black)); }); + + test('Android pages slide quickly, without a fade', () { + final builder = + appDarkTheme().pageTransitionsTheme.builders[TargetPlatform.android]!; + expect(builder, isA()); + expect(builder.transitionDuration.inMilliseconds, lessThanOrEqualTo(200)); + expect( + builder.reverseTransitionDuration.inMilliseconds, + lessThanOrEqualTo(200), + ); + expect( + appLightTheme().pageTransitionsTheme.builders[TargetPlatform.android], + isA(), + ); + }); }