Skip to content

Commit

Permalink
Fix parsing of course units json
Browse files Browse the repository at this point in the history
  • Loading branch information
bdmendes committed Jul 28, 2023
1 parent b344ab8 commit 72aacdd
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import 'package:uni/model/entities/session.dart';

class AllCourseUnitsFetcher {
Future<List<CourseUnit>> getAllCourseUnitsAndCourseAverages(
List<Course> courses,
Session session,
) async {
List<Course> courses,
Session session,) async {
final allCourseUnits = <CourseUnit>[];

for (final course in courses) {
try {
final courseUnits = await _getAllCourseUnitsAndCourseAveragesFromCourse(
Expand All @@ -22,13 +22,13 @@ class AllCourseUnitsFetcher {
Logger().e('Failed to fetch course units for ${course.name}', e);
}
}

return allCourseUnits;
}

Future<List<CourseUnit>> _getAllCourseUnitsAndCourseAveragesFromCourse(
Course course,
Session session,
) async {
Course course,
Session session,) async {
if (course.faculty == null) {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ class CurrentCourseUnitsFetcher implements SessionDependantFetcher {
final ucs = <CourseUnit>[];

for (final course in responseBody) {
final enrollments = (course as Map<String, dynamic>)['inscricoes']
as List<Map<String, dynamic>>;
final enrollments =
(course as Map<String, dynamic>)['inscricoes'] as List<dynamic>;
for (final uc in enrollments) {
final courseUnit = CourseUnit.fromJson(uc);
final courseUnit = CourseUnit.fromJson(uc as Map<String, dynamic>);
if (courseUnit != null) {
ucs.add(courseUnit);
}
Expand Down
49 changes: 25 additions & 24 deletions uni/lib/controller/fetchers/profile_fetcher.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ProfileFetcher implements SessionDependantFetcher {
}

/// Returns the user's [Profile].
static Future<Profile> fetchProfile(Session session) async {
static Future<Profile?> fetchProfile(Session session) async {
final url = '${NetworkRouter.getBaseUrlsFromSession(session)[0]}'
'mob_fest_geral.perfil?';
final response = await NetworkRouter.getWithCookies(
Expand All @@ -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;
}
}
8 changes: 4 additions & 4 deletions uni/lib/model/entities/course.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ class Course {
Course.fromJson(Map<String, dynamic> 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;
Expand Down
2 changes: 1 addition & 1 deletion uni/lib/model/entities/course_units/course_unit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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?,
);
}

Expand Down
12 changes: 7 additions & 5 deletions uni/lib/model/entities/profile.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'dart:convert';

import 'package:http/http.dart';
import 'package:tuple/tuple.dart';
import 'package:uni/model/entities/course.dart';
Expand All @@ -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<String, dynamic>;
var responseBody = json.decode(response.body);
responseBody = responseBody as Map<String, dynamic>;
final courses = <Course>[];
for (final c in responseBody['cursos'] as Iterable<Map<String, dynamic>>) {
courses.add(Course.fromJson(c));
for (final c in responseBody['cursos'] as List<dynamic>) {
courses.add(Course.fromJson(c as Map<String, dynamic>));
}

return Profile(
name: responseBody['nome'] as String,
email: responseBody['email'] as String,
name: responseBody['nome'] as String? ?? '',
email: responseBody['email'] as String? ?? '',
courses: courses,
);
}
Expand Down
19 changes: 9 additions & 10 deletions uni/lib/model/providers/startup/profile_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,23 +72,21 @@ 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,
printBalance: _profile.printBalance,
feesBalance: feesBalance,
feesLimit: feesLimit,
);

_profile = newProfile;
} catch (e) {
updateStatus(RequestStatus.failed);
}
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -165,7 +163,8 @@ class ProfileProvider extends StateProviderNotifier {
}
}

static Future<File?> fetchOrGetCachedProfilePicture(Session session, {
static Future<File?> fetchOrGetCachedProfilePicture(
Session session, {
bool forceRetrieval = false,
int? studentNumber,
}) {
Expand Down

0 comments on commit 72aacdd

Please sign in to comment.