Add device-local quick switch animation settings

This commit is contained in:
2026-11-07 19:09:29 +00:00
parent 87a140754c
commit 5eb59a6813
6 changed files with 318 additions and 2 deletions
+4
View File
@@ -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<void> 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,
@@ -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,
),
),
],
);
},
@@ -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<void> _choose(BuildContext context) async {
final choice = await showCupertinoModalPopup<QuickSwitchAnimation>(
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<QuickSwitchAnimation>(
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)),
),
],
),
);
}
@@ -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<String?> load();
Future<bool> save(String animation);
}
class SharedQuickSwitchAnimationStore implements QuickSwitchAnimationStore {
const SharedQuickSwitchAnimationStore();
@override
Future<String?> load() async {
try {
return (await SharedPreferences.getInstance()).getString(
quickSwitchAnimationKey,
);
} catch (_) {
return null;
}
}
@override
Future<bool> 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<void> 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<void> 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();
}
}
@@ -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<String?> load() async => saved;
@override
Future<bool> 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();
});
}
}
@@ -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<String?>? pending;
@override
Future<String?> load() async => pending == null ? saved : pending!.future;
@override
Future<bool> 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<String?>();
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();
});
}