nc-photos/lib/widget/account_picker_dialog.dart

158 lines
5 KiB
Dart
Raw Normal View History

2021-04-10 06:28:12 +02:00
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/account.dart';
2021-07-25 07:00:38 +02:00
import 'package:nc_photos/app_localizations.dart';
import 'package:nc_photos/exception_util.dart' as exception_util;
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/k.dart' as k;
import 'package:nc_photos/pref.dart';
import 'package:nc_photos/snack_bar_manager.dart';
2021-04-17 10:59:16 +02:00
import 'package:nc_photos/theme.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/widget/home.dart';
2021-10-19 22:11:35 +02:00
import 'package:nc_photos/widget/settings.dart';
2021-04-10 06:28:12 +02:00
import 'package:nc_photos/widget/sign_in.dart';
/// A dialog that allows the user to switch between accounts
2021-04-10 06:28:12 +02:00
class AccountPickerDialog extends StatefulWidget {
2021-09-15 08:58:06 +02:00
const AccountPickerDialog({
2021-07-23 22:05:57 +02:00
Key? key,
required this.account,
2021-04-10 06:28:12 +02:00
}) : super(key: key);
@override
createState() => _AccountPickerDialogState();
final Account account;
}
class _AccountPickerDialogState extends State<AccountPickerDialog> {
@override
initState() {
super.initState();
2021-10-27 22:40:54 +02:00
_accounts = Pref().getAccounts2Or([]);
2021-04-10 06:28:12 +02:00
}
@override
build(BuildContext context) {
final otherAccountOptions = _accounts
2021-10-19 21:49:41 +02:00
.where((a) => a.account != widget.account)
2021-04-10 06:28:12 +02:00
.map((a) => SimpleDialogOption(
2021-07-05 07:24:09 +02:00
padding: const EdgeInsets.symmetric(horizontal: 8),
2021-04-10 06:28:12 +02:00
onPressed: () => _onItemPressed(a),
child: ListTile(
2021-07-05 07:24:09 +02:00
dense: true,
2021-10-19 21:49:41 +02:00
title: Text(a.account.url),
2021-11-12 22:13:02 +01:00
subtitle: Text(a.account.username.toString()),
2021-04-10 06:28:12 +02:00
trailing: IconButton(
2021-07-02 21:47:01 +02:00
icon: Icon(
Icons.close,
2021-10-19 22:15:43 +02:00
color: AppTheme.getUnfocusedIconColor(context),
2021-07-02 21:47:01 +02:00
),
tooltip: L10n.global().deleteTooltip,
2021-07-02 21:47:01 +02:00
onPressed: () => _onRemoveItemPressed(a),
),
2021-04-10 06:28:12 +02:00
),
))
.toList();
final addAccountOptions = [
SimpleDialogOption(
2021-07-05 07:24:09 +02:00
padding: const EdgeInsets.all(8),
2021-04-10 06:28:12 +02:00
onPressed: () {
Navigator.of(context)
..pop()
..pushNamed(SignIn.routeName);
},
child: Tooltip(
message: L10n.global().addServerTooltip,
2021-04-17 10:59:16 +02:00
child: Center(
child: Icon(
Icons.add,
color: AppTheme.getSecondaryTextColor(context),
),
2021-04-10 06:28:12 +02:00
),
),
),
];
return AppTheme(
child: SimpleDialog(
title: ListTile(
dense: true,
title: Text(
widget.account.url,
style: const TextStyle(fontWeight: FontWeight.bold),
),
subtitle: Text(
2021-11-12 22:13:02 +01:00
widget.account.username.toString(),
style: const TextStyle(fontWeight: FontWeight.bold),
),
trailing: IconButton(
icon: Icon(
2021-10-19 22:11:35 +02:00
Icons.settings_outlined,
2021-10-19 22:15:43 +02:00
color: AppTheme.getUnfocusedIconColor(context),
),
2021-10-19 22:11:35 +02:00
tooltip: L10n.global().settingsAccountPageTitle,
onPressed: _onEditPressed,
),
),
titlePadding: const EdgeInsetsDirectional.fromSTEB(8, 16, 8, 0),
contentPadding: const EdgeInsetsDirectional.fromSTEB(0, 12, 0, 8),
children: otherAccountOptions + addAccountOptions,
2021-04-10 06:28:12 +02:00
),
);
}
2021-10-19 21:49:41 +02:00
void _onItemPressed(PrefAccount account) {
2021-10-27 22:40:54 +02:00
Pref().setCurrentAccountIndex(_accounts.indexOf(account));
2021-04-10 06:28:12 +02:00
Navigator.of(context).pushNamedAndRemoveUntil(Home.routeName, (_) => false,
2021-10-19 21:49:41 +02:00
arguments: HomeArguments(account.account));
2021-04-10 06:28:12 +02:00
}
2021-10-19 21:49:41 +02:00
void _onRemoveItemPressed(PrefAccount account) {
2021-07-23 22:05:57 +02:00
try {
_removeAccount(account);
setState(() {
2021-10-27 22:40:54 +02:00
_accounts = Pref().getAccounts2()!;
2021-07-23 22:05:57 +02:00
});
SnackBarManager().showSnackBar(SnackBar(
2021-10-19 21:49:41 +02:00
content: Text(
L10n.global().removeServerSuccessNotification(account.account.url)),
2021-07-23 22:05:57 +02:00
duration: k.snackBarDurationNormal,
));
} catch (e) {
SnackBarManager().showSnackBar(SnackBar(
content: Text(exception_util.toUserString(e)),
2021-07-23 22:05:57 +02:00
duration: k.snackBarDurationNormal,
));
}
2021-04-10 06:28:12 +02:00
}
2021-10-19 22:11:35 +02:00
void _onEditPressed() {
Navigator.of(context)
..pop()
..pushNamed(AccountSettingsWidget.routeName,
arguments: AccountSettingsWidgetArguments(widget.account));
}
2021-10-19 21:49:41 +02:00
void _removeAccount(PrefAccount account) {
_log.info("[_removeAccount] Remove account: ${account.account}");
2021-10-27 22:40:54 +02:00
final currentAccounts = Pref().getAccounts2()!;
2021-04-10 06:28:12 +02:00
final currentAccount =
2021-10-27 22:40:54 +02:00
currentAccounts[Pref().getCurrentAccountIndex()!];
2021-10-19 21:49:41 +02:00
final newAccounts = currentAccounts
.where((element) => element.account != account.account)
.toList();
2021-04-10 06:28:12 +02:00
final newAccountIndex = newAccounts.indexOf(currentAccount);
if (newAccountIndex == -1) {
throw StateError("Active account not found in resulting account list");
}
2021-10-27 22:40:54 +02:00
Pref()
2021-10-19 21:49:41 +02:00
..setAccounts2(newAccounts)
2021-04-10 06:28:12 +02:00
..setCurrentAccountIndex(newAccountIndex);
}
2021-10-19 21:49:41 +02:00
late List<PrefAccount> _accounts;
static final _log =
Logger("widget.account_picker_dialog._AccountPickerDialogState");
2021-04-10 06:28:12 +02:00
}