claude refactor
This commit is contained in:
+11
-12
@@ -13,8 +13,8 @@ abstract class RequestCache<T extends ApiResponse?> {
|
||||
static String collection = 'MarianumMobile';
|
||||
|
||||
int maxCacheTime;
|
||||
Function(T) onUpdate;
|
||||
Function(Exception) onError;
|
||||
void Function(T)? onUpdate;
|
||||
void Function(Exception) onError;
|
||||
bool? renew;
|
||||
|
||||
RequestCache(this.maxCacheTime, this.onUpdate, {this.onError = ignore, this.renew = false});
|
||||
@@ -22,24 +22,23 @@ abstract class RequestCache<T extends ApiResponse?> {
|
||||
static void ignore(Exception e) {}
|
||||
|
||||
Future<void> start(String document) async {
|
||||
var tableData = await Localstore.instance.collection(collection).doc(document).get();
|
||||
if(tableData != null) {
|
||||
onUpdate(onLocalData(tableData['json']));
|
||||
final tableData = await Localstore.instance.collection(collection).doc(document).get();
|
||||
if (tableData != null) {
|
||||
onUpdate?.call(onLocalData(tableData['json']));
|
||||
}
|
||||
|
||||
if(DateTime.now().millisecondsSinceEpoch - (maxCacheTime * 1000) < (tableData?['lastupdate'] ?? 0)) {
|
||||
if(renew == null || !renew!) return;
|
||||
if (DateTime.now().millisecondsSinceEpoch - (maxCacheTime * 1000) < (tableData?['lastupdate'] ?? 0)) {
|
||||
if (renew == null || !renew!) return;
|
||||
}
|
||||
|
||||
try {
|
||||
var newValue = await onLoad();
|
||||
onUpdate(newValue);
|
||||
|
||||
final newValue = await onLoad();
|
||||
onUpdate?.call(newValue);
|
||||
Localstore.instance.collection(collection).doc(document).set({
|
||||
'json': jsonEncode(newValue),
|
||||
'lastupdate': DateTime.now().millisecondsSinceEpoch
|
||||
'lastupdate': DateTime.now().millisecondsSinceEpoch,
|
||||
});
|
||||
} on Exception catch(e) {
|
||||
} on Exception catch (e) {
|
||||
onError(e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user