forked from firka/firka
fix: sync data in background if cache exists
This commit is contained in:
@@ -154,8 +154,23 @@ Future<void> _initData(AppInitialization init) async {
|
||||
}
|
||||
logger.fine("Initializing kréta client as: ${token.username}");
|
||||
init.client = KretaClient(token);
|
||||
await init.client!.init();
|
||||
init.client!.renewCache(reInit: false);
|
||||
|
||||
// Don't block first paint on the network: render whatever is already
|
||||
// cached, then stream in student/timetable/grades/etc. as they arrive.
|
||||
unawaited(() async {
|
||||
try {
|
||||
await init.client!.init();
|
||||
} catch (e) {
|
||||
logger.warning("[Init] Failed to initialize KretaClient: $e");
|
||||
if (!isTokenExpired(e)) {
|
||||
init.toastCubit.setActiveToast(.error, e);
|
||||
}
|
||||
}
|
||||
init.homeRefreshCubit.requestRefresh();
|
||||
|
||||
await init.client!.renewCache(reInit: false);
|
||||
init.homeRefreshCubit.requestRefresh();
|
||||
}());
|
||||
|
||||
if (Platform.isIOS) {
|
||||
final expiryDate = token.expiryDate;
|
||||
|
||||
@@ -50,6 +50,7 @@ class _HomeMainScreen extends FirkaState<HomeMainScreen> {
|
||||
|
||||
Widget _buildContent(BuildContext context) {
|
||||
final now = timeNow();
|
||||
final student = widget.data.client!.cache.findStudentOrNull();
|
||||
final infoItems = widget.data.client!.cache.getMessages().findAllSync();
|
||||
final gradeItems = widget.data.client!.cache.getGrades().findAllSync();
|
||||
final testItems = widget.data.client!.cache.getTests().findAllSync();
|
||||
@@ -116,12 +117,8 @@ class _HomeMainScreen extends FirkaState<HomeMainScreen> {
|
||||
clipBehavior: Clip.none,
|
||||
children: [
|
||||
SizedBox(height: 24),
|
||||
WelcomeWidget(
|
||||
widget.data.l10n,
|
||||
now,
|
||||
widget.data.client!.cache.findStudent(),
|
||||
todayLesson,
|
||||
),
|
||||
if (student != null)
|
||||
WelcomeWidget(widget.data.l10n, now, student, todayLesson),
|
||||
SizedBox(height: 48),
|
||||
LessonSlider(todayLesson, testsTomorrow),
|
||||
SizedBox(height: 24),
|
||||
|
||||
@@ -27,10 +27,16 @@ class CacheManager {
|
||||
CacheManager(this.token);
|
||||
|
||||
StudentCacheModel findStudent() {
|
||||
return findStudentOrNull()!;
|
||||
}
|
||||
|
||||
/// Same as [findStudent], but returns null instead of throwing when the
|
||||
/// student hasn't been synced yet (e.g. before the first successful sync).
|
||||
StudentCacheModel? findStudentOrNull() {
|
||||
return isarInit.studentCacheModels
|
||||
.filter()
|
||||
.group(isCurrentStudent)
|
||||
.findFirstSync()!;
|
||||
.findFirstSync();
|
||||
}
|
||||
|
||||
int genCacheKey(Identifiable identifiable) {
|
||||
|
||||
Reference in New Issue
Block a user