Store and show groups offline - #297
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR implements offline storage functionality that allows users to view their groups and balance even when the server is unavailable, providing resilience against potential server downtime or shutdown.
- Adds offline storage methods for groups, bills, and items using SharedPreferences
- Implements fallback logic to load cached data when server requests fail
- Enhances existing domain models with offline-specific serialization methods
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| lib/infrastructure/shared_preferences.dart | Adds methods to store and retrieve groups from local storage |
| lib/domain/group/states/groups_state.dart | Implements try-catch logic to fallback to cached data when server is unavailable |
| lib/domain/group/group.dart | Adds toMapOffline() method for complete group serialization |
| lib/domain/bill/item.dart | Adds toMapOffline() method for item serialization |
| lib/domain/bill/bill.dart | Adds toMapOffline() method for bill serialization |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
GR0ZA
left a comment
There was a problem hiding this comment.
nice work 🚀
just one small thing
| Future<List<Group>> _getGroupsByUser(String userId) async { | ||
| return await _groupRepository.getGroupsByUser(userId); | ||
| try { | ||
| // try to get groups from server |
There was a problem hiding this comment.
the comments are kinda superfluous here 😅 it's pretty clear what's happening already
| Future<List<Group>> _getGroupsByUser(String userId) async { | ||
| return await _groupRepository.getGroupsByUser(userId); | ||
| try { | ||
| // try to get groups from server |
There was a problem hiding this comment.
IMO this caching logic should live inside the repository instead of here. The repository is responsible for data access, whether it comes from the network or local storage (yeah I know maybe we've done it differently somewhere else). This way we don't have to think about loading the local storage when we want to fetch groups, the repository does it automatically for us.
The repository could receive SharedUtility as a dependency:
@Riverpod(keepAlive: true)
GroupRepository groupRepository(Ref ref) {
return RemoteGroupRepository(
api: GroupAPI(),
client: ref.read(httpClientProvider),
sharedUtility: ref.read(sharedUtilityProvider),
);
}And the repository method could look like this:
...
final SharedUtility sharedUtility;
...
@override
Future<List<Group>> getGroupsByUser(String userId) async {
try {
final groups = await client.get(
uri: api.getGroupsByUser(userId),
builder: (data) {
if (data == null || data.isEmpty) return [];
return data.map((g) => Group.fromMap(g)).toList();
},
);
sharedUtility.setGroups(groups);
return groups;
} catch (e) {
return sharedUtility.getGroups();
}
}
Now users can see their groups and balance even if they are offline.
This PR was created in anticipation of longer server downtime or a permanent shutdown.
Closes #296