api and storage restructure
This commit is contained in:
@@ -79,3 +79,38 @@ abstract class RequestCache<T extends ApiResponse?> {
|
||||
Future<T> onLoad();
|
||||
|
||||
}
|
||||
|
||||
/// Concrete [RequestCache] that delegates the two overrides to functions
|
||||
/// passed in the constructor. Used to collapse the dozens of one-class-per-
|
||||
/// endpoint cache files that all just forward to `<Endpoint>().run()` and
|
||||
/// `<Response>.fromJson(jsonDecode(...))`.
|
||||
class SimpleCache<T extends ApiResponse?> extends RequestCache<T> {
|
||||
final Future<T> Function() _loader;
|
||||
final T Function(Map<String, dynamic> json) _fromJson;
|
||||
|
||||
SimpleCache({
|
||||
required int cacheTime,
|
||||
required Future<T> Function() loader,
|
||||
required T Function(Map<String, dynamic> json) fromJson,
|
||||
void Function(T)? onUpdate,
|
||||
void Function(T)? onCacheData,
|
||||
void Function(T)? onNetworkData,
|
||||
void Function(Exception)? onError,
|
||||
bool? renew,
|
||||
}) : _loader = loader,
|
||||
_fromJson = fromJson,
|
||||
super(
|
||||
cacheTime,
|
||||
onUpdate,
|
||||
onError: onError ?? RequestCache.ignore,
|
||||
renew: renew,
|
||||
onCacheData: onCacheData,
|
||||
onNetworkData: onNetworkData,
|
||||
);
|
||||
|
||||
@override
|
||||
Future<T> onLoad() => _loader();
|
||||
|
||||
@override
|
||||
T onLocalData(String json) => _fromJson(jsonDecode(json));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user