Skip to content

Commit 032cc64

Browse files
Merge pull request #126 from ChandruWritesCode/81-feat-local-caching-of-chat-history-to-minimize-api-calls
#81 implemented the local caching system
2 parents 689ae03 + 34003b8 commit 032cc64

24 files changed

Lines changed: 1422 additions & 1039 deletions

mobile/analysis_options.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
# packages, and plugins designed to encourage good coding practices.
1010
analyzer:
1111
errors:
12+
invalid_return_type_for_catch_error: ignore
1213
use_build_context_synchronously: ignore
1314
use_null_aware_elements: ignore
1415
include: package:flutter_lints/flutter.yaml

mobile/lib/controllers/auth.dart

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,29 @@ class AuthState extends ChangeNotifier {
6464
try {
6565
final res = await _authService.getCurrentUser(_token!);
6666
final data = res.data;
67-
6867
final userData = data['data'] ?? data['user'] ?? data;
6968

7069
_currentUser = UserModel.fromJson(userData);
70+
71+
await _authService.saveUserProfile(jsonEncode(userData));
7172
notifyListeners();
73+
7274
} catch (e) {
73-
debugPrint("Failed to load user profile: $e");
75+
debugPrint("Network failed, attempting to load cached user profile: $e");
76+
77+
try {
78+
final cachedData = await _authService.getCachedUserProfile();
79+
if (cachedData != null) {
80+
final decodedData = jsonDecode(cachedData);
81+
_currentUser = UserModel.fromJson(decodedData);
82+
notifyListeners();
83+
} else {
84+
_errorMessage = "No internet connection and no cached profile.";
85+
notifyListeners();
86+
}
87+
} catch (cacheError) {
88+
debugPrint("Cache read failed: $cacheError");
89+
}
7490
}
7591
}
7692

0 commit comments

Comments
 (0)