Skip to content

Commit

Permalink
Misc release hotfixes (#986)
Browse files Browse the repository at this point in the history
Misc release hotfixes
  • Loading branch information
limwa authored Oct 4, 2023
2 parents f5afe10 + 59558fc commit 739ff0a
Show file tree
Hide file tree
Showing 34 changed files with 1,581 additions and 202 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
.pub/
build/
*.bin
uni/pubspec.lock
.flutter-plugins*

# IDE files
Expand Down
1 change: 0 additions & 1 deletion uni/.gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# Miscellaneous
*.class
*.lock
*.log
*.pyc
*.swp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ Future<void> workerStartCallback() async {
//try to keep the usage of this function BELOW +-30 seconds
//to not be punished by the scheduler in future runs.
await taskMap[taskName]!.item1();
} catch (err, stackstrace) {
Logger().e('Error while running $taskName job:', err, stackstrace);
} catch (err, stackTrace) {
Logger().e(
'Error while running $taskName job:',
error: err,
stackTrace: stackTrace,
);
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:logger/logger.dart';
import 'package:uni/controller/networking/network_router.dart';
import 'package:uni/controller/parsers/parser_course_units.dart';
import 'package:uni/model/entities/course.dart';
Expand All @@ -14,17 +13,12 @@ class AllCourseUnitsFetcher {
final allCourseUnits = <CourseUnit>[];

for (final course in courses) {
try {
final courseUnits = await _getAllCourseUnitsAndCourseAveragesFromCourse(
course,
session,
currentCourseUnits: currentCourseUnits,
);
allCourseUnits.addAll(courseUnits.where((c) => c.enrollmentIsValid()));
} catch (e) {
Logger().e('Failed to fetch course units for ${course.name}', e);
return null;
}
final courseUnits = await _getAllCourseUnitsAndCourseAveragesFromCourse(
course,
session,
currentCourseUnits: currentCourseUnits,
);
allCourseUnits.addAll(courseUnits.where((c) => c.enrollmentIsValid()));
}

return allCourseUnits;
Expand Down
35 changes: 15 additions & 20 deletions uni/lib/controller/fetchers/profile_fetcher.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:logger/logger.dart';
import 'package:uni/controller/fetchers/courses_fetcher.dart';
import 'package:uni/controller/fetchers/session_dependant_fetcher.dart';
import 'package:uni/controller/networking/network_router.dart';
Expand Down Expand Up @@ -30,27 +29,23 @@ class ProfileFetcher implements SessionDependantFetcher {
}

final profile = Profile.fromResponse(response);
try {
final coursesResponses = await Future.wait(
CoursesFetcher().getCoursesListResponses(session),
);
final courses = parseMultipleCourses(coursesResponses);
for (final course in courses) {
if (profile.courses
.map((c) => c.festId)
.toList()
.contains(course.festId)) {
profile.courses
.where((c) => c.festId == course.festId)
.first
.state ??= course.state;
continue;
}
profile.courses.add(course);
final coursesResponses = await Future.wait(
CoursesFetcher().getCoursesListResponses(session),
);
final courses = parseMultipleCourses(coursesResponses);

for (final course in courses) {
if (profile.courses
.map((c) => c.festId)
.toList()
.contains(course.festId)) {
profile.courses.where((c) => c.festId == course.festId).first.state ??=
course.state;
continue;
}
} catch (e) {
Logger().e('Failed to get user courses via scrapping: $e');
profile.courses.add(course);
}

return profile;
}
}
24 changes: 6 additions & 18 deletions uni/lib/controller/load_static/terms_and_conditions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:crypto/crypto.dart';
import 'package:flutter/services.dart' show rootBundle;
import 'package:http/http.dart' as http;
import 'package:logger/logger.dart';
import 'package:uni/controller/local_storage/app_shared_preferences.dart';

/// Returns the content of the Terms and Conditions remote file,
Expand All @@ -13,25 +12,14 @@ import 'package:uni/controller/local_storage/app_shared_preferences.dart';
/// If this operation is unsuccessful, an error message is returned.
Future<String> fetchTermsAndConditions() async {
if (await Connectivity().checkConnectivity() != ConnectivityResult.none) {
try {
const url =
'https://raw.githubusercontent.com/NIAEFEUP/project-schrodinger/develop/uni/assets/text/TermsAndConditions.md';
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
return response.body;
}
} catch (e) {
Logger().e('Failed to fetch Terms and Conditions: $e');
const url =
'https://raw.githubusercontent.com/NIAEFEUP/project-schrodinger/develop/uni/assets/text/TermsAndConditions.md';
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
return response.body;
}
}

try {
return await rootBundle.loadString('assets/text/TermsAndConditions.md');
} catch (e) {
Logger().e('Failed to read Terms and Conditions: $e');
return 'Não foi possível carregar os Termos e Condições. '
'Por favor tente mais tarde.';
}
return rootBundle.loadString('assets/text/TermsAndConditions.md');
}

/// Checks if the current Terms and Conditions have been accepted by the user,
Expand Down
2 changes: 1 addition & 1 deletion uni/lib/controller/local_storage/app_courses_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:uni/model/entities/course.dart';
/// See the [Course] class to see what data is stored in this database.
class AppCoursesDatabase extends AppDatabase {
AppCoursesDatabase()
: super('courses.db', [createScript], onUpgrade: migrate, version: 2);
: 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, '''
Expand Down
28 changes: 17 additions & 11 deletions uni/lib/controller/local_storage/app_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ class AppDatabase {
/// A list of commands to be executed on database creation.
List<String> commands;

// A lock that synchronizes all database insertions.
/// The lock timeout for database operations.
static const Duration lockTimeout = Duration(seconds: 5);

/// A lock that synchronizes all database insertions.
static Lock lock = Lock();

/// A function that is called when the [version] changes.
Expand All @@ -42,16 +45,19 @@ class AppDatabase {
String? nullColumnHack,
ConflictAlgorithm? conflictAlgorithm,
}) async {
await lock.synchronized(() async {
final db = await getDatabase();

await db.insert(
table,
values,
nullColumnHack: nullColumnHack,
conflictAlgorithm: conflictAlgorithm,
);
});
await lock.synchronized(
() async {
final db = await getDatabase();

await db.insert(
table,
values,
nullColumnHack: nullColumnHack,
conflictAlgorithm: conflictAlgorithm,
);
},
timeout: lockTimeout,
);
}

/// Initializes this database.
Expand Down
2 changes: 1 addition & 1 deletion uni/lib/controller/local_storage/app_exams_database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import 'package:uni/model/entities/exam.dart';
/// See the [Exam] class to see what data is stored in this database.
class AppExamsDatabase extends AppDatabase {
AppExamsDatabase()
: super('exams.db', [_createScript], onUpgrade: migrate, version: 4);
: super('exams.db', [_createScript], onUpgrade: migrate, version: 5);
Map<String, String> months = {
'Janeiro': '01',
'Fevereiro': '02',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class AppLecturesDatabase extends AppDatabase {
createScript,
],
onUpgrade: migrate,
version: 6,
version: 7,
);
static const createScript = '''
CREATE TABLE lectures(subject TEXT, typeClass TEXT,
Expand Down
75 changes: 40 additions & 35 deletions uni/lib/controller/networking/network_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class NetworkRouter {
static http.Client? httpClient;

/// The timeout for Sigarra login requests.
static const Duration _requestTimeout = Duration(seconds: 10);
static const Duration _requestTimeout = Duration(seconds: 30);

/// The mutual exclusion primitive for login requests.
static final Lock _loginLock = Lock();
Expand All @@ -44,44 +44,47 @@ class NetworkRouter {
List<String> faculties, {
required bool persistentSession,
}) async {
return _loginLock.synchronized(() async {
if (_lastLoginTime != null &&
DateTime.now().difference(_lastLoginTime!) <
const Duration(minutes: 1) &&
_cachedSession != null) {
Logger().d('Login request ignored due to recent login');
return _cachedSession;
}
return _loginLock.synchronized(
() async {
if (_lastLoginTime != null &&
DateTime.now().difference(_lastLoginTime!) <
const Duration(minutes: 1) &&
_cachedSession != null) {
Logger().d('Login request ignored due to recent login');
return _cachedSession;
}

final url =
'${NetworkRouter.getBaseUrls(faculties)[0]}mob_val_geral.autentica';
final response = await http.post(
url.toUri(),
body: {'pv_login': username, 'pv_password': password},
).timeout(_requestTimeout);
final url =
'${NetworkRouter.getBaseUrls(faculties)[0]}mob_val_geral.autentica';
final response = await http.post(
url.toUri(),
body: {'pv_login': username, 'pv_password': password},
).timeout(_requestTimeout);

if (response.statusCode != 200) {
Logger().e('Login failed with status code ${response.statusCode}');
return null;
}
if (response.statusCode != 200) {
Logger().e('Login failed with status code ${response.statusCode}');
return null;
}

final session = Session.fromLogin(
response,
faculties,
persistentSession: persistentSession,
);
final session = Session.fromLogin(
response,
faculties,
persistentSession: persistentSession,
);

if (session == null) {
Logger().e('Login failed: user not authenticated');
return null;
}
if (session == null) {
Logger().e('Login failed: user not authenticated');
return null;
}

Logger().i('Login successful');
_lastLoginTime = DateTime.now();
_cachedSession = session;
Logger().i('Login successful');
_lastLoginTime = DateTime.now();
_cachedSession = session;

return session;
});
return session;
},
timeout: _requestTimeout,
);
}

/// Re-authenticates the user via the Sigarra API
Expand Down Expand Up @@ -182,8 +185,10 @@ class NetworkRouter {
final newSession = await reLoginFromSession(session);

if (newSession == null) {
NavigationService.logoutAndPopHistory(null);
return Future.error('Login failed');
NavigationService.logoutAndPopHistory();
return Future.error(
'Re-login failed; user might have changed password',
);
}

session
Expand Down
2 changes: 1 addition & 1 deletion uni/lib/controller/parsers/parser_course_units.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ List<CourseUnit> parseCourseUnitsAndCourseAverage(
codeName, // FIXME: this is not the abbreviation
status: status,
grade: grade,
ects: double.parse(ects),
ects: double.tryParse(ects),
name: name,
curricularYear: int.parse(year),
semesterCode: semester,
Expand Down
2 changes: 1 addition & 1 deletion uni/lib/generated/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class MessageLookup extends MessageLookupByLibrary {
"D. Beatriz\'s stationery store"),
"dona_bia_building": MessageLookupByLibrary.simpleMessage(
"Floor -1 of building B (B-142)"),
"ects": MessageLookupByLibrary.simpleMessage("ECTs performed: "),
"ects": MessageLookupByLibrary.simpleMessage("ECTS performed: "),
"edit_off": MessageLookupByLibrary.simpleMessage("Edit"),
"edit_on": MessageLookupByLibrary.simpleMessage("Finish editing"),
"empty_text":
Expand Down
2 changes: 1 addition & 1 deletion uni/lib/generated/intl/messages_pt_PT.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class MessageLookup extends MessageLookupByLibrary {
MessageLookupByLibrary.simpleMessage("Papelaria D. Beatriz"),
"dona_bia_building": MessageLookupByLibrary.simpleMessage(
"Piso -1 do edifício B (B-142)"),
"ects": MessageLookupByLibrary.simpleMessage("ECTs realizados: "),
"ects": MessageLookupByLibrary.simpleMessage("ECTS realizados: "),
"edit_off": MessageLookupByLibrary.simpleMessage("Editar\n"),
"edit_on": MessageLookupByLibrary.simpleMessage("Concluir edição"),
"empty_text": MessageLookupByLibrary.simpleMessage(
Expand Down
4 changes: 2 additions & 2 deletions uni/lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion uni/lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"@dona_bia": {},
"dona_bia_building": "Floor -1 of building B (B-142)",
"@dona_bia_building": {},
"ects": "ECTs performed: ",
"ects": "ECTS performed: ",
"@ects": {},
"edit_off": "Edit",
"@edit_off": {},
Expand Down
2 changes: 1 addition & 1 deletion uni/lib/l10n/intl_pt_PT.arb
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@
"@dona_bia": {},
"dona_bia_building": "Piso -1 do edifício B (B-142)",
"@dona_bia_building": {},
"ects": "ECTs realizados: ",
"ects": "ECTS realizados: ",
"@ects": {},
"edit_off": "Editar\n",
"@edit_off": {},
Expand Down
Loading

0 comments on commit 739ff0a

Please sign in to comment.