Skip to content

Commit

Permalink
Feature/stories (#26)
Browse files Browse the repository at this point in the history
* 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
Bishoywadea and marwan2232004 authored Oct 31, 2024
1 parent 9b055c7 commit eab2003
Show file tree
Hide file tree
Showing 27 changed files with 1,565 additions and 676 deletions.
43 changes: 26 additions & 17 deletions lib/core/routes/routes.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import 'package:telware_cross_platform/features/user/view/screens/profile_info_s
import 'package:telware_cross_platform/features/user/view/screens/settings_screen.dart';
import 'package:telware_cross_platform/features/user/view/screens/user_profile_screen.dart';

import '../../features/user/view/screens/devices_screen.dart';

class Routes {
static const String home = HomeScreen.route;
static const String splash = SplashScreen.route;
Expand All @@ -34,7 +36,8 @@ class Routes {
static const String inboxScreen = InboxScreen.route;
static const String addMyStory = AddMyStoryScreen.route;
static const String showTakenStory = ShowTakenStoryScreen.route;
static const storyScreen = StoryScreen.route;
static const String storyScreen = StoryScreen.route;
static const String devicesScreen = DevicesScreen.route;
static const String settings = SettingsScreen.route;
static const String changeNumber = ChangeNumberScreen.route;
static const String changeNumberForm = ChangeNumberFormScreen.route;
Expand All @@ -44,21 +47,21 @@ class Routes {
static const String userProfile = UserProfileScreen.route;
static const String privacySettings = PrivacySettingsScreen.route;


static GoRouter appRouter(WidgetRef ref) => GoRouter(
initialLocation: Routes.splash,
redirect: (context, state) {
final isAuthenticated = ref.read(authViewModelProvider.notifier).isAuthenticated();
if (!isAuthenticated) {
if (state.fullPath != Routes.logIn &&
state.fullPath != Routes.signUp &&
state.fullPath != Routes.verification &&
state.fullPath != Routes.splash) {
return Routes.logIn;
}
}
return null;
},
initialLocation: Routes.home,
// redirect: (context, state) {
// final isAuthenticated =
// ref.read(authViewModelProvider.notifier).isAuthenticated();
// if (!isAuthenticated) {
// if (state.fullPath != Routes.logIn &&
// state.fullPath != Routes.signUp &&
// state.fullPath != Routes.verification &&
// state.fullPath != Routes.splash) {
// return Routes.logIn;
// }
// }
// return null;
// },
routes: [
GoRoute(
path: Routes.splash,
Expand Down Expand Up @@ -124,8 +127,10 @@ class Routes {
pageBuilder: (context, state) => CustomTransitionPage(
key: state.pageKey,
child: StoryScreen(
userId: (state.extra as Map<String, dynamic>)['userId'] as String,
showSeens: (state.extra as Map<String, dynamic>)['showSeens'] as bool,
userId:
(state.extra as Map<String, dynamic>)['userId'] as String,
showSeens:
(state.extra as Map<String, dynamic>)['showSeens'] as bool,
),
transitionsBuilder: _slideRightTransitionBuilder,
),
Expand Down Expand Up @@ -162,6 +167,10 @@ class Routes {
path: Routes.privacySettings,
builder: (context, state) => const PrivacySettingsScreen(),
),
GoRoute(
path: Routes.devicesScreen,
builder: (context, state) => const DevicesScreen(),
),
],
);

Expand Down
108 changes: 108 additions & 0 deletions lib/features/stories/repository/contacts_local_repository.dart
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 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.

Loading

0 comments on commit eab2003

Please sign in to comment.