@@ -5,14 +5,19 @@ import 'package:equatable/equatable.dart';
55import 'package:flutter_bloc/flutter_bloc.dart' ;
66import 'package:monumento/domain/entities/user_entity.dart' ;
77import 'package:monumento/domain/repositories/social_repository.dart' ;
8+ import 'package:monumento/application/authentication/authentication_bloc.dart' ;
9+ import 'package:monumento/domain/repositories/authentication_repository.dart' ;
810
911part 'follow_event.dart' ;
1012part 'follow_state.dart' ;
1113
1214class FollowBloc extends Bloc <FollowEvent , FollowState > {
1315 final SocialRepository _socialRepository;
16+ final AuthenticationBloc _authenticationBloc;
17+ final AuthenticationRepository _authenticationRepository;
18+ bool _currentFollowStatus = false ;
1419
15- FollowBloc (this ._socialRepository) : super (FollowInitial ()) {
20+ FollowBloc (this ._socialRepository, this ._authenticationBloc, this ._authenticationRepository ) : super (FollowInitial ()) {
1621 on < FollowUser > (_mapFollowUserToState);
1722 on < UnfollowUser > (_mapUnfollowUserToState);
1823 on < GetFollowStatus > (_mapGetFollowStatusToState);
@@ -23,23 +28,42 @@ class FollowBloc extends Bloc<FollowEvent, FollowState> {
2328 try {
2429 emit (LoadingFollowState ());
2530 await _socialRepository.followUser (targetUser: event.targetUser);
26- add (GetFollowStatus (targetUser: event.targetUser));
31+ _currentFollowStatus = true ;
32+ emit (FollowStatusRetrieved (following: _currentFollowStatus));
33+
34+ // Update authentication state
35+ _authenticationBloc.add (LoggedIn ());
36+
37+ // Get the current user's following and followers lists
38+ final (userLoggedIn, user) = await _authenticationRepository.getUser ();
39+ if (userLoggedIn && user != null ) {
40+ add (LoadUser (following: user.following));
41+ add (LoadUser (following: user.followers));
42+ }
2743 } catch (e) {
2844 log ('${e .toString ()} follow' );
29-
3045 emit (FollowStateError (e.toString ()));
3146 }
3247 }
3348
3449 _mapUnfollowUserToState (UnfollowUser event, Emitter <FollowState > emit) async {
3550 try {
3651 emit (LoadingFollowState ());
37-
3852 await _socialRepository.unfollowUser (targetUser: event.targetUser);
39- add (GetFollowStatus (targetUser: event.targetUser));
53+ _currentFollowStatus = false ;
54+ emit (FollowStatusRetrieved (following: _currentFollowStatus));
55+
56+ // Update authentication state
57+ _authenticationBloc.add (LoggedIn ());
58+
59+ // Get the current user's following and followers lists
60+ final (userLoggedIn, user) = await _authenticationRepository.getUser ();
61+ if (userLoggedIn && user != null ) {
62+ add (LoadUser (following: user.following));
63+ add (LoadUser (following: user.followers));
64+ }
4065 } catch (e) {
4166 log ('${e .toString ()} unfollow' );
42-
4367 emit (FollowStateError (e.toString ()));
4468 }
4569 }
@@ -48,12 +72,11 @@ class FollowBloc extends Bloc<FollowEvent, FollowState> {
4872 GetFollowStatus event, Emitter <FollowState > emit) async {
4973 try {
5074 emit (LoadingFollowState ());
51- bool following =
75+ _currentFollowStatus =
5276 await _socialRepository.getFollowStatus (targetUser: event.targetUser);
53- emit (FollowStatusRetrieved (following: following ));
77+ emit (FollowStatusRetrieved (following: _currentFollowStatus ));
5478 } catch (e) {
5579 log ('${e .toString ()} status' );
56-
5780 emit (FollowStateError (e.toString ()));
5881 }
5982 }
@@ -71,7 +94,6 @@ class FollowBloc extends Bloc<FollowEvent, FollowState> {
7194 emit (LoadedFollowUserListState (userData: userDataEntity));
7295 } catch (e) {
7396 log ('${e .toString ()} follow' );
74-
7597 emit (FollowUserListErrorState (message: e.toString ()));
7698 }
7799 }
0 commit comments