diff --git a/uni/lib/view/common_widgets/pages_layouts/general/general.dart b/uni/lib/view/common_widgets/pages_layouts/general/general.dart index ef5753178..70635ab22 100644 --- a/uni/lib/view/common_widgets/pages_layouts/general/general.dart +++ b/uni/lib/view/common_widgets/pages_layouts/general/general.dart @@ -13,6 +13,8 @@ import 'package:uni/view/profile/profile.dart'; /// Page with a hamburger menu and the user profile picture abstract class GeneralPageViewState extends State { final double borderMargin = 18; + bool _loadedOnce = false; + bool _loading = true; Future onRefresh(BuildContext context); @@ -20,7 +22,19 @@ abstract class GeneralPageViewState extends State { @override Widget build(BuildContext context) { - WidgetsBinding.instance.addPostFrameCallback((_) => onLoad(context)); + WidgetsBinding.instance.addPostFrameCallback((_) async { + if (_loadedOnce) { + return; + } + _loadedOnce = true; + setState(() { + _loading = true; + }); + await onLoad(context); + setState(() { + _loading = false; + }); + }); return getScaffold(context, getBody(context)); } diff --git a/uni/lib/view/course_unit_info/course_unit_info.dart b/uni/lib/view/course_unit_info/course_unit_info.dart index ade00b29d..9742afc37 100644 --- a/uni/lib/view/course_unit_info/course_unit_info.dart +++ b/uni/lib/view/course_unit_info/course_unit_info.dart @@ -1,18 +1,20 @@ import 'package:flutter/material.dart'; import 'package:provider/provider.dart'; +import 'package:sentry_flutter/sentry_flutter.dart'; import 'package:uni/generated/l10n.dart'; import 'package:uni/model/entities/course_units/course_unit.dart'; import 'package:uni/model/providers/lazy/course_units_info_provider.dart'; import 'package:uni/model/providers/startup/session_provider.dart'; +import 'package:uni/model/request_status.dart'; import 'package:uni/view/common_widgets/page_title.dart'; import 'package:uni/view/common_widgets/pages_layouts/secondary/secondary.dart'; import 'package:uni/view/common_widgets/request_dependent_widget_builder.dart'; import 'package:uni/view/course_unit_info/widgets/course_unit_classes.dart'; import 'package:uni/view/course_unit_info/widgets/course_unit_sheet.dart'; -import 'package:uni/view/lazy_consumer.dart'; class CourseUnitDetailPageView extends StatefulWidget { const CourseUnitDetailPageView(this.courseUnit, {super.key}); + final CourseUnit courseUnit; @override @@ -23,28 +25,45 @@ class CourseUnitDetailPageView extends StatefulWidget { class CourseUnitDetailPageViewState extends SecondaryPageViewState { + RequestStatus _status = RequestStatus.busy; + Future loadInfo({required bool force}) async { + setState(() { + _status = RequestStatus.busy; + }); final courseUnitsProvider = Provider.of(context, listen: false); final session = context.read().session; - final courseUnitSheet = - courseUnitsProvider.courseUnitsSheets[widget.courseUnit]; - if (courseUnitSheet == null || force) { - await courseUnitsProvider.fetchCourseUnitSheet( - widget.courseUnit, - session, - ); - } + try { + final courseUnitSheet = + courseUnitsProvider.courseUnitsSheets[widget.courseUnit]; + if (courseUnitSheet == null || force) { + await courseUnitsProvider.fetchCourseUnitSheet( + widget.courseUnit, + session, + ); + } - final courseUnitClasses = - courseUnitsProvider.courseUnitsClasses[widget.courseUnit]; - if (courseUnitClasses == null || force) { - await courseUnitsProvider.fetchCourseUnitClasses( - widget.courseUnit, - session, - ); + final courseUnitClasses = + courseUnitsProvider.courseUnitsClasses[widget.courseUnit]; + if (courseUnitClasses == null || force) { + await courseUnitsProvider.fetchCourseUnitClasses( + widget.courseUnit, + session, + ); + } + } catch (e, stackTrace) { + await Sentry.captureException(e, stackTrace: stackTrace); + setState(() { + _status = RequestStatus.failed; + }); + return; } + + setState(() { + _status = RequestStatus.successful; + }); } @override @@ -91,50 +110,40 @@ class CourseUnitDetailPageViewState } Widget _courseUnitSheetView(BuildContext context) { - return LazyConsumer( - builder: (context, courseUnitsInfoProvider) { - return RequestDependentWidgetBuilder( - onNullContent: Center( - child: Text( - S.of(context).no_info, - textAlign: TextAlign.center, - ), - ), - status: courseUnitsInfoProvider.status, - builder: () => CourseUnitSheetView( - courseUnitsInfoProvider.courseUnitsSheets[widget.courseUnit]!, - ), - hasContentPredicate: - courseUnitsInfoProvider.courseUnitsSheets[widget.courseUnit] != - null && - courseUnitsInfoProvider.courseUnitsSheets[widget.courseUnit]! - .sections.isNotEmpty, - ); - }, + final sheet = context + .read() + .courseUnitsSheets[widget.courseUnit]; + return RequestDependentWidgetBuilder( + onNullContent: Center( + child: Text( + S.of(context).no_info, + textAlign: TextAlign.center, + ), + ), + status: _status, + builder: () => CourseUnitSheetView( + sheet!, + ), + hasContentPredicate: sheet != null && sheet.sections.isNotEmpty, ); } Widget _courseUnitClassesView(BuildContext context) { - return LazyConsumer( - builder: (context, courseUnitsInfoProvider) { - return RequestDependentWidgetBuilder( - onNullContent: Center( - child: Text( - S.of(context).no_class, - textAlign: TextAlign.center, - ), - ), - status: courseUnitsInfoProvider.status, - builder: () => CourseUnitClassesView( - courseUnitsInfoProvider.courseUnitsClasses[widget.courseUnit]!, - ), - hasContentPredicate: - courseUnitsInfoProvider.courseUnitsClasses[widget.courseUnit] != - null && - courseUnitsInfoProvider - .courseUnitsClasses[widget.courseUnit]!.isNotEmpty, - ); - }, + final classes = context + .read() + .courseUnitsClasses[widget.courseUnit]; + return RequestDependentWidgetBuilder( + onNullContent: Center( + child: Text( + S.of(context).no_class, + textAlign: TextAlign.center, + ), + ), + status: _status, + builder: () => CourseUnitClassesView( + classes!, + ), + hasContentPredicate: classes != null && classes.isNotEmpty, ); } }