Skip to content

Commit

Permalink
Ajouter un skeleton pendant le chargement des nouvelles dans ÉTSMobile (
Browse files Browse the repository at this point in the history
#927)

* WIP

* Skeleton

* Fixing tests WIP

* Fix test

* Skeleton test

* Clean imports

* [BOT] Applying version.

* [BOT] Applying format.

* Added nbSkeleton

---------

Co-authored-by: Udito3 <[email protected]>
Co-authored-by: clubapplets-server <[email protected]>
  • Loading branch information
3 people authored Feb 12, 2024
1 parent c255aca commit 0e823a1
Show file tree
Hide file tree
Showing 114 changed files with 1,237 additions and 1,487 deletions.
41 changes: 22 additions & 19 deletions lib/core/managers/course_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class CourseRepository {
await getSessions();
}

if(_userRepository.monETSUser != null) {
if (_userRepository.monETSUser != null) {
final String password = await _userRepository.getPassword();
for (final Session session in activeSessions) {
fetchedCoursesActivities.addAll(
Expand Down Expand Up @@ -159,7 +159,8 @@ class CourseRepository {

// Update the list of activities to avoid duplicate activities
for (final CourseActivity activity in fetchedCoursesActivities) {
if (_coursesActivities != null && !_coursesActivities!.contains(activity)) {
if (_coursesActivities != null &&
!_coursesActivities!.contains(activity)) {
_coursesActivities!.add(activity);
}
}
Expand Down Expand Up @@ -220,7 +221,7 @@ class CourseRepository {
await getSessions();
}

if(_userRepository.monETSUser != null) {
if (_userRepository.monETSUser != null) {
final String password = await _userRepository.getPassword();

for (final Session session in activeSessions) {
Expand All @@ -243,7 +244,8 @@ class CourseRepository {

// Update the list of activities to avoid duplicate activities
for (final ScheduleActivity activity in fetchedScheduleActivities) {
if (_scheduleActivities != null && !_scheduleActivities!.contains(activity)) {
if (_scheduleActivities != null &&
!_scheduleActivities!.contains(activity)) {
_scheduleActivities!.add(activity);
}
}
Expand Down Expand Up @@ -290,15 +292,16 @@ class CourseRepository {
}

try {
if(_userRepository.monETSUser != null) {
if (_userRepository.monETSUser != null) {
// getPassword will try to authenticate the user if not authenticated.
final String password = await _userRepository.getPassword();

final List<Session> fetchedSession = await _signetsApiClient.getSessions(
username: _userRepository.monETSUser!.universalCode,
password: password);
_logger
.d("$tag - getSessions: ${fetchedSession.length} sessions fetched.");
final List<Session> fetchedSession =
await _signetsApiClient.getSessions(
username: _userRepository.monETSUser!.universalCode,
password: password);
_logger.d(
"$tag - getSessions: ${fetchedSession.length} sessions fetched.");
for (final Session session in fetchedSession) {
if (!_sessions!.contains(session)) {
_sessions!.add(session);
Expand Down Expand Up @@ -358,13 +361,14 @@ class CourseRepository {
final Map<String, List<CourseReview>> fetchedCourseReviews = {};

try {
if(_userRepository.monETSUser != null) {
if (_userRepository.monETSUser != null) {
final String password = await _userRepository.getPassword();

fetchedCourses.addAll(await _signetsApiClient.getCourses(
username: _userRepository.monETSUser!.universalCode,
password: password));
_logger.d("$tag - getCourses: fetched ${fetchedCourses.length} courses.");
_logger
.d("$tag - getCourses: fetched ${fetchedCourses.length} courses.");
}
} on Exception catch (e, stacktrace) {
_analyticsService.logError(
Expand Down Expand Up @@ -423,14 +427,15 @@ class CourseRepository {
}

try {
if(_userRepository.monETSUser != null) {
if (_userRepository.monETSUser != null) {
final String password = await _userRepository.getPassword();

summary = await _signetsApiClient.getCourseSummary(
username: _userRepository.monETSUser!.universalCode,
password: password,
course: course);
_logger.d("$tag - getCourseSummary: fetched ${course.acronym} summary.");
_logger
.d("$tag - getCourseSummary: fetched ${course.acronym} summary.");
}
} on Exception catch (e, stacktrace) {
if (e is ApiException) {
Expand Down Expand Up @@ -481,7 +486,7 @@ class CourseRepository {
await getSessions();
}

if(_userRepository.monETSUser != null) {
if (_userRepository.monETSUser != null) {
for (final Session session in _sessions!) {
sessionReviews = await _signetsApiClient.getCourseReviews(
username: _userRepository.monETSUser!.universalCode,
Expand All @@ -506,10 +511,8 @@ class CourseRepository {
CourseReview? _getReviewForCourse(
Course course, Map<String, List<CourseReview>> reviews) {
if (reviews.containsKey(course.session)) {
return reviews[course.session]!.firstWhere(
(element) =>
element.acronym == course.acronym &&
element.group == course.group);
return reviews[course.session]!.firstWhere((element) =>
element.acronym == course.acronym && element.group == course.group);
}
return null;
}
Expand Down
32 changes: 16 additions & 16 deletions lib/core/managers/news_repository.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
// FLUTTER / DART / THIRD-PARTIES
// Dart imports:
import 'dart:convert';

// Flutter imports:
import 'package:flutter/material.dart';

// Package imports:
import 'package:logger/logger.dart';

// SERVICES
import 'package:notredame/core/services/networking_service.dart';
// Project imports:
import 'package:notredame/core/managers/cache_manager.dart';

// MODELS
import 'package:notredame/core/models/news.dart';

// UTILS
import 'package:notredame/core/services/networking_service.dart';
import 'package:notredame/core/utils/cache_exception.dart';

// OTHER
import 'package:notredame/locator.dart';

/// Repository to access all the news
Expand All @@ -32,7 +30,7 @@ class NewsRepository {
final NetworkingService _networkingService = locator<NetworkingService>();

/// List of the news with 3 test news.
List<News> _news = <News>[
List<News>? _news = <News>[
News(
id: 1,
title:
Expand Down Expand Up @@ -60,12 +58,12 @@ class NewsRepository {
),
];

List<News> get news => _news;
List<News>? get news => _news;

/// Get and update the list of news.
/// After fetching the news from the [?] the [CacheManager]
/// is updated with the latest version of the news.
Future<List<News>> getNews({bool fromCacheOnly = false}) async {
Future<List<News>?> getNews({bool fromCacheOnly = false}) async {
// Force fromCacheOnly mode when user has no connectivity
if (!(await _networkingService.hasConnectivity())) {
// ignore: parameter_assignments
Expand All @@ -83,10 +81,12 @@ class NewsRepository {

final List<News> fetchedNews = fetchNewsFromAPI();

_news ??= [];

// Update the list of news to avoid duplicate news
for (final News news in fetchedNews) {
if (!_news.contains(news)) {
_news.add(news);
if (_news?.contains(news) ?? false) {
_news?.add(news);
}
}

Expand All @@ -113,8 +113,8 @@ class NewsRepository {
.map((e) => News.fromJson(e as Map<String, dynamic>))
.toList();

_logger
.d("$tag - getNewsFromCache: ${_news.length} news loaded from cache");
_logger.d(
"$tag - getNewsFromCache: ${_news?.length} news loaded from cache");
} on CacheException catch (_) {
_logger.e(
"$tag - getNewsFromCache: exception raised will trying to load news from cache.");
Expand Down
9 changes: 5 additions & 4 deletions lib/core/managers/settings_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,9 @@ class SettingsManager with ChangeNotifier {
"${tag}_${EnumToString.convertToString(PreferencesFlag.locale)}",
_locale?.languageCode ?? 'Not found');

if(_locale != null) {
_preferencesService.setString(PreferencesFlag.locale, _locale!.languageCode);
if (_locale != null) {
_preferencesService.setString(
PreferencesFlag.locale, _locale!.languageCode);
_locale = Locale(_locale!.languageCode);
notifyListeners();
}
Expand All @@ -166,8 +167,8 @@ class SettingsManager with ChangeNotifier {
.getString(PreferencesFlag.scheduleCalendarFormat)
.then((value) => value == null
? CalendarFormat.week
: EnumToString.fromString(CalendarFormat.values, value)
?? CalendarFormat.week);
: EnumToString.fromString(CalendarFormat.values, value) ??
CalendarFormat.week);
settings.putIfAbsent(
PreferencesFlag.scheduleCalendarFormat, () => calendarFormat);

Expand Down
21 changes: 9 additions & 12 deletions lib/core/managers/user_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,10 @@ class UserRepository {
final username = await _secureStorage.read(key: usernameSecureKey);
if (username != null) {
final password = await _secureStorage.read(key: passwordSecureKey);
if(password == null) {
if (password == null) {
await _secureStorage.deleteAll();
_analyticsService.logError(
tag,
"SilentAuthenticate - PlatformException(Handled) - $passwordSecureKey not found");
_analyticsService.logError(tag,
"SilentAuthenticate - PlatformException(Handled) - $passwordSecureKey not found");
return false;
}
return await authenticate(
Expand Down Expand Up @@ -185,7 +184,7 @@ class UserRepository {
}
try {
final password = await _secureStorage.read(key: passwordSecureKey);
if(password == null) {
if (password == null) {
throw const ApiException(prefix: tag, message: "Not authenticated");
}
return password;
Expand Down Expand Up @@ -236,10 +235,10 @@ class UserRepository {
// getPassword will try to authenticate the user if not authenticated.
final String password = await getPassword();

if(_monETSUser != null) {
if (_monETSUser != null) {
_programs = await _signetsApiClient.getPrograms(
username: _monETSUser!.universalCode, password: password);
username: _monETSUser!.universalCode, password: password);

_logger.d("$tag - getPrograms: ${_programs!.length} programs fetched.");

// Update cache
Expand Down Expand Up @@ -291,7 +290,7 @@ class UserRepository {
// getPassword will try to authenticate the user if not authenticated.
final String password = await getPassword();

if(_monETSUser != null) {
if (_monETSUser != null) {
final fetchedInfo = await _signetsApiClient.getStudentInfo(
username: _monETSUser!.universalCode, password: password);

Expand All @@ -304,7 +303,6 @@ class UserRepository {
_cacheManager.update(infoCacheKey, jsonEncode(_info));
}
}

} on CacheException catch (_) {
_logger.e(
"$tag - getInfo: exception raised while trying to update the cache.");
Expand All @@ -323,8 +321,7 @@ class UserRepository {
try {
final username = await _secureStorage.read(key: passwordSecureKey);
if (username != null) {
final password =
await _secureStorage.read(key: passwordSecureKey);
final password = await _secureStorage.read(key: passwordSecureKey);
return password != null && password.isNotEmpty;
}
} on PlatformException catch (e, stacktrace) {
Expand Down
2 changes: 1 addition & 1 deletion lib/core/models/feedback_issue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class FeedbackIssue {
late bool isOpen;
late int number;
late String createdAt;

// Constructor
FeedbackIssue(Issue issue) {
id = issue.id;
Expand Down
3 changes: 2 additions & 1 deletion lib/core/services/analytics_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ class AnalyticsService {
}

/// Set user properties to identify the user against firebase app.
Future setUserProperties({required String userId, required String domain}) async {
Future setUserProperties(
{required String userId, required String domain}) async {
await _analytics.setUserId(id: userId);
await _analytics.setUserProperty(
name: _userPropertiesDomainKey, value: domain);
Expand Down
16 changes: 7 additions & 9 deletions lib/core/services/navigation_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,30 @@ class NavigationService {
Future<dynamic> pushNamed(String routeName, {dynamic arguments}) {
final currentState = _navigatorKey.currentState;

if(currentState == null) {
if (currentState == null) {
return Future.error("Navigator state is null");
}

if (remoteConfigService.outage) {
return currentState
.pushNamedAndRemoveUntil(RouterPaths.serviceOutage, (route) => false);
return currentState.pushNamedAndRemoveUntil(
RouterPaths.serviceOutage, (route) => false);
}
return currentState
.pushNamed(routeName, arguments: arguments);
return currentState.pushNamed(routeName, arguments: arguments);
}

/// Replace the current route of the navigator by pushing the route named
/// [routeName] and then delete the stack of previous routes
Future<dynamic> pushNamedAndRemoveUntil(String routeName,
[String removeUntilRouteNamed = RouterPaths.dashboard,
Object? arguments]) {

final currentState = _navigatorKey.currentState;
if(currentState == null) {
if (currentState == null) {
return Future.error("Navigator state is null");
}

if (remoteConfigService.outage) {
return currentState
.pushNamedAndRemoveUntil(RouterPaths.serviceOutage, (route) => false);
return currentState.pushNamedAndRemoveUntil(
RouterPaths.serviceOutage, (route) => false);
}
return currentState.pushNamedAndRemoveUntil(
routeName, ModalRoute.withName(removeUntilRouteNamed),
Expand Down
2 changes: 1 addition & 1 deletion lib/core/services/preferences_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class PreferencesService {
final SharedPreferences prefs = await SharedPreferences.getInstance();
final flagPreference = prefs.getString(flag.toString());

if(flagPreference != null) {
if (flagPreference != null) {
return DateTime.parse(flagPreference);
}

Expand Down
Loading

0 comments on commit 0e823a1

Please sign in to comment.