diff --git a/firka/lib/api/client/kreta_client.dart b/firka/lib/api/client/kreta_client.dart index 7acf7ebb..4f71d4f0 100644 --- a/firka/lib/api/client/kreta_client.dart +++ b/firka/lib/api/client/kreta_client.dart @@ -199,6 +199,49 @@ class KretaClient { } } + /// Runs [action] without the fetching toast. Sets the error toast on failure. + Future pullRefresh(Future Function() action) async { + try { + await action(); + } catch (e) { + if (!isTokenExpired(e)) { + _toastCubit.setActiveToast(.error, e); + } + } + } + + /// Refetches everything shown on the home feed (not the full school-year cache). + Future refreshHomeFeed() async { + await pullRefresh(() async { + final weekFrom = timeNow().getMonday(); + final weekTo = weekFrom.add(const Duration(days: 7)); + await Future.wait([ + getStudent(), + getTests(), + getHomework(), + renewMessages(), + getGrades(), + getLessons(weekFrom, weekTo), + ]); + // manual link + await getOmissions(); + cache.resolveTeachers(); + }); + } + + /// Fetches lessons in 14-day chunks so [from]..[to] stays under the API range limit. + Future getLessonsCovering(DateTime from, DateTime to) async { + DateTime date = from; + final requests = []; + while (date.isBefore(to)) { + final tmpTo = date.add(const Duration(days: 14)); + final end = tmpTo.isAfter(to) ? to : tmpTo; + requests.add(getLessons(date, end)); + date = tmpTo; + } + await Future.wait(requests); + } + Future _setReauthFlag() async { if (Platform.isIOS) { try { diff --git a/firka/lib/ui/phone/pages/home/home_grades.dart b/firka/lib/ui/phone/pages/home/home_grades.dart index 56d10180..c93a1751 100644 --- a/firka/lib/ui/phone/pages/home/home_grades.dart +++ b/firka/lib/ui/phone/pages/home/home_grades.dart @@ -93,8 +93,13 @@ class _HomeGradesScreen extends FirkaState { GradeSummaryBar(grades: allGrades, l10n: widget.data.l10n), SizedBox(height: 20), Expanded( - child: ListView( - children: [ + child: RefreshIndicator( + onRefresh: () => widget.data.client!.pullRefresh( + () => widget.data.client!.getGrades(), + ), + child: ListView( + physics: const AlwaysScrollableScrollPhysics(), + children: [ Text( widget.data.l10n.your_subjects, style: appStyle.fonts.H_14px.apply( @@ -241,6 +246,7 @@ class _HomeGradesScreen extends FirkaState { ], ), ), + ), ], ), ); diff --git a/firka/lib/ui/phone/pages/home/home_main.dart b/firka/lib/ui/phone/pages/home/home_main.dart index 7aa54f20..dead50e1 100644 --- a/firka/lib/ui/phone/pages/home/home_main.dart +++ b/firka/lib/ui/phone/pages/home/home_main.dart @@ -126,13 +126,13 @@ class _HomeMainScreen extends FirkaState { return Padding( padding: const EdgeInsets.symmetric(horizontal: 20.0), child: RefreshIndicator( - onRefresh: () => Future.value(), + onRefresh: () => widget.data.client!.refreshHomeFeed(), notificationPredicate: (ScrollNotification notification) { return notification.depth == 0; }, triggerMode: RefreshIndicatorTriggerMode.onEdge, - displacement: 0, child: ListView( + physics: const AlwaysScrollableScrollPhysics(), clipBehavior: Clip.none, children: [ SizedBox(height: 24), diff --git a/firka/lib/ui/phone/pages/home/home_omissions.dart b/firka/lib/ui/phone/pages/home/home_omissions.dart index 0115915a..6aa423c9 100644 --- a/firka/lib/ui/phone/pages/home/home_omissions.dart +++ b/firka/lib/ui/phone/pages/home/home_omissions.dart @@ -177,10 +177,15 @@ class _HomeOmissionsScreen extends FirkaState { OmissionBar(), SizedBox(height: 20), Expanded( - child: ListView( - children: [ - Text( - initData.l10n.subjects, + child: RefreshIndicator( + onRefresh: () => widget.data.client!.pullRefresh( + () => widget.data.client!.getOmissions(), + ), + child: ListView( + physics: const AlwaysScrollableScrollPhysics(), + children: [ + Text( + initData.l10n.subjects, style: appStyle.fonts.B_16SB.copyWith( color: appStyle.colors.textPrimary, ), @@ -293,6 +298,7 @@ class _HomeOmissionsScreen extends FirkaState { ], ), ), + ), ], ), ); diff --git a/firka/lib/ui/phone/pages/home/home_timetable.dart b/firka/lib/ui/phone/pages/home/home_timetable.dart index 24f25e50..a8e2ba5b 100644 --- a/firka/lib/ui/phone/pages/home/home_timetable.dart +++ b/firka/lib/ui/phone/pages/home/home_timetable.dart @@ -186,6 +186,7 @@ class _HomeTimetableScreen extends FirkaState listenWhen: (previous, current) => current.refreshTrigger != previous.refreshTrigger, listener: (context, state) { + _lessonsCache.clear(); setState(() {}); }, child: _buildContent(context), @@ -243,7 +244,16 @@ class _HomeTimetableScreen extends FirkaState )..removeWhere(isEvent); final events = List.from(_lessonsFor(day)) ..retainWhere(isEvent); - return TimeTableDayWidget(lessons, events); + return TimeTableDayWidget( + lessons, + events, + onRefresh: () => widget.data.client!.pullRefresh( + () => widget.data.client!.getLessonsCovering( + _currentTabWeekMonday, + _currentTabWeekMonday.add(const Duration(days: 7)), + ), + ), + ); }, ), ), diff --git a/firka/lib/ui/phone/pages/home/home_timetable_mo.dart b/firka/lib/ui/phone/pages/home/home_timetable_mo.dart index acffb117..fc1b2abe 100644 --- a/firka/lib/ui/phone/pages/home/home_timetable_mo.dart +++ b/firka/lib/ui/phone/pages/home/home_timetable_mo.dart @@ -373,11 +373,20 @@ class _HomeTimetableMonthlyScreen left: 20, right: 20, ), - child: GridView.count( - crossAxisCount: 7, - mainAxisSpacing: 16, - mainAxisExtent: 62, - children: ttDays, + child: RefreshIndicator( + onRefresh: () => widget.data.client!.pullRefresh( + () => widget.data.client!.getLessonsCovering( + currentMonthStart, + currentMonthEnd, + ), + ), + child: GridView.count( + physics: const AlwaysScrollableScrollPhysics(), + crossAxisCount: 7, + mainAxisSpacing: 16, + mainAxisExtent: 62, + children: ttDays, + ), ), ), ), diff --git a/firka/lib/ui/phone/widgets/tt_day.dart b/firka/lib/ui/phone/widgets/tt_day.dart index 71cda5ab..f99aa523 100644 --- a/firka/lib/ui/phone/widgets/tt_day.dart +++ b/firka/lib/ui/phone/widgets/tt_day.dart @@ -14,8 +14,14 @@ import 'lesson.dart'; class TimeTableDayWidget extends StatelessWidget { final List lessons; final List events; + final Future Function()? onRefresh; - const TimeTableDayWidget(this.lessons, this.events, {super.key}); + const TimeTableDayWidget( + this.lessons, + this.events, { + this.onRefresh, + super.key, + }); static String simplifyEventName(String e) { return e @@ -58,40 +64,52 @@ class TimeTableDayWidget extends StatelessWidget { @override Widget build(BuildContext context) { if (lessons.isEmpty) { - return Column( - mainAxisAlignment: MainAxisAlignment.center, - crossAxisAlignment: CrossAxisAlignment.center, - children: [ - SvgPicture.asset( - "assets/images/logos/dave.svg", - width: 48, - height: 48, - ), - SizedBox(height: 12), - Text( - initData.l10n.tt_no_classes_l1, - style: appStyle.fonts.B_16R.apply( - color: appStyle.colors.textSecondary, - ), - ), - Text( - initData.l10n.tt_no_classes_l2, - style: appStyle.fonts.B_16R.apply( - color: appStyle.colors.textSecondary, - ), - ), - if (events.isNotEmpty) - ...events.map( - (event) => Center( - child: Text( - simplifyEventName(event.name), - style: appStyle.fonts.B_16R.apply( - color: appStyle.colors.textSecondary, + return RefreshIndicator( + onRefresh: onRefresh ?? () async {}, + child: ListView( + physics: const AlwaysScrollableScrollPhysics(), + padding: const EdgeInsets.only(top: 70 + 16 + 20, left: 20, right: 20), + children: [ + SizedBox( + height: MediaQuery.sizeOf(context).height * 0.45, + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + crossAxisAlignment: CrossAxisAlignment.center, + children: [ + SvgPicture.asset( + "assets/images/logos/dave.svg", + width: 48, + height: 48, ), - ), + SizedBox(height: 12), + Text( + initData.l10n.tt_no_classes_l1, + style: appStyle.fonts.B_16R.apply( + color: appStyle.colors.textSecondary, + ), + ), + Text( + initData.l10n.tt_no_classes_l2, + style: appStyle.fonts.B_16R.apply( + color: appStyle.colors.textSecondary, + ), + ), + if (events.isNotEmpty) + ...events.map( + (event) => Center( + child: Text( + simplifyEventName(event.name), + style: appStyle.fonts.B_16R.apply( + color: appStyle.colors.textSecondary, + ), + ), + ), + ), + ], ), ), - ], + ], + ), ); } @@ -164,15 +182,19 @@ class TimeTableDayWidget extends StatelessWidget { return Padding( padding: const EdgeInsets.only(top: 70 + 16 + 20, left: 20, right: 20), - child: ListView.separated( - separatorBuilder: (context, index) => SizedBox(height: 16), - itemBuilder: (context, index) { - if (ttLessons.length == index) { - return SizedBox(height: 55); - } - return ttLessons[index]; - }, - itemCount: ttLessons.length + 1, + child: RefreshIndicator( + onRefresh: onRefresh ?? () async {}, + child: ListView.separated( + physics: const AlwaysScrollableScrollPhysics(), + separatorBuilder: (context, index) => SizedBox(height: 16), + itemBuilder: (context, index) { + if (ttLessons.length == index) { + return SizedBox(height: 55); + } + return ttLessons[index]; + }, + itemCount: ttLessons.length + 1, + ), ), ); }