Merge remote-tracking branch 'origin/develop' into develop-bloc

This commit is contained in:
Elias Müller 2024-05-12 14:01:57 +02:00
commit 76a1f65083
16 changed files with 24 additions and 23 deletions

View File

@ -6,7 +6,7 @@ import 'getHolidaysResponse.dart';
class GetHolidaysCache extends RequestCache<GetHolidaysResponse> {
GetHolidaysCache({onUpdate, renew}) : super(RequestCache.cacheDay, onUpdate, renew: renew) {
start('MarianumMobile', 'state-holidays');
start('state-holidays');
}
@override

View File

@ -9,7 +9,7 @@ class GetChatCache extends RequestCache<GetChatResponse> {
String chatToken;
GetChatCache({required onUpdate, required this.chatToken}) : super(RequestCache.cacheNothing, onUpdate) {
start('MarianumMobile', 'nc-chat-$chatToken');
start('nc-chat-$chatToken');
}
@override

View File

@ -8,7 +8,7 @@ class GetParticipantsCache extends RequestCache<GetParticipantsResponse> {
String chatToken;
GetParticipantsCache({required onUpdate, required this.chatToken}) : super(RequestCache.cacheNothing, onUpdate) {
start('MarianumMobile', 'nc-chat-participants-$chatToken');
start('nc-chat-participants-$chatToken');
}
@override

View File

@ -8,7 +8,7 @@ import 'getRoomResponse.dart';
class GetRoomCache extends RequestCache<GetRoomResponse> {
GetRoomCache({onUpdate, renew}) : super(RequestCache.cacheMinute, onUpdate, renew: renew) {
start('MarianumMobile', 'nc-rooms');
start('nc-rooms');
}
@override

View File

@ -12,7 +12,7 @@ class ListFilesCache extends RequestCache<ListFilesResponse> {
ListFilesCache({required onUpdate, required this.path}) : super(RequestCache.cacheNothing, onUpdate) {
var bytes = utf8.encode('MarianumMobile-$path');
var cacheName = md5.convert(bytes).toString();
start('MarianumMobile', 'wd-folder-$cacheName');
start('wd-folder-$cacheName');
}
@override

View File

@ -6,7 +6,7 @@ import 'getBreakersResponse.dart';
class GetBreakersCache extends RequestCache<GetBreakersResponse> {
GetBreakersCache({onUpdate, renew}) : super(RequestCache.cacheMinute, onUpdate, renew: renew) {
start('MarianumMobile', 'breakers');
start('breakers');
}
@override

View File

@ -9,7 +9,7 @@ class GetCustomTimetableEventCache extends RequestCache<GetCustomTimetableEventR
GetCustomTimetableEventParams params;
GetCustomTimetableEventCache(this.params, {onUpdate, renew}) : super(RequestCache.cacheMinute, onUpdate, renew: renew) {
start('MarianumMobile', 'customTimetableEvents');
start('customTimetableEvents');
}
@override

View File

@ -6,7 +6,7 @@ import 'getMessagesResponse.dart';
class GetMessagesCache extends RequestCache<GetMessagesResponse> {
GetMessagesCache({onUpdate, renew}) : super(RequestCache.cacheMinute, onUpdate, renew: renew) {
start('MarianumMobile', 'message');
start('message');
}
@override

View File

@ -11,6 +11,8 @@ abstract class RequestCache<T extends ApiResponse?> {
static const int cacheHour = 60 * 60;
static const int cacheDay = 60 * 60 * 24;
static String collection = 'MarianumMobile';
int maxCacheTime;
Function(T) onUpdate;
Function(Exception) onError;
@ -20,8 +22,8 @@ abstract class RequestCache<T extends ApiResponse?> {
static void ignore(Exception e) {}
Future<void> start(String file, String document) async {
var tableData = await Localstore.instance.collection(file).doc(document).get();
Future<void> start(String document) async {
var tableData = await Localstore.instance.collection(collection).doc(document).get();
if(tableData != null) {
onUpdate(onLocalData(tableData['json']));
}
@ -34,7 +36,7 @@ abstract class RequestCache<T extends ApiResponse?> {
var newValue = await onLoad();
onUpdate(newValue);
Localstore.instance.collection(file).doc(document).set({
Localstore.instance.collection(collection).doc(document).set({
'json': jsonEncode(newValue),
'lastupdate': DateTime.now().millisecondsSinceEpoch
});

View File

@ -6,7 +6,7 @@ import 'getHolidaysResponse.dart';
class GetHolidaysCache extends RequestCache<GetHolidaysResponse> {
GetHolidaysCache({onUpdate}) : super(RequestCache.cacheDay, onUpdate) {
start('MarianumMobile', 'wu-holidays');
start('wu-holidays');
}
@override

View File

@ -6,7 +6,7 @@ import 'getRoomsResponse.dart';
class GetRoomsCache extends RequestCache<GetRoomsResponse> {
GetRoomsCache({onUpdate}) : super(RequestCache.cacheHour, onUpdate) {
start('MarianumMobile', 'wu-rooms');
start('wu-rooms');
}
@override

View File

@ -6,7 +6,7 @@ import 'getSubjectsResponse.dart';
class GetSubjectsCache extends RequestCache<GetSubjectsResponse> {
GetSubjectsCache({onUpdate}) : super(RequestCache.cacheHour, onUpdate) {
start('MarianumMobile', 'wu-subjects');
start('wu-subjects');
}
@override

View File

@ -11,7 +11,7 @@ class GetTimetableCache extends RequestCache<GetTimetableResponse> {
int enddate;
GetTimetableCache({required onUpdate, onError, required this.startdate, required this.enddate}) : super(RequestCache.cacheMinute, onUpdate, onError: onError) {
start('MarianumMobile', 'wu-timetable-$startdate-$enddate');
start('wu-timetable-$startdate-$enddate');
}
@override

View File

@ -1,14 +1,14 @@
import 'package:localstore/localstore.dart';
import '../widget/debug/cacheView.dart';
import '../api/requestCache.dart';
class DataCleaner {
static Future<void> cleanOldCache() async {
var cacheData = await Localstore.instance.collection(CacheView.collection).get();
var cacheData = await Localstore.instance.collection(RequestCache.collection).get();
cacheData?.forEach((key, value) async {
var lastUpdate = DateTime.fromMillisecondsSinceEpoch(value['lastupdate']);
if(DateTime.now().subtract(const Duration(days: 200)).isAfter(lastUpdate)) {
await Localstore.instance.collection(CacheView.collection).doc(key.split('/').last).delete();
await Localstore.instance.collection(RequestCache.collection).doc(key.split('/').last).delete();
}
});
}

View File

@ -1,5 +1,4 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:jiffy/jiffy.dart';

View File

@ -8,21 +8,21 @@ import 'package:jiffy/jiffy.dart';
import 'package:localstore/localstore.dart';
import '../../../widget/placeholderView.dart';
import '../../api/requestCache.dart';
import 'jsonViewer.dart';
class CacheView extends StatefulWidget {
static String collection = 'MarianumMobile';
const CacheView({super.key});
@override
State<CacheView> createState() => _CacheViewState();
Future<void> clear() async {
await Localstore.instance.collection(collection).delete();
await Localstore.instance.collection(RequestCache.collection).delete();
}
Future<int> totalSize() async {
var data = await Localstore.instance.collection(collection).get();
var data = await Localstore.instance.collection(RequestCache.collection).get();
if(data!.length <= 1) return jsonEncode(data.values.first).length * 8;
return data.values.reduce((a, b) => jsonEncode(a).length + jsonEncode(b).length) * 8;
}
@ -34,7 +34,7 @@ class _CacheViewState extends State<CacheView> {
@override
void initState() {
files = Localstore.instance.collection(CacheView.collection).get();
files = Localstore.instance.collection(RequestCache.collection).get();
super.initState();
}