perf(app): quick slide page transitions, close work after the page leaves, instant star/close/reopen
This commit is contained in:
@@ -137,14 +137,23 @@ class ChatSessionsController extends ChangeNotifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> star(String sessionId, bool starred) =>
|
Future<void> star(String sessionId, bool starred) => _mutate(
|
||||||
_mutate(sessionId, () => repository.star(sessionId, starred));
|
sessionId,
|
||||||
|
() => repository.star(sessionId, starred),
|
||||||
|
patch: {'starred': starred},
|
||||||
|
);
|
||||||
|
|
||||||
Future<void> close(String sessionId) =>
|
Future<void> close(String sessionId) => _mutate(
|
||||||
_mutate(sessionId, () => repository.close(sessionId));
|
sessionId,
|
||||||
|
() => repository.close(sessionId),
|
||||||
|
patch: {'closed_at': DateTime.now().toUtc().toIso8601String()},
|
||||||
|
);
|
||||||
|
|
||||||
Future<void> reopen(String sessionId) =>
|
Future<void> reopen(String sessionId) => _mutate(
|
||||||
_mutate(sessionId, () => repository.reopen(sessionId));
|
sessionId,
|
||||||
|
() => repository.reopen(sessionId),
|
||||||
|
patch: {'closed_at': null},
|
||||||
|
);
|
||||||
|
|
||||||
/// Renames through the shared server field. Throws the server's refusal so the
|
/// 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.
|
/// open dialog can keep it in view; success refreshes the list from the server.
|
||||||
@@ -241,15 +250,29 @@ class ChatSessionsController extends ChangeNotifier {
|
|||||||
_notify();
|
_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<void> _mutate(
|
Future<void> _mutate(
|
||||||
String sessionId,
|
String sessionId,
|
||||||
Future<JsonMap> Function() action,
|
Future<JsonMap> Function() action, {
|
||||||
) async {
|
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 {
|
try {
|
||||||
await action();
|
await action();
|
||||||
_actionErrors.remove(sessionId);
|
_actionErrors.remove(sessionId);
|
||||||
await refresh();
|
unawaited(refresh());
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
if (patch != null) _sessions = before;
|
||||||
_actionErrors[sessionId] = '$error';
|
_actionErrors[sessionId] = '$error';
|
||||||
_notify();
|
_notify();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -54,7 +54,9 @@ class DetailPageHost {
|
|||||||
Navigator.of(context).push(route).then((_) {
|
Navigator.of(context).push(route).then((_) {
|
||||||
if (_route != route) return;
|
if (_route != route) return;
|
||||||
_route = null;
|
_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) {
|
} else if (!selected && _route != null) {
|
||||||
@@ -66,6 +68,21 @@ class DetailPageHost {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void _afterExit(Route<void> route, VoidCallback onClosed) {
|
||||||
|
final animation = route is ModalRoute<void> ? 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.
|
/// Removes the page with its owner.
|
||||||
void dispose() {
|
void dispose() {
|
||||||
// The notifier is left to the page: a page still animating out keeps
|
// The notifier is left to the page: a page still animating out keeps
|
||||||
|
|||||||
@@ -25,8 +25,58 @@ abstract final class AppColors {
|
|||||||
|
|
||||||
const _seed = Color(0xFF6750A4);
|
const _seed = Color(0xFF6750A4);
|
||||||
|
|
||||||
ThemeData appLightTheme() =>
|
ThemeData appLightTheme() => ThemeData(
|
||||||
ThemeData(colorScheme: ColorScheme.fromSeed(seedColor: _seed));
|
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<T>(
|
||||||
|
PageRoute<T> route,
|
||||||
|
BuildContext context,
|
||||||
|
Animation<double> animation,
|
||||||
|
Animation<double> secondaryAnimation,
|
||||||
|
Widget child,
|
||||||
|
) {
|
||||||
|
final curved = CurvedAnimation(
|
||||||
|
parent: animation,
|
||||||
|
curve: Curves.easeOutCubic,
|
||||||
|
reverseCurve: Curves.easeInCubic,
|
||||||
|
);
|
||||||
|
return SlideTransition(
|
||||||
|
position: Tween<Offset>(
|
||||||
|
begin: const Offset(1, 0),
|
||||||
|
end: Offset.zero,
|
||||||
|
).animate(curved),
|
||||||
|
child: child,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Material's dark scheme with gray surfaces stepping up from the page.
|
/// Material's dark scheme with gray surfaces stepping up from the page.
|
||||||
ThemeData appDarkTheme() {
|
ThemeData appDarkTheme() {
|
||||||
@@ -47,6 +97,7 @@ ThemeData appDarkTheme() {
|
|||||||
return ThemeData(
|
return ThemeData(
|
||||||
colorScheme: scheme,
|
colorScheme: scheme,
|
||||||
scaffoldBackgroundColor: scheme.surface,
|
scaffoldBackgroundColor: scheme.surface,
|
||||||
|
pageTransitionsTheme: appPageTransitions,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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<JsonMap> reply;
|
||||||
|
|
||||||
|
setUp(() async {
|
||||||
|
gateway = FakeChatGateway()
|
||||||
|
..sessionsReply = {
|
||||||
|
'chat_sessions': [
|
||||||
|
{'id': 's1', 'title': 'One', 'directory': '/w', 'starred': false},
|
||||||
|
],
|
||||||
|
};
|
||||||
|
reply = Completer<JsonMap>();
|
||||||
|
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'));
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:flutter_test/flutter_test.dart';
|
import 'package:flutter_test/flutter_test.dart';
|
||||||
import 'package:pipelines_app/ui/theme/app_theme.dart';
|
import 'package:pipelines_app/ui/theme/app_theme.dart';
|
||||||
@@ -25,4 +26,19 @@ void main() {
|
|||||||
expect(appCupertinoTheme.scaffoldBackgroundColor, AppColors.background);
|
expect(appCupertinoTheme.scaffoldBackgroundColor, AppColors.background);
|
||||||
expect(AppColors.groupedBackground.darkColor, isNot(CupertinoColors.black));
|
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<FastSlidePageTransitionsBuilder>());
|
||||||
|
expect(builder.transitionDuration.inMilliseconds, lessThanOrEqualTo(200));
|
||||||
|
expect(
|
||||||
|
builder.reverseTransitionDuration.inMilliseconds,
|
||||||
|
lessThanOrEqualTo(200),
|
||||||
|
);
|
||||||
|
expect(
|
||||||
|
appLightTheme().pageTransitionsTheme.builders[TargetPlatform.android],
|
||||||
|
isA<FastSlidePageTransitionsBuilder>(),
|
||||||
|
);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user