feat: make core pages local-first offline

This commit is contained in:
2026-08-21 12:40:01 +08:00
parent 8828851631
commit fefa2d74e6
20 changed files with 921 additions and 188 deletions
@@ -52,6 +52,17 @@ class CurrentLedgerStore extends ChangeNotifier {
return _loading ??= _loadCached().whenComplete(() => _loading = null);
}
Future<void> refreshRemote() async {
if (!SessionStore.instance.isAccount ||
SessionStore.instance.shouldUseLocalOnly) {
return;
}
final response = await _dio.get('/api/ledgers');
final values = response.data as List;
LocalDatabase.instance.cacheLedgers(values);
_apply(values);
}
Future<void> _loadCached() async {
_apply(LocalDatabase.instance.ledgers());
}
@@ -78,11 +89,24 @@ class CurrentLedgerStore extends ChangeNotifier {
final items = values
.map((item) => LedgerInfo.fromJson(item as Map<String, dynamic>))
.toList();
_ledgers = items;
_current =
final current =
items.where((item) => item.isDefault).firstOrNull ??
(items.isEmpty ? null : items.first);
notifyListeners();
final changed =
_current?.id != current?.id ||
_ledgers.length != items.length ||
Iterable<int>.generate(items.length).any((index) {
final before = _ledgers[index];
final after = items[index];
return before.id != after.id ||
before.name != after.name ||
before.iconKey != after.iconKey ||
before.isDefault != after.isDefault ||
before.transactionCount != after.transactionCount;
});
_ledgers = items;
_current = current;
if (changed) notifyListeners();
}
Future<void> select(int id) async {