Skip to content

Commit

Permalink
Improve course units loading behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
bdmendes committed Sep 28, 2023
1 parent 172d85e commit 3df1b74
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 57 deletions.
16 changes: 15 additions & 1 deletion uni/lib/view/common_widgets/pages_layouts/general/general.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,28 @@ import 'package:uni/view/profile/profile.dart';
/// Page with a hamburger menu and the user profile picture
abstract class GeneralPageViewState<T extends StatefulWidget> extends State<T> {
final double borderMargin = 18;
bool _loadedOnce = false;
bool _loading = true;

Future<void> onRefresh(BuildContext context);

Future<void> onLoad(BuildContext context) async {}

@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));
}

Expand Down
121 changes: 65 additions & 56 deletions uni/lib/view/course_unit_info/course_unit_info.dart
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,28 +25,45 @@ class CourseUnitDetailPageView extends StatefulWidget {

class CourseUnitDetailPageViewState
extends SecondaryPageViewState<CourseUnitDetailPageView> {
RequestStatus _status = RequestStatus.busy;

Future<void> loadInfo({required bool force}) async {
setState(() {
_status = RequestStatus.busy;
});
final courseUnitsProvider =
Provider.of<CourseUnitsInfoProvider>(context, listen: false);
final session = context.read<SessionProvider>().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
Expand Down Expand Up @@ -91,50 +110,40 @@ class CourseUnitDetailPageViewState
}

Widget _courseUnitSheetView(BuildContext context) {
return LazyConsumer<CourseUnitsInfoProvider>(
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<CourseUnitsInfoProvider>()
.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<CourseUnitsInfoProvider>(
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<CourseUnitsInfoProvider>()
.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,
);
}
}

0 comments on commit 3df1b74

Please sign in to comment.