lemmur-app-android/lib/pages/settings.dart

416 lines
13 KiB
Dart
Raw Normal View History

2021-01-02 16:41:08 +01:00
import 'package:flutter/foundation.dart';
2020-08-31 16:17:39 +02:00
import 'package:flutter/material.dart';
2020-09-08 00:34:09 +02:00
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:flutter_speed_dial/flutter_speed_dial.dart';
2021-04-16 21:50:23 +02:00
import 'package:lemmy_api_client/v3.dart';
2020-08-31 16:17:39 +02:00
2020-09-16 23:22:04 +02:00
import '../hooks/stores.dart';
2021-02-27 17:02:55 +01:00
import '../l10n/l10n.dart';
import '../util/goto.dart';
2020-10-01 17:57:04 +02:00
import '../widgets/about_tile.dart';
2021-04-06 14:42:42 +02:00
import '../widgets/bottom_modal.dart';
2021-02-28 20:01:32 +01:00
import '../widgets/radio_picker.dart';
import 'add_account.dart';
import 'add_instance.dart';
2021-01-06 00:51:19 +01:00
import 'manage_account.dart';
2020-08-31 16:17:39 +02:00
2020-09-30 19:05:00 +02:00
/// Page with a list of different settings sections
2020-09-03 14:33:17 +02:00
class SettingsPage extends StatelessWidget {
2021-01-03 19:43:39 +01:00
const SettingsPage();
2020-08-31 16:17:39 +02:00
@override
2021-02-09 15:12:13 +01:00
Widget build(BuildContext context) => Scaffold(
appBar: AppBar(
title: Text(L10n.of(context)!.settings),
2021-02-09 15:12:13 +01:00
),
body: ListView(
children: [
2021-04-21 16:12:18 +02:00
ListTile(
leading: const Icon(Icons.settings),
title: const Text('General'),
onTap: () {
goTo(context, (_) => const GeneralConfigPage());
},
),
2021-02-09 15:12:13 +01:00
ListTile(
leading: const Icon(Icons.person),
2021-03-03 12:31:36 +01:00
title: const Text('Accounts'),
2021-02-09 15:12:13 +01:00
onTap: () {
goTo(context, (_) => AccountsConfigPage());
},
),
ListTile(
leading: const Icon(Icons.color_lens),
2021-03-03 12:31:36 +01:00
title: const Text('Appearance'),
2021-02-09 15:12:13 +01:00
onTap: () {
goTo(context, (_) => const AppearanceConfigPage());
},
),
const AboutTile()
],
),
);
2020-08-31 16:17:39 +02:00
}
2020-09-30 19:05:00 +02:00
/// Settings for theme color, AMOLED switch
2020-09-17 00:24:49 +02:00
class AppearanceConfigPage extends HookWidget {
2021-01-03 19:43:39 +01:00
const AppearanceConfigPage();
2020-08-31 16:17:39 +02:00
@override
Widget build(BuildContext context) {
2020-09-16 23:15:42 +02:00
final configStore = useConfigStore();
2020-08-31 16:17:39 +02:00
return Scaffold(
2021-04-18 16:32:35 +02:00
appBar: AppBar(title: const Text('Appearance')),
body: ListView(
children: [
2021-04-18 16:38:02 +02:00
const _SectionHeading('Theme'),
for (final theme in ThemeMode.values)
RadioListTile<ThemeMode>(
value: theme,
2021-01-02 16:41:08 +01:00
title: Text(describeEnum(theme)),
groupValue: configStore.theme,
onChanged: (selected) {
if (selected != null) configStore.theme = selected;
},
),
2021-04-16 20:32:25 +02:00
SwitchListTile.adaptive(
2021-02-27 17:02:55 +01:00
title: const Text('AMOLED dark mode'),
value: configStore.amoledDarkMode,
onChanged: (checked) {
configStore.amoledDarkMode = checked;
},
),
2021-04-18 16:38:02 +02:00
const SizedBox(height: 12),
const _SectionHeading('Other'),
SwitchListTile.adaptive(
title: Text(L10n.of(context)!.show_avatars),
value: configStore.showAvatars,
onChanged: (checked) {
configStore.showAvatars = checked;
},
),
SwitchListTile.adaptive(
title: const Text('Show scores'),
value: configStore.showScores,
onChanged: (checked) {
configStore.showScores = checked;
},
),
2021-04-18 16:32:35 +02:00
],
),
);
}
}
/// General settings
class GeneralConfigPage extends HookWidget {
const GeneralConfigPage();
@override
Widget build(BuildContext context) {
final configStore = useConfigStore();
return Scaffold(
appBar: AppBar(title: const Text('General')),
body: ListView(
children: [
2021-02-28 20:01:32 +01:00
ListTile(
2021-04-21 16:12:18 +02:00
title: Text(L10n.of(context)!.sort_type),
2021-02-28 20:01:32 +01:00
trailing: SizedBox(
width: 120,
2021-04-21 16:12:18 +02:00
child: RadioPicker<SortType>(
values: SortType.values,
groupValue: configStore.defaultSortType,
onChanged: (value) => configStore.defaultSortType = value,
mapValueToString: (value) => value.value,
2021-02-28 20:01:32 +01:00
),
2021-02-27 17:02:55 +01:00
),
2021-02-28 20:01:32 +01:00
),
2021-04-16 21:50:23 +02:00
ListTile(
title: Text(L10n.of(context)!.type),
trailing: SizedBox(
width: 120,
child: RadioPicker<PostListingType>(
values: const [
PostListingType.all,
PostListingType.local,
PostListingType.subscribed,
],
groupValue: configStore.defaultListingType,
onChanged: (value) => configStore.defaultListingType = value,
mapValueToString: (value) => value.value,
),
),
),
ListTile(
2021-04-21 16:12:18 +02:00
title: Text(L10n.of(context)!.language),
2021-04-16 21:50:23 +02:00
trailing: SizedBox(
width: 120,
2021-04-21 16:12:18 +02:00
child: RadioPicker<Locale>(
title: 'Choose language',
groupValue: configStore.locale,
values: L10n.supportedLocales,
mapValueToString: (locale) => locale.languageName,
onChanged: (selected) {
configStore.locale = selected;
},
2021-04-16 21:50:23 +02:00
),
),
),
],
2020-08-31 16:17:39 +02:00
),
);
}
}
2021-04-16 21:19:59 +02:00
/// Popup for an account
class _AccountOptions extends HookWidget {
final String instanceHost;
final String username;
const _AccountOptions({
Key? key,
required this.instanceHost,
required this.username,
}) : super(key: key);
2020-09-08 00:34:09 +02:00
@override
Widget build(BuildContext context) {
2020-09-16 23:15:42 +02:00
final accountsStore = useAccountsStore();
2021-04-16 21:19:59 +02:00
final configStore = useConfigStore();
2021-04-21 21:11:48 +02:00
final importLoading = useState(false);
2021-04-16 21:19:59 +02:00
Future<void> removeUserDialog(String instanceHost, String username) async {
if (await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
2021-04-16 21:19:59 +02:00
title: const Text('Remove user?'),
content: Text(
'Are you sure you want to remove $username@$instanceHost?'),
actions: [
2021-02-09 15:12:13 +01:00
TextButton(
2020-09-23 23:37:26 +02:00
onPressed: () => Navigator.of(context).pop(false),
child: Text(L10n.of(context)!.no),
2020-09-23 23:37:26 +02:00
),
2021-02-09 15:12:13 +01:00
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: Text(L10n.of(context)!.yes),
),
],
),
) ??
false) {
2021-04-16 21:19:59 +02:00
await accountsStore.removeAccount(instanceHost, username);
2021-04-06 14:42:42 +02:00
Navigator.of(context).pop();
}
}
2021-04-16 21:19:59 +02:00
return Column(
children: [
if (accountsStore.defaultUsernameFor(instanceHost) != username)
ListTile(
leading: const Icon(Icons.check_circle_outline),
title: const Text('Set as default'),
onTap: () {
accountsStore.setDefaultAccountFor(instanceHost, username);
Navigator.of(context).pop();
},
),
ListTile(
leading: const Icon(Icons.delete),
title: const Text('Remove account'),
onTap: () => removeUserDialog(instanceHost, username),
),
ListTile(
2021-04-21 21:11:48 +02:00
leading: importLoading.value
2021-04-16 21:19:59 +02:00
? const SizedBox(
height: 25,
width: 25,
child: CircularProgressIndicator(),
)
2021-04-21 21:11:48 +02:00
: const Icon(Icons.cloud_download),
2021-04-16 21:19:59 +02:00
title: const Text('Import settings to lemmur'),
onTap: () async {
2021-04-21 21:11:48 +02:00
importLoading.value = true;
2021-04-16 21:19:59 +02:00
try {
await configStore.importLemmyUserSettings(
accountsStore.userDataFor(instanceHost, username)!.jwt,
);
2021-04-21 21:11:48 +02:00
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(
content: Text('Import successful'),
));
} on Exception catch (err) {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(err.toString()),
));
} finally {
2021-04-21 16:12:18 +02:00
Navigator.of(context).pop();
2021-04-21 21:11:48 +02:00
importLoading.value = false;
2021-04-16 21:19:59 +02:00
}
}),
],
);
}
}
/// Settings for managing accounts
class AccountsConfigPage extends HookWidget {
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
final accountsStore = useAccountsStore();
removeInstanceDialog(String instanceHost) async {
if (await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
2021-04-16 21:19:59 +02:00
title: const Text('Remove instance?'),
content: Text('Are you sure you want to remove $instanceHost?'),
actions: [
2021-02-09 15:12:13 +01:00
TextButton(
2020-09-23 23:37:26 +02:00
onPressed: () => Navigator.of(context).pop(false),
child: Text(L10n.of(context)!.no),
2020-09-23 23:37:26 +02:00
),
2021-02-09 15:12:13 +01:00
TextButton(
onPressed: () => Navigator.of(context).pop(true),
child: Text(L10n.of(context)!.yes),
),
],
),
) ??
false) {
2021-04-16 21:19:59 +02:00
await accountsStore.removeInstance(instanceHost);
2021-04-06 14:42:42 +02:00
Navigator.of(context).pop();
}
}
2021-04-06 14:42:42 +02:00
void accountActions(String instanceHost, String username) {
showBottomModal(
context: context,
2021-04-16 21:19:59 +02:00
builder: (context) => _AccountOptions(
instanceHost: instanceHost,
username: username,
2021-04-06 14:42:42 +02:00
),
);
}
void instanceActions(String instanceHost) {
showBottomModal(
context: context,
builder: (context) => Column(
children: [
ListTile(
leading: const Icon(Icons.delete),
title: const Text('Remove instance'),
onTap: () => removeInstanceDialog(instanceHost),
),
],
),
);
}
return Scaffold(
2020-09-08 00:34:09 +02:00
key: _scaffoldKey,
appBar: AppBar(
2021-03-03 12:31:36 +01:00
title: const Text('Accounts'),
),
floatingActionButton: SpeedDial(
animatedIcon: AnimatedIcons.menu_close, // TODO: change to + => x
curve: Curves.bounceIn,
tooltip: 'Add account or instance',
children: [
SpeedDialChild(
2021-01-03 19:43:39 +01:00
child: const Icon(Icons.person_add),
2021-03-03 12:31:36 +01:00
label: 'Add account',
onTap: () => Navigator.of(context)
.push(AddAccountPage.route(accountsStore.instances.last)),
),
SpeedDialChild(
2021-01-03 19:43:39 +01:00
child: const Icon(Icons.dns),
2021-03-03 12:31:36 +01:00
label: 'Add instance',
onTap: () => Navigator.of(context).push(AddInstancePage.route()),
),
],
2021-01-03 19:43:39 +01:00
child: const Icon(Icons.add),
2020-09-08 00:34:09 +02:00
),
body: ListView(
children: [
2021-01-17 17:35:47 +01:00
if (accountsStore.instances.isEmpty)
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Padding(
padding: const EdgeInsets.only(top: 100),
2021-02-09 15:12:13 +01:00
child: TextButton.icon(
onPressed: () =>
Navigator.of(context).push(AddInstancePage.route()),
2021-02-09 15:12:13 +01:00
icon: const Icon(Icons.add),
2021-03-03 12:31:36 +01:00
label: const Text('Add instance'),
2021-02-09 15:12:13 +01:00
),
),
],
),
2021-01-06 00:51:19 +01:00
for (final instance in accountsStore.instances) ...[
2021-01-03 19:43:39 +01:00
const SizedBox(height: 40),
2021-04-06 14:42:42 +02:00
ListTile(
dense: true,
contentPadding: EdgeInsets.zero,
onLongPress: () => instanceActions(instance),
title: _SectionHeading(instance),
),
2021-01-17 17:35:47 +01:00
for (final username in accountsStore.usernamesFor(instance)) ...[
2021-04-06 14:42:42 +02:00
ListTile(
trailing: username == accountsStore.defaultUsernameFor(instance)
? Icon(
Icons.check_circle_outline,
color: theme.colorScheme.secondary,
2021-04-06 14:42:42 +02:00
)
: null,
title: Text(username),
onLongPress: () => accountActions(instance, username),
onTap: () {
goTo(
context,
(_) => ManageAccountPage(
instanceHost: instance,
username: username,
));
},
),
2020-09-08 16:37:58 +02:00
],
2021-01-17 17:35:47 +01:00
if (accountsStore.usernamesFor(instance).isEmpty)
ListTile(
2021-01-03 19:43:39 +01:00
leading: const Icon(Icons.add),
2021-03-03 12:31:36 +01:00
title: const Text('Add account'),
onTap: () {
Navigator.of(context).push(AddAccountPage.route(instance));
},
),
]
],
),
);
}
}
2020-09-03 23:14:26 +02:00
class _SectionHeading extends StatelessWidget {
final String text;
const _SectionHeading(this.text);
@override
Widget build(BuildContext context) {
final theme = Theme.of(context);
return Padding(
2021-01-03 19:43:39 +01:00
padding: const EdgeInsets.only(left: 20),
2020-09-03 23:14:26 +02:00
child: Text(text.toUpperCase(),
style: theme.textTheme.subtitle2
?.copyWith(color: theme.colorScheme.secondary)),
2020-09-03 23:14:26 +02:00
);
}
}