forked from firka/firka
home_widget: save info needed for the widget
This commit is contained in:
71
firka/lib/helpers/db/widget.dart
Normal file
71
firka/lib/helpers/db/widget.dart
Normal file
@@ -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<String, dynamic> toJson(FirkaStyle style, List<Lesson> timetable) {
|
||||
List<Map<String, dynamic>> 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<void> 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!)));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -89,6 +89,8 @@ Future<AppInitialization> 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<AppInitialization> initializeApp() async {
|
||||
init.profilePicture = await pfpFile.readAsBytes();
|
||||
}
|
||||
|
||||
await WidgetCacheHelper.updateWidgetCache(appStyle, init.client);
|
||||
|
||||
return init;
|
||||
}
|
||||
|
||||
|
||||
@@ -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<HomeScreen> {
|
||||
|
||||
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;
|
||||
|
||||
Reference in New Issue
Block a user