Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Temporarily change account in settings page #1171

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions lib/account/bloc/account_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ class AccountBloc extends Bloc<AccountEvent, AccountState> {
}

Future<void> _refreshAccountInformation(RefreshAccountInformation event, Emitter<AccountState> emit) async {
await _getAccountInformation(GetAccountInformation(), emit);
await _getAccountSubscriptions(GetAccountSubscriptions(), emit);
await _getFavoritedCommunities(GetFavoritedCommunities(), emit);
await _getAccountInformation(GetAccountInformation(reload: event.reload), emit);
await _getAccountSubscriptions(GetAccountSubscriptions(reload: event.reload), emit);
await _getFavoritedCommunities(GetFavoritedCommunities(reload: event.reload), emit);
}

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

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

try {
emit(state.copyWith(status: AccountStatus.loading));
emit(state.copyWith(status: AccountStatus.loading, reload: event.reload));
LemmyApiV3 lemmy = LemmyClient.instance.lemmyApiV3;

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

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

if (account == null || account.jwt == null) {
return emit(state.copyWith(status: AccountStatus.success, subsciptions: [], personView: null));
return emit(state.copyWith(status: AccountStatus.success, subsciptions: [], personView: null, reload: event.reload));
}

try {
emit(state.copyWith(status: AccountStatus.loading));
emit(state.copyWith(status: AccountStatus.loading, reload: event.reload));

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

// Sort subscriptions by their name
subscriptions.sort((CommunityView a, CommunityView b) => a.community.title.toLowerCase().compareTo(b.community.title.toLowerCase()));
return emit(state.copyWith(status: AccountStatus.success, subsciptions: subscriptions));
return emit(state.copyWith(status: AccountStatus.success, subsciptions: subscriptions, reload: event.reload));
} catch (e) {
emit(state.copyWith(status: AccountStatus.failure, errorMessage: e.toString()));
emit(state.copyWith(status: AccountStatus.failure, errorMessage: e.toString(), reload: event.reload));
}
}

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

if (account == null || account.jwt == null) {
return emit(state.copyWith(status: AccountStatus.success));
return emit(state.copyWith(status: AccountStatus.success, reload: event.reload));
}

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

return emit(state.copyWith(status: AccountStatus.success, favorites: favoritedCommunities));
return emit(state.copyWith(status: AccountStatus.success, favorites: favoritedCommunities, reload: event.reload));
}
}
20 changes: 15 additions & 5 deletions lib/account/bloc/account_event.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
part of 'account_bloc.dart';

abstract class AccountEvent extends Equatable {
const AccountEvent();
final bool reload;

const AccountEvent({this.reload = true});

@override
List<Object> get props => [];
}

class RefreshAccountInformation extends AccountEvent {}
class RefreshAccountInformation extends AccountEvent {
const RefreshAccountInformation({super.reload});
}

class GetAccountInformation extends AccountEvent {}
class GetAccountInformation extends AccountEvent {
const GetAccountInformation({super.reload});
}

class GetAccountSubscriptions extends AccountEvent {}
class GetAccountSubscriptions extends AccountEvent {
const GetAccountSubscriptions({super.reload});
}

class GetFavoritedCommunities extends AccountEvent {}
class GetFavoritedCommunities extends AccountEvent {
const GetFavoritedCommunities({super.reload});
}
16 changes: 15 additions & 1 deletion lib/account/bloc/account_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class AccountState extends Equatable {
this.moderates = const [],
this.personView,
this.errorMessage,
this.reload = true,
});

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

/// Whether changes to the account state should force a reload in certain parts of the app
final bool reload;

AccountState copyWith({
AccountStatus? status,
List<CommunityView>? subsciptions,
List<CommunityView>? favorites,
List<CommunityModeratorView>? moderates,
PersonView? personView,
String? errorMessage,
bool? reload,
}) {
return AccountState(
status: status ?? this.status,
Expand All @@ -42,9 +47,18 @@ class AccountState extends Equatable {
moderates: moderates ?? this.moderates,
personView: personView ?? this.personView,
errorMessage: errorMessage ?? this.errorMessage,
reload: reload ?? this.reload,
);
}

@override
List<Object?> get props => [status, subsciptions, favorites, moderates, personView, errorMessage];
List<Object?> get props => [
status,
subsciptions,
favorites,
moderates,
personView,
errorMessage,
reload,
];
}
2 changes: 2 additions & 0 deletions lib/account/pages/account_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ class _AccountPageState extends State<AccountPage> with AutomaticKeepAliveClient
listeners: [
BlocListener<AuthBloc, AuthState>(
listener: (context, state) {
if (!state.reload) return;
setState(() => authState = state);
},
),
BlocListener<AccountBloc, AccountState>(
listener: (context, state) {
if (!state.reload) return;
setState(() => accountState = state);
},
),
Expand Down
Loading
Loading