fluffychat/lib/views/settings.dart

586 lines
20 KiB
Dart
Raw Normal View History

2021-02-05 16:30:52 +01:00
import 'dart:async';
2020-11-14 10:08:13 +01:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2021-01-16 12:46:38 +01:00
import 'package:adaptive_page_layout/adaptive_page_layout.dart';
2021-02-23 15:15:54 +01:00
import 'package:fluffychat/components/default_bottom_navigation_bar.dart';
import 'package:fluffychat/components/dialogs/bootstrap_dialog.dart';
2021-01-17 15:33:31 +01:00
import 'package:fluffychat/components/sentry_switch_list_tile.dart';
2021-01-19 16:36:21 +01:00
import 'package:fluffychat/components/settings_switch_list_tile.dart';
import 'package:flushbar/flushbar_helper.dart';
2020-01-01 19:10:13 +01:00
import 'package:famedlysdk/famedlysdk.dart';
2020-10-04 17:01:54 +02:00
import 'package:file_picker_cross/file_picker_cross.dart';
2020-12-19 13:06:31 +01:00
import 'package:fluffychat/utils/beautify_string_extension.dart';
2020-10-04 17:01:54 +02:00
2020-12-11 14:14:33 +01:00
import 'package:fluffychat/app_config.dart';
2020-10-04 17:01:54 +02:00
import 'package:fluffychat/utils/platform_infos.dart';
2020-09-08 10:55:32 +02:00
import 'package:fluffychat/utils/sentry_controller.dart';
2020-01-01 19:10:13 +01:00
import 'package:flutter/material.dart';
2021-01-18 09:43:53 +01:00
import 'package:flutter_app_lock/flutter_app_lock.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-01-18 08:38:19 +01:00
import 'package:flutter_screen_lock/lock_screen.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
2020-01-01 19:10:13 +01:00
import 'package:image_picker/image_picker.dart';
2020-01-04 09:40:38 +01:00
import 'package:url_launcher/url_launcher.dart';
2020-01-01 19:10:13 +01:00
2021-02-13 11:55:22 +01:00
import '../components/content_banner.dart';
2020-12-25 09:58:34 +01:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2021-02-13 11:55:22 +01:00
import '../components/matrix.dart';
import '../app_config.dart';
import '../config/setting_keys.dart';
2020-01-01 19:10:13 +01:00
class Settings extends StatefulWidget {
@override
_SettingsState createState() => _SettingsState();
}
class _SettingsState extends State<Settings> {
Future<dynamic> profileFuture;
dynamic profile;
2020-06-25 16:29:06 +02:00
Future<bool> crossSigningCachedFuture;
bool crossSigningCached;
Future<bool> megolmBackupCachedFuture;
bool megolmBackupCached;
2020-01-01 19:10:13 +01:00
void logoutAction(BuildContext context) async {
2020-11-14 10:08:13 +01:00
if (await showOkCancelAlertDialog(
context: context,
2021-02-06 20:16:58 +01:00
title: L10n.of(context).areYouSureYouWantToLogout,
2021-02-18 14:23:22 +01:00
okLabel: L10n.of(context).yes,
cancelLabel: L10n.of(context).cancel,
2020-11-14 10:08:13 +01:00
) ==
OkCancelResult.cancel) {
2020-02-16 09:56:17 +01:00
return;
}
2020-05-13 15:58:59 +02:00
var matrix = Matrix.of(context);
2020-12-25 09:58:34 +01:00
await showFutureLoadingDialog(
context: context,
future: () => matrix.client.logout(),
);
2020-01-01 19:10:13 +01:00
}
2020-09-21 17:50:01 +02:00
void _changePasswordAccountAction(BuildContext context) async {
2020-11-14 10:08:13 +01:00
final input = await showTextInputDialog(
context: context,
title: L10n.of(context).changePassword,
2021-02-23 11:03:54 +01:00
okLabel: L10n.of(context).ok,
cancelLabel: L10n.of(context).cancel,
2020-11-14 10:08:13 +01:00
textFields: [
DialogTextField(
hintText: L10n.of(context).pleaseEnterYourPassword,
obscureText: true,
minLines: 1,
maxLines: 1,
2020-11-14 10:08:13 +01:00
),
DialogTextField(
hintText: L10n.of(context).chooseAStrongPassword,
obscureText: true,
minLines: 1,
maxLines: 1,
2020-11-14 10:08:13 +01:00
),
],
2020-09-21 17:50:01 +02:00
);
2020-11-14 10:08:13 +01:00
if (input == null) return;
2020-12-25 09:58:34 +01:00
await showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context)
2020-09-21 17:50:01 +02:00
.client
2020-11-14 10:08:13 +01:00
.changePassword(input.last, oldPassword: input.first),
2020-09-21 17:50:01 +02:00
);
await FlushbarHelper.createSuccess(
message: L10n.of(context).passwordHasBeenChanged)
.show(context);
2020-09-21 17:50:01 +02:00
}
void _deleteAccountAction(BuildContext context) async {
2020-11-14 10:08:13 +01:00
if (await showOkCancelAlertDialog(
context: context,
title: L10n.of(context).warning,
message: L10n.of(context).deactivateAccountWarning,
2021-02-18 14:23:22 +01:00
okLabel: L10n.of(context).ok,
cancelLabel: L10n.of(context).cancel,
2020-09-21 17:50:01 +02:00
) ==
2020-11-14 10:08:13 +01:00
OkCancelResult.cancel) {
2020-09-21 17:50:01 +02:00
return;
}
2020-11-14 10:08:13 +01:00
if (await showOkCancelAlertDialog(
2021-02-18 14:23:22 +01:00
context: context,
title: L10n.of(context).areYouSure,
okLabel: L10n.of(context).yes,
cancelLabel: L10n.of(context).cancel,
) ==
2020-11-14 10:08:13 +01:00
OkCancelResult.cancel) {
2020-09-21 17:50:01 +02:00
return;
}
2020-11-14 10:08:13 +01:00
final input = await showTextInputDialog(
context: context,
title: L10n.of(context).pleaseEnterYourPassword,
2021-02-23 11:03:54 +01:00
okLabel: L10n.of(context).ok,
cancelLabel: L10n.of(context).cancel,
textFields: [
DialogTextField(
obscureText: true,
hintText: '******',
minLines: 1,
maxLines: 1,
)
],
2020-09-21 17:50:01 +02:00
);
2020-11-14 10:08:13 +01:00
if (input == null) return;
2020-12-25 09:58:34 +01:00
await showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context).client.deactivateAccount(
2020-12-11 10:27:38 +01:00
auth: AuthenticationPassword(
password: input.single,
user: Matrix.of(context).client.userID,
identifier: AuthenticationUserIdentifier(
user: Matrix.of(context).client.userID),
),
),
2020-09-21 17:50:01 +02:00
);
}
2020-04-08 17:43:07 +02:00
void setJitsiInstanceAction(BuildContext context) async {
2020-11-23 14:27:10 +01:00
const prefix = 'https://';
2020-11-14 10:08:13 +01:00
var input = await showTextInputDialog(
context: context,
title: L10n.of(context).editJitsiInstance,
2021-02-23 11:03:54 +01:00
okLabel: L10n.of(context).ok,
cancelLabel: L10n.of(context).cancel,
2020-11-14 10:08:13 +01:00
textFields: [
2020-11-23 14:27:10 +01:00
DialogTextField(
2020-12-11 14:14:33 +01:00
initialText: AppConfig.jitsiInstance.replaceFirst(prefix, ''),
2020-11-23 14:27:10 +01:00
prefixText: prefix,
),
2020-11-14 10:08:13 +01:00
],
2020-04-08 17:43:07 +02:00
);
2020-11-14 10:08:13 +01:00
if (input == null) return;
2020-11-23 14:27:10 +01:00
var jitsi = prefix + input.single;
2020-04-08 17:43:07 +02:00
if (!jitsi.endsWith('/')) {
jitsi += '/';
}
2020-05-13 15:58:59 +02:00
final matrix = Matrix.of(context);
await matrix.store.setItem(SettingKeys.jitsiInstance, jitsi);
2020-12-11 14:14:33 +01:00
AppConfig.jitsiInstance = jitsi;
2020-04-08 17:43:07 +02:00
}
2020-02-16 09:56:17 +01:00
void setDisplaynameAction(BuildContext context) async {
2020-11-14 10:08:13 +01:00
final input = await showTextInputDialog(
context: context,
title: L10n.of(context).editDisplayname,
2021-02-23 11:03:54 +01:00
okLabel: L10n.of(context).ok,
cancelLabel: L10n.of(context).cancel,
2020-11-14 10:08:13 +01:00
textFields: [
DialogTextField(
initialText: profile?.displayname ??
Matrix.of(context).client.userID.localpart,
)
],
2020-02-16 09:56:17 +01:00
);
2020-11-14 10:08:13 +01:00
if (input == null) return;
2020-05-13 15:58:59 +02:00
final matrix = Matrix.of(context);
2020-12-25 09:58:34 +01:00
final success = await showFutureLoadingDialog(
context: context,
future: () =>
matrix.client.setDisplayname(matrix.client.userID, input.single),
2020-01-01 19:10:13 +01:00
);
2020-12-25 09:58:34 +01:00
if (success.error == null) {
2020-01-01 19:10:13 +01:00
setState(() {
profileFuture = null;
profile = null;
});
}
}
void setAvatarAction(BuildContext context) async {
2020-10-04 17:01:54 +02:00
MatrixFile file;
if (PlatformInfos.isMobile) {
final result = await ImagePicker().getImage(
source: ImageSource.gallery,
imageQuality: 50,
maxWidth: 1600,
maxHeight: 1600);
if (result == null) return;
file = MatrixFile(
bytes: await result.readAsBytes(),
name: result.path,
);
} else {
final result =
await FilePickerCross.importFromStorage(type: FileTypeCross.image);
if (result == null) return;
file = MatrixFile(
bytes: result.toUint8List(),
name: result.fileName,
);
}
2020-05-13 15:58:59 +02:00
final matrix = Matrix.of(context);
2020-12-25 09:58:34 +01:00
final success = await showFutureLoadingDialog(
context: context,
future: () => matrix.client.setAvatar(file),
2020-01-01 19:10:13 +01:00
);
2020-12-25 09:58:34 +01:00
if (success.error == null) {
2020-01-01 19:10:13 +01:00
setState(() {
profileFuture = null;
profile = null;
});
}
}
2020-06-25 16:29:06 +02:00
Future<void> requestSSSSCache(BuildContext context) async {
final handle = Matrix.of(context).client.encryption.ssss.open();
2020-11-14 10:08:13 +01:00
final input = await showTextInputDialog(
context: context,
title: L10n.of(context).askSSSSCache,
2021-02-23 11:03:54 +01:00
okLabel: L10n.of(context).ok,
cancelLabel: L10n.of(context).cancel,
2020-11-14 10:08:13 +01:00
textFields: [
DialogTextField(
hintText: L10n.of(context).passphraseOrKey,
obscureText: true,
minLines: 1,
maxLines: 1,
)
2020-11-14 10:08:13 +01:00
],
2020-06-25 16:29:06 +02:00
);
2020-11-14 10:08:13 +01:00
if (input != null) {
2020-12-25 09:58:34 +01:00
final valid = await showFutureLoadingDialog(
context: context,
future: () async {
// make sure the loading spinner shows before we test the keys
await Future.delayed(Duration(milliseconds: 100));
var valid = false;
try {
await handle.unlock(recoveryKey: input.single);
valid = true;
} catch (e, s) {
SentryController.captureException(e, s);
}
return valid;
});
2020-11-14 10:08:13 +01:00
2020-12-25 09:58:34 +01:00
if (valid.result == true) {
2020-06-25 16:29:06 +02:00
await handle.maybeCacheAll();
2020-11-14 10:08:13 +01:00
await showOkAlertDialog(
context: context,
message: L10n.of(context).cachedKeys,
2020-06-25 16:29:06 +02:00
);
setState(() {
crossSigningCachedFuture = null;
crossSigningCached = null;
megolmBackupCachedFuture = null;
megolmBackupCached = null;
});
} else {
2020-11-14 10:08:13 +01:00
await showOkAlertDialog(
context: context,
message: L10n.of(context).incorrectPassphraseOrKey,
2020-06-25 16:29:06 +02:00
);
}
}
}
2021-01-18 08:38:19 +01:00
void _setAppLockAction(BuildContext context) async {
final currentLock =
await FlutterSecureStorage().read(key: SettingKeys.appLockKey);
if (currentLock?.isNotEmpty ?? false) {
var unlocked = false;
await showLockScreen(
context: context,
correctString: currentLock,
onUnlocked: () => unlocked = true,
canBiometric: true,
);
if (unlocked != true) return;
}
final newLock = await showTextInputDialog(
context: context,
title: L10n.of(context).pleaseChooseAPasscode,
message: L10n.of(context).pleaseEnter4Digits,
2021-02-23 11:03:54 +01:00
cancelLabel: L10n.of(context).cancel,
2021-01-18 08:38:19 +01:00
textFields: [
DialogTextField(
validator: (text) {
2021-02-11 15:27:07 +01:00
if (int.tryParse(text) == null || int.tryParse(text) < 0) {
return L10n.of(context).pleaseEnter4Digits;
}
2021-01-18 08:38:19 +01:00
if (text.length != 4 && text.isNotEmpty) {
return L10n.of(context).pleaseEnter4Digits;
}
return null;
},
keyboardType: TextInputType.number,
obscureText: true,
maxLines: 1,
minLines: 1,
)
],
);
if (newLock != null) {
await FlutterSecureStorage()
2021-01-18 09:43:53 +01:00
.write(key: SettingKeys.appLockKey, value: newLock.single);
if (newLock.single.isEmpty) {
AppLock.of(context).disable();
} else {
AppLock.of(context).enable();
}
2021-01-18 08:38:19 +01:00
}
}
2020-01-01 19:10:13 +01:00
@override
Widget build(BuildContext context) {
2020-05-13 15:58:59 +02:00
final client = Matrix.of(context).client;
2020-06-25 16:29:06 +02:00
profileFuture ??= client.ownProfile.then((p) {
2020-01-08 14:19:15 +01:00
if (mounted) setState(() => profile = p);
2020-06-25 16:29:06 +02:00
return p;
});
2021-01-16 14:24:52 +01:00
if (client.encryption != null) {
crossSigningCachedFuture ??=
client.encryption.crossSigning.isCached().then((c) {
if (mounted) setState(() => crossSigningCached = c);
return c;
});
megolmBackupCachedFuture ??=
client.encryption.keyManager.isCached().then((c) {
if (mounted) setState(() => megolmBackupCached = c);
return c;
});
}
2021-02-13 11:55:22 +01:00
return Scaffold(
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) =>
<Widget>[
SliverAppBar(
2021-02-23 15:15:54 +01:00
elevation: 1,
automaticallyImplyLeading: false,
2021-02-13 11:55:22 +01:00
expandedHeight: 300.0,
floating: true,
pinned: true,
title: Text(L10n.of(context).settings,
style: TextStyle(
color: Theme.of(context)
.appBarTheme
.textTheme
.headline6
.color)),
backgroundColor: Theme.of(context).appBarTheme.color,
flexibleSpace: FlexibleSpaceBar(
background: ContentBanner(profile?.avatarUrl,
onEdit: () => setAvatarAction(context)),
2020-01-01 19:10:13 +01:00
),
),
2021-02-13 11:55:22 +01:00
],
body: ListView(
children: <Widget>[
ListTile(
title: Text(
L10n.of(context).notifications,
style: TextStyle(
color: Theme.of(context).accentColor,
fontWeight: FontWeight.bold,
),
),
2020-02-16 10:42:59 +01:00
),
2021-02-13 11:55:22 +01:00
ListTile(
trailing: Icon(Icons.notifications_outlined),
title: Text(L10n.of(context).notifications),
onTap: () => AdaptivePageLayout.of(context)
.pushNamed('/settings/notifications'),
2020-09-21 17:50:01 +02:00
),
2021-02-13 11:55:22 +01:00
Divider(thickness: 1),
ListTile(
title: Text(
L10n.of(context).chat,
style: TextStyle(
color: Theme.of(context).accentColor,
fontWeight: FontWeight.bold,
),
2020-02-16 09:56:17 +01:00
),
),
2020-02-16 10:42:59 +01:00
ListTile(
2021-02-13 11:55:22 +01:00
title: Text(L10n.of(context).changeTheme),
onTap: () =>
AdaptivePageLayout.of(context).pushNamed('/settings/style'),
trailing: Icon(Icons.style_outlined),
2020-10-03 11:11:28 +02:00
),
2021-02-13 11:55:22 +01:00
SettingsSwitchListTile(
title: L10n.of(context).renderRichContent,
onChanged: (b) => AppConfig.renderHtml = b,
storeKey: SettingKeys.renderHtml,
defaultValue: AppConfig.renderHtml,
2020-02-16 10:42:59 +01:00
),
2021-02-13 11:55:22 +01:00
SettingsSwitchListTile(
title: L10n.of(context).hideRedactedEvents,
onChanged: (b) => AppConfig.hideRedactedEvents = b,
storeKey: SettingKeys.hideRedactedEvents,
defaultValue: AppConfig.hideRedactedEvents,
2021-01-17 15:43:38 +01:00
),
2021-02-13 11:55:22 +01:00
SettingsSwitchListTile(
title: L10n.of(context).hideUnknownEvents,
onChanged: (b) => AppConfig.hideUnknownEvents = b,
storeKey: SettingKeys.hideUnknownEvents,
defaultValue: AppConfig.hideUnknownEvents,
),
ListTile(
title: Text(L10n.of(context).emoteSettings),
onTap: () =>
AdaptivePageLayout.of(context).pushNamed('/settings/emotes'),
trailing: Icon(Icons.insert_emoticon_outlined),
),
ListTile(
title: Text(L10n.of(context).archive),
onTap: () => AdaptivePageLayout.of(context).pushNamed('/archive'),
trailing: Icon(Icons.archive_outlined),
),
Divider(thickness: 1),
ListTile(
title: Text(
L10n.of(context).account,
style: TextStyle(
color: Theme.of(context).accentColor,
fontWeight: FontWeight.bold,
),
),
),
ListTile(
trailing: Icon(Icons.edit_outlined),
title: Text(L10n.of(context).editDisplayname),
subtitle: Text(profile?.displayname ?? client.userID.localpart),
onTap: () => setDisplaynameAction(context),
),
ListTile(
trailing: Icon(Icons.phone_outlined),
title: Text(L10n.of(context).editJitsiInstance),
subtitle: Text(AppConfig.jitsiInstance),
onTap: () => setJitsiInstanceAction(context),
),
ListTile(
trailing: Icon(Icons.devices_other_outlined),
title: Text(L10n.of(context).devices),
onTap: () =>
AdaptivePageLayout.of(context).pushNamed('/settings/devices'),
),
ListTile(
trailing: Icon(Icons.block_outlined),
title: Text(L10n.of(context).ignoredUsers),
onTap: () =>
AdaptivePageLayout.of(context).pushNamed('/settings/ignore'),
),
SentrySwitchListTile(),
Divider(thickness: 1),
ListTile(
trailing: Icon(Icons.security_outlined),
title: Text(
L10n.of(context).changePassword,
),
onTap: () => _changePasswordAccountAction(context),
),
ListTile(
trailing: Icon(Icons.email_outlined),
title: Text(L10n.of(context).passwordRecovery),
onTap: () =>
AdaptivePageLayout.of(context).pushNamed('/settings/3pid'),
),
ListTile(
trailing: Icon(Icons.exit_to_app_outlined),
title: Text(L10n.of(context).logout),
onTap: () => logoutAction(context),
),
ListTile(
trailing: Icon(Icons.delete_forever_outlined),
title: Text(
L10n.of(context).deleteAccount,
style: TextStyle(color: Colors.red),
),
onTap: () => _deleteAccountAction(context),
),
if (client.encryption != null) ...{
Divider(thickness: 1),
ListTile(
title: Text(
L10n.of(context).security,
style: TextStyle(
color: Theme.of(context).accentColor,
fontWeight: FontWeight.bold,
),
),
),
if (PlatformInfos.isMobile)
ListTile(
trailing: Icon(Icons.lock_outlined),
title: Text(L10n.of(context).appLock),
onTap: () => _setAppLockAction(context),
),
ListTile(
title: Text(L10n.of(context).yourPublicKey),
onTap: () => showOkAlertDialog(
context: context,
title: L10n.of(context).yourPublicKey,
message: client.fingerprintKey.beautified,
),
trailing: Icon(Icons.vpn_key_outlined),
),
ListTile(
title: Text(L10n.of(context).cachedKeys),
trailing: Icon(Icons.wb_cloudy_outlined),
subtitle: Text(
'${client.encryption.keyManager.enabled ? L10n.of(context).onlineKeyBackupEnabled : L10n.of(context).onlineKeyBackupDisabled}\n${client.encryption.crossSigning.enabled ? L10n.of(context).crossSigningEnabled : L10n.of(context).crossSigningDisabled}'),
2021-02-13 14:26:03 +01:00
onTap: () async {
if (await client.encryption.keyManager.isCached()) {
if (OkCancelResult.ok ==
await showOkCancelAlertDialog(
context: context,
title: L10n.of(context).keysCached,
2021-02-13 14:33:43 +01:00
message: L10n.of(context).wipeChatBackup,
2021-02-13 14:26:03 +01:00
isDestructiveAction: true,
2021-02-18 14:23:22 +01:00
okLabel: L10n.of(context).ok,
cancelLabel: L10n.of(context).cancel,
2021-02-13 14:26:03 +01:00
)) {
2021-02-13 17:32:58 +01:00
await BootstrapDialog(
2021-02-13 14:26:03 +01:00
l10n: L10n.of(context),
client: Matrix.of(context).client,
wipe: true,
).show(context);
}
2021-02-13 17:32:58 +01:00
return;
2021-02-13 14:26:03 +01:00
}
2021-02-13 17:32:58 +01:00
await BootstrapDialog(
2021-02-13 14:26:03 +01:00
l10n: L10n.of(context),
client: Matrix.of(context).client,
).show(context);
},
2021-02-13 11:55:22 +01:00
),
},
Divider(thickness: 1),
ListTile(
title: Text(
L10n.of(context).about,
style: TextStyle(
color: Theme.of(context).accentColor,
fontWeight: FontWeight.bold,
),
),
onTap: () => AdaptivePageLayout.of(context).pushNamed('/logs'),
),
ListTile(
trailing: Icon(Icons.help_outlined),
title: Text(L10n.of(context).help),
onTap: () => launch(AppConfig.supportUrl),
),
ListTile(
trailing: Icon(Icons.privacy_tip_outlined),
title: Text(L10n.of(context).privacy),
onTap: () => launch(AppConfig.privacyUrl),
),
ListTile(
trailing: Icon(Icons.link_outlined),
title: Text(L10n.of(context).about),
onTap: () => PlatformInfos.showDialog(context),
),
],
2021-02-03 15:47:51 +01:00
),
2021-02-13 11:55:22 +01:00
),
2021-02-23 15:15:54 +01:00
bottomNavigationBar: DefaultBottomNavigationBar(currentIndex: 3),
2020-01-01 19:10:13 +01:00
);
}
}