Implement Webuntis HTTP Api and Display
This commit is contained in:
.idea/libraries
lib
api
requestCache.dart
app.dartwebuntis
data
dataOld
accountModel.dartincomingPacket.dart
main.dartincommingPackets
authenticatePacket.darterrorPacket.dartfileListPacket.dartserverInfoPacket.darttalkChatPacket.darttalkContactsPacket.darttalkNotificationsPacket.darttimetablePacket.dart
outgoingPacket.dartoutgoingPackets
socketConnection.dartscreen
login
pages
files
talk
timetable
settings
widget
macos/Flutter
pubspec.yaml
32
lib/api/requestCache.dart
Normal file
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();
|
||||
}
|
Reference in New Issue
Block a user