Skip to content

Commit

Permalink
Handle provider errors on the abstract class (#977)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisDuarte1 authored Sep 29, 2023
2 parents 46ab53b + 9d145da commit 14e7f05
Show file tree
Hide file tree
Showing 22 changed files with 250 additions and 330 deletions.
24 changes: 7 additions & 17 deletions uni/lib/model/providers/lazy/bus_stop_provider.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import 'dart:async';
import 'dart:collection';

import 'package:logger/logger.dart';
import 'package:uni/controller/fetchers/departures_fetcher.dart';
import 'package:uni/controller/local_storage/app_bus_stop_database.dart';
import 'package:uni/model/entities/bus_stop.dart';
import 'package:uni/model/entities/profile.dart';
import 'package:uni/model/entities/session.dart';
import 'package:uni/model/providers/state_provider_notifier.dart';
import 'package:uni/model/request_status.dart';

class BusStopProvider extends StateProviderNotifier {
BusStopProvider() : super(dependsOnSession: false, cacheDuration: null);
Expand All @@ -33,20 +31,14 @@ class BusStopProvider extends StateProviderNotifier {
}

Future<void> fetchUserBusTrips() async {
try {
for (final stopCode in configuredBusStops.keys) {
final stopTrips = await DeparturesFetcher.getNextArrivalsStop(
stopCode,
configuredBusStops[stopCode]!,
);
_configuredBusStops[stopCode]?.trips = stopTrips;
}
_timeStamp = DateTime.now();
updateStatus(RequestStatus.successful);
} catch (e) {
Logger().e('Failed to get Bus Stop information');
updateStatus(RequestStatus.failed);
for (final stopCode in configuredBusStops.keys) {
final stopTrips = await DeparturesFetcher.getNextArrivalsStop(
stopCode,
configuredBusStops[stopCode]!,
);
_configuredBusStops[stopCode]?.trips = stopTrips;
}
_timeStamp = DateTime.now();
}

Future<void> addUserBusStop(String stopCode, BusStopData stopData) async {
Expand All @@ -59,7 +51,6 @@ class BusStopProvider extends StateProviderNotifier {
_configuredBusStops[stopCode] = stopData;
}

updateStatus(RequestStatus.busy);
await fetchUserBusTrips();

final db = AppBusStopDatabase();
Expand All @@ -69,7 +60,6 @@ class BusStopProvider extends StateProviderNotifier {
Future<void> removeUserBusStop(
String stopCode,
) async {
updateStatus(RequestStatus.busy);
_configuredBusStops.remove(stopCode);
notifyListeners();

Expand Down
14 changes: 3 additions & 11 deletions uni/lib/model/providers/lazy/calendar_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:uni/model/entities/calendar_event.dart';
import 'package:uni/model/entities/profile.dart';
import 'package:uni/model/entities/session.dart';
import 'package:uni/model/providers/state_provider_notifier.dart';
import 'package:uni/model/request_status.dart';

class CalendarProvider extends StateProviderNotifier {
CalendarProvider()
Expand All @@ -23,16 +22,9 @@ class CalendarProvider extends StateProviderNotifier {
}

Future<void> fetchCalendar(Session session) async {
try {
_calendar = await CalendarFetcherHtml().getCalendar(session);

final db = CalendarDatabase();
unawaited(db.saveCalendar(calendar));

updateStatus(RequestStatus.successful);
} catch (e) {
updateStatus(RequestStatus.failed);
}
_calendar = await CalendarFetcherHtml().getCalendar(session);
final db = CalendarDatabase();
unawaited(db.saveCalendar(calendar));
}

@override
Expand Down
27 changes: 4 additions & 23 deletions uni/lib/model/providers/lazy/course_units_info_provider.dart
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import 'dart:collection';

import 'package:logger/logger.dart';
import 'package:uni/controller/fetchers/course_units_fetcher/course_units_info_fetcher.dart';
import 'package:uni/model/entities/course_units/course_unit.dart';
import 'package:uni/model/entities/course_units/course_unit_class.dart';
import 'package:uni/model/entities/course_units/course_unit_sheet.dart';
import 'package:uni/model/entities/profile.dart';
import 'package:uni/model/entities/session.dart';
import 'package:uni/model/providers/state_provider_notifier.dart';
import 'package:uni/model/request_status.dart';

class CourseUnitsInfoProvider extends StateProviderNotifier {
CourseUnitsInfoProvider()
Expand All @@ -26,33 +24,16 @@ class CourseUnitsInfoProvider extends StateProviderNotifier {
CourseUnit courseUnit,
Session session,
) async {
updateStatus(RequestStatus.busy);
try {
_courseUnitsSheets[courseUnit] = await CourseUnitsInfoFetcher()
.fetchCourseUnitSheet(session, courseUnit.occurrId);
} catch (e) {
updateStatus(RequestStatus.failed);
Logger().e('Failed to get course unit sheet for ${courseUnit.name}: $e');
return;
}
updateStatus(RequestStatus.successful);
_courseUnitsSheets[courseUnit] = await CourseUnitsInfoFetcher()
.fetchCourseUnitSheet(session, courseUnit.occurrId);
}

Future<void> fetchCourseUnitClasses(
CourseUnit courseUnit,
Session session,
) async {
updateStatus(RequestStatus.busy);
try {
_courseUnitsClasses[courseUnit] = await CourseUnitsInfoFetcher()
.fetchCourseUnitClasses(session, courseUnit.occurrId);
} catch (e) {
updateStatus(RequestStatus.failed);
Logger()
.e('Failed to get course unit classes for ${courseUnit.name}: $e');
return;
}
updateStatus(RequestStatus.successful);
_courseUnitsClasses[courseUnit] = await CourseUnitsInfoFetcher()
.fetchCourseUnitClasses(session, courseUnit.occurrId);
}

@override
Expand Down
20 changes: 7 additions & 13 deletions uni/lib/model/providers/lazy/exam_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'package:uni/model/entities/exam.dart';
import 'package:uni/model/entities/profile.dart';
import 'package:uni/model/entities/session.dart';
import 'package:uni/model/providers/state_provider_notifier.dart';
import 'package:uni/model/request_status.dart';

class ExamProvider extends StateProviderNotifier {
ExamProvider()
Expand Down Expand Up @@ -56,21 +55,16 @@ class ExamProvider extends StateProviderNotifier {
List<CourseUnit> userUcs, {
required bool persistentSession,
}) async {
try {
final exams = await ExamFetcher(profile.courses, userUcs)
.extractExams(session, parserExams);
final exams = await ExamFetcher(profile.courses, userUcs)
.extractExams(session, parserExams);

exams.sort((exam1, exam2) => exam1.begin.compareTo(exam2.begin));
exams.sort((exam1, exam2) => exam1.begin.compareTo(exam2.begin));

if (persistentSession) {
await AppExamsDatabase().saveNewExams(exams);
}

_exams = exams;
updateStatus(RequestStatus.successful);
} catch (e) {
updateStatus(RequestStatus.failed);
if (persistentSession) {
await AppExamsDatabase().saveNewExams(exams);
}

_exams = exams;
}

Future<void> updateFilteredExams() async {
Expand Down
7 changes: 1 addition & 6 deletions uni/lib/model/providers/lazy/faculty_locations_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'package:uni/model/entities/location_group.dart';
import 'package:uni/model/entities/profile.dart';
import 'package:uni/model/entities/session.dart';
import 'package:uni/model/providers/state_provider_notifier.dart';
import 'package:uni/model/request_status.dart';

class FacultyLocationsProvider extends StateProviderNotifier {
FacultyLocationsProvider()
Expand All @@ -17,13 +16,9 @@ class FacultyLocationsProvider extends StateProviderNotifier {

@override
Future<void> loadFromStorage() async {
updateStatus(RequestStatus.busy);
_locations = await LocationFetcherAsset().getLocations();
updateStatus(RequestStatus.successful);
}

@override
Future<void> loadFromRemote(Session session, Profile profile) async {
updateStatus(RequestStatus.successful);
}
Future<void> loadFromRemote(Session session, Profile profile) async {}
}
5 changes: 1 addition & 4 deletions uni/lib/model/providers/lazy/home_page_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import 'package:uni/controller/local_storage/app_shared_preferences.dart';
import 'package:uni/model/entities/profile.dart';
import 'package:uni/model/entities/session.dart';
import 'package:uni/model/providers/state_provider_notifier.dart';
import 'package:uni/model/request_status.dart';
import 'package:uni/utils/favorite_widget_type.dart';

class HomePageProvider extends StateProviderNotifier {
Expand All @@ -20,9 +19,7 @@ class HomePageProvider extends StateProviderNotifier {
}

@override
Future<void> loadFromRemote(Session session, Profile profile) async {
updateStatus(RequestStatus.successful);
}
Future<void> loadFromRemote(Session session, Profile profile) async {}

void setHomePageEditingMode({required bool editingMode}) {
_isEditing = editingMode;
Expand Down
20 changes: 7 additions & 13 deletions uni/lib/model/providers/lazy/lecture_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'package:uni/model/entities/lecture.dart';
import 'package:uni/model/entities/profile.dart';
import 'package:uni/model/entities/session.dart';
import 'package:uni/model/providers/state_provider_notifier.dart';
import 'package:uni/model/request_status.dart';

class LectureProvider extends StateProviderNotifier {
LectureProvider()
Expand Down Expand Up @@ -42,20 +41,15 @@ class LectureProvider extends StateProviderNotifier {
required bool persistentSession,
ScheduleFetcher? fetcher,
}) async {
try {
final lectures =
await getLecturesFromFetcherOrElse(fetcher, session, profile);
final lectures =
await getLecturesFromFetcherOrElse(fetcher, session, profile);

if (persistentSession) {
final db = AppLecturesDatabase();
await db.saveNewLectures(lectures);
}

_lectures = lectures;
updateStatus(RequestStatus.successful);
} catch (e) {
updateStatus(RequestStatus.failed);
if (persistentSession) {
final db = AppLecturesDatabase();
await db.saveNewLectures(lectures);
}

_lectures = lectures;
}

Future<List<Lecture>> getLecturesFromFetcherOrElse(
Expand Down
15 changes: 4 additions & 11 deletions uni/lib/model/providers/lazy/library_occupation_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import 'package:uni/model/entities/library_occupation.dart';
import 'package:uni/model/entities/profile.dart';
import 'package:uni/model/entities/session.dart';
import 'package:uni/model/providers/state_provider_notifier.dart';
import 'package:uni/model/request_status.dart';

class LibraryOccupationProvider extends StateProviderNotifier {
LibraryOccupationProvider()
Expand All @@ -28,16 +27,10 @@ class LibraryOccupationProvider extends StateProviderNotifier {
}

Future<void> fetchLibraryOccupation(Session session) async {
try {
_occupation = await LibraryOccupationFetcherSheets()
.getLibraryOccupationFromSheets(session);
_occupation = await LibraryOccupationFetcherSheets()
.getLibraryOccupationFromSheets(session);

final db = LibraryOccupationDatabase();
unawaited(db.saveOccupation(_occupation!));

updateStatus(RequestStatus.successful);
} catch (e) {
updateStatus(RequestStatus.failed);
}
final db = LibraryOccupationDatabase();
unawaited(db.saveOccupation(_occupation!));
}
}
16 changes: 4 additions & 12 deletions uni/lib/model/providers/lazy/reference_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:uni/model/entities/profile.dart';
import 'package:uni/model/entities/reference.dart';
import 'package:uni/model/entities/session.dart';
import 'package:uni/model/providers/state_provider_notifier.dart';
import 'package:uni/model/request_status.dart';

class ReferenceProvider extends StateProviderNotifier {
ReferenceProvider()
Expand All @@ -31,18 +30,11 @@ class ReferenceProvider extends StateProviderNotifier {
}

Future<void> fetchUserReferences(Session session) async {
try {
final response =
await ReferenceFetcher().getUserReferenceResponse(session);
final response = await ReferenceFetcher().getUserReferenceResponse(session);

_references = await parseReferences(response);
_references = await parseReferences(response);

updateStatus(RequestStatus.successful);

final referencesDb = AppReferencesDatabase();
unawaited(referencesDb.saveNewReferences(references));
} catch (e) {
updateStatus(RequestStatus.failed);
}
final referencesDb = AppReferencesDatabase();
unawaited(referencesDb.saveNewReferences(references));
}
}
15 changes: 4 additions & 11 deletions uni/lib/model/providers/lazy/restaurant_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:uni/model/entities/profile.dart';
import 'package:uni/model/entities/restaurant.dart';
import 'package:uni/model/entities/session.dart';
import 'package:uni/model/providers/state_provider_notifier.dart';
import 'package:uni/model/request_status.dart';

class RestaurantProvider extends StateProviderNotifier {
RestaurantProvider()
Expand Down Expand Up @@ -37,18 +36,12 @@ class RestaurantProvider extends StateProviderNotifier {
}

Future<void> fetchRestaurants(Session session) async {
try {
final restaurants = await RestaurantFetcher().getRestaurants(session);
final restaurants = await RestaurantFetcher().getRestaurants(session);

final db = RestaurantDatabase();
unawaited(db.saveRestaurants(restaurants));
final db = RestaurantDatabase();
unawaited(db.saveRestaurants(restaurants));

_restaurants = filterPastMeals(restaurants);

updateStatus(RequestStatus.successful);
} catch (e) {
updateStatus(RequestStatus.failed);
}
_restaurants = filterPastMeals(restaurants);
}

Future<void> toggleFavoriteRestaurant(
Expand Down
Loading

0 comments on commit 14e7f05

Please sign in to comment.