Skip to content

Commit

Permalink
Remove RequestDependentWidgetBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
bdmendes committed Dec 14, 2023
1 parent fed2cb9 commit 119e48d
Show file tree
Hide file tree
Showing 41 changed files with 803 additions and 833 deletions.
3 changes: 2 additions & 1 deletion uni/lib/controller/local_storage/preferences_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import 'package:uni/utils/favorite_widget_type.dart';
/// This database stores the user's student number, password and favorite
/// widgets.
class PreferencesController {
static SharedPreferences get prefs => Application.sharedPreferences;
// TODO: Initilizate this also on workmanager
static late SharedPreferences prefs;

static final iv = encrypt.IV.fromBase64('jF9jjdSEPgsKnf0jCl1GAQ==');
static final key =
Expand Down
4 changes: 1 addition & 3 deletions uni/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ Future<Widget> firstRoute() async {
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();

Application.sharedPreferences = await SharedPreferences.getInstance();
PreferencesController.prefs = await SharedPreferences.getInstance();

final stateProviders = StateProviders(
LectureProvider(),
Expand Down Expand Up @@ -165,8 +165,6 @@ class Application extends StatefulWidget {

static GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

static late SharedPreferences sharedPreferences;

@override
State<Application> createState() => ApplicationState();
}
Expand Down
10 changes: 5 additions & 5 deletions uni/lib/model/providers/lazy/bus_stop_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@ import 'dart:async';
import 'package:uni/controller/fetchers/departures_fetcher.dart';
import 'package:uni/controller/local_storage/database/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/providers/state_providers.dart';

class BusStopProvider extends StateProviderNotifier<Map<String, BusStopData>> {
BusStopProvider() : super(cacheDuration: null);

@override
Future<Map<String, BusStopData>> loadFromStorage() {
Future<Map<String, BusStopData>> loadFromStorage(
StateProviders stateProviders,
) {
final busStopsDb = AppBusStopDatabase();
return busStopsDb.busStops();
}

@override
Future<Map<String, BusStopData>> loadFromRemote(
Session session,
Profile profile,
StateProviders stateProviders,
) async {
return fetchUserBusTrips(state!);
}
Expand Down
19 changes: 11 additions & 8 deletions uni/lib/model/providers/lazy/calendar_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,27 @@ 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/providers/state_providers.dart';

class CalendarProvider extends StateProviderNotifier<List<CalendarEvent>> {
CalendarProvider() : super(cacheDuration: const Duration(days: 30));

@override
Future<List<CalendarEvent>> loadFromRemote(
Session session,
Profile profile,
Future<List<CalendarEvent>> loadFromStorage(
StateProviders stateProviders,
) async {
final calendar = await CalendarFetcherHtml().getCalendar(session);
final db = CalendarDatabase();
unawaited(db.saveCalendar(calendar));
return calendar;
return db.calendar();
}

@override
Future<List<CalendarEvent>> loadFromStorage() async {
Future<List<CalendarEvent>> loadFromRemote(
StateProviders stateProviders,
) async {
final session = stateProviders.sessionProvider.state!;
final calendar = await CalendarFetcherHtml().getCalendar(session);
final db = CalendarDatabase();
return db.calendar();
unawaited(db.saveCalendar(calendar));
return calendar;
}
}
12 changes: 8 additions & 4 deletions uni/lib/model/providers/lazy/course_units_info_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ 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/providers/state_providers.dart';

typedef SheetsMap = Map<CourseUnit, CourseUnitSheet>;
typedef ClassesMap = Map<CourseUnit, List<CourseUnitClass>>;

class CourseUnitsInfoProvider
extends StateProviderNotifier<Tuple2<SheetsMap, ClassesMap>> {
CourseUnitsInfoProvider() : super(cacheDuration: null);
CourseUnitsInfoProvider() : super(cacheDuration: null) {
updateState(const Tuple2({}, {}));
}

UnmodifiableMapView<CourseUnit, CourseUnitSheet> get courseUnitsSheets =>
UnmodifiableMapView(state!.item1);
Expand All @@ -41,14 +44,15 @@ class CourseUnitsInfoProvider

@override
Future<Tuple2<SheetsMap, ClassesMap>> loadFromRemote(
Session session,
Profile profile,
StateProviders stateProviders,
) async {
return const Tuple2({}, {});
}

@override
Future<Tuple2<SheetsMap, ClassesMap>> loadFromStorage() async {
Future<Tuple2<SheetsMap, ClassesMap>> loadFromStorage(
StateProviders stateProviders,
) async {
return const Tuple2({}, {});
}
}
23 changes: 13 additions & 10 deletions uni/lib/model/providers/lazy/exam_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,38 @@ 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/providers/state_providers.dart';

class ExamProvider extends StateProviderNotifier<List<Exam>> {
ExamProvider() : super(cacheDuration: const Duration(days: 1));

@override
Future<List<Exam>> loadFromStorage() async {
Future<List<Exam>> loadFromStorage(StateProviders stateProviders) async {
final db = AppExamsDatabase();
return db.exams();
}

@override
Future<List<Exam>> loadFromRemote(Session session, Profile profile) async {
Future<List<Exam>> loadFromRemote(StateProviders stateProviders) async {
final session = stateProviders.sessionProvider.state!;
final profile = stateProviders.profileProvider.state!;

return fetchUserExams(
ParserExams(),
profile,
session,
profile.courseUnits,
persistentSession:
(PreferencesController.getPersistentUserInfo()) != null,
(PreferencesController.getPersistentUserInfo()) != null,
);
}

Future<List<Exam>> fetchUserExams(
ParserExams parserExams,
Profile profile,
Session session,
List<CourseUnit> userUcs, {
required bool persistentSession,
}) async {
Future<List<Exam>> fetchUserExams(ParserExams parserExams,
Profile profile,
Session session,
List<CourseUnit> userUcs, {
required bool persistentSession,
}) async {
final exams = await ExamFetcher(profile.courses, userUcs)
.extractExams(session, parserExams);

Expand Down
9 changes: 3 additions & 6 deletions uni/lib/model/providers/lazy/faculty_locations_provider.dart
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import 'package:uni/controller/fetchers/location_fetcher/location_fetcher_asset.dart';
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/providers/state_providers.dart';

class FacultyLocationsProvider
extends StateProviderNotifier<List<LocationGroup>> {
FacultyLocationsProvider() : super(cacheDuration: const Duration(days: 30));

@override
Future<List<LocationGroup>> loadFromStorage() {
Future<List<LocationGroup>> loadFromStorage(StateProviders stateProviders) {
return LocationFetcherAsset().getLocations();
}

@override
Future<List<LocationGroup>> loadFromRemote(
Session session,
Profile profile,
) async {
StateProviders stateProviders) async {
return state!;
}
}
9 changes: 5 additions & 4 deletions uni/lib/model/providers/lazy/lecture_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ 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/providers/state_providers.dart';

class LectureProvider extends StateProviderNotifier<List<Lecture>> {
LectureProvider() : super(cacheDuration: const Duration(hours: 6));

@override
Future<List<Lecture>> loadFromStorage() async {
Future<List<Lecture>> loadFromStorage(StateProviders stateProviders) async {
final db = AppLecturesDatabase();
return db.lectures();
}

@override
Future<List<Lecture>> loadFromRemote(Session session, Profile profile) async {
Future<List<Lecture>> loadFromRemote(StateProviders stateProviders) async {
return fetchUserLectures(
session,
profile,
stateProviders.sessionProvider.state!,
stateProviders.profileProvider.state!,
persistentSession:
(PreferencesController.getPersistentUserInfo()) != null,
);
Expand Down
10 changes: 5 additions & 5 deletions uni/lib/model/providers/lazy/library_occupation_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@ import 'dart:async';
import 'package:uni/controller/fetchers/library_occupation_fetcher.dart';
import 'package:uni/controller/local_storage/database/app_library_occupation_database.dart';
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/providers/state_providers.dart';

class LibraryOccupationProvider
extends StateProviderNotifier<LibraryOccupation> {
LibraryOccupationProvider() : super(cacheDuration: const Duration(hours: 1));

@override
Future<LibraryOccupation> loadFromStorage() async {
Future<LibraryOccupation> loadFromStorage(
StateProviders stateProviders) async {
final db = LibraryOccupationDatabase();
return db.occupation();
}

@override
Future<LibraryOccupation> loadFromRemote(
Session session,
Profile profile,
StateProviders stateProviders,
) async {
final session = stateProviders.sessionProvider.state!;
final occupation = await LibraryOccupationFetcherSheets()
.getLibraryOccupationFromSheets(session);

Expand Down
11 changes: 6 additions & 5 deletions uni/lib/model/providers/lazy/reference_provider.dart
Original file line number Diff line number Diff line change
@@ -1,27 +1,28 @@
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:uni/controller/fetchers/reference_fetcher.dart';
import 'package:uni/controller/local_storage/database/app_references_database.dart';
import 'package:uni/controller/parsers/parser_references.dart';
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/providers/state_providers.dart';

class ReferenceProvider extends StateProviderNotifier<List<Reference>> {
ReferenceProvider() : super(cacheDuration: const Duration(hours: 1));

@override
Future<List<Reference>> loadFromStorage() {
Future<List<Reference>> loadFromStorage(StateProviders stateProviders) {
final referencesDb = AppReferencesDatabase();
return referencesDb.references();
}

@override
Future<List<Reference>> loadFromRemote(
Session session,
Profile profile,
) async {
Future<List<Reference>> loadFromRemote(StateProviders stateProviders) async {
final session = stateProviders.sessionProvider.state!;

final response = await ReferenceFetcher().getUserReferenceResponse(session);
final references = await parseReferences(response);

Expand Down
13 changes: 6 additions & 7 deletions uni/lib/model/providers/lazy/restaurant_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,25 @@ import 'dart:async';

import 'package:uni/controller/fetchers/restaurant_fetcher.dart';
import 'package:uni/controller/local_storage/database/app_restaurant_database.dart';
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/providers/state_providers.dart';

class RestaurantProvider extends StateProviderNotifier<List<Restaurant>> {
RestaurantProvider() : super(cacheDuration: const Duration(days: 1));

@override
Future<List<Restaurant>> loadFromStorage() async {
Future<List<Restaurant>> loadFromStorage(
StateProviders stateProviders,
) async {
final restaurantDb = RestaurantDatabase();
final restaurants = await restaurantDb.getRestaurants();
return restaurants;
}

@override
Future<List<Restaurant>> loadFromRemote(
Session session,
Profile profile,
) async {
Future<List<Restaurant>> loadFromRemote(StateProviders stateProviders) async {
final session = stateProviders.sessionProvider.state!;
final restaurants = await RestaurantFetcher().getRestaurants(session);

final db = RestaurantDatabase();
Expand Down
7 changes: 5 additions & 2 deletions uni/lib/model/providers/startup/profile_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ import 'package:uni/model/entities/course_units/course_unit.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/providers/state_providers.dart';

class ProfileProvider extends StateProviderNotifier<Profile> {
ProfileProvider()
: super(cacheDuration: const Duration(days: 1), dependsOnSession: false);

@override
Future<Profile> loadFromStorage() async {
Future<Profile> loadFromStorage(StateProviders stateProviders) async {
final profile = await loadProfile();
profile
..courses = await loadCourses()
Expand All @@ -34,7 +35,9 @@ class ProfileProvider extends StateProviderNotifier<Profile> {
}

@override
Future<Profile> loadFromRemote(Session session, Profile profile) async {
Future<Profile> loadFromRemote(StateProviders stateProviders) async {
final session = stateProviders.sessionProvider.state!;

final profile = await fetchUserInfo(session);

final userBalanceAndFeesLimit = await fetchUserFeesBalanceAndLimit(session);
Expand Down
8 changes: 5 additions & 3 deletions uni/lib/model/providers/startup/session_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ import 'package:uni/controller/local_storage/preferences_controller.dart';
import 'package:uni/controller/networking/network_router.dart';
import 'package:uni/controller/parsers/parser_session.dart';
import 'package:uni/model/entities/login_exceptions.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/providers/state_providers.dart';
import 'package:uni/model/request_status.dart';
import 'package:uni/view/locale_notifier.dart';

class SessionProvider extends StateProviderNotifier<Session> {
SessionProvider()
: super(
cacheDuration: null,
initialStatus: RequestStatus.none,
dependsOnSession: false,
);

@override
Future<Session> loadFromStorage() async {
Future<Session> loadFromStorage(StateProviders stateProviders) async {
final userPersistentInfo = PreferencesController.getPersistentUserInfo();
final faculties = PreferencesController.getUserFaculties();

Expand All @@ -38,7 +40,7 @@ class SessionProvider extends StateProviderNotifier<Session> {
}

@override
Future<Session> loadFromRemote(Session session, Profile profile) async {
Future<Session> loadFromRemote(StateProviders stateProviders) async {
return state!;
}

Expand Down
Loading

0 comments on commit 119e48d

Please sign in to comment.