diff --git a/firka/lib/helpers/db/widget.dart b/firka/lib/helpers/db/widget.dart new file mode 100644 index 00000000..eda88882 --- /dev/null +++ b/firka/lib/helpers/db/widget.dart @@ -0,0 +1,71 @@ +import 'dart:convert'; +import 'dart:io'; + +import 'package:firka/helpers/api/client/kreta_client.dart'; +import 'package:firka/helpers/api/model/timetable.dart'; +import 'package:firka/helpers/debug_helper.dart'; +import 'package:path/path.dart' as p; +import 'package:path_provider/path_provider.dart'; + +import '../../ui/model/style.dart'; + +class WidgetCacheHelper { + static Map toJson(FirkaStyle style, List timetable) { + List> timetableJson = []; + + for (var lesson in timetable) { + timetableJson.add(lesson.toJson()); + } + + return { + 'colors': { + 'background': style.colors.background.toARGB32(), + 'backgroundAmoled': style.colors.backgroundAmoled.toARGB32(), + 'background0p': style.colors.background0p.toARGB32(), + 'success': style.colors.success.toARGB32(), + 'textPrimary': style.colors.textPrimary.toARGB32(), + 'textSecondary': style.colors.textSecondary.toARGB32(), + 'textTertiary': style.colors.textTertiary.toARGB32(), + 'card': style.colors.card.toARGB32(), + 'cardTranslucent': style.colors.cardTranslucent.toARGB32(), + 'buttonSecondaryFill': style.colors.buttonSecondaryFill.toARGB32(), + 'accent': style.colors.accent.toARGB32(), + 'secondary': style.colors.secondary.toARGB32(), + 'shadowColor': style.colors.shadowColor.toARGB32(), + 'a15p': style.colors.a15p.toARGB32(), + 'warningAccent': style.colors.warningAccent.toARGB32(), + 'warningText': style.colors.warningText.toARGB32(), + 'warning15p': style.colors.warning15p.toARGB32(), + 'warningCard': style.colors.warningCard.toARGB32(), + 'errorAccent': style.colors.errorAccent.toARGB32(), + 'errorText': style.colors.errorText.toARGB32(), + 'error15p': style.colors.error15p.toARGB32(), + 'errorCard': style.colors.errorCard.toARGB32(), + 'grade5': style.colors.grade5.toARGB32(), + 'grade4': style.colors.grade4.toARGB32(), + 'grade3': style.colors.grade3.toARGB32(), + 'grade2': style.colors.grade2.toARGB32(), + 'grade1': style.colors.grade1.toARGB32(), + }, + 'timetable': timetableJson, + }; + } + + static Future updateWidgetCache( + FirkaStyle style, KretaClient client) async { + final dataDir = await getApplicationDocumentsDirectory(); + + final now = timeNow(); + + final start = now.subtract(Duration(days: 7)); + final end = now.add(Duration(days: 14)); + final lessons = await client.getTimeTable(start, end); + + final widgetFile = File(p.join(dataDir.path, "widget_state.json")); + + if (lessons.response != null) { + widgetFile.writeAsString( + jsonEncode(WidgetCacheHelper.toJson(style, lessons.response!))); + } + } +} diff --git a/firka/lib/main.dart b/firka/lib/main.dart index b8b3ab88..953ab7cc 100644 --- a/firka/lib/main.dart +++ b/firka/lib/main.dart @@ -89,6 +89,8 @@ Future initializeApp() async { if (tokenCount > 0) { init.client = KretaClient((await isar.tokenModels.where().findFirst())!, isar); + + await WidgetCacheHelper.updateWidgetCache(appStyle, init.client); } final dataDir = await getApplicationDocumentsDirectory(); @@ -98,8 +100,6 @@ Future initializeApp() async { init.profilePicture = await pfpFile.readAsBytes(); } - await WidgetCacheHelper.updateWidgetCache(appStyle, init.client); - return init; } diff --git a/firka/lib/ui/phone/screens/home/home_screen.dart b/firka/lib/ui/phone/screens/home/home_screen.dart index c3060fd9..25a7e7be 100644 --- a/firka/lib/ui/phone/screens/home/home_screen.dart +++ b/firka/lib/ui/phone/screens/home/home_screen.dart @@ -11,7 +11,7 @@ import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:majesticons_flutter/majesticons_flutter.dart'; - +import '../../../../helpers/db/widget.dart'; import '../../../../helpers/debug_helper.dart'; import '../../../../l10n/app_localizations.dart'; import '../../../widget/firka_icon.dart'; @@ -19,7 +19,6 @@ import '../../pages/extras/extras.dart'; import '../../pages/extras/main_error.dart'; import '../../pages/home/home_grades_subject.dart'; import '../../pages/home/home_timetable.dart'; -import '../debug/debug_screen.dart'; class HomeScreen extends StatefulWidget { final AppInitialization data; @@ -85,10 +84,12 @@ class _HomeScreenState extends State { var now = timeNow(); var start = now.subtract(Duration(days: now.weekday - 1)); - var end = start.add(Duration(days: 6)); + var end = start.add(Duration(days: 7)); res = await data.client.getTimeTable(start, end, forceCache: false); + await WidgetCacheHelper.updateWidgetCache(appStyle, data.client); + if (res.err != null) throw res.err!; } catch (e) { activeToast = ActiveToastType.error;