-
Notifications
You must be signed in to change notification settings - Fork 1
Feat/add academic debts feature #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| import 'package:progres/features/debts/data/models/debt_course.dart'; | ||
|
|
||
| class AcademicYearDebt { | ||
| final String annee; | ||
| final int idAnneeAcademique; | ||
| final int rang; | ||
| final List<DebtCourse> dette; | ||
|
|
||
| AcademicYearDebt({ | ||
| required this.annee, | ||
| required this.idAnneeAcademique, | ||
| required this.rang, | ||
| required this.dette, | ||
| }); | ||
|
|
||
| factory AcademicYearDebt.fromJson(Map<String, dynamic> json) { | ||
| return AcademicYearDebt( | ||
| annee: json['annee'] as String, | ||
| idAnneeAcademique: json['id_annee_academique'] as int, | ||
| rang: json['rang'] as int, | ||
| dette: (json['dette'] as List<dynamic>) | ||
| .map((e) => DebtCourse.fromJson(e as Map<String, dynamic>)) | ||
| .toList(), | ||
| ); | ||
| } | ||
|
|
||
| Map<String, dynamic> toJson() { | ||
| return { | ||
| 'annee': annee, | ||
| 'id_annee_academique': idAnneeAcademique, | ||
| 'rang': rang, | ||
| 'dette': dette.map((e) => e.toJson()).toList(), | ||
| }; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| class DebtCourse { | ||
| final String mcFr; | ||
| final String mcAr; | ||
| final String nfr; | ||
| final String nar; | ||
| final String pfr; | ||
| final String par; | ||
| final double m; | ||
| final double md; | ||
| final double cc; | ||
| final double ccd; | ||
| final double ex; | ||
| final double exd; | ||
|
|
||
| DebtCourse({ | ||
| required this.mcFr, | ||
| required this.mcAr, | ||
| required this.nfr, | ||
| required this.nar, | ||
| required this.pfr, | ||
| required this.par, | ||
| required this.m, | ||
| required this.md, | ||
| required this.cc, | ||
| required this.ccd, | ||
| required this.ex, | ||
| required this.exd, | ||
| }); | ||
|
|
||
| factory DebtCourse.fromJson(Map<String, dynamic> json) { | ||
| return DebtCourse( | ||
| mcFr: json['mcFr'] as String, | ||
| mcAr: json['mcAr'] as String, | ||
| nfr: json['nfr'] as String, | ||
| nar: json['nar'] as String, | ||
| pfr: json['pfr'] as String, | ||
| par: json['par'] as String, | ||
| m: (json['m'] as num).toDouble(), | ||
| md: (json['md'] as num).toDouble(), | ||
| cc: (json['cc'] as num).toDouble(), | ||
| ccd: (json['ccd'] as num).toDouble(), | ||
| ex: (json['ex'] as num).toDouble(), | ||
| exd: (json['exd'] as num).toDouble(), | ||
| ); | ||
| } | ||
|
|
||
| Map<String, dynamic> toJson() { | ||
| return { | ||
| 'mcFr': mcFr, | ||
| 'mcAr': mcAr, | ||
| 'nfr': nfr, | ||
| 'nar': nar, | ||
| 'pfr': pfr, | ||
| 'par': par, | ||
| 'm': m, | ||
| 'md': md, | ||
| 'cc': cc, | ||
| 'ccd': ccd, | ||
| 'ex': ex, | ||
| 'exd': exd, | ||
| }; | ||
| } | ||
| } |
30 changes: 30 additions & 0 deletions
30
lib/features/debts/data/repositories/debts_repository_impl.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import 'package:progres/core/network/api_client.dart'; | ||
| import 'package:progres/features/debts/data/models/academic_year_debt.dart'; | ||
|
|
||
| class DebtsRepositoryImpl { | ||
| final ApiClient _apiClient; | ||
|
|
||
| DebtsRepositoryImpl({ApiClient? apiClient}) | ||
| : _apiClient = apiClient ?? ApiClient(); | ||
|
|
||
| Future<List<AcademicYearDebt>> getStudentDebts() async { | ||
| try { | ||
| final uuid = await _apiClient.getUuid(); | ||
| if (uuid == null) { | ||
| throw Exception('UUID not found, please login again'); | ||
| } | ||
| final response = await _apiClient.get('/infos/dettes/$uuid'); | ||
| if (response.data == null) { | ||
| return []; | ||
| } | ||
| final List<dynamic> debtsJson = response.data; | ||
| final debts = debtsJson | ||
| .map((debtJson) => AcademicYearDebt.fromJson(debtJson)) | ||
| .toList(); | ||
| debts.sort((a, b) => b.idAnneeAcademique.compareTo(a.idAnneeAcademique)); | ||
| return debts; | ||
AliAkrem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (e) { | ||
| rethrow; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| import 'dart:convert'; | ||
| import 'package:flutter/foundation.dart'; | ||
| import 'package:shared_preferences/shared_preferences.dart'; | ||
| import 'package:progres/features/debts/data/models/academic_year_debt.dart'; | ||
|
|
||
| class DebtsCacheService { | ||
| // Keys for SharedPreferences | ||
| static const String _debtsKey = 'cached_debts'; | ||
| static const String _lastUpdatedKey = 'last_updated_debts'; | ||
|
|
||
| // Save debts | ||
| Future<bool> cacheDebts(List<AcademicYearDebt> debts) async { | ||
| try { | ||
| final prefs = await SharedPreferences.getInstance(); | ||
| final debtsJson = debts.map((d) => d.toJson()).toList(); | ||
| await prefs.setString(_debtsKey, jsonEncode(debtsJson)); | ||
| await prefs.setString(_lastUpdatedKey, DateTime.now().toIso8601String()); | ||
| return true; | ||
| } catch (e) { | ||
| debugPrint('Error caching debts: $e'); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // Retrieve debts | ||
| Future<List<AcademicYearDebt>?> getCachedDebts() async { | ||
| try { | ||
| final prefs = await SharedPreferences.getInstance(); | ||
| final debtsString = prefs.getString(_debtsKey); | ||
|
|
||
| if (debtsString == null) return null; | ||
|
|
||
| final List<dynamic> decodedJson = jsonDecode(debtsString); | ||
| return decodedJson | ||
| .map((json) => AcademicYearDebt.fromJson(json)) | ||
| .toList(); | ||
| } catch (e) { | ||
| debugPrint('Error retrieving cached debts: $e'); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| // Get last update timestamp | ||
| Future<DateTime?> getLastUpdated() async { | ||
| try { | ||
| final prefs = await SharedPreferences.getInstance(); | ||
| final timestamp = prefs.getString(_lastUpdatedKey); | ||
| if (timestamp == null) return null; | ||
|
|
||
| return DateTime.parse(timestamp); | ||
| } catch (e) { | ||
| debugPrint('Error getting last updated time: $e'); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| // Clear all debts cached data | ||
| Future<bool> clearCache() async { | ||
| try { | ||
| final prefs = await SharedPreferences.getInstance(); | ||
| await prefs.remove(_debtsKey); | ||
| await prefs.remove(_lastUpdatedKey); | ||
| return true; | ||
| } catch (e) { | ||
| debugPrint('Error clearing cache: $e'); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| // Check if data is stale (older than specified duration) | ||
| Future<bool> isDataStale({ | ||
| Duration staleDuration = const Duration(hours: 12), | ||
| }) async { | ||
| final lastUpdated = await getLastUpdated(); | ||
| if (lastUpdated == null) return true; | ||
|
|
||
| final now = DateTime.now(); | ||
| return now.difference(lastUpdated) > staleDuration; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove the fallback
ApiClient()instantiation.Creating a new
ApiClient()when none is provided defeats dependency injection and makes the class difficult to test. According to the DI configuration,apiClientis always injected when registering this repository. Make the parameter required to enforce proper DI usage.🔎 Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents