From 5eb59a68135162393c2712737fc4853be7898c1d Mon Sep 17 00:00:00 2001 From: Alan Silva Date: Sat, 7 Nov 2026 19:09:29 +0000 Subject: [PATCH] Add device-local quick switch animation settings --- app/lib/application/bootstrap.dart | 4 + .../settings/settings_animation_section.dart | 18 +++- .../settings_quick_switch_control.dart | 79 ++++++++++++++++ app/lib/ui/motion/quick_switch_animation.dart | 92 +++++++++++++++++++ .../settings_quick_switch_control_test.dart | 66 +++++++++++++ .../motion/quick_switch_animation_test.dart | 61 ++++++++++++ 6 files changed, 318 insertions(+), 2 deletions(-) create mode 100644 app/lib/features/settings/settings_quick_switch_control.dart create mode 100644 app/lib/ui/motion/quick_switch_animation.dart create mode 100644 app/test/features/settings/settings_quick_switch_control_test.dart create mode 100644 app/test/ui/motion/quick_switch_animation_test.dart diff --git a/app/lib/application/bootstrap.dart b/app/lib/application/bootstrap.dart index bbbba8c8..07022de2 100644 --- a/app/lib/application/bootstrap.dart +++ b/app/lib/application/bootstrap.dart @@ -9,6 +9,7 @@ import '../features/settings/settings_store.dart'; import '../ui/theme/app_font_controller.dart'; import '../ui/theme/app_font_licenses.dart'; import '../ui/motion/animation_speed.dart'; +import '../ui/motion/quick_switch_animation.dart'; /// Native builds must supply their server explicitly; there is no baked host. const configuredServerUrl = String.fromEnvironment('PIPELINES_SERVER_URL'); @@ -57,11 +58,14 @@ Future runPipelinesApp({ // The device's chosen font loads beside the session; a slow download does // not hold the first frame, and applies when it lands. final animationSpeed = AnimationSpeedController.instance.restore(); + final quickSwitchAnimation = QuickSwitchAnimationController.instance + .restore(); final font = AppFontController.instance.restore(wait: appFontStartupWait); // A phone behind the web login reconnects with its kept session. await ServerSession.load(); await font; await animationSpeed; + await quickSwitchAnimation; final server = await resolveLaunchServer( base: base, configured: configured, diff --git a/app/lib/features/settings/settings_animation_section.dart b/app/lib/features/settings/settings_animation_section.dart index 35269e83..28c5fc4f 100644 --- a/app/lib/features/settings/settings_animation_section.dart +++ b/app/lib/features/settings/settings_animation_section.dart @@ -2,12 +2,19 @@ import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../ui/motion/animation_speed.dart'; +import '../../ui/motion/quick_switch_animation.dart'; +import 'settings_quick_switch_control.dart'; import 'settings_group.dart'; import 'settings_theme.dart'; class SettingsAnimationSection extends StatelessWidget { - const SettingsAnimationSection({super.key, required this.controller}); + const SettingsAnimationSection({ + super.key, + required this.controller, + this.quickSwitchController, + }); final AnimationSpeedController controller; + final QuickSwitchAnimationController? quickSwitchController; @override Widget build(BuildContext context) => ListenableBuilder( @@ -18,7 +25,7 @@ class SettingsAnimationSection extends StatelessWidget { final label = '${speed.toStringAsFixed(1)}×'; final reset = speed == 1 ? null : () => unawaited(controller.reset()); return SettingsGroup( - title: 'Animation speed', + title: 'Animations', footer: Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ @@ -83,6 +90,13 @@ class SettingsAnimationSection extends StatelessWidget { ], ), ), + SettingsGroupItem( + child: SettingsQuickSwitchControl( + controller: + quickSwitchController ?? + QuickSwitchAnimationController.instance, + ), + ), ], ); }, diff --git a/app/lib/features/settings/settings_quick_switch_control.dart b/app/lib/features/settings/settings_quick_switch_control.dart new file mode 100644 index 00000000..a2f37df8 --- /dev/null +++ b/app/lib/features/settings/settings_quick_switch_control.dart @@ -0,0 +1,79 @@ +import 'dart:async'; +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import '../../ui/motion/quick_switch_animation.dart'; +import 'settings_theme.dart'; + +class SettingsQuickSwitchControl extends StatelessWidget { + const SettingsQuickSwitchControl({super.key, required this.controller}); + final QuickSwitchAnimationController controller; + + Future _choose(BuildContext context) async { + final choice = await showCupertinoModalPopup( + context: context, + builder: (context) => CupertinoActionSheet( + title: const Text('Quick switch animation'), + actions: [ + for (final animation in QuickSwitchAnimation.values) + CupertinoActionSheetAction( + isDefaultAction: animation == controller.value, + onPressed: () => Navigator.pop(context, animation), + child: Text(animation.label), + ), + ], + cancelButton: CupertinoActionSheetAction( + onPressed: () => Navigator.pop(context), + child: const Text('Cancel'), + ), + ), + ); + if (choice != null) await controller.setAnimation(choice); + } + + @override + Widget build(BuildContext context) => ListenableBuilder( + listenable: controller, + builder: (context, _) => Column( + crossAxisAlignment: CrossAxisAlignment.stretch, + children: [ + Text( + 'Quick switch animation', + style: settingsTextStyle(context, title: true), + ), + if (usesCupertinoControls(context)) + CupertinoButton( + onPressed: () => unawaited(_choose(context)), + child: Row( + mainAxisSize: MainAxisSize.min, + children: [ + Text(controller.value.label), + const SizedBox(width: 8), + const Icon(CupertinoIcons.chevron_down), + ], + ), + ) + else + DropdownButton( + value: controller.value, + isExpanded: true, + items: [ + for (final animation in QuickSwitchAnimation.values) + DropdownMenuItem( + value: animation, + child: Text(animation.label), + ), + ], + onChanged: (value) { + if (value != null) unawaited(controller.setAnimation(value)); + }, + ), + Text(controller.value.description), + if (controller.saveFailed) + Text( + 'Could not save; applies until reload.', + style: TextStyle(color: settingsErrorColor(context)), + ), + ], + ), + ); +} diff --git a/app/lib/ui/motion/quick_switch_animation.dart b/app/lib/ui/motion/quick_switch_animation.dart new file mode 100644 index 00000000..87e4e7d9 --- /dev/null +++ b/app/lib/ui/motion/quick_switch_animation.dart @@ -0,0 +1,92 @@ +import 'package:flutter/foundation.dart'; +import 'package:shared_preferences/shared_preferences.dart'; + +const quickSwitchAnimationKey = 'pipelines.quick-switch-animation.v1'; + +enum QuickSwitchAnimation { + tape('Tape', 'Chats travel together in a continuous vertical loop.'), + swap('Swap', 'Chats slide past each other into their new positions.'), + cardStack( + 'Card stack', + 'The front card lifts and settles behind the next chat.', + ); + + const QuickSwitchAnimation(this.label, this.description); + final String label; + final String description; +} + +abstract interface class QuickSwitchAnimationStore { + Future load(); + Future save(String animation); +} + +class SharedQuickSwitchAnimationStore implements QuickSwitchAnimationStore { + const SharedQuickSwitchAnimationStore(); + @override + Future load() async { + try { + return (await SharedPreferences.getInstance()).getString( + quickSwitchAnimationKey, + ); + } catch (_) { + return null; + } + } + + @override + Future save(String animation) async { + try { + return await (await SharedPreferences.getInstance()).setString( + quickSwitchAnimationKey, + animation, + ); + } catch (_) { + return false; + } + } +} + +/// Device-local quick-switch style, independent of the global animation clock. +class QuickSwitchAnimationController extends ChangeNotifier { + QuickSwitchAnimationController({QuickSwitchAnimationStore? store}) + : _store = store ?? const SharedQuickSwitchAnimationStore(); + static final instance = QuickSwitchAnimationController(); + final QuickSwitchAnimationStore _store; + QuickSwitchAnimation _value = QuickSwitchAnimation.tape; + int _revision = 0; + bool _disposed = false; + bool saveFailed = false; + QuickSwitchAnimation get value => _value; + + Future restore() async { + final revision = _revision; + final saved = await _store.load(); + if (_disposed || revision != _revision) return; + for (final animation in QuickSwitchAnimation.values) { + if (animation.name == saved) { + _value = animation; + notifyListeners(); + return; + } + } + } + + Future setAnimation(QuickSwitchAnimation animation) async { + if (_disposed) return; + final revision = ++_revision; + _value = animation; + saveFailed = false; + notifyListeners(); + final saved = await _store.save(animation.name); + if (_disposed || revision != _revision) return; + saveFailed = !saved; + notifyListeners(); + } + + @override + void dispose() { + _disposed = true; + super.dispose(); + } +} diff --git a/app/test/features/settings/settings_quick_switch_control_test.dart b/app/test/features/settings/settings_quick_switch_control_test.dart new file mode 100644 index 00000000..44c26886 --- /dev/null +++ b/app/test/features/settings/settings_quick_switch_control_test.dart @@ -0,0 +1,66 @@ +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:pipelines_app/features/settings/settings_animation_section.dart'; +import 'package:pipelines_app/ui/motion/animation_speed.dart'; +import 'package:pipelines_app/ui/motion/quick_switch_animation.dart'; + +class _Store implements QuickSwitchAnimationStore { + String? saved; + bool success = true; + @override + Future load() async => saved; + @override + Future save(String value) async { + saved = value; + return success; + } +} + +void main() { + for (final cupertino in [false, true]) { + testWidgets('chooses and saves quick switch animation ($cupertino)', ( + tester, + ) async { + final store = _Store(); + final controller = QuickSwitchAnimationController(store: store); + final speed = AnimationSpeedController(); + final child = SettingsAnimationSection( + controller: speed, + quickSwitchController: controller, + ); + await tester.pumpWidget( + cupertino + ? CupertinoApp(home: CupertinoPageScaffold(child: child)) + : MaterialApp(home: Scaffold(body: child)), + ); + expect(find.text('Quick switch animation'), findsOneWidget); + expect( + cupertino ? find.byType(CupertinoSlider) : find.byType(Slider), + findsOneWidget, + ); + await tester.tap(find.text('Tape')); + await tester.pumpAndSettle(); + await tester.tap(find.text('Card stack').last); + await tester.pumpAndSettle(); + expect(controller.value, QuickSwitchAnimation.cardStack); + expect(store.saved, 'cardStack'); + expect( + find.text(QuickSwitchAnimation.cardStack.description), + findsOneWidget, + ); + store.success = false; + await tester.tap(find.text('Card stack')); + await tester.pumpAndSettle(); + await tester.tap(find.text('Swap').last); + await tester.pumpAndSettle(); + expect( + find.text('Could not save; applies until reload.'), + findsOneWidget, + ); + expect(tester.takeException(), isNull); + controller.dispose(); + speed.dispose(); + }); + } +} diff --git a/app/test/ui/motion/quick_switch_animation_test.dart b/app/test/ui/motion/quick_switch_animation_test.dart new file mode 100644 index 00000000..5b33e412 --- /dev/null +++ b/app/test/ui/motion/quick_switch_animation_test.dart @@ -0,0 +1,61 @@ +import 'dart:async'; +import 'package:flutter_test/flutter_test.dart'; +import 'package:shared_preferences/shared_preferences.dart'; +import 'package:pipelines_app/ui/motion/quick_switch_animation.dart'; + +class _Store implements QuickSwitchAnimationStore { + String? saved; + bool success = true; + Completer? pending; + @override + Future load() async => pending == null ? saved : pending!.future; + @override + Future save(String value) async { + saved = value; + return success; + } +} + +void main() { + test('persists every style and restores the device choice', () async { + SharedPreferences.setMockInitialValues({}); + for (final value in QuickSwitchAnimation.values) { + final first = QuickSwitchAnimationController(); + await first.setAnimation(value); + final second = QuickSwitchAnimationController(); + await second.restore(); + expect(second.value, value); + first.dispose(); + second.dispose(); + } + }); + + test( + 'unknown preference uses tape; delayed restore preserves new choice', + () async { + final store = _Store()..saved = 'removed-style'; + final controller = QuickSwitchAnimationController(store: store); + await controller.restore(); + expect(controller.value, QuickSwitchAnimation.tape); + store.pending = Completer(); + final restore = controller.restore(); + await controller.setAnimation(QuickSwitchAnimation.cardStack); + store.pending!.complete('swap'); + await restore; + expect(controller.value, QuickSwitchAnimation.cardStack); + controller.dispose(); + }, + ); + + test('failed save keeps current style and reports failure', () async { + final store = _Store()..success = false; + final controller = QuickSwitchAnimationController(store: store); + await controller.setAnimation(QuickSwitchAnimation.swap); + expect(controller.value, QuickSwitchAnimation.swap); + expect(controller.saveFailed, isTrue); + store.success = true; + await controller.setAnimation(QuickSwitchAnimation.cardStack); + expect(controller.saveFailed, isFalse); + controller.dispose(); + }); +}