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

View File

@ -0,0 +1,44 @@
import 'dart:async';
import 'dart:convert';
import 'dart:developer';
import 'package:flutter/cupertino.dart';
import 'package:marianum_mobile/dataOld/socketConnection.dart';
class IncomingPacket extends ChangeNotifier {
String packetId;
bool useJsonDecode;
bool _isReceived = false;
bool get isReceived => _isReceived;
IncomingPacket(this.packetId, {this.useJsonDecode = true}) {
SocketConnection.read.listen((event) {
if(event.startsWith("$packetId:")) {
_isReceived = true;
// THIS listener handles the incomming request
log("$packetId is handled!");
String content = event.split("$packetId:")[1];
handle(useJsonDecode ? jsonDecode(content) : content);
}
notifyListeners();
});
}
void invoke({Object? data, bool indicateLoading = false, bool allowNotifyListeners = true}) {
data = data ?? {};
log("$packetId is invoked with data: $data");
SocketConnection.write.add("$packetId:${jsonEncode(data)}");
if(indicateLoading) {
_isReceived = false;
if(allowNotifyListeners) notifyListeners();
}
}
void handle(dynamic data) {
log("Warning: $packetId packet listener is registered, but no handle is defined!");
}
}