refactor(apps): 主题系统迁移至 ColorScheme + 扩展架构并支持 Dark Mode
This commit is contained in:
@@ -1,57 +1,49 @@
|
||||
import 'dart:async';
|
||||
|
||||
import '../../../core/cache/cache_entry.dart';
|
||||
import '../../../core/cache/cache_invalidator.dart';
|
||||
import '../../../core/cache/hybrid_cache_store.dart';
|
||||
import '../../../data/cache/cache_entry.dart';
|
||||
import '../../../data/cache/cache_invalidator.dart';
|
||||
import '../../../data/cache/cache_policy.dart';
|
||||
import '../../../data/cache/cached_repository.dart';
|
||||
import 'todo_api.dart';
|
||||
|
||||
class TodoRepository {
|
||||
class TodoRepository extends CachedRepository<List<TodoResponse>> {
|
||||
static const String pendingListKey = 'todo:list:pending';
|
||||
|
||||
final TodoApi api;
|
||||
final HybridCacheStore store;
|
||||
final CacheInvalidator invalidator;
|
||||
final DateTime Function() now;
|
||||
|
||||
TodoRepository({
|
||||
required this.api,
|
||||
required this.store,
|
||||
required super.store,
|
||||
required this.invalidator,
|
||||
DateTime Function()? now,
|
||||
}) : now = now ?? DateTime.now;
|
||||
super.now,
|
||||
}) : super(
|
||||
policy: const CachePolicy(
|
||||
softTtl: Duration(days: 3650),
|
||||
hardTtl: Duration(days: 3650),
|
||||
minRefreshInterval: Duration(days: 3650),
|
||||
),
|
||||
);
|
||||
|
||||
Future<List<TodoResponse>> getPendingTodos({
|
||||
bool forceRefresh = false,
|
||||
}) async {
|
||||
if (!forceRefresh) {
|
||||
final cached = await store.read<CacheEntry<List<TodoResponse>>>(
|
||||
pendingListKey,
|
||||
);
|
||||
if (cached != null) {
|
||||
return cached.value;
|
||||
}
|
||||
}
|
||||
|
||||
final remote = await api.getPendingTodos();
|
||||
await store.write<CacheEntry<List<TodoResponse>>>(
|
||||
pendingListKey,
|
||||
CacheEntry(value: remote, fetchedAt: now()),
|
||||
return getOrLoad(
|
||||
key: pendingListKey,
|
||||
forceRefresh: forceRefresh,
|
||||
loadFromRemote: api.getPendingTodos,
|
||||
);
|
||||
return remote;
|
||||
}
|
||||
|
||||
Future<void> completeTodo(String id) async {
|
||||
final cached = await store.read<CacheEntry<List<TodoResponse>>>(
|
||||
final CacheEntry<List<TodoResponse>>? cached = await readCacheEntry(
|
||||
pendingListKey,
|
||||
);
|
||||
if (cached != null) {
|
||||
final next = cached.value
|
||||
.where((todo) => todo.id != id)
|
||||
.toList(growable: false);
|
||||
await store.write<CacheEntry<List<TodoResponse>>>(
|
||||
pendingListKey,
|
||||
CacheEntry(value: next, fetchedAt: now()),
|
||||
);
|
||||
await writeCacheEntry(pendingListKey, next);
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user