Merge pull request 'updated localstore collection to global constant' (#69) from develop-fixLogoutCache into develop

Reviewed-on: #69
Reviewed-by: Pupsi <larslukasneuhaus@gmx.de>
This commit is contained in:
Lars Neuhaus 2024-05-12 11:27:17 +00:00
commit ed35e91d59
16 changed files with 24 additions and 23 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -11,7 +11,7 @@ class GetTimetableCache extends RequestCache<GetTimetableResponse> {
int enddate; int enddate;
GetTimetableCache({required onUpdate, onError, required this.startdate, required this.enddate}) : super(RequestCache.cacheMinute, onUpdate, onError: onError) { 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 @override

View File

@ -1,14 +1,14 @@
import 'package:localstore/localstore.dart'; import 'package:localstore/localstore.dart';
import '../widget/debug/cacheView.dart'; import '../api/requestCache.dart';
class DataCleaner { class DataCleaner {
static Future<void> cleanOldCache() async { 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 { cacheData?.forEach((key, value) async {
var lastUpdate = DateTime.fromMillisecondsSinceEpoch(value['lastupdate']); var lastUpdate = DateTime.fromMillisecondsSinceEpoch(value['lastupdate']);
if(DateTime.now().subtract(const Duration(days: 200)).isAfter(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/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:jiffy/jiffy.dart'; import 'package:jiffy/jiffy.dart';

View File

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