2022-08-29 20:38:28 -06:00
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
import 'package:wonders/logic/common/json_prefs_file.dart';
|
|
|
|
import 'package:wonders/logic/common/throttler.dart';
|
|
|
|
|
|
|
|
mixin ThrottledSaveLoadMixin {
|
|
|
|
late final _file = JsonPrefsFile(fileName);
|
|
|
|
final _throttle = Throttler(const Duration(seconds: 2));
|
|
|
|
|
|
|
|
Future<void> load() async {
|
|
|
|
final results = await _file.load();
|
|
|
|
try {
|
|
|
|
copyFromJson(results);
|
|
|
|
} on Exception catch (e) {
|
|
|
|
debugPrint(e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> save() async {
|
2024-01-18 10:30:02 -07:00
|
|
|
if (!kIsWeb) debugPrint('Saving...');
|
2022-08-29 20:38:28 -06:00
|
|
|
try {
|
|
|
|
await _file.save(toJson());
|
|
|
|
} on Exception catch (e) {
|
|
|
|
debugPrint(e.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> scheduleSave() async => _throttle.call(save);
|
|
|
|
|
|
|
|
/// Serialization
|
|
|
|
String get fileName;
|
|
|
|
Map<String, dynamic> toJson();
|
|
|
|
void copyFromJson(Map<String, dynamic> value);
|
|
|
|
}
|