Skip to content

Commit

Permalink
Bind providers to a model state (#1057)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdmendes authored Dec 27, 2023
2 parents ad1d7a7 + cd0eb29 commit 435464e
Show file tree
Hide file tree
Showing 97 changed files with 1,715 additions and 1,953 deletions.
8 changes: 5 additions & 3 deletions uni/lib/controller/background_workers/notifications.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:logger/logger.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tuple/tuple.dart';
import 'package:uni/controller/background_workers/notifications/tuition_notification.dart';
import 'package:uni/controller/local_storage/app_shared_preferences.dart';
import 'package:uni/controller/local_storage/notification_timeout_storage.dart';
import 'package:uni/controller/local_storage/preferences_controller.dart';
import 'package:uni/controller/networking/network_router.dart';
import 'package:uni/model/entities/session.dart';
import 'package:workmanager/workmanager.dart';
Expand Down Expand Up @@ -68,8 +69,9 @@ class NotificationManager {
static const Duration _notificationWorkerPeriod = Duration(hours: 1);

static Future<void> updateAndTriggerNotifications() async {
final userInfo = await AppSharedPreferences.getPersistentUserInfo();
final faculties = await AppSharedPreferences.getUserFaculties();
PreferencesController.prefs = await SharedPreferences.getInstance();
final userInfo = PreferencesController.getPersistentUserInfo();
final faculties = PreferencesController.getUserFaculties();

if (userInfo == null || faculties.isEmpty) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:tuple/tuple.dart';
import 'package:uni/controller/background_workers/notifications.dart';
import 'package:uni/controller/fetchers/fees_fetcher.dart';
import 'package:uni/controller/local_storage/app_shared_preferences.dart';
import 'package:uni/controller/local_storage/preferences_controller.dart';
import 'package:uni/controller/parsers/parser_fees.dart';
import 'package:uni/model/entities/session.dart';
import 'package:uni/utils/duration_string_formatter.dart';
Expand Down Expand Up @@ -53,7 +53,7 @@ class TuitionNotification extends Notification {
@override
Future<bool> shouldDisplay(Session session) async {
final notificationsAreDisabled =
!(await AppSharedPreferences.getTuitionNotificationToggle());
!PreferencesController.getTuitionNotificationToggle();
if (notificationsAreDisabled) return false;
final feesFetcher = FeesFetcher();
final dueDate = parseFeesNextLimit(
Expand Down
20 changes: 10 additions & 10 deletions uni/lib/controller/cleanup.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ import 'dart:io';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:uni/controller/local_storage/app_bus_stop_database.dart';
import 'package:uni/controller/local_storage/app_course_units_database.dart';
import 'package:uni/controller/local_storage/app_courses_database.dart';
import 'package:uni/controller/local_storage/app_exams_database.dart';
import 'package:uni/controller/local_storage/app_last_user_info_update_database.dart';
import 'package:uni/controller/local_storage/app_lectures_database.dart';
import 'package:uni/controller/local_storage/app_shared_preferences.dart';
import 'package:uni/controller/local_storage/app_user_database.dart';
import 'package:uni/controller/local_storage/database/app_bus_stop_database.dart';
import 'package:uni/controller/local_storage/database/app_course_units_database.dart';
import 'package:uni/controller/local_storage/database/app_courses_database.dart';
import 'package:uni/controller/local_storage/database/app_exams_database.dart';
import 'package:uni/controller/local_storage/database/app_last_user_info_update_database.dart';
import 'package:uni/controller/local_storage/database/app_lectures_database.dart';
import 'package:uni/controller/local_storage/database/app_user_database.dart';
import 'package:uni/controller/local_storage/preferences_controller.dart';
import 'package:uni/controller/networking/network_router.dart';
import 'package:uni/model/providers/state_providers.dart';

Future<void> cleanupStoredData(BuildContext context) async {
StateProviders.fromContext(context).markAsNotInitialized();
StateProviders.fromContext(context).invalidate();

final prefs = await SharedPreferences.getInstance();
final faculties = await AppSharedPreferences.getUserFaculties();
final faculties = PreferencesController.getUserFaculties();
await prefs.clear();

unawaited(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:convert';

import 'package:crypto/crypto.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:uni/controller/local_storage/app_shared_preferences.dart';
import 'package:uni/controller/local_storage/preferences_controller.dart';

/// Returns the content of the Terms and Conditions remote file,
/// or the local one if the remote file is not available.
Expand All @@ -19,34 +19,38 @@ Future<String> fetchTermsAndConditions() async {
/// or true if they haven't.
/// Returns the updated value.
Future<bool> updateTermsAndConditionsAcceptancePreference() async {
final hash = await AppSharedPreferences.getTermsAndConditionHash();
final hash = PreferencesController.getTermsAndConditionHash();
final termsAndConditions = await fetchTermsAndConditions();
final currentHash = md5.convert(utf8.encode(termsAndConditions)).toString();

if (hash == null) {
await AppSharedPreferences.setTermsAndConditionsAcceptance(
await PreferencesController.setTermsAndConditionsAcceptance(
areAccepted: true,
);
await AppSharedPreferences.setTermsAndConditionHash(currentHash);
await PreferencesController.setTermsAndConditionHash(currentHash);
return true;
}

if (currentHash != hash) {
await AppSharedPreferences.setTermsAndConditionsAcceptance(
await PreferencesController.setTermsAndConditionsAcceptance(
areAccepted: false,
);
await AppSharedPreferences.setTermsAndConditionHash(currentHash);
await PreferencesController.setTermsAndConditionHash(currentHash);
return false;
}

await AppSharedPreferences.setTermsAndConditionsAcceptance(areAccepted: true);
await PreferencesController.setTermsAndConditionsAcceptance(
areAccepted: true,
);
return true;
}

/// Accepts the current Terms and Conditions.
Future<void> acceptTermsAndConditions() async {
final termsAndConditions = await fetchTermsAndConditions();
final currentHash = md5.convert(utf8.encode(termsAndConditions)).toString();
await AppSharedPreferences.setTermsAndConditionHash(currentHash);
await AppSharedPreferences.setTermsAndConditionsAcceptance(areAccepted: true);
await PreferencesController.setTermsAndConditionHash(currentHash);
await PreferencesController.setTermsAndConditionsAcceptance(
areAccepted: true,
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'dart:async';

import 'package:collection/collection.dart';
import 'package:sqflite/sqflite.dart';
import 'package:uni/controller/local_storage/app_database.dart';
import 'package:uni/controller/local_storage/database/app_database.dart';
import 'package:uni/model/entities/bus_stop.dart';

/// Manages the app's Bus Stops database.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:uni/controller/local_storage/app_database.dart';
import 'package:uni/controller/local_storage/database/app_database.dart';
import 'package:uni/model/entities/calendar_event.dart';

class CalendarDatabase extends AppDatabase {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:async';

import 'package:sqflite/sqflite.dart';
import 'package:uni/controller/local_storage/app_database.dart';
import 'package:uni/controller/local_storage/database/app_database.dart';
import 'package:uni/model/entities/course_units/course_unit.dart';

class AppCourseUnitsDatabase extends AppDatabase {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:async';

import 'package:sqflite/sqflite.dart';
import 'package:uni/controller/local_storage/app_database.dart';
import 'package:uni/controller/local_storage/database/app_database.dart';
import 'package:uni/model/entities/course.dart';

/// Manages the app's Courses database.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:async';

import 'package:sqflite/sqflite.dart';
import 'package:uni/controller/local_storage/app_database.dart';
import 'package:uni/controller/local_storage/database/app_database.dart';
import 'package:uni/model/entities/exam.dart';

/// Manages the app's Exams database.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:uni/controller/local_storage/app_database.dart';
import 'package:uni/controller/local_storage/database/app_database.dart';

/// Manages the app's Last User Info Update database.
///
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:async';

import 'package:sqflite/sqflite.dart';
import 'package:uni/controller/local_storage/app_database.dart';
import 'package:uni/controller/local_storage/database/app_database.dart';
import 'package:uni/model/entities/lecture.dart';

/// Manages the app's Lectures database.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:uni/controller/local_storage/app_database.dart';
import 'package:uni/controller/local_storage/database/app_database.dart';
import 'package:uni/model/entities/library_occupation.dart';

class LibraryOccupationDatabase extends AppDatabase {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'dart:async';

import 'package:sqflite/sqflite.dart';
import 'package:uni/controller/local_storage/app_database.dart';
import 'package:uni/controller/local_storage/database/app_database.dart';
import 'package:uni/model/entities/reference.dart';

/// Manages the app's References database.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:intl/intl.dart';
import 'package:sqflite/sqflite.dart';
import 'package:uni/controller/local_storage/app_database.dart';
import 'package:uni/controller/local_storage/database/app_database.dart';
import 'package:uni/model/entities/meal.dart';
import 'package:uni/model/entities/restaurant.dart';
import 'package:uni/model/utils/day_of_week.dart';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'dart:async';

import 'package:uni/controller/local_storage/app_database.dart';
import 'package:uni/controller/local_storage/database/app_database.dart';
import 'package:uni/model/entities/course.dart';
import 'package:uni/model/entities/profile.dart';

Expand Down
Loading

0 comments on commit 435464e

Please sign in to comment.