|
1 | 1 | import 'dart:typed_data'; |
2 | 2 |
|
| 3 | +import 'package:flutter/material.dart'; |
3 | 4 | import 'package:hooks_riverpod/hooks_riverpod.dart'; |
| 5 | +import 'package:run_flutter_run/data/repositories/activity_repository_impl.dart'; |
| 6 | +import 'package:run_flutter_run/domain/entities/activity.dart'; |
4 | 7 | import '../../../../data/repositories/user_repository_impl.dart'; |
| 8 | +import '../../../../main.dart'; |
| 9 | +import '../../../my_activities/screens/activity_details_screen.dart'; |
5 | 10 | import 'state/activity_item_state.dart'; |
6 | 11 |
|
7 | 12 | /// Provider for the activity item view model. |
8 | | -final activityItemViewModelProvider = |
9 | | - StateNotifierProvider.autoDispose<ActivityItemViewModel, ActivityItemState>( |
10 | | - (ref) => ActivityItemViewModel(ref)); |
| 13 | +final activityItemViewModelProvider = StateNotifierProvider.family< |
| 14 | + ActivityItemViewModel, |
| 15 | + ActivityItemState, |
| 16 | + String>((ref, activityId) => ActivityItemViewModel(ref, activityId)); |
11 | 17 |
|
12 | 18 | /// View model for the activity item widget. |
13 | 19 | class ActivityItemViewModel extends StateNotifier<ActivityItemState> { |
14 | | - late final Ref ref; |
| 20 | + final String activityId; |
| 21 | + final Ref ref; |
15 | 22 |
|
16 | | - ActivityItemViewModel(this.ref) : super(ActivityItemState.initial()); |
| 23 | + ActivityItemViewModel(this.ref, this.activityId) |
| 24 | + : super(ActivityItemState.initial()); |
| 25 | + |
| 26 | + void setActivity(Activity activity) { |
| 27 | + state = state.copyWith(activity: activity); |
| 28 | + } |
17 | 29 |
|
18 | 30 | Future<Uint8List?> getProfilePicture(String userId) async { |
19 | 31 | return ref.read(userRepositoryProvider).downloadProfilePicture(userId); |
20 | 32 | } |
| 33 | + |
| 34 | + /// Like the activity. |
| 35 | + Future<void> like(Activity activity) async { |
| 36 | + await ref.read(activityRepositoryProvider).like(activity.id); |
| 37 | + setActivity(activity.copy( |
| 38 | + likesCount: activity.likesCount + 1, hasCurrentUserLiked: true)); |
| 39 | + } |
| 40 | + |
| 41 | + /// Dislike the activity. |
| 42 | + Future<void> dislike(Activity activity) async { |
| 43 | + await ref.read(activityRepositoryProvider).dislike(activity.id); |
| 44 | + setActivity(activity.copy( |
| 45 | + likesCount: activity.likesCount - 1, hasCurrentUserLiked: false)); |
| 46 | + } |
| 47 | + |
| 48 | + /// Retrieves the details of an activity. |
| 49 | + Future<Activity> getActivityDetails(Activity activity) async { |
| 50 | + try { |
| 51 | + final activityDetails = await ref |
| 52 | + .read(activityRepositoryProvider) |
| 53 | + .getActivityById(id: activity.id); |
| 54 | + return activityDetails; |
| 55 | + } catch (error) { |
| 56 | + // Handle error |
| 57 | + rethrow; |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + /// Navigates to the activity details screen. |
| 62 | + void goToActivity(Activity activityDetails) { |
| 63 | + navigatorKey.currentState?.push( |
| 64 | + PageRouteBuilder( |
| 65 | + transitionDuration: const Duration(milliseconds: 500), |
| 66 | + pageBuilder: (context, animation, secondaryAnimation) => |
| 67 | + SlideTransition( |
| 68 | + position: Tween<Offset>( |
| 69 | + begin: const Offset(1.0, 0.0), |
| 70 | + end: Offset.zero, |
| 71 | + ).animate(animation), |
| 72 | + child: ActivityDetailsScreen(activity: activityDetails), |
| 73 | + ), |
| 74 | + ), |
| 75 | + ); |
| 76 | + } |
21 | 77 | } |
0 commit comments