Skip to content

Commit 1102a31

Browse files
authored
Add ability to switch user in user account settings page (#1171)
1 parent 2d402cd commit 1102a31

13 files changed

+674
-593
lines changed

lib/account/bloc/account_bloc.dart

+19-14
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,21 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
4343
}
4444

4545
Future<void> _refreshAccountInformation(RefreshAccountInformation event, Emitter<AccountState> emit) async {
46-
await _getAccountInformation(GetAccountInformation(), emit);
47-
await _getAccountSubscriptions(GetAccountSubscriptions(), emit);
48-
await _getFavoritedCommunities(GetFavoritedCommunities(), emit);
46+
await _getAccountInformation(GetAccountInformation(reload: event.reload), emit);
47+
await _getAccountSubscriptions(GetAccountSubscriptions(reload: event.reload), emit);
48+
await _getFavoritedCommunities(GetFavoritedCommunities(reload: event.reload), emit);
4949
}
5050

5151
/// Fetches the current account's information. This updates [personView] which holds moderated community information.
5252
Future<void> _getAccountInformation(GetAccountInformation event, Emitter<AccountState> emit) async {
5353
Account? account = await fetchActiveProfileAccount();
5454

5555
if (account == null || account.jwt == null) {
56-
return emit(state.copyWith(status: AccountStatus.success, personView: null, moderates: []));
56+
return emit(state.copyWith(status: AccountStatus.success, personView: null, moderates: [], reload: event.reload));
5757
}
5858

5959
try {
60-
emit(state.copyWith(status: AccountStatus.loading));
60+
emit(state.copyWith(status: AccountStatus.loading, reload: event.reload));
6161
LemmyApiV3 lemmy = LemmyClient.instance.lemmyApiV3;
6262

6363
GetPersonDetailsResponse? getPersonDetailsResponse = await lemmy.run(GetPersonDetails(
@@ -70,12 +70,17 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
7070
// This eliminates an issue which has plagued me a lot which is that there's a race condition
7171
// with so many calls to GetAccountInformation, we can return success for the new and old account.
7272
if (getPersonDetailsResponse?.personView.person.id == account.userId) {
73-
return emit(state.copyWith(status: AccountStatus.success, personView: getPersonDetailsResponse?.personView, moderates: getPersonDetailsResponse?.moderates));
73+
return emit(state.copyWith(
74+
status: AccountStatus.success,
75+
personView: getPersonDetailsResponse?.personView,
76+
moderates: getPersonDetailsResponse?.moderates,
77+
reload: event.reload,
78+
));
7479
} else {
75-
return emit(state.copyWith(status: AccountStatus.success, personView: null));
80+
return emit(state.copyWith(status: AccountStatus.success, personView: null, reload: event.reload));
7681
}
7782
} catch (e) {
78-
emit(state.copyWith(status: AccountStatus.failure, errorMessage: e.toString()));
83+
emit(state.copyWith(status: AccountStatus.failure, errorMessage: e.toString(), reload: event.reload));
7984
}
8085
}
8186

@@ -84,11 +89,11 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
8489
Account? account = await fetchActiveProfileAccount();
8590

8691
if (account == null || account.jwt == null) {
87-
return emit(state.copyWith(status: AccountStatus.success, subsciptions: [], personView: null));
92+
return emit(state.copyWith(status: AccountStatus.success, subsciptions: [], personView: null, reload: event.reload));
8893
}
8994

9095
try {
91-
emit(state.copyWith(status: AccountStatus.loading));
96+
emit(state.copyWith(status: AccountStatus.loading, reload: event.reload));
9297

9398
LemmyApiV3 lemmy = LemmyClient.instance.lemmyApiV3;
9499
List<CommunityView> subscriptions = [];
@@ -113,9 +118,9 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
113118

114119
// Sort subscriptions by their name
115120
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));
121+
return emit(state.copyWith(status: AccountStatus.success, subsciptions: subscriptions, reload: event.reload));
117122
} catch (e) {
118-
emit(state.copyWith(status: AccountStatus.failure, errorMessage: e.toString()));
123+
emit(state.copyWith(status: AccountStatus.failure, errorMessage: e.toString(), reload: event.reload));
119124
}
120125
}
121126

@@ -124,13 +129,13 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
124129
Account? account = await fetchActiveProfileAccount();
125130

126131
if (account == null || account.jwt == null) {
127-
return emit(state.copyWith(status: AccountStatus.success));
132+
return emit(state.copyWith(status: AccountStatus.success, reload: event.reload));
128133
}
129134

130135
List<Favorite> favorites = await Favorite.favorites(account.id);
131136
List<CommunityView> favoritedCommunities =
132137
state.subsciptions.where((CommunityView communityView) => favorites.any((Favorite favorite) => favorite.communityId == communityView.community.id)).toList();
133138

134-
return emit(state.copyWith(status: AccountStatus.success, favorites: favoritedCommunities));
139+
return emit(state.copyWith(status: AccountStatus.success, favorites: favoritedCommunities, reload: event.reload));
135140
}
136141
}

lib/account/bloc/account_event.dart

+15-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
part of 'account_bloc.dart';
22

33
abstract class AccountEvent extends Equatable {
4-
const AccountEvent();
4+
final bool reload;
5+
6+
const AccountEvent({this.reload = true});
57

68
@override
79
List<Object> get props => [];
810
}
911

10-
class RefreshAccountInformation extends AccountEvent {}
12+
class RefreshAccountInformation extends AccountEvent {
13+
const RefreshAccountInformation({super.reload});
14+
}
1115

12-
class GetAccountInformation extends AccountEvent {}
16+
class GetAccountInformation extends AccountEvent {
17+
const GetAccountInformation({super.reload});
18+
}
1319

14-
class GetAccountSubscriptions extends AccountEvent {}
20+
class GetAccountSubscriptions extends AccountEvent {
21+
const GetAccountSubscriptions({super.reload});
22+
}
1523

16-
class GetFavoritedCommunities extends AccountEvent {}
24+
class GetFavoritedCommunities extends AccountEvent {
25+
const GetFavoritedCommunities({super.reload});
26+
}

lib/account/bloc/account_state.dart

+15-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class AccountState extends Equatable {
1010
this.moderates = const [],
1111
this.personView,
1212
this.errorMessage,
13+
this.reload = true,
1314
});
1415

1516
final AccountStatus status;
@@ -27,13 +28,17 @@ class AccountState extends Equatable {
2728
/// The user's information
2829
final PersonView? personView;
2930

31+
/// Whether changes to the account state should force a reload in certain parts of the app
32+
final bool reload;
33+
3034
AccountState copyWith({
3135
AccountStatus? status,
3236
List<CommunityView>? subsciptions,
3337
List<CommunityView>? favorites,
3438
List<CommunityModeratorView>? moderates,
3539
PersonView? personView,
3640
String? errorMessage,
41+
bool? reload,
3742
}) {
3843
return AccountState(
3944
status: status ?? this.status,
@@ -42,9 +47,18 @@ class AccountState extends Equatable {
4247
moderates: moderates ?? this.moderates,
4348
personView: personView ?? this.personView,
4449
errorMessage: errorMessage ?? this.errorMessage,
50+
reload: reload ?? this.reload,
4551
);
4652
}
4753

4854
@override
49-
List<Object?> get props => [status, subsciptions, favorites, moderates, personView, errorMessage];
55+
List<Object?> get props => [
56+
status,
57+
subsciptions,
58+
favorites,
59+
moderates,
60+
personView,
61+
errorMessage,
62+
reload,
63+
];
5064
}

lib/account/pages/account_page.dart

+2
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,13 @@ class _AccountPageState extends State<AccountPage> with AutomaticKeepAliveClient
3030
listeners: [
3131
BlocListener<AuthBloc, AuthState>(
3232
listener: (context, state) {
33+
if (!state.reload) return;
3334
setState(() => authState = state);
3435
},
3536
),
3637
BlocListener<AccountBloc, AccountState>(
3738
listener: (context, state) {
39+
if (!state.reload) return;
3840
setState(() => accountState = state);
3941
},
4042
),

0 commit comments

Comments
 (0)