diff --git a/uni/lib/controller/local_storage/database/app_bus_stop_database.dart b/uni/lib/controller/local_storage/database/app_bus_stop_database.dart index a2b0e9af5..c312f52f9 100644 --- a/uni/lib/controller/local_storage/database/app_bus_stop_database.dart +++ b/uni/lib/controller/local_storage/database/app_bus_stop_database.dart @@ -10,12 +10,15 @@ import 'package:uni/model/entities/bus_stop.dart'; /// This database stores information about the bus stops that the user /// wants to keep track of. It also stores information about /// which ones are the user's favorite stops. -class AppBusStopDatabase extends AppDatabase { +class AppBusStopDatabase extends AppDatabase> { AppBusStopDatabase() - : super('busstops.db', [ - 'CREATE TABLE busstops(stopCode TEXT, busCode TEXT)', - 'CREATE TABLE favoritestops(stopCode TEXT, favorited TEXT)', - ]); + : super( + 'busstops.db', + [ + 'CREATE TABLE busstops(stopCode TEXT, busCode TEXT)', + 'CREATE TABLE favoritestops(stopCode TEXT, favorited TEXT)', + ], + ); /// Returns a map containing all the data stored in this database. /// @@ -103,9 +106,10 @@ class AppBusStopDatabase extends AppDatabase { } /// Replaces all the bus stops in this database with entries - /// from [stops]. - Future setBusStops(Map stops) async { + /// from [data]. + @override + Future saveToDatabase(Map data) async { await deleteBusStops(); - await _insertBusStops(stops); + await _insertBusStops(data); } } diff --git a/uni/lib/controller/local_storage/database/app_calendar_database.dart b/uni/lib/controller/local_storage/database/app_calendar_database.dart index c4ff2b1ff..9d99b61d4 100644 --- a/uni/lib/controller/local_storage/database/app_calendar_database.dart +++ b/uni/lib/controller/local_storage/database/app_calendar_database.dart @@ -1,25 +1,18 @@ import 'package:uni/controller/local_storage/database/app_database.dart'; import 'package:uni/model/entities/calendar_event.dart'; -class CalendarDatabase extends AppDatabase { +class CalendarDatabase extends AppDatabase> { CalendarDatabase() - : super('calendar.bd', [ - ''' + : super( + 'calendar.bd', + [ + ''' CREATE TABLE CALENDAR( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, date TEXT)''' - ]); - - Future saveCalendar(List calendar) async { - final db = await getDatabase(); - await db.transaction((txn) async { - await txn.delete('CALENDAR'); - for (final event in calendar) { - await txn.insert('CALENDAR', event.toMap()); - } - }); - } + ], + ); // Returns a list with all calendar events stored in the database Future> calendar() async { @@ -34,4 +27,15 @@ class CalendarDatabase extends AppDatabase { ); }); } + + @override + Future saveToDatabase(List data) async { + final db = await getDatabase(); + await db.transaction((txn) async { + await txn.delete('CALENDAR'); + for (final event in data) { + await txn.insert('CALENDAR', event.toMap()); + } + }); + } } diff --git a/uni/lib/controller/local_storage/database/app_course_units_database.dart b/uni/lib/controller/local_storage/database/app_course_units_database.dart index db82545f2..3cf3181ed 100644 --- a/uni/lib/controller/local_storage/database/app_course_units_database.dart +++ b/uni/lib/controller/local_storage/database/app_course_units_database.dart @@ -4,19 +4,18 @@ import 'package:sqflite/sqflite.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 { - AppCourseUnitsDatabase() : super('course_units.db', [createScript]); +class AppCourseUnitsDatabase extends AppDatabase> { + AppCourseUnitsDatabase() + : super( + 'course_units.db', + [createScript], + ); static const String createScript = '''CREATE TABLE course_units(id INTEGER, code TEXT, abbreviation TEXT , ''' '''name TEXT, curricularYear INTEGER, occurrId INTEGER, semesterCode TEXT, ''' '''semesterName TEXT, type TEXT, status TEXT, grade TEXT, ectsGrade TEXT, ''' '''result TEXT, ects REAL, schoolYear TEXT)'''; - Future saveNewCourseUnits(List courseUnits) async { - await deleteCourseUnits(); - await _insertCourseUnits(courseUnits); - } - Future> courseUnits() async { final db = await getDatabase(); final List> maps = await db.query('course_units'); @@ -56,4 +55,10 @@ class AppCourseUnitsDatabase extends AppDatabase { final db = await getDatabase(); await db.delete('course_units'); } + + @override + Future saveToDatabase(List data) async { + await deleteCourseUnits(); + await _insertCourseUnits(data); + } } diff --git a/uni/lib/controller/local_storage/database/app_courses_database.dart b/uni/lib/controller/local_storage/database/app_courses_database.dart index b459918e7..61026f105 100644 --- a/uni/lib/controller/local_storage/database/app_courses_database.dart +++ b/uni/lib/controller/local_storage/database/app_courses_database.dart @@ -8,20 +8,19 @@ import 'package:uni/model/entities/course.dart'; /// /// This database stores information about the user's courses. /// See the [Course] class to see what data is stored in this database. -class AppCoursesDatabase extends AppDatabase { +class AppCoursesDatabase extends AppDatabase> { AppCoursesDatabase() - : super('courses.db', [createScript], onUpgrade: migrate, version: 3); + : super( + 'courses.db', + [createScript], + onUpgrade: migrate, + version: 3, + ); static const String createScript = '''CREATE TABLE courses(id INTEGER, fest_id INTEGER, name TEXT, ''' '''abbreviation TEXT, currYear TEXT, firstEnrollment INTEGER, state TEXT, ''' '''faculty TEXT, currentAverage REAL, finishedEcts REAL)'''; - /// Replaces all of the data in this database with the data from [courses]. - Future saveNewCourses(List courses) async { - await deleteCourses(); - await _insertCourses(courses); - } - /// Returns a list containing all of the courses stored in this database. Future> courses() async { final db = await getDatabase(); @@ -77,4 +76,11 @@ class AppCoursesDatabase extends AppDatabase { ..execute(createScript); await batch.commit(); } + + /// Replaces all of the data in this database with the data from [data]. + @override + Future saveToDatabase(List data) async { + await deleteCourses(); + await _insertCourses(data); + } } diff --git a/uni/lib/controller/local_storage/database/app_database.dart b/uni/lib/controller/local_storage/database/app_database.dart index bcb3b56c5..c1fac18f0 100644 --- a/uni/lib/controller/local_storage/database/app_database.dart +++ b/uni/lib/controller/local_storage/database/app_database.dart @@ -1,15 +1,19 @@ -import 'dart:async'; - import 'package:path/path.dart'; import 'package:path_provider/path_provider.dart'; import 'package:sqflite/sqflite.dart'; import 'package:synchronized/synchronized.dart'; +import 'package:uni/controller/local_storage/preferences_controller.dart'; /// Manages a generic database. /// /// This class is the foundation for all other database managers. -class AppDatabase { - AppDatabase(this.name, this.commands, {this.onUpgrade, this.version = 1}); +abstract class AppDatabase { + AppDatabase( + this.name, + this.commands, { + this.onUpgrade, + this.version = 1, + }); /// An instance of this database. Database? _db; @@ -20,6 +24,9 @@ class AppDatabase { /// A list of commands to be executed on database creation. List commands; + /// Whether the session is persistent or not. + bool? _persistentSession; + /// The lock timeout for database operations. static const Duration lockTimeout = Duration(seconds: 5); @@ -32,12 +39,28 @@ class AppDatabase { /// The version of this database. final int version; + /// Getter to determine if the session is persistent. + Future get persistentSession async { + _persistentSession ??= + await PreferencesController.getPersistentUserInfo() != null; + return _persistentSession!; + } + /// Returns an instance of this database. Future getDatabase() async { _db ??= await initializeDatabase(); return _db!; } + Future saveToDatabase(T data); + + /// Calls saveToDatabase if the session is persistent + Future saveIfPersistentSession(T data) async { + if (await persistentSession) { + await saveToDatabase(data); + } + } + /// Inserts [values] into the corresponding [table] in this database. Future insertInDatabase( String table, diff --git a/uni/lib/controller/local_storage/database/app_exams_database.dart b/uni/lib/controller/local_storage/database/app_exams_database.dart index 8d53df3da..31bbead47 100644 --- a/uni/lib/controller/local_storage/database/app_exams_database.dart +++ b/uni/lib/controller/local_storage/database/app_exams_database.dart @@ -8,20 +8,19 @@ import 'package:uni/model/entities/exam.dart'; /// /// This database stores information about the user's exams. /// See the [Exam] class to see what data is stored in this database. -class AppExamsDatabase extends AppDatabase { +class AppExamsDatabase extends AppDatabase> { AppExamsDatabase() - : super('exams.db', [_createScript], onUpgrade: migrate, version: 5); + : super( + 'exams.db', + [_createScript], + onUpgrade: migrate, + version: 5, + ); static const _createScript = ''' CREATE TABLE exams(id TEXT, subject TEXT, begin TEXT, end TEXT, rooms TEXT, examType TEXT, faculty TEXT, PRIMARY KEY (id,faculty)) '''; - /// Replaces all of the data in this database with [exams]. - Future saveNewExams(List exams) async { - await deleteExams(); - await _insertExams(exams); - } - /// Returns a list containing all of the exams stored in this database. Future> exams() async { final db = await getDatabase(); @@ -70,4 +69,11 @@ CREATE TABLE exams(id TEXT, subject TEXT, begin TEXT, end TEXT, ..execute(_createScript); await batch.commit(); } + + /// Replaces all of the data in this database with [data]. + @override + Future saveToDatabase(List data) async { + await deleteExams(); + await _insertExams(data); + } } diff --git a/uni/lib/controller/local_storage/database/app_last_user_info_update_database.dart b/uni/lib/controller/local_storage/database/app_last_user_info_update_database.dart index e2d4656ac..a0f1fb807 100644 --- a/uni/lib/controller/local_storage/database/app_last_user_info_update_database.dart +++ b/uni/lib/controller/local_storage/database/app_last_user_info_update_database.dart @@ -4,15 +4,12 @@ import 'package:uni/controller/local_storage/database/app_database.dart'; /// /// This database stores information about when the app last fetched and updated /// the user's data. -class AppLastUserInfoUpdateDatabase extends AppDatabase { +class AppLastUserInfoUpdateDatabase extends AppDatabase { AppLastUserInfoUpdateDatabase() - : super('last_update.db', ['CREATE TABLE last_update(lastUpdate DATE)']); - - /// Replaces the timestamp in this database with [timestamp]. - Future insertNewTimeStamp(DateTime timestamp) async { - await deleteLastUpdate(); - await _insertTimeStamp(timestamp); - } + : super( + 'last_update.db', + ['CREATE TABLE last_update(lastUpdate DATE)'], + ); /// Deletes all of the data from this database. Future deleteLastUpdate() async { @@ -41,4 +38,11 @@ class AppLastUserInfoUpdateDatabase extends AppDatabase { } return DateTime.now(); } + + /// Replaces the timestamp in this database with [data]. + @override + Future saveToDatabase(DateTime data) async { + await deleteLastUpdate(); + await _insertTimeStamp(data); + } } diff --git a/uni/lib/controller/local_storage/database/app_lectures_database.dart b/uni/lib/controller/local_storage/database/app_lectures_database.dart index 6327cec4c..af0deed03 100644 --- a/uni/lib/controller/local_storage/database/app_lectures_database.dart +++ b/uni/lib/controller/local_storage/database/app_lectures_database.dart @@ -8,7 +8,7 @@ import 'package:uni/model/entities/lecture.dart'; /// /// This database stores information about the user's lectures. /// See the [Lecture] class to see what data is stored in this database. -class AppLecturesDatabase extends AppDatabase { +class AppLecturesDatabase extends AppDatabase> { AppLecturesDatabase() : super( 'lectures.db', @@ -22,12 +22,6 @@ class AppLecturesDatabase extends AppDatabase { CREATE TABLE lectures(subject TEXT, typeClass TEXT, startDateTime TEXT, blocks INTEGER, room TEXT, teacher TEXT, classNumber TEXT, occurrId INTEGER)'''; - /// Replaces all of the data in this database with [lectures]. - Future saveNewLectures(List lectures) async { - await deleteLectures(); - await _insertLectures(lectures); - } - /// Returns a list containing all of the lectures stored in this database. Future> lectures() async { final db = await getDatabase(); @@ -82,4 +76,11 @@ CREATE TABLE lectures(subject TEXT, typeClass TEXT, ..execute(createScript); await batch.commit(); } + + /// Replaces all of the data in this database with [lectures]. + @override + Future saveToDatabase(List data) async { + await deleteLectures(); + await _insertLectures(data); + } } diff --git a/uni/lib/controller/local_storage/database/app_library_occupation_database.dart b/uni/lib/controller/local_storage/database/app_library_occupation_database.dart index 6f39d77c7..665ff3193 100644 --- a/uni/lib/controller/local_storage/database/app_library_occupation_database.dart +++ b/uni/lib/controller/local_storage/database/app_library_occupation_database.dart @@ -1,28 +1,21 @@ import 'package:uni/controller/local_storage/database/app_database.dart'; import 'package:uni/model/entities/library_occupation.dart'; -class LibraryOccupationDatabase extends AppDatabase { +class LibraryOccupationDatabase extends AppDatabase { LibraryOccupationDatabase() - : super('occupation.db', [ - ''' -CREATE TABLE FLOOR_OCCUPATION( - id INTEGER PRIMARY KEY AUTOINCREMENT, - number INT, - occupation INT, - capacity INT - ) - ''' - ]); - - Future saveOccupation(LibraryOccupation occupation) async { - final db = await getDatabase(); - await db.transaction((txn) async { - await txn.delete('FLOOR_OCCUPATION'); - for (final floor in occupation.floors) { - await txn.insert('FLOOR_OCCUPATION', floor.toMap()); - } - }); - } + : super( + 'occupation.db', + [ + ''' + CREATE TABLE FLOOR_OCCUPATION( + id INTEGER PRIMARY KEY AUTOINCREMENT, + number INT, + occupation INT, + capacity INT + ) + ''' + ], + ); Future occupation() async { final db = await getDatabase(); @@ -43,4 +36,15 @@ CREATE TABLE FLOOR_OCCUPATION( return occupation; } + + @override + Future saveToDatabase(LibraryOccupation data) async { + final db = await getDatabase(); + await db.transaction((txn) async { + await txn.delete('FLOOR_OCCUPATION'); + for (final floor in data.floors) { + await txn.insert('FLOOR_OCCUPATION', floor.toMap()); + } + }); + } } diff --git a/uni/lib/controller/local_storage/database/app_references_database.dart b/uni/lib/controller/local_storage/database/app_references_database.dart index feff82385..375d4c893 100644 --- a/uni/lib/controller/local_storage/database/app_references_database.dart +++ b/uni/lib/controller/local_storage/database/app_references_database.dart @@ -8,19 +8,18 @@ import 'package:uni/model/entities/reference.dart'; /// /// This database stores information about the user's references. /// See the [Reference] class to see what data is stored in this database. -class AppReferencesDatabase extends AppDatabase { +class AppReferencesDatabase extends AppDatabase> { AppReferencesDatabase() - : super('refs.db', [createScript], onUpgrade: migrate, version: 2); + : super( + 'refs.db', + [createScript], + onUpgrade: migrate, + version: 2, + ); static const String createScript = '''CREATE TABLE refs(description TEXT, entity INTEGER, ''' '''reference INTEGER, amount REAL, limitDate TEXT)'''; - /// Replaces all of the data in this database with the data from [references]. - Future saveNewReferences(List references) async { - await deleteReferences(); - await insertReferences(references); - } - /// Returns a list containing all the references stored in this database. Future> references() async { final db = await getDatabase(); @@ -67,4 +66,11 @@ class AppReferencesDatabase extends AppDatabase { ..execute(createScript); await batch.commit(); } + + /// Replaces all of the data in this database with the data from [data]. + @override + Future saveToDatabase(List data) async { + await deleteReferences(); + await insertReferences(data); + } } diff --git a/uni/lib/controller/local_storage/database/app_restaurant_database.dart b/uni/lib/controller/local_storage/database/app_restaurant_database.dart index 7e1ceb6c3..443a2c869 100644 --- a/uni/lib/controller/local_storage/database/app_restaurant_database.dart +++ b/uni/lib/controller/local_storage/database/app_restaurant_database.dart @@ -5,16 +5,18 @@ import 'package:uni/model/entities/meal.dart'; import 'package:uni/model/entities/restaurant.dart'; import 'package:uni/model/utils/day_of_week.dart'; -class RestaurantDatabase extends AppDatabase { +class RestaurantDatabase extends AppDatabase> { RestaurantDatabase() - : super('restaurant.db', [ - ''' + : super( + 'restaurant.db', + [ + ''' CREATE TABLE RESTAURANTS( id INTEGER PRIMARY KEY, ref TEXT, name TEXT) ''', - ''' + ''' CREATE TABLE MEALS( id INTEGER PRIMARY KEY AUTOINCREMENT, day TEXT, @@ -24,18 +26,8 @@ class RestaurantDatabase extends AppDatabase { id_restaurant INTEGER, FOREIGN KEY (id_restaurant) REFERENCES RESTAURANTS(id)) ''' - ]); - - /// Deletes all data, and saves the new restaurants - Future saveRestaurants(List restaurants) async { - final db = await getDatabase(); - await db.transaction((transaction) async { - await deleteAll(transaction); - for (final restaurant in restaurants) { - await insertRestaurant(transaction, restaurant); - } - }); - } + ], + ); /// Get all restaurants and meals, if day is null, all meals are returned Future> restaurants({DayOfWeek? day}) async { @@ -125,6 +117,17 @@ class RestaurantDatabase extends AppDatabase { await txn.delete('meals'); await txn.delete('restaurants'); } + + @override + Future saveToDatabase(List data) async { + final db = await getDatabase(); + await db.transaction((transaction) async { + await deleteAll(transaction); + for (final restaurant in data) { + await insertRestaurant(transaction, restaurant); + } + }); + } } List filterPastMeals(List restaurants) { diff --git a/uni/lib/controller/local_storage/database/app_user_database.dart b/uni/lib/controller/local_storage/database/app_user_database.dart index 5ccdb02bf..fca077922 100644 --- a/uni/lib/controller/local_storage/database/app_user_database.dart +++ b/uni/lib/controller/local_storage/database/app_user_database.dart @@ -7,13 +7,14 @@ import 'package:uni/model/entities/profile.dart'; /// Manages the app's User Data database. /// /// This database stores information about the user's university profile. -class AppUserDataDatabase extends AppDatabase { +class AppUserDataDatabase extends AppDatabase { AppUserDataDatabase() : super('userdata.db', ['CREATE TABLE userdata(key TEXT, value TEXT)']); - /// Adds [profile] to this database. - Future insertUserData(Profile profile) async { - for (final keymap in profile.keymapValues()) { + /// Adds [data] (profile) to this database. + @override + Future saveToDatabase(Profile data) async { + for (final keymap in data.keymapValues()) { await insertInDatabase( 'userdata', {'key': keymap.item1, 'value': keymap.item2}, @@ -70,25 +71,4 @@ class AppUserDataDatabase extends AppDatabase { await db.delete('userdata'); } - - /// Saves the user's print balance to the database. - Future saveUserPrintBalance(String userBalance) async { - await insertInDatabase( - 'userdata', - {'key': 'printBalance', 'value': userBalance}, - ); - } - - /// Saves the user's balance and payment due date to the database. - /// - Future saveUserFees(String feesBalance, DateTime? feesLimit) async { - await insertInDatabase( - 'userdata', - {'key': 'feesBalance', 'value': feesBalance}, - ); - await insertInDatabase('userdata', { - 'key': 'feesLimit', - 'value': feesLimit != null ? feesLimit.toIso8601String() : '', - }); - } } diff --git a/uni/lib/controller/local_storage/preferences_controller.dart b/uni/lib/controller/local_storage/preferences_controller.dart index fe8c324de..bb375bb9a 100644 --- a/uni/lib/controller/local_storage/preferences_controller.dart +++ b/uni/lib/controller/local_storage/preferences_controller.dart @@ -185,13 +185,27 @@ class PreferencesController { } /// Returns the user's student number. - static Future getUserNumber() { - return _secureStorage.read(key: _userNumber); + static Future getUserNumber() async { + try { + if (await _secureStorage.containsKey(key: _userNumber)) { + return await _secureStorage.read(key: _userNumber); + } + return null; + } catch (err) { + return null; + } } /// Returns the user's password, in plain text format. - static Future getUserPassword() { - return _secureStorage.read(key: _userPw); + static Future getUserPassword() async { + try { + if (await _secureStorage.containsKey(key: _userPw)) { + return await _secureStorage.read(key: _userPw); + } + return null; + } catch (err) { + return null; + } } /// Replaces the user's favorite widgets with [newFavorites]. diff --git a/uni/lib/model/entities/profile.dart b/uni/lib/model/entities/profile.dart index 0085f0955..be88c562f 100644 --- a/uni/lib/model/entities/profile.dart +++ b/uni/lib/model/entities/profile.dart @@ -50,6 +50,12 @@ class Profile { return [ Tuple2('name', name), Tuple2('email', email), + Tuple2('printBalance', printBalance), + Tuple2('feesBalance', feesBalance), + Tuple2( + 'feesLimit', + feesLimit != null ? feesLimit!.toIso8601String() : '', + ), ]; } } diff --git a/uni/lib/model/providers/lazy/bus_stop_provider.dart b/uni/lib/model/providers/lazy/bus_stop_provider.dart index 453867eb6..11838e4ec 100644 --- a/uni/lib/model/providers/lazy/bus_stop_provider.dart +++ b/uni/lib/model/providers/lazy/bus_stop_provider.dart @@ -50,7 +50,7 @@ class BusStopProvider extends StateProviderNotifier> { notifyListeners(); final db = AppBusStopDatabase(); - await db.setBusStops(state!); + await db.saveIfPersistentSession(state!); } Future removeUserBusStop( @@ -63,7 +63,7 @@ class BusStopProvider extends StateProviderNotifier> { notifyListeners(); final db = AppBusStopDatabase(); - await db.setBusStops(state!); + await db.saveIfPersistentSession(state!); } Future toggleFavoriteUserBusStop( diff --git a/uni/lib/model/providers/lazy/calendar_provider.dart b/uni/lib/model/providers/lazy/calendar_provider.dart index 9884118ae..94466f15c 100644 --- a/uni/lib/model/providers/lazy/calendar_provider.dart +++ b/uni/lib/model/providers/lazy/calendar_provider.dart @@ -24,7 +24,7 @@ class CalendarProvider extends StateProviderNotifier> { final session = stateProviders.sessionProvider.state!; final calendar = await CalendarFetcherHtml().getCalendar(session); final db = CalendarDatabase(); - unawaited(db.saveCalendar(calendar)); + unawaited(db.saveIfPersistentSession(calendar)); return calendar; } } diff --git a/uni/lib/model/providers/lazy/exam_provider.dart b/uni/lib/model/providers/lazy/exam_provider.dart index ce71f81af..e8d6d3209 100644 --- a/uni/lib/model/providers/lazy/exam_provider.dart +++ b/uni/lib/model/providers/lazy/exam_provider.dart @@ -2,7 +2,6 @@ import 'dart:async'; import 'package:uni/controller/fetchers/exam_fetcher.dart'; import 'package:uni/controller/local_storage/database/app_exams_database.dart'; -import 'package:uni/controller/local_storage/preferences_controller.dart'; import 'package:uni/controller/parsers/parser_exams.dart'; import 'package:uni/model/entities/course_units/course_unit.dart'; import 'package:uni/model/entities/exam.dart'; @@ -30,8 +29,6 @@ class ExamProvider extends StateProviderNotifier> { profile, session, profile.courseUnits, - persistentSession: - (await PreferencesController.getPersistentUserInfo()) != null, ); } @@ -39,18 +36,15 @@ class ExamProvider extends StateProviderNotifier> { ParserExams parserExams, Profile profile, Session session, - List userUcs, { - required bool persistentSession, - }) async { + List userUcs, + ) async { final exams = await ExamFetcher(profile.courses, userUcs) .extractExams(session, parserExams); exams.sort((exam1, exam2) => exam1.begin.compareTo(exam2.begin)); - if (persistentSession) { - await AppExamsDatabase().saveNewExams(exams); - } - + final db = AppExamsDatabase(); + await db.saveIfPersistentSession(exams); return exams; } } diff --git a/uni/lib/model/providers/lazy/lecture_provider.dart b/uni/lib/model/providers/lazy/lecture_provider.dart index 2aa424de6..2dc1264a0 100644 --- a/uni/lib/model/providers/lazy/lecture_provider.dart +++ b/uni/lib/model/providers/lazy/lecture_provider.dart @@ -4,7 +4,6 @@ import 'package:uni/controller/fetchers/schedule_fetcher/schedule_fetcher.dart'; import 'package:uni/controller/fetchers/schedule_fetcher/schedule_fetcher_api.dart'; import 'package:uni/controller/fetchers/schedule_fetcher/schedule_fetcher_html.dart'; import 'package:uni/controller/local_storage/database/app_lectures_database.dart'; -import 'package:uni/controller/local_storage/preferences_controller.dart'; import 'package:uni/model/entities/lecture.dart'; import 'package:uni/model/entities/profile.dart'; import 'package:uni/model/entities/session.dart'; @@ -25,24 +24,19 @@ class LectureProvider extends StateProviderNotifier> { return fetchUserLectures( stateProviders.sessionProvider.state!, stateProviders.profileProvider.state!, - persistentSession: - (await PreferencesController.getPersistentUserInfo()) != null, ); } Future> fetchUserLectures( Session session, Profile profile, { - required bool persistentSession, ScheduleFetcher? fetcher, }) async { final lectures = await getLecturesFromFetcherOrElse(fetcher, session, profile); - if (persistentSession) { - final db = AppLecturesDatabase(); - await db.saveNewLectures(lectures); - } + final db = AppLecturesDatabase(); + await db.saveIfPersistentSession(lectures); return lectures; } diff --git a/uni/lib/model/providers/lazy/library_occupation_provider.dart b/uni/lib/model/providers/lazy/library_occupation_provider.dart index 258acf2e7..702ffa1ed 100644 --- a/uni/lib/model/providers/lazy/library_occupation_provider.dart +++ b/uni/lib/model/providers/lazy/library_occupation_provider.dart @@ -27,7 +27,7 @@ class LibraryOccupationProvider .getLibraryOccupationFromSheets(session); final db = LibraryOccupationDatabase(); - unawaited(db.saveOccupation(occupation)); + unawaited(db.saveIfPersistentSession(occupation)); return occupation; } diff --git a/uni/lib/model/providers/lazy/reference_provider.dart b/uni/lib/model/providers/lazy/reference_provider.dart index 013d565ec..0e4f47559 100644 --- a/uni/lib/model/providers/lazy/reference_provider.dart +++ b/uni/lib/model/providers/lazy/reference_provider.dart @@ -24,7 +24,7 @@ class ReferenceProvider extends StateProviderNotifier> { final references = await parseReferences(response); final referencesDb = AppReferencesDatabase(); - unawaited(referencesDb.saveNewReferences(references)); + unawaited(referencesDb.saveIfPersistentSession(references)); return references; } diff --git a/uni/lib/model/providers/lazy/restaurant_provider.dart b/uni/lib/model/providers/lazy/restaurant_provider.dart index 0721d3754..f6f9599f7 100644 --- a/uni/lib/model/providers/lazy/restaurant_provider.dart +++ b/uni/lib/model/providers/lazy/restaurant_provider.dart @@ -24,7 +24,7 @@ class RestaurantProvider extends StateProviderNotifier> { final restaurants = await RestaurantFetcher().getRestaurants(session); final db = RestaurantDatabase(); - unawaited(db.saveRestaurants(restaurants)); + unawaited(db.saveIfPersistentSession(restaurants)); return filterPastMeals(restaurants); } diff --git a/uni/lib/model/providers/startup/profile_provider.dart b/uni/lib/model/providers/startup/profile_provider.dart index b8921bca5..629499aeb 100644 --- a/uni/lib/model/providers/startup/profile_provider.dart +++ b/uni/lib/model/providers/startup/profile_provider.dart @@ -11,7 +11,6 @@ import 'package:uni/controller/local_storage/database/app_course_units_database. import 'package:uni/controller/local_storage/database/app_courses_database.dart'; import 'package:uni/controller/local_storage/database/app_user_database.dart'; import 'package:uni/controller/local_storage/file_offline_storage.dart'; -import 'package:uni/controller/local_storage/preferences_controller.dart'; import 'package:uni/controller/parsers/parser_fees.dart'; import 'package:uni/controller/parsers/parser_print_balance.dart'; import 'package:uni/model/entities/course.dart'; @@ -73,6 +72,9 @@ class ProfileProvider extends StateProviderNotifier { profile.courseUnits = courseUnits; } + final profileDb = AppUserDataDatabase(); + await profileDb.saveIfPersistentSession(profile); + return profile; } @@ -99,14 +101,6 @@ class ProfileProvider extends StateProviderNotifier { final feesBalance = parseFeesBalance(response); final feesLimit = parseFeesNextLimit(response); - final userPersistentInfo = - await PreferencesController.getPersistentUserInfo(); - - if (userPersistentInfo != null) { - final profileDb = AppUserDataDatabase(); - await profileDb.saveUserFees(feesBalance, feesLimit); - } - return Tuple2(feesBalance, feesLimit); } @@ -114,13 +108,6 @@ class ProfileProvider extends StateProviderNotifier { final response = await PrintFetcher().getUserPrintsResponse(session); final printBalance = await getPrintsBalance(response); - final userPersistentInfo = - await PreferencesController.getPersistentUserInfo(); - if (userPersistentInfo != null) { - final profileDb = AppUserDataDatabase(); - await profileDb.saveUserPrintBalance(printBalance); - } - return printBalance; } @@ -135,14 +122,6 @@ class ProfileProvider extends StateProviderNotifier { profile.courseUnits = currentCourseUnits; - final userPersistentInfo = - await PreferencesController.getPersistentUserInfo(); - if (userPersistentInfo != null) { - // Course units are saved later, so we don't it here - final profileDb = AppUserDataDatabase(); - await profileDb.insertUserData(profile); - } - return profile; } @@ -161,15 +140,11 @@ class ProfileProvider extends StateProviderNotifier { return allCourseUnits; } - final userPersistentInfo = - await PreferencesController.getPersistentUserInfo(); - if (userPersistentInfo != null) { - final coursesDb = AppCoursesDatabase(); - unawaited(coursesDb.saveNewCourses(profile.courses)); + final coursesDb = AppCoursesDatabase(); + unawaited(coursesDb.saveIfPersistentSession(profile.courses)); - final courseUnitsDatabase = AppCourseUnitsDatabase(); - unawaited(courseUnitsDatabase.saveNewCourseUnits(allCourseUnits)); - } + final courseUnitsDatabase = AppCourseUnitsDatabase(); + unawaited(courseUnitsDatabase.saveIfPersistentSession(allCourseUnits)); return allCourseUnits; } diff --git a/uni/test/integration/src/exams2_page_test.dart b/uni/test/integration/src/exams2_page_test.dart index ec02101a7..a61a8b962 100644 --- a/uni/test/integration/src/exams2_page_test.dart +++ b/uni/test/integration/src/exams2_page_test.dart @@ -101,7 +101,6 @@ void main() async { profile, Session(username: '', cookies: '', faculties: ['fcup']), [fis3022CourseUnit, m2030CourseUnit], - persistentSession: false, ); examProvider.setState(exams); diff --git a/uni/test/integration/src/exams_page_test.dart b/uni/test/integration/src/exams_page_test.dart index 850f682e0..c265460d4 100644 --- a/uni/test/integration/src/exams_page_test.dart +++ b/uni/test/integration/src/exams_page_test.dart @@ -86,7 +86,6 @@ void main() async { profile, Session(username: '', cookies: '', faculties: ['feup']), [sopeCourseUnit, sdisCourseUnit], - persistentSession: false, ); examProvider.setState(exams); @@ -124,7 +123,6 @@ void main() async { profile, Session(username: '', cookies: '', faculties: ['feup']), [sopeCourseUnit, sdisCourseUnit], - persistentSession: false, ); examProvider.setState(exams); diff --git a/uni/test/integration/src/schedule_page_test.dart b/uni/test/integration/src/schedule_page_test.dart index ddc1bd514..f163d8dfb 100644 --- a/uni/test/integration/src/schedule_page_test.dart +++ b/uni/test/integration/src/schedule_page_test.dart @@ -74,7 +74,6 @@ void main() async { final lectures = await scheduleProvider.fetchUserLectures( Session(username: '', cookies: '', faculties: ['feup']), profile, - persistentSession: false, ); scheduleProvider.setState(lectures); diff --git a/uni/test/test_widget.dart b/uni/test/test_widget.dart index 8021138fb..3d3329a93 100644 --- a/uni/test/test_widget.dart +++ b/uni/test/test_widget.dart @@ -1,5 +1,6 @@ import 'package:flutter/material.dart'; import 'package:flutter_localizations/flutter_localizations.dart'; +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:sqflite_common_ffi/sqflite_ffi.dart'; @@ -13,6 +14,7 @@ import 'package:uni/view/locale_notifier.dart'; Future initTestEnvironment() async { SharedPreferences.setMockInitialValues({}); PreferencesController.prefs = await SharedPreferences.getInstance(); + FlutterSecureStorage.setMockInitialValues({}); databaseFactory = databaseFactoryFfi; } diff --git a/uni/test/unit/providers/exams_provider_test.dart b/uni/test/unit/providers/exams_provider_test.dart index ec59490d9..cd4ebf8e3 100644 --- a/uni/test/unit/providers/exams_provider_test.dart +++ b/uni/test/unit/providers/exams_provider_test.dart @@ -88,7 +88,6 @@ void main() async { profile, session, userUcs, - persistentSession: false, ); provider.setState(exams); @@ -106,7 +105,6 @@ void main() async { profile, session, userUcs, - persistentSession: false, ); provider.setState(exams); @@ -137,7 +135,6 @@ When given three exams but one is to be parsed out, profile, session, userUcs, - persistentSession: false, ); provider.setState(exams); @@ -155,7 +152,6 @@ When given three exams but one is to be parsed out, profile, session, userUcs, - persistentSession: false, ), ); }); @@ -181,7 +177,6 @@ When given three exams but one is to be parsed out, profile, session, userUcs, - persistentSession: false, ); provider.setState(exams); @@ -210,7 +205,6 @@ When given three exams but one is to be parsed out, profile, session, userUcs, - persistentSession: false, ); provider.setState(exams); @@ -239,7 +233,6 @@ When given three exams but one is to be parsed out, profile, session, userUcs, - persistentSession: false, ); provider.setState(exams); diff --git a/uni/test/unit/providers/lecture_provider_test.dart b/uni/test/unit/providers/lecture_provider_test.dart index e9c870c4c..b01445938 100644 --- a/uni/test/unit/providers/lecture_provider_test.dart +++ b/uni/test/unit/providers/lecture_provider_test.dart @@ -70,7 +70,6 @@ void main() async { session, profile, fetcher: fetcherMock, - persistentSession: false, ); provider.setState(lectures); @@ -87,7 +86,6 @@ void main() async { session, profile, fetcher: fetcherMock, - persistentSession: false, ), ); });