perf(app): quick slide page transitions, close work after the page leaves, instant star/close/reopen

This commit is contained in:
2026-10-24 00:47:32 +01:00
parent fe7311ade0
commit f997964761
5 changed files with 172 additions and 12 deletions
@@ -137,14 +137,23 @@ class ChatSessionsController extends ChangeNotifier {
}
}
Future<void> star(String sessionId, bool starred) =>
_mutate(sessionId, () => repository.star(sessionId, starred));
Future<void> star(String sessionId, bool starred) => _mutate(
sessionId,
() => repository.star(sessionId, starred),
patch: {'starred': starred},
);
Future<void> close(String sessionId) =>
_mutate(sessionId, () => repository.close(sessionId));
Future<void> close(String sessionId) => _mutate(
sessionId,
() => repository.close(sessionId),
patch: {'closed_at': DateTime.now().toUtc().toIso8601String()},
);
Future<void> reopen(String sessionId) =>
_mutate(sessionId, () => repository.reopen(sessionId));
Future<void> 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<void> _mutate(
String sessionId,
Future<JsonMap> Function() action,
) async {
Future<JsonMap> 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();
}
+18 -1
View File
@@ -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<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.
void dispose() {
// The notifier is left to the page: a page still animating out keeps
+53 -2
View File
@@ -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<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.
ThemeData appDarkTheme() {
@@ -47,6 +97,7 @@ ThemeData appDarkTheme() {
return ThemeData(
colorScheme: scheme,
scaffoldBackgroundColor: scheme.surface,
pageTransitionsTheme: appPageTransitions,
);
}