Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/lazy provider #823

Merged
merged 25 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
77eb123
Remove unused faculties provider
bdmendes Jul 6, 2023
3c531b6
Implement LazyConsumer
bdmendes Jul 6, 2023
972a189
Move local data loaders to the providers
bdmendes Jul 7, 2023
b1e1070
Move remote fetching logic to providers
bdmendes Jul 7, 2023
4842265
Combine favorite cards and home page editing providers
bdmendes Jul 7, 2023
1beb069
Fix course units loading
bdmendes Jul 7, 2023
d8c6730
Simplify RequestDependentWidgetBuilder
bdmendes Jul 7, 2023
0365cf9
Further simplify RequestDependentWidget
bdmendes Jul 7, 2023
a3f4b4e
Fix tests
bdmendes Jul 7, 2023
4e14bdf
Fix last update timestamp widget
bdmendes Jul 7, 2023
6aee552
Make each page responsible for refreshing
bdmendes Jul 7, 2023
b23fe1b
Refresh home page
bdmendes Jul 7, 2023
068a02a
Do not wait for session provider if not dependant on it
bdmendes Jul 10, 2023
c7c6854
Refine request status logic
bdmendes Jul 10, 2023
5fa3a75
Delete load info file
bdmendes Jul 10, 2023
1d13aa3
Delete onstartup file
bdmendes Jul 10, 2023
a231d07
Add provider caching
bdmendes Jul 10, 2023
18d2b44
Merge remote-tracking branch 'origin/develop' into refactor/lazy-prov…
bdmendes Jul 10, 2023
9304191
Merge remote-tracking branch 'origin/develop' into refactor/lazy-prov…
bdmendes Jul 10, 2023
dd041a1
Merge remote-tracking branch 'origin/develop' into refactor/lazy-prov…
bdmendes Jul 12, 2023
c41385e
Make generic expansion card stateless
bdmendes Jul 12, 2023
8142b3f
Encapsulate card refresh logic
bdmendes Jul 12, 2023
4dde44f
Do not allow concurrent refreshes
bdmendes Jul 12, 2023
d3a41d5
Merge remote-tracking branch 'origin/develop' into refactor/lazy-prov…
bdmendes Jul 12, 2023
884e3b6
Always load from storage
bdmendes Jul 13, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
Sirze01 marked this conversation as resolved.
Show resolved Hide resolved
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