From 72aacdd523d405a7836ca7c0394d5c743b2ec4d3 Mon Sep 17 00:00:00 2001 From: Bruno Mendes Date: Fri, 28 Jul 2023 22:42:45 +0100 Subject: [PATCH] Fix parsing of course units json --- .../all_course_units_fetcher.dart | 12 ++--- .../current_course_units_fetcher.dart | 6 +-- .../controller/fetchers/profile_fetcher.dart | 49 ++++++++++--------- uni/lib/model/entities/course.dart | 8 +-- .../entities/course_units/course_unit.dart | 2 +- uni/lib/model/entities/profile.dart | 12 +++-- .../providers/startup/profile_provider.dart | 19 ++++--- 7 files changed, 55 insertions(+), 53 deletions(-) diff --git a/uni/lib/controller/fetchers/course_units_fetcher/all_course_units_fetcher.dart b/uni/lib/controller/fetchers/course_units_fetcher/all_course_units_fetcher.dart index 01e1a97cf..c0e28c435 100644 --- a/uni/lib/controller/fetchers/course_units_fetcher/all_course_units_fetcher.dart +++ b/uni/lib/controller/fetchers/course_units_fetcher/all_course_units_fetcher.dart @@ -7,10 +7,10 @@ import 'package:uni/model/entities/session.dart'; class AllCourseUnitsFetcher { Future> getAllCourseUnitsAndCourseAverages( - List courses, - Session session, - ) async { + List courses, + Session session,) async { final allCourseUnits = []; + for (final course in courses) { try { final courseUnits = await _getAllCourseUnitsAndCourseAveragesFromCourse( @@ -22,13 +22,13 @@ class AllCourseUnitsFetcher { Logger().e('Failed to fetch course units for ${course.name}', e); } } + return allCourseUnits; } Future> _getAllCourseUnitsAndCourseAveragesFromCourse( - Course course, - Session session, - ) async { + Course course, + Session session,) async { if (course.faculty == null) { return []; } diff --git a/uni/lib/controller/fetchers/course_units_fetcher/current_course_units_fetcher.dart b/uni/lib/controller/fetchers/course_units_fetcher/current_course_units_fetcher.dart index 67070823d..e4250858a 100644 --- a/uni/lib/controller/fetchers/course_units_fetcher/current_course_units_fetcher.dart +++ b/uni/lib/controller/fetchers/course_units_fetcher/current_course_units_fetcher.dart @@ -31,10 +31,10 @@ class CurrentCourseUnitsFetcher implements SessionDependantFetcher { final ucs = []; for (final course in responseBody) { - final enrollments = (course as Map)['inscricoes'] - as List>; + final enrollments = + (course as Map)['inscricoes'] as List; for (final uc in enrollments) { - final courseUnit = CourseUnit.fromJson(uc); + final courseUnit = CourseUnit.fromJson(uc as Map); if (courseUnit != null) { ucs.add(courseUnit); } diff --git a/uni/lib/controller/fetchers/profile_fetcher.dart b/uni/lib/controller/fetchers/profile_fetcher.dart index b1d0a1c10..56a239d64 100644 --- a/uni/lib/controller/fetchers/profile_fetcher.dart +++ b/uni/lib/controller/fetchers/profile_fetcher.dart @@ -16,7 +16,7 @@ class ProfileFetcher implements SessionDependantFetcher { } /// Returns the user's [Profile]. - static Future fetchProfile(Session session) async { + static Future fetchProfile(Session session) async { final url = '${NetworkRouter.getBaseUrlsFromSession(session)[0]}' 'mob_fest_geral.perfil?'; final response = await NetworkRouter.getWithCookies( @@ -25,31 +25,32 @@ class ProfileFetcher implements SessionDependantFetcher { session, ); - if (response.statusCode == 200) { - final profile = Profile.fromResponse(response); - try { - final coursesResponses = - CoursesFetcher().getCoursesListResponses(session); - final courses = - parseMultipleCourses(await Future.wait(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); + if (response.statusCode != 200) { + return null; + } + + 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; } - } catch (e) { - Logger().e('Failed to get user courses via scrapping: $e'); + profile.courses.add(course); } - return profile; + } catch (e) { + Logger().e('Failed to get user courses via scrapping: $e'); } - return Profile(); + return profile; } } diff --git a/uni/lib/model/entities/course.dart b/uni/lib/model/entities/course.dart index 864b7e229..1c739fb42 100644 --- a/uni/lib/model/entities/course.dart +++ b/uni/lib/model/entities/course.dart @@ -25,11 +25,11 @@ class Course { Course.fromJson(Map data) : id = data['cur_id'] as int, festId = data['fest_id'] as int, - name = data['cur_nome'] as String, - currYear = data['ano_curricular'] as String, + name = data['cur_nome'] as String?, + currYear = data['ano_curricular'] as String?, firstEnrollment = data['fest_a_lect_1_insc'] as int, - abbreviation = data['abbreviation'] as String, - faculty = data['inst_sigla'].toString().toLowerCase(); + abbreviation = data['abbreviation'] as String?, + faculty = data['inst_sigla']?.toString().toLowerCase(); final int id; final int? festId; diff --git a/uni/lib/model/entities/course_units/course_unit.dart b/uni/lib/model/entities/course_units/course_unit.dart index 3863e4def..3f2ba6ee9 100644 --- a/uni/lib/model/entities/course_units/course_unit.dart +++ b/uni/lib/model/entities/course_units/course_unit.dart @@ -53,7 +53,7 @@ class CourseUnit { grade: data['resultado_melhor'] as String?, ectsGrade: data['resultado_ects'] as String?, result: data['resultado_insc'] as String?, - ects: data['creditos_ects'] as double?, + ects: data['creditos_ects'] as num?, ); } diff --git a/uni/lib/model/entities/profile.dart b/uni/lib/model/entities/profile.dart index 22343100a..12f73289d 100644 --- a/uni/lib/model/entities/profile.dart +++ b/uni/lib/model/entities/profile.dart @@ -1,4 +1,5 @@ import 'dart:convert'; + import 'package:http/http.dart'; import 'package:tuple/tuple.dart'; import 'package:uni/model/entities/course.dart'; @@ -18,15 +19,16 @@ class Profile { /// Creates a new instance from a JSON object. factory Profile.fromResponse(Response response) { - final responseBody = json.decode(response.body) as Map; + var responseBody = json.decode(response.body); + responseBody = responseBody as Map; final courses = []; - for (final c in responseBody['cursos'] as Iterable>) { - courses.add(Course.fromJson(c)); + for (final c in responseBody['cursos'] as List) { + courses.add(Course.fromJson(c as Map)); } return Profile( - name: responseBody['nome'] as String, - email: responseBody['email'] as String, + name: responseBody['nome'] as String? ?? '', + email: responseBody['email'] as String? ?? '', courses: courses, ); } diff --git a/uni/lib/model/providers/startup/profile_provider.dart b/uni/lib/model/providers/startup/profile_provider.dart index dfd003d71..cf41a625e 100644 --- a/uni/lib/model/providers/startup/profile_provider.dart +++ b/uni/lib/model/providers/startup/profile_provider.dart @@ -72,14 +72,14 @@ class ProfileProvider extends StateProviderNotifier { final feesLimit = parseFeesNextLimit(response); final userPersistentInfo = - await AppSharedPreferences.getPersistentUserInfo(); + await AppSharedPreferences.getPersistentUserInfo(); if (userPersistentInfo.item1 != '' && userPersistentInfo.item2 != '') { final profileDb = AppUserDataDatabase(); await profileDb.saveUserFees(feesBalance, feesLimit); } - final newProfile = Profile( + _profile = Profile( name: _profile.name, email: _profile.email, courses: _profile.courses, @@ -87,8 +87,6 @@ class ProfileProvider extends StateProviderNotifier { feesBalance: feesBalance, feesLimit: feesLimit, ); - - _profile = newProfile; } catch (e) { updateStatus(RequestStatus.failed); } @@ -100,7 +98,7 @@ class ProfileProvider extends StateProviderNotifier { final printBalance = await getPrintsBalance(response); final userPersistentInfo = - await AppSharedPreferences.getPersistentUserInfo(); + await AppSharedPreferences.getPersistentUserInfo(); if (userPersistentInfo.item1 != '' && userPersistentInfo.item2 != '') { final profileDb = AppUserDataDatabase(); await profileDb.saveUserPrintBalance(printBalance); @@ -125,15 +123,15 @@ class ProfileProvider extends StateProviderNotifier { try { final profile = await ProfileFetcher.fetchProfile(session); final currentCourseUnits = - await CurrentCourseUnitsFetcher().getCurrentCourseUnits(session); + await CurrentCourseUnitsFetcher().getCurrentCourseUnits(session); - _profile = profile; + _profile = profile ?? Profile(); _profile.courseUnits = currentCourseUnits; updateStatus(RequestStatus.successful); final userPersistentInfo = - await AppSharedPreferences.getPersistentUserInfo(); + await AppSharedPreferences.getPersistentUserInfo(); if (userPersistentInfo.item1 != '' && userPersistentInfo.item2 != '') { final profileDb = AppUserDataDatabase(); await profileDb.insertUserData(_profile); @@ -152,7 +150,7 @@ class ProfileProvider extends StateProviderNotifier { _profile.courseUnits = allCourseUnits; final userPersistentInfo = - await AppSharedPreferences.getPersistentUserInfo(); + await AppSharedPreferences.getPersistentUserInfo(); if (userPersistentInfo.item1 != '' && userPersistentInfo.item2 != '') { final coursesDb = AppCoursesDatabase(); await coursesDb.saveNewCourses(courses); @@ -165,7 +163,8 @@ class ProfileProvider extends StateProviderNotifier { } } - static Future fetchOrGetCachedProfilePicture(Session session, { + static Future fetchOrGetCachedProfilePicture( + Session session, { bool forceRetrieval = false, int? studentNumber, }) {