-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* FEAT(Session Managment): Implement UI for Devices Screen * FEAT(Stories): Implement functions to integrate with backend * REF(Stories): refactored the statemangment classes to be keepAlive * REF(Stories): refactored story screen widget tree * REF(Stories): refactored take story screen * REF(Stories): refactored edit taken image screen * REF(Sessions): add keys for devices screen * feat(stories): adding .g files. --------- Co-authored-by: marwan2232004 <[email protected]>
- Loading branch information
1 parent
9b055c7
commit eab2003
Showing
27 changed files
with
1,565 additions
and
676 deletions.
There are no files selected for viewing
This file contains 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
108 changes: 108 additions & 0 deletions
108
lib/features/stories/repository/contacts_local_repository.dart
This file contains 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,108 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:hive/hive.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
import 'package:riverpod_annotation/riverpod_annotation.dart'; | ||
import 'package:telware_cross_platform/features/stories/models/contact_model.dart'; | ||
import '../models/story_model.dart'; | ||
import '../utils/utils_functions.dart'; | ||
|
||
part 'contacts_local_repository.g.dart'; | ||
|
||
|
||
@Riverpod(keepAlive: true) | ||
ContactsLocalRepository contactsLocalRepository(ContactsLocalRepositoryRef ref) { | ||
final userBox = Hive.box<ContactModel>('contacts'); | ||
return ContactsLocalRepository(userBox); | ||
} | ||
|
||
class ContactsLocalRepository { | ||
final Box<ContactModel> _userBox; | ||
|
||
ContactsLocalRepository(this._userBox); | ||
|
||
|
||
|
||
Future<void> saveContactsToHive(List<ContactModel> contacts) async { | ||
for (ContactModel contact in contacts) { | ||
final existingContact = _userBox.get(contact.userId); | ||
if (existingContact == null || | ||
contact != existingContact || | ||
existingContact.userImage == null) { | ||
if (contact.userImage == null && existingContact?.userImage != null) { | ||
return; | ||
} | ||
await _saveContactImageInHive(contact); | ||
} | ||
} | ||
} | ||
|
||
Future<void> _saveContactImageInHive(ContactModel contact) async { | ||
try { | ||
Uint8List? imageBytes = await downloadImage(contact.userImageUrl); | ||
final contactWithImage = contact.copyWith(userImage: imageBytes); | ||
await _userBox.put(contact.userId, contactWithImage); | ||
} catch (e) { | ||
if (kDebugMode) { | ||
print('Error updating contact in Hive: $e'); | ||
} | ||
} | ||
} | ||
|
||
Future<List<StoryModel>> getContactStoriesFromHive(String userId) async { | ||
final contact = _userBox.get(userId); | ||
return contact?.stories ?? []; | ||
} | ||
|
||
Future<ContactModel?> getContactFromHive(String userId) async { | ||
return _userBox.get(userId); | ||
} | ||
|
||
List<ContactModel> getAllContactsFromHive() { | ||
return _userBox.values.toList(); | ||
} | ||
|
||
Future<void> deleteContactFromHive(String userId) async { | ||
final box = await Hive.openBox<ContactModel>('contacts'); | ||
await box.delete(userId); | ||
} | ||
|
||
Future<void> updateContactInHive(ContactModel updatedContact) async { | ||
try { | ||
final existingContact = _userBox.get(updatedContact.userId); | ||
|
||
if (existingContact != null) { | ||
await _userBox.put(updatedContact.userId, updatedContact); | ||
} else { | ||
if (kDebugMode) { | ||
print('User not found'); | ||
} | ||
} | ||
} catch (e) { | ||
if (kDebugMode) { | ||
print('Failed to update user in Hive: $e'); | ||
} | ||
throw Exception('Error updating user in Hive'); | ||
} | ||
} | ||
|
||
Future<void> saveStoryImageLocally( | ||
String userId, String storyId, Uint8List imageData) async { | ||
try { | ||
final user = await getContactFromHive(userId); | ||
if (user != null) { | ||
for (var story in user.stories) { | ||
if (story.storyId == storyId) { | ||
story.storyContent = imageData; | ||
break; | ||
} | ||
} | ||
await updateContactInHive(user); | ||
} | ||
} catch (e) { | ||
if (kDebugMode) { | ||
print('Error saving story image to Hive: $e'); | ||
} | ||
} | ||
} | ||
} | ||
|
27 changes: 27 additions & 0 deletions
27
lib/features/stories/repository/contacts_local_repository.g.dart
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.