Skip to content

Commit

Permalink
Refactor/lazy provider (#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sirze01 authored Jul 14, 2023
2 parents 0525ff9 + 884e3b6 commit b726c30
Show file tree
Hide file tree
Showing 79 changed files with 1,409 additions and 1,253 deletions.
4 changes: 2 additions & 2 deletions uni/android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ subprojects {
project.evaluationDependsOn(':app')
}

task clean(type: Delete) {
tasks.register("clean", Delete) {
delete rootProject.buildDir
}
}
2 changes: 1 addition & 1 deletion uni/lib/controller/background_workers/notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class NotificationManager {
}

void initializeNotifications() async {
//guarentees that the execution is only done once in the lifetime of the app.
// guarantees that the execution is only done once in the lifetime of the app.
if (_initialized) return;
_initialized = true;
_initFlutterNotificationsPlugin();
Expand Down
149 changes: 0 additions & 149 deletions uni/lib/controller/load_info.dart

This file was deleted.

23 changes: 19 additions & 4 deletions uni/lib/controller/local_storage/app_shared_preferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ import 'package:uni/utils/favorite_widget_type.dart';
/// This database stores the user's student number, password and favorite
/// widgets.
class AppSharedPreferences {
static const lastUpdateTimeKeySuffix = "_last_update_time";
static const String userNumber = 'user_number';
static const String userPw = 'user_password';
static const String userFaculties = 'user_faculties';
static const String termsAndConditions = 'terms_and_conditions';
static const String areTermsAndConditionsAcceptedKey = 'is_t&c_accepted';
static const String tuitionNotificationsToggleKey = "tuition_notification_toogle";
static const String tuitionNotificationsToggleKey =
"tuition_notification_toogle";
static const String themeMode = 'theme_mode';
static const int keyLength = 32;
static const int ivLength = 16;
Expand All @@ -33,6 +35,20 @@ class AppSharedPreferences {
static const String filteredExamsTypes = 'filtered_exam_types';
static final List<String> defaultFilteredExamTypes = Exam.displayedTypes;

/// Returns the last time the data with given key was updated.
static Future<DateTime?> getLastDataClassUpdateTime(String dataKey) async {
final prefs = await SharedPreferences.getInstance();
final lastUpdateTime = prefs.getString(dataKey + lastUpdateTimeKeySuffix);
return lastUpdateTime != null ? DateTime.parse(lastUpdateTime) : null;
}

/// Sets the last time the data with given key was updated.
static Future<void> setLastDataClassUpdateTime(
String dataKey, DateTime dateTime) async {
final prefs = await SharedPreferences.getInstance();
prefs.setString(dataKey + lastUpdateTimeKeySuffix, dateTime.toString());
}

/// Saves the user's student number, password and faculties.
static Future savePersistentUserInfo(user, pass, faculties) async {
final prefs = await SharedPreferences.getInstance();
Expand Down Expand Up @@ -203,14 +219,13 @@ class AppSharedPreferences {
return encrypt.Encrypter(encrypt.AES(key));
}

static Future<bool> getTuitionNotificationToggle() async{
static Future<bool> getTuitionNotificationToggle() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getBool(tuitionNotificationsToggleKey) ?? true;
}

static setTuitionNotificationToggle(bool value) async{
static setTuitionNotificationToggle(bool value) async {
final prefs = await SharedPreferences.getInstance();
prefs.setBool(tuitionNotificationsToggleKey, value);
}

}
16 changes: 7 additions & 9 deletions uni/lib/controller/networking/network_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:logger/logger.dart';
import 'package:synchronized/synchronized.dart';
import 'package:uni/controller/local_storage/app_shared_preferences.dart';
import 'package:uni/model/entities/session.dart';
import 'package:uni/view/navigation_service.dart';

extension UriString on String {
/// Converts a [String] to an [Uri].
Expand All @@ -16,13 +17,9 @@ extension UriString on String {
/// Manages the networking of the app.
class NetworkRouter {
static http.Client? httpClient;

static const int loginRequestTimeout = 20;

static Lock loginLock = Lock();

static Function onReloginFail = () {};

/// Creates an authenticated [Session] on the given [faculty] with the
/// given username [user] and password [pass].
static Future<Session> login(String user, String pass, List<String> faculties,
Expand Down Expand Up @@ -52,7 +49,7 @@ class NetworkRouter {
}

/// Determines if a re-login with the [session] is possible.
static Future<bool> relogin(Session session) {
static Future<bool> reLogin(Session session) {
return loginLock.synchronized(() async {
if (!session.persistentSession) {
return false;
Expand Down Expand Up @@ -94,10 +91,11 @@ class NetworkRouter {

/// Returns the response body of the login in Sigarra
/// given username [user] and password [pass].
static Future<String> loginInSigarra(String user, String pass, List<String> faculties) async {
static Future<String> loginInSigarra(
String user, String pass, List<String> faculties) async {
final String url =
'${NetworkRouter.getBaseUrls(faculties)[0]}vld_validacao.validacao';

final response = await http.post(url.toUri(), body: {
'p_user': user,
'p_pass': pass
Expand Down Expand Up @@ -149,12 +147,12 @@ class NetworkRouter {
return response;
} else if (response.statusCode == 403 && !(await userLoggedIn(session))) {
// HTTP403 - Forbidden
final bool reLoginSuccessful = await relogin(session);
final bool reLoginSuccessful = await reLogin(session);
if (reLoginSuccessful) {
headers['cookie'] = session.cookies;
return http.get(url.toUri(), headers: headers);
} else {
onReloginFail();
NavigationService.logout();
Logger().e('Login failed');
return Future.error('Login failed');
}
Expand Down
18 changes: 0 additions & 18 deletions uni/lib/controller/on_start_up.dart

This file was deleted.

46 changes: 15 additions & 31 deletions uni/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,18 @@ import 'package:provider/provider.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:uni/controller/background_workers/background_callback.dart';
import 'package:uni/controller/local_storage/app_shared_preferences.dart';
import 'package:uni/controller/on_start_up.dart';
import 'package:uni/model/providers/bus_stop_provider.dart';
import 'package:uni/model/providers/calendar_provider.dart';
import 'package:uni/model/providers/exam_provider.dart';
import 'package:uni/model/providers/faculty_locations_provider.dart';
import 'package:uni/model/providers/favorite_cards_provider.dart';
import 'package:uni/model/providers/home_page_editing_mode_provider.dart';
import 'package:uni/model/providers/last_user_info_provider.dart';
import 'package:uni/model/providers/lecture_provider.dart';
import 'package:uni/model/providers/library_occupation_provider.dart';
import 'package:uni/model/providers/profile_state_provider.dart';
import 'package:uni/model/providers/reference_provider.dart';
import 'package:uni/model/providers/restaurant_provider.dart';
import 'package:uni/model/providers/session_provider.dart';
import 'package:uni/model/providers/lazy/bus_stop_provider.dart';
import 'package:uni/model/providers/lazy/calendar_provider.dart';
import 'package:uni/model/providers/lazy/exam_provider.dart';
import 'package:uni/model/providers/lazy/faculty_locations_provider.dart';
import 'package:uni/model/providers/lazy/home_page_provider.dart';
import 'package:uni/model/providers/lazy/lecture_provider.dart';
import 'package:uni/model/providers/lazy/library_occupation_provider.dart';
import 'package:uni/model/providers/lazy/reference_provider.dart';
import 'package:uni/model/providers/lazy/restaurant_provider.dart';
import 'package:uni/model/providers/startup/profile_provider.dart';
import 'package:uni/model/providers/startup/session_provider.dart';
import 'package:uni/model/providers/state_providers.dart';
import 'package:uni/model/providers/user_faculties_provider.dart';
import 'package:uni/utils/drawer_items.dart';
import 'package:uni/view/about/about.dart';
import 'package:uni/view/bug_report/bug_report.dart';
Expand All @@ -34,10 +30,10 @@ import 'package:uni/view/common_widgets/page_transition.dart';
import 'package:uni/view/course_units/course_units.dart';
import 'package:uni/view/exams/exams.dart';
import 'package:uni/view/home/home.dart';
import 'package:uni/view/library/library.dart';
import 'package:uni/view/locations/locations.dart';
import 'package:uni/view/logout_route.dart';
import 'package:uni/view/navigation_service.dart';
import 'package:uni/view/library/library.dart';
import 'package:uni/view/restaurant/restaurant_page_view.dart';
import 'package:uni/view/schedule/schedule.dart';
import 'package:uni/view/splash/splash.dart';
Expand All @@ -56,18 +52,14 @@ Future<void> main() async {
ExamProvider(),
BusStopProvider(),
RestaurantProvider(),
ProfileStateProvider(),
ProfileProvider(),
SessionProvider(),
CalendarProvider(),
LibraryOccupationProvider(),
FacultyLocationsProvider(),
LastUserInfoProvider(),
UserFacultiesProvider(),
FavoriteCardsProvider(),
HomePageEditingModeProvider(),
HomePageProvider(),
ReferenceProvider());

OnStartUp.onStart(stateProviders.sessionProvider);
WidgetsFlutterBinding.ensureInitialized();

await Workmanager().initialize(workerStartCallback,
Expand Down Expand Up @@ -110,15 +102,7 @@ Future<void> main() async {
create: (context) =>
stateProviders.facultyLocationsProvider),
ChangeNotifierProvider(
create: (context) => stateProviders.lastUserInfoProvider),
ChangeNotifierProvider(
create: (context) =>
stateProviders.userFacultiesProvider),
ChangeNotifierProvider(
create: (context) =>
stateProviders.favoriteCardsProvider),
ChangeNotifierProvider(
create: (context) => stateProviders.homePageEditingMode),
create: (context) => stateProviders.homePageProvider),
ChangeNotifierProvider(
create: (context) => stateProviders.referenceProvider),
],
Expand Down
Loading

0 comments on commit b726c30

Please sign in to comment.