Files
pipelines/app/lib/ui/motion/quick_switch_animation.dart
T

105 lines
3.2 KiB
Dart

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.'),
arcSwap('Arc swap', 'Chats trade places along opposite left and right arcs.'),
cardStack(
'Card deck',
'Lift the front card, slide it aside and tuck it behind.',
),
deckSlide(
'Deck slide',
'Slide the front card clear of the deck and behind it.',
),
fan('Fan', 'Cards fan apart with a gentle planar rotation.'),
slide('Slide', 'Cards sweep sideways in opposite directions.'),
verticalPush('Push', 'Cards push past each other vertically.'),
fade('Crossfade', 'The old view fades as the next chat takes its place.'),
zoom('Zoom', 'The old chat recedes while the incoming view grows forward.'),
flip('Flip', 'Cards turn edge-on with perspective before exchanging places.'),
rolodex('Rolodex', 'The previous tilted perspective exchange.');
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();
}
}