FurryChat/lib/views/settings.dart

581 lines
20 KiB
Dart
Raw Normal View History

2020-06-20 09:32:49 +00:00
import 'dart:io';
2020-09-21 15:50:01 +00:00
import 'package:bot_toast/bot_toast.dart';
2020-01-01 18:10:13 +00:00
import 'package:famedlysdk/famedlysdk.dart';
2020-10-04 15:01:54 +00:00
import 'package:file_picker_cross/file_picker_cross.dart';
2020-02-23 07:49:58 +00:00
import 'package:fluffychat/components/settings_themes.dart';
2020-09-18 10:58:22 +00:00
import 'package:fluffychat/config/app_config.dart';
2020-10-04 15:01:54 +00:00
import 'package:fluffychat/utils/platform_infos.dart';
2020-02-19 15:23:13 +00:00
import 'package:fluffychat/views/settings_devices.dart';
2020-09-19 13:29:12 +00:00
import 'package:fluffychat/views/settings_ignore_list.dart';
2020-04-03 18:24:25 +00:00
import 'package:flutter/foundation.dart';
2020-01-01 18:10:13 +00:00
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2020-01-01 18:10:13 +00:00
import 'package:image_picker/image_picker.dart';
2020-01-04 08:40:38 +00:00
import 'package:url_launcher/url_launcher.dart';
2020-01-01 18:10:13 +00:00
2020-02-23 07:49:58 +00:00
import '../components/adaptive_page_layout.dart';
import '../components/content_banner.dart';
import '../components/dialogs/simple_dialogs.dart';
import '../components/matrix.dart';
import '../utils/app_route.dart';
import 'app_info.dart';
import 'chat_list.dart';
2020-05-12 07:02:33 +00:00
import 'settings_emotes.dart';
2020-01-01 18:10:13 +00:00
class SettingsView extends StatelessWidget {
@override
Widget build(BuildContext context) {
return AdaptivePageLayout(
primaryPage: FocusPage.SECOND,
firstScaffold: ChatList(),
secondScaffold: Settings(),
);
}
}
class Settings extends StatefulWidget {
@override
_SettingsState createState() => _SettingsState();
}
class _SettingsState extends State<Settings> {
Future<dynamic> profileFuture;
dynamic profile;
2020-06-25 14:29:06 +00:00
Future<bool> crossSigningCachedFuture;
bool crossSigningCached;
Future<bool> megolmBackupCachedFuture;
bool megolmBackupCached;
2020-01-01 18:10:13 +00:00
void logoutAction(BuildContext context) async {
2020-02-16 08:56:17 +00:00
if (await SimpleDialogs(context).askConfirmation() == false) {
return;
}
2020-05-13 13:58:59 +00:00
var matrix = Matrix.of(context);
2020-04-27 11:36:39 +00:00
await SimpleDialogs(context)
.tryRequestWithLoadingDialog(matrix.client.logout());
2020-01-01 18:10:13 +00:00
}
2020-09-21 15:50:01 +00:00
void _changePasswordAccountAction(BuildContext context) async {
final oldPassword = await SimpleDialogs(context).enterText(
password: true,
titleText: L10n.of(context).pleaseEnterYourPassword,
);
if (oldPassword == null) return;
final newPassword = await SimpleDialogs(context).enterText(
password: true,
titleText: L10n.of(context).chooseAStrongPassword,
);
if (newPassword == null) return;
await SimpleDialogs(context).tryRequestWithLoadingDialog(
Matrix.of(context)
.client
.changePassword(newPassword, oldPassword: oldPassword),
);
BotToast.showText(text: L10n.of(context).passwordHasBeenChanged);
}
void _deleteAccountAction(BuildContext context) async {
if (await SimpleDialogs(context).askConfirmation(
titleText: L10n.of(context).warning,
contentText: L10n.of(context).deactivateAccountWarning,
dangerous: true,
) ==
false) {
return;
}
if (await SimpleDialogs(context).askConfirmation(dangerous: true) ==
false) {
return;
}
final password = await SimpleDialogs(context).enterText(
password: true,
titleText: L10n.of(context).pleaseEnterYourPassword,
);
if (password == null) return;
await SimpleDialogs(context).tryRequestWithLoadingDialog(
Matrix.of(context).client.deactivateAccount(auth: {
'type': 'm.login.password',
'user': Matrix.of(context).client.userID,
'password': password,
}),
);
}
2020-04-08 15:43:07 +00:00
void setJitsiInstanceAction(BuildContext context) async {
var jitsi = await SimpleDialogs(context).enterText(
2020-05-07 05:52:40 +00:00
titleText: L10n.of(context).editJitsiInstance,
2020-04-08 15:43:07 +00:00
hintText: Matrix.of(context).jitsiInstance,
2020-05-07 05:52:40 +00:00
labelText: L10n.of(context).editJitsiInstance,
2020-04-08 15:43:07 +00:00
);
if (jitsi == null) return;
if (!jitsi.endsWith('/')) {
jitsi += '/';
}
2020-05-13 13:58:59 +00:00
final matrix = Matrix.of(context);
await matrix.store.setItem('chat.fluffy.jitsi_instance', jitsi);
2020-04-08 15:43:07 +00:00
matrix.jitsiInstance = jitsi;
}
2020-02-16 08:56:17 +00:00
void setDisplaynameAction(BuildContext context) async {
2020-05-13 13:58:59 +00:00
final displayname = await SimpleDialogs(context).enterText(
2020-05-07 05:52:40 +00:00
titleText: L10n.of(context).editDisplayname,
2020-02-16 08:56:17 +00:00
hintText:
profile?.displayname ?? Matrix.of(context).client.userID.localpart,
2020-05-07 05:52:40 +00:00
labelText: L10n.of(context).enterAUsername,
2020-02-16 08:56:17 +00:00
);
if (displayname == null) return;
2020-05-13 13:58:59 +00:00
final matrix = Matrix.of(context);
2020-04-27 11:36:39 +00:00
final success = await SimpleDialogs(context).tryRequestWithLoadingDialog(
2020-08-16 10:54:43 +00:00
matrix.client.setDisplayname(matrix.client.userID, displayname),
2020-01-01 18:10:13 +00:00
);
2020-02-16 08:56:17 +00:00
if (success != false) {
2020-01-01 18:10:13 +00:00
setState(() {
profileFuture = null;
profile = null;
});
}
}
void setAvatarAction(BuildContext context) async {
2020-10-04 15:01:54 +00: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 13:58:59 +00:00
final matrix = Matrix.of(context);
2020-04-27 11:36:39 +00:00
final success = await SimpleDialogs(context).tryRequestWithLoadingDialog(
2020-10-04 15:01:54 +00:00
matrix.client.setAvatar(file),
2020-01-01 18:10:13 +00:00
);
2020-01-04 08:40:38 +00:00
if (success != false) {
2020-01-01 18:10:13 +00:00
setState(() {
profileFuture = null;
profile = null;
});
}
}
2020-04-03 18:24:25 +00:00
void setWallpaperAction(BuildContext context) async {
2020-06-20 09:32:49 +00:00
final wallpaper = await ImagePicker().getImage(source: ImageSource.gallery);
2020-04-03 18:24:25 +00:00
if (wallpaper == null) return;
2020-06-20 09:32:49 +00:00
Matrix.of(context).wallpaper = File(wallpaper.path);
2020-04-03 18:24:25 +00:00
await Matrix.of(context)
2020-05-13 13:58:59 +00:00
.store
.setItem('chat.fluffy.wallpaper', wallpaper.path);
2020-04-03 18:24:25 +00:00
setState(() => null);
}
void deleteWallpaperAction(BuildContext context) async {
Matrix.of(context).wallpaper = null;
2020-05-13 13:58:59 +00:00
await Matrix.of(context).store.setItem('chat.fluffy.wallpaper', null);
2020-04-03 18:24:25 +00:00
setState(() => null);
}
2020-06-25 14:29:06 +00:00
Future<void> requestSSSSCache(BuildContext context) async {
final handle = Matrix.of(context).client.encryption.ssss.open();
final str = await SimpleDialogs(context).enterText(
titleText: L10n.of(context).askSSSSCache,
hintText: L10n.of(context).passphraseOrKey,
password: true,
);
if (str != null) {
SimpleDialogs(context).showLoadingDialog(context);
// make sure the loading spinner shows before we test the keys
await Future.delayed(Duration(milliseconds: 100));
var valid = false;
try {
handle.unlock(recoveryKey: str);
valid = true;
} catch (e, s) {
debugPrint('Couldn\'t use recovery key: ' + e.toString());
debugPrint(s.toString());
2020-06-25 14:29:06 +00:00
try {
handle.unlock(passphrase: str);
valid = true;
} catch (e, s) {
debugPrint('Couldn\'t use recovery passphrase: ' + e.toString());
debugPrint(s.toString());
2020-06-25 14:29:06 +00:00
valid = false;
}
}
await Navigator.of(context)?.pop();
if (valid) {
await handle.maybeCacheAll();
await SimpleDialogs(context).inform(
contentText: L10n.of(context).cachedKeys,
);
setState(() {
crossSigningCachedFuture = null;
crossSigningCached = null;
megolmBackupCachedFuture = null;
megolmBackupCached = null;
});
} else {
await SimpleDialogs(context).inform(
contentText: L10n.of(context).incorrectPassphraseOrKey,
);
}
}
}
2020-01-01 18:10:13 +00:00
@override
Widget build(BuildContext context) {
2020-05-13 13:58:59 +00:00
final client = Matrix.of(context).client;
2020-06-25 14:29:06 +00:00
profileFuture ??= client.ownProfile.then((p) {
2020-01-08 13:19:15 +00:00
if (mounted) setState(() => profile = p);
2020-06-25 14:29:06 +00:00
return p;
});
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;
2020-01-08 13:19:15 +00:00
});
2020-01-01 18:10:13 +00:00
return Scaffold(
2020-02-16 09:42:59 +00:00
body: NestedScrollView(
headerSliverBuilder: (BuildContext context, bool innerBoxIsScrolled) =>
<Widget>[
SliverAppBar(
expandedHeight: 300.0,
floating: true,
pinned: true,
backgroundColor: Theme.of(context).appBarTheme.color,
flexibleSpace: FlexibleSpaceBar(
2020-02-16 19:11:39 +00:00
title: Text(
2020-05-07 05:52:40 +00:00
L10n.of(context).settings,
2020-02-16 19:11:39 +00:00
style: TextStyle(
2020-05-06 16:43:30 +00:00
color: Theme.of(context)
.appBarTheme
.textTheme
.headline6
.color),
2020-02-16 19:11:39 +00:00
),
2020-02-16 09:42:59 +00:00
background: ContentBanner(
2020-04-28 12:11:56 +00:00
profile?.avatarUrl,
2020-02-16 09:42:59 +00:00
height: 300,
defaultIcon: Icons.account_circle,
loading: profile == null,
onEdit: () => setAvatarAction(context),
2020-01-01 18:10:13 +00:00
),
),
),
2020-02-16 09:42:59 +00:00
],
body: ListView(
children: <Widget>[
2020-02-23 07:49:58 +00:00
ListTile(
title: Text(
2020-05-07 05:52:40 +00:00
L10n.of(context).changeTheme,
2020-02-23 07:49:58 +00:00
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
),
),
),
ThemesSettings(),
2020-05-13 13:58:59 +00:00
if (!kIsWeb && Matrix.of(context).store != null)
Divider(thickness: 1),
if (!kIsWeb && Matrix.of(context).store != null)
2020-04-03 18:24:25 +00:00
ListTile(
title: Text(
2020-05-07 05:52:40 +00:00
L10n.of(context).wallpaper,
2020-04-03 18:24:25 +00:00
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
),
),
),
if (Matrix.of(context).wallpaper != null)
ListTile(
title: Image.file(
Matrix.of(context).wallpaper,
height: 38,
fit: BoxFit.cover,
),
trailing: Icon(
Icons.delete_forever,
color: Colors.red,
),
onTap: () => deleteWallpaperAction(context),
),
2020-05-13 13:58:59 +00:00
if (!kIsWeb && Matrix.of(context).store != null)
2020-04-03 18:24:25 +00:00
Builder(builder: (context) {
return ListTile(
2020-05-07 05:52:40 +00:00
title: Text(L10n.of(context).changeWallpaper),
2020-04-03 18:24:25 +00:00
trailing: Icon(Icons.wallpaper),
onTap: () => setWallpaperAction(context),
);
}),
2020-02-23 07:49:58 +00:00
Divider(thickness: 1),
2020-05-09 11:36:41 +00:00
ListTile(
title: Text(
L10n.of(context).chat,
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
),
),
),
ListTile(
title: Text(L10n.of(context).renderRichContent),
trailing: Switch(
value: Matrix.of(context).renderHtml,
activeColor: Theme.of(context).primaryColor,
onChanged: (bool newValue) async {
Matrix.of(context).renderHtml = newValue;
2020-05-13 13:58:59 +00:00
await Matrix.of(context)
.store
.setItem('chat.fluffy.renderHtml', newValue ? '1' : '0');
2020-05-09 11:36:41 +00:00
setState(() => null);
},
),
),
2020-05-12 07:02:33 +00:00
ListTile(
title: Text(L10n.of(context).emoteSettings),
onTap: () async => await Navigator.of(context).push(
AppRoute.defaultRoute(
context,
EmotesSettingsView(),
),
),
trailing: Icon(Icons.insert_emoticon),
),
2020-05-09 11:36:41 +00:00
Divider(thickness: 1),
2020-02-16 09:42:59 +00:00
ListTile(
title: Text(
2020-05-07 05:52:40 +00:00
L10n.of(context).account,
2020-02-16 09:42:59 +00:00
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
),
2020-01-04 08:40:38 +00:00
),
),
2020-02-16 09:42:59 +00:00
ListTile(
trailing: Icon(Icons.edit),
2020-05-07 05:52:40 +00:00
title: Text(L10n.of(context).editDisplayname),
2020-02-16 09:42:59 +00:00
subtitle: Text(profile?.displayname ?? client.userID.localpart),
onTap: () => setDisplaynameAction(context),
2020-02-15 08:20:08 +00:00
),
2020-04-08 15:43:07 +00:00
ListTile(
trailing: Icon(Icons.phone),
2020-05-07 05:52:40 +00:00
title: Text(L10n.of(context).editJitsiInstance),
2020-04-08 15:43:07 +00:00
subtitle: Text(Matrix.of(context).jitsiInstance),
onTap: () => setJitsiInstanceAction(context),
),
ListTile(
2020-02-23 07:49:58 +00:00
trailing: Icon(Icons.devices_other),
2020-05-07 05:52:40 +00:00
title: Text(L10n.of(context).devices),
onTap: () async => await Navigator.of(context).push(
AppRoute.defaultRoute(
context,
2020-02-23 07:49:58 +00:00
DevicesSettingsView(),
),
),
),
2020-09-19 13:29:12 +00:00
ListTile(
trailing: Icon(Icons.block),
title: Text(L10n.of(context).ignoredUsers),
onTap: () async => await Navigator.of(context).push(
AppRoute.defaultRoute(
context,
SettingsIgnoreListView(),
),
),
),
2020-02-19 15:23:13 +00:00
ListTile(
2020-02-23 07:49:58 +00:00
trailing: Icon(Icons.account_circle),
title: Text(L10n.of(context).accountInformation),
2020-02-23 07:49:58 +00:00
onTap: () => Navigator.of(context).push(
2020-02-19 15:23:13 +00:00
AppRoute.defaultRoute(
context,
2020-02-23 07:49:58 +00:00
AppInfoView(),
2020-02-19 15:23:13 +00:00
),
),
),
2020-09-21 15:50:01 +00:00
Divider(thickness: 1),
ListTile(
trailing: Icon(Icons.vpn_key),
title: Text(
'Change password',
),
onTap: () => _changePasswordAccountAction(context),
),
2020-02-16 09:42:59 +00:00
ListTile(
trailing: Icon(Icons.exit_to_app),
2020-05-07 05:52:40 +00:00
title: Text(L10n.of(context).logout),
2020-02-16 09:42:59 +00:00
onTap: () => logoutAction(context),
),
2020-09-21 15:50:01 +00:00
ListTile(
trailing: Icon(Icons.delete_forever),
title: Text(
L10n.of(context).deleteAccount,
style: TextStyle(color: Colors.red),
),
onTap: () => _deleteAccountAction(context),
),
2020-02-16 09:42:59 +00:00
Divider(thickness: 1),
2020-06-25 14:29:06 +00:00
ListTile(
title: Text(
L10n.of(context).encryption,
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
),
),
),
ListTile(
trailing: Icon(Icons.compare_arrows),
title: Text(client.encryption.crossSigning.enabled
? L10n.of(context).crossSigningEnabled
: L10n.of(context).crossSigningDisabled),
subtitle: client.encryption.crossSigning.enabled
? Text(client.isUnknownSession
? L10n.of(context).unknownSessionVerify
: L10n.of(context).sessionVerified +
', ' +
(crossSigningCached == null
? ''
: (crossSigningCached
? L10n.of(context).keysCached
: L10n.of(context).keysMissing)))
: null,
onTap: () async {
if (!client.encryption.crossSigning.enabled) {
await SimpleDialogs(context).inform(
contentText: L10n.of(context).noCrossSignBootstrap,
);
return;
}
if (client.isUnknownSession) {
final str = await SimpleDialogs(context).enterText(
titleText: L10n.of(context).askSSSSVerify,
hintText: L10n.of(context).passphraseOrKey,
password: true,
);
if (str != null) {
SimpleDialogs(context).showLoadingDialog(context);
// make sure the loading spinner shows before we test the keys
await Future.delayed(Duration(milliseconds: 100));
var valid = false;
try {
await client.encryption.crossSigning
.selfSign(recoveryKey: str);
valid = true;
} catch (_) {
try {
await client.encryption.crossSigning
.selfSign(passphrase: str);
valid = true;
} catch (_) {
valid = false;
}
}
await Navigator.of(context)?.pop();
if (valid) {
await SimpleDialogs(context).inform(
contentText: L10n.of(context).verifiedSession,
);
setState(() {
crossSigningCachedFuture = null;
crossSigningCached = null;
megolmBackupCachedFuture = null;
megolmBackupCached = null;
});
} else {
await SimpleDialogs(context).inform(
contentText: L10n.of(context).incorrectPassphraseOrKey,
);
}
}
}
if (!(await client.encryption.crossSigning.isCached())) {
await requestSSSSCache(context);
}
},
),
ListTile(
trailing: Icon(Icons.wb_cloudy),
title: Text(client.encryption.keyManager.enabled
? L10n.of(context).onlineKeyBackupEnabled
: L10n.of(context).onlineKeyBackupDisabled),
subtitle: client.encryption.keyManager.enabled
? Text(megolmBackupCached == null
? ''
: (megolmBackupCached
? L10n.of(context).keysCached
: L10n.of(context).keysMissing))
: null,
onTap: () async {
if (!client.encryption.keyManager.enabled) {
await SimpleDialogs(context).inform(
contentText: L10n.of(context).noMegolmBootstrap,
);
return;
}
if (!(await client.encryption.keyManager.isCached())) {
await requestSSSSCache(context);
}
},
),
Divider(thickness: 1),
2020-02-16 09:42:59 +00:00
ListTile(
title: Text(
2020-05-07 05:52:40 +00:00
L10n.of(context).about,
2020-02-16 09:42:59 +00:00
style: TextStyle(
color: Theme.of(context).primaryColor,
fontWeight: FontWeight.bold,
),
2020-02-16 08:56:17 +00:00
),
),
2020-02-16 09:42:59 +00:00
ListTile(
2020-03-29 10:06:25 +00:00
trailing: Icon(Icons.help),
2020-05-07 05:52:40 +00:00
title: Text(L10n.of(context).help),
2020-10-03 09:11:28 +00:00
onTap: () => launch(AppConfig.supportUrl),
),
ListTile(
trailing: Icon(Icons.privacy_tip_rounded),
title: Text(L10n.of(context).privacy),
onTap: () => launch(AppConfig.privacyUrl),
2020-02-16 09:42:59 +00:00
),
ListTile(
2020-03-29 10:06:25 +00:00
trailing: Icon(Icons.link),
2020-05-07 05:52:40 +00:00
title: Text(L10n.of(context).license),
2020-09-18 10:58:22 +00:00
onTap: () => showLicensePage(
context: context,
applicationIcon:
Image.asset('assets/logo.png', width: 100, height: 100),
applicationName: AppConfig.applicationName,
),
2020-02-16 09:42:59 +00:00
),
2020-10-02 09:24:19 +00:00
ListTile(
trailing: Icon(Icons.code),
title: Text(L10n.of(context).sourceCode),
2020-10-03 09:11:28 +00:00
onTap: () => launch(AppConfig.sourceCodeUrl),
2020-10-02 09:24:19 +00:00
),
2020-02-16 09:42:59 +00:00
],
),
2020-01-01 18:10:13 +00:00
),
);
}
}