@@ -14,95 +14,123 @@ part 'account_event.dart';
1414part 'account_state.dart' ;
1515
1616const throttleDuration = Duration (seconds: 1 );
17- const timeout = Duration (seconds: 5 );
1817
1918EventTransformer <E > throttleDroppable <E >(Duration duration) {
2019 return (events, mapper) => droppable <E >().call (events.throttle (duration), mapper);
2120}
2221
2322class AccountBloc extends Bloc <AccountEvent , AccountState > {
2423 AccountBloc () : super (const AccountState ()) {
25- on < GetAccountInformation > ((event, emit) async {
26- int attemptCount = 0 ;
24+ on < RefreshAccountInformation > (
25+ _refreshAccountInformation,
26+ transformer: restartable (),
27+ );
28+
29+ on < GetAccountInformation > (
30+ _getAccountInformation,
31+ transformer: restartable (),
32+ );
33+
34+ on < GetAccountSubscriptions > (
35+ _getAccountSubscriptions,
36+ transformer: restartable (),
37+ );
38+
39+ on < GetFavoritedCommunities > (
40+ _getFavoritedCommunities,
41+ transformer: restartable (),
42+ );
43+ }
2744
28- bool hasFetchedAllSubsciptions = false ;
29- int currentPage = 1 ;
45+ Future <void > _refreshAccountInformation (RefreshAccountInformation event, Emitter <AccountState > emit) async {
46+ add (GetAccountInformation ());
47+ add (GetAccountSubscriptions ());
48+ add (GetFavoritedCommunities ());
49+ }
3050
31- try {
32- var exception;
33-
34- Account ? account = await fetchActiveProfileAccount ();
35-
36- while (attemptCount < 2 ) {
37- try {
38- LemmyApiV3 lemmy = LemmyClient .instance.lemmyApiV3;
39- emit (state.copyWith (status: AccountStatus .loading));
40-
41- if (account == null || account.jwt == null ) {
42- return emit (state.copyWith (status: AccountStatus .success, subsciptions: [], personView: null ));
43- } else {
44- emit (state.copyWith (status: AccountStatus .loading));
45- }
46-
47- List <CommunityView > subsciptions = [];
48- List <CommunityView > favoritedCommunities = [];
49-
50- while (! hasFetchedAllSubsciptions) {
51- ListCommunitiesResponse listCommunitiesResponse = await lemmy.run (
52- ListCommunities (
53- auth: account.jwt,
54- page: currentPage,
55- type: ListingType .subscribed,
56- limit: 50 , // Temporarily increasing this to address issue of missing subscriptions
57- ),
58- );
59-
60- subsciptions.addAll (listCommunitiesResponse.communities);
61- currentPage++ ;
62- hasFetchedAllSubsciptions = listCommunitiesResponse.communities.isEmpty;
63- }
64-
65- // Sort subscriptions by their name
66- subsciptions.sort ((CommunityView a, CommunityView b) => a.community.title.toLowerCase ().compareTo (b.community.title.toLowerCase ()));
67-
68- List <Favorite > favorites = await Favorite .favorites (account.id);
69- favoritedCommunities = subsciptions.where ((CommunityView communityView) => favorites.any ((Favorite favorite) => favorite.communityId == communityView.community.id)).toList ();
70-
71- GetPersonDetailsResponse ? getPersonDetailsResponse =
72- await lemmy.run (GetPersonDetails (username: account.username, auth: account.jwt, sort: SortType .new_, page: 1 )).timeout (timeout, onTimeout: () {
73- throw Exception ('Error: Timeout when attempting to fetch account details' );
74- });
75-
76- // This eliminates an issue which has plagued me a lot which is that there's a race condition
77- // with so many calls to GetAccountInformation, we can return success for the new and old account.
78- if (getPersonDetailsResponse.personView.person.id == (await fetchActiveProfileAccount ())? .userId) {
79- return emit (state.copyWith (status: AccountStatus .success, subsciptions: subsciptions, favorites: favoritedCommunities, personView: getPersonDetailsResponse.personView));
80- } else {
81- return emit (state.copyWith (status: AccountStatus .success));
82- }
83- } catch (e) {
84- exception = e;
85- attemptCount++ ;
86- }
87- }
88- emit (state.copyWith (status: AccountStatus .failure, errorMessage: exception.toString ()));
89- } catch (e) {
90- emit (state.copyWith (status: AccountStatus .failure, errorMessage: e.toString ()));
51+ /// Fetches the current account's information. This updates [personView] which holds moderated community information.
52+ Future <void > _getAccountInformation (GetAccountInformation event, Emitter <AccountState > emit) async {
53+ Account ? account = await fetchActiveProfileAccount ();
54+
55+ if (account == null || account.jwt == null ) {
56+ return emit (state.copyWith (status: AccountStatus .success, personView: null , moderates: []));
57+ }
58+
59+ try {
60+ emit (state.copyWith (status: AccountStatus .loading));
61+ LemmyApiV3 lemmy = LemmyClient .instance.lemmyApiV3;
62+
63+ GetPersonDetailsResponse ? getPersonDetailsResponse = await lemmy.run (GetPersonDetails (
64+ username: account.username,
65+ auth: account.jwt,
66+ sort: SortType .new_,
67+ page: 1 ,
68+ ));
69+
70+ // This eliminates an issue which has plagued me a lot which is that there's a race condition
71+ // with so many calls to GetAccountInformation, we can return success for the new and old account.
72+ if (getPersonDetailsResponse? .personView.person.id == account.userId) {
73+ return emit (state.copyWith (status: AccountStatus .success, personView: getPersonDetailsResponse? .personView, moderates: getPersonDetailsResponse? .moderates));
74+ } else {
75+ return emit (state.copyWith (status: AccountStatus .success, personView: null ));
9176 }
92- });
77+ } catch (e) {
78+ emit (state.copyWith (status: AccountStatus .failure, errorMessage: e.toString ()));
79+ }
80+ }
81+
82+ /// Fetches the current account's subscriptions.
83+ Future <void > _getAccountSubscriptions (GetAccountSubscriptions event, Emitter <AccountState > emit) async {
84+ Account ? account = await fetchActiveProfileAccount ();
85+
86+ if (account == null || account.jwt == null ) {
87+ return emit (state.copyWith (status: AccountStatus .success, subsciptions: [], personView: null ));
88+ }
89+
90+ try {
91+ emit (state.copyWith (status: AccountStatus .loading));
9392
94- on < GetFavoritedCommunities > ((event, emit) async {
95- Account ? account = await fetchActiveProfileAccount () ;
93+ LemmyApiV3 lemmy = LemmyClient .instance.lemmyApiV3;
94+ List < CommunityView > subscriptions = [] ;
9695
97- if (account == null || account.jwt == null ) {
98- return emit (state.copyWith (status: AccountStatus .success));
96+ int currentPage = 1 ;
97+ bool hasFetchedAllSubsciptions = false ;
98+
99+ while (! hasFetchedAllSubsciptions) {
100+ ListCommunitiesResponse listCommunitiesResponse = await lemmy.run (
101+ ListCommunities (
102+ auth: account.jwt,
103+ page: currentPage,
104+ type: ListingType .subscribed,
105+ limit: 50 , // Temporarily increasing this to address issue of missing subscriptions
106+ ),
107+ );
108+
109+ subscriptions.addAll (listCommunitiesResponse.communities);
110+ currentPage++ ;
111+ hasFetchedAllSubsciptions = listCommunitiesResponse.communities.isEmpty;
99112 }
100113
101- List <Favorite > favorites = await Favorite .favorites (account.id);
102- List <CommunityView > favoritedCommunities =
103- state.subsciptions.where ((CommunityView communityView) => favorites.any ((Favorite favorite) => favorite.communityId == communityView.community.id)).toList ();
114+ // Sort subscriptions by their name
115+ subscriptions.sort ((CommunityView a, CommunityView b) => a.community.title.toLowerCase ().compareTo (b.community.title.toLowerCase ()));
116+ return emit (state.copyWith (status: AccountStatus .success, subsciptions: subscriptions));
117+ } catch (e) {
118+ emit (state.copyWith (status: AccountStatus .failure, errorMessage: e.toString ()));
119+ }
120+ }
121+
122+ /// Fetches the current account's favorited communities.
123+ Future <void > _getFavoritedCommunities (GetFavoritedCommunities event, Emitter <AccountState > emit) async {
124+ Account ? account = await fetchActiveProfileAccount ();
125+
126+ if (account == null || account.jwt == null ) {
127+ return emit (state.copyWith (status: AccountStatus .success));
128+ }
129+
130+ List <Favorite > favorites = await Favorite .favorites (account.id);
131+ List <CommunityView > favoritedCommunities =
132+ state.subsciptions.where ((CommunityView communityView) => favorites.any ((Favorite favorite) => favorite.communityId == communityView.community.id)).toList ();
104133
105- emit (state.copyWith (status: AccountStatus .success, favorites: favoritedCommunities));
106- });
134+ return emit (state.copyWith (status: AccountStatus .success, favorites: favoritedCommunities));
107135 }
108136}
0 commit comments