93 lines
2.5 KiB
Dart
93 lines
2.5 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.'),
|
|
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();
|
|
}
|
|
}
|