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

Fix a couple logout issues #980

Merged
merged 6 commits into from
Jan 4, 2024
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
6 changes: 3 additions & 3 deletions lib/account/utils/profiles.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
import 'package:thunder/account/widgets/profile_modal_body.dart';

void showProfileModalSheet(BuildContext context) {
void showProfileModalSheet(BuildContext context, {bool showLogoutDialog = false}) {
AuthBloc authBloc = context.read<AuthBloc>();
ThunderBloc thunderBloc = context.read<ThunderBloc>();

Expand All @@ -20,9 +20,9 @@ void showProfileModalSheet(BuildContext context) {
BlocProvider.value(value: authBloc),
BlocProvider.value(value: thunderBloc),
],
child: const FractionallySizedBox(
child: FractionallySizedBox(
heightFactor: 0.9,
child: ProfileModalBody(),
child: ProfileModalBody(showLogoutDialog: showLogoutDialog),
),
);
},
Expand Down
89 changes: 66 additions & 23 deletions lib/account/widgets/profile_modal_body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';

class ProfileModalBody extends StatefulWidget {
final bool anonymous;
final bool showLogoutDialog;

const ProfileModalBody({super.key, this.anonymous = false});
const ProfileModalBody({super.key, this.anonymous = false, this.showLogoutDialog = false});

static final GlobalKey<NavigatorState> shellNavigatorKey = GlobalKey<NavigatorState>();

Expand All @@ -40,7 +41,14 @@ class _ProfileModalBodyState extends State<ProfileModalBody> {
return Navigator(
key: ProfileModalBody.shellNavigatorKey,
onPopPage: (route, result) => false,
pages: [MaterialPage(child: ProfileSelect(pushRegister: pushRegister))],
pages: [
MaterialPage(
child: ProfileSelect(
pushRegister: pushRegister,
showLogoutDialog: widget.showLogoutDialog,
),
)
],
onGenerateRoute: _onGenerateRoute,
);
}
Expand All @@ -49,7 +57,10 @@ class _ProfileModalBodyState extends State<ProfileModalBody> {
late Widget page;
switch (settings.name) {
case '/':
page = ProfileSelect(pushRegister: pushRegister);
page = ProfileSelect(
pushRegister: pushRegister,
showLogoutDialog: widget.showLogoutDialog,
);
break;

case '/login':
Expand All @@ -68,7 +79,13 @@ class _ProfileModalBodyState extends State<ProfileModalBody> {

class ProfileSelect extends StatefulWidget {
final void Function({bool anonymous}) pushRegister;
ProfileSelect({Key? key, required this.pushRegister}) : super(key: key);
final bool showLogoutDialog;

const ProfileSelect({
super.key,
required this.pushRegister,
this.showLogoutDialog = false,
});

@override
State<ProfileSelect> createState() => _ProfileSelectState();
Expand All @@ -82,6 +99,18 @@ class _ProfileSelectState extends State<ProfileSelect> {
// Represents the ID of the account/instance we're currently logging out of / removing
String? loggingOutId;

@override
void initState() {
super.initState();

if (widget.showLogoutDialog) {
WidgetsBinding.instance.addPostFrameCallback((_) async {
await Future.delayed(const Duration(milliseconds: 250));
_logOutOfActiveAccount();
});
}
}

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
Expand Down Expand Up @@ -295,25 +324,7 @@ class _ProfileSelectState extends State<ProfileSelect> {
child: CircularProgressIndicator(),
)
: Icon(Icons.logout, semanticLabel: AppLocalizations.of(context)!.logOut),
onPressed: () async {
if (await showLogOutDialog(context)) {
setState(() => loggingOutId = accounts![index].account.id);

await Future.delayed(const Duration(milliseconds: 1000), () {
if ((anonymousInstances?.length ?? 0) > 0) {
context.read<ThunderBloc>().add(OnSetCurrentAnonymousInstance(anonymousInstances!.last.instance));
context.read<AuthBloc>().add(InstanceChanged(instance: anonymousInstances!.last.instance));
} else {
context.read<AuthBloc>().add(SwitchAccount(accountId: accounts!.lastWhere((account) => account.account.id != currentAccountId).account.id));
}

setState(() {
accounts = null;
loggingOutId = null;
});
});
}
},
onPressed: () => _logOutOfActiveAccount(activeAccountId: accounts![index].account.id),
)
: IconButton(
icon: loggingOutId == accounts![index].account.id
Expand Down Expand Up @@ -527,6 +538,38 @@ class _ProfileSelectState extends State<ProfileSelect> {
);
}

Future<void> _logOutOfActiveAccount({String? activeAccountId}) async {
activeAccountId ??= context.read<AuthBloc>().state.account?.id;

final AuthBloc authBloc = context.read<AuthBloc>();
final ThunderBloc thunderBloc = context.read<ThunderBloc>();

final List<Account> accountsNotCurrent = (await Account.accounts()).where((a) => a.id != activeAccountId).toList();

if (context.mounted && activeAccountId != null && await showLogOutDialog(context)) {
setState(() => loggingOutId = activeAccountId);

await Future.delayed(const Duration(milliseconds: 1000), () {
if ((anonymousInstances?.length ?? 0) > 0) {
thunderBloc.add(OnSetCurrentAnonymousInstance(anonymousInstances!.last.instance));
authBloc.add(InstanceChanged(instance: anonymousInstances!.last.instance));
} else if (accountsNotCurrent.isNotEmpty) {
authBloc.add(SwitchAccount(accountId: accountsNotCurrent.last.id));
} else {
// No accounts and no anonymous instances left. Create a new one.
authBloc.add(const LogOutOfAllAccounts());
thunderBloc.add(const OnAddAnonymousInstance('lemmy.ml'));
thunderBloc.add(const OnSetCurrentAnonymousInstance('lemmy.ml'));
}

setState(() {
accounts = null;
loggingOutId = null;
});
});
}
}

Future<void> fetchAccounts() async {
List<Account> accounts = await Account.accounts();

Expand Down
4 changes: 3 additions & 1 deletion lib/user/pages/user_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:swipeable_page_route/swipeable_page_route.dart';

import 'package:thunder/account/bloc/account_bloc.dart';
import 'package:thunder/account/models/account.dart';
import 'package:thunder/account/utils/profiles.dart';
import 'package:thunder/core/auth/bloc/auth_bloc.dart';
import 'package:thunder/shared/primitive_wrapper.dart';
import 'package:thunder/shared/snackbar.dart';
import 'package:thunder/thunder/bloc/thunder_bloc.dart';
Expand Down Expand Up @@ -47,7 +49,7 @@ class _UserPageState extends State<UserPage> {
scrolledUnderElevation: 0,
leading: widget.isAccountUser
? IconButton(
onPressed: () => showLogOutDialog(context),
onPressed: () => showProfileModalSheet(context, showLogoutDialog: true),
icon: Icon(
Icons.logout,
semanticLabel: AppLocalizations.of(context)!.logOut,
Expand Down
Loading