Implement Webuntis HTTP Api and Display

This commit is contained in:
2023-02-15 20:33:05 +01:00
parent 7432972b3c
commit 2b6dc8e3e6
42 changed files with 746 additions and 287 deletions

32
lib/api/requestCache.dart Normal file

@ -0,0 +1,32 @@
import 'dart:convert';
import 'package:localstore/localstore.dart';
abstract class RequestCache<T> {
int maxCacheTime;
Function(T) onUpdate;
RequestCache(this.maxCacheTime, this.onUpdate);
void start(String file, String document) async {
Map<String, dynamic>? tableData = await Localstore.instance.collection(file).doc(document).get();
if(tableData != null) {
onUpdate(onLocalData(tableData['json']));
}
if(DateTime.now().millisecondsSinceEpoch - (maxCacheTime * 1000) < (tableData?['lastupdate'] ?? 0)) {
return;
}
T newValue = await onLoad();
onUpdate(newValue);
Localstore.instance.collection(file).doc(document).set({
"json": jsonEncode(newValue),
"lastupdate": DateTime.now().millisecondsSinceEpoch
});
}
T onLocalData(String json);
Future<T> onLoad();
}