fluffychat/lib/pages/settings/settings.dart

220 lines
6.2 KiB
Dart
Raw Normal View History

2021-04-24 06:22:42 +00:00
import 'dart:async';
2021-10-26 16:50:34 +00:00
import 'package:flutter/material.dart';
2021-05-23 11:11:55 +00:00
2021-10-26 16:50:34 +00:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:collection/collection.dart';
import 'package:file_picker/file_picker.dart';
2021-04-24 06:22:42 +00:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-10-26 16:50:34 +00:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2021-04-24 06:22:42 +00:00
import 'package:image_picker/image_picker.dart';
2021-10-26 16:50:34 +00:00
import 'package:matrix/matrix.dart';
2021-04-24 06:22:42 +00:00
2021-10-26 16:50:34 +00:00
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/widgets/app_lock.dart';
2021-11-09 20:32:16 +00:00
import '../../widgets/matrix.dart';
import '../bootstrap/bootstrap_dialog.dart';
2021-11-09 20:32:16 +00:00
import 'settings_view.dart';
2021-04-24 06:22:42 +00:00
class Settings extends StatefulWidget {
const Settings({super.key});
2021-10-14 16:09:30 +00:00
2021-04-24 06:22:42 +00:00
@override
SettingsController createState() => SettingsController();
}
class SettingsController extends State<Settings> {
2023-02-04 17:32:56 +00:00
Future<Profile>? profileFuture;
bool profileUpdated = false;
void updateProfile() => setState(() {
profileUpdated = true;
2023-02-04 17:32:56 +00:00
profileFuture = null;
});
2021-04-24 06:22:42 +00:00
2023-02-04 17:32:56 +00:00
void setDisplaynameAction() async {
final profile = await profileFuture;
final input = await showTextInputDialog(
useRootNavigator: false,
2023-02-04 17:32:56 +00:00
context: context,
title: L10n.of(context)!.editDisplayname,
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
textFields: [
DialogTextField(
initialText: profile?.displayName ??
Matrix.of(context).client.userID!.localpart,
2023-08-18 05:24:31 +00:00
),
2023-02-04 17:32:56 +00:00
],
);
if (input == null) return;
final matrix = Matrix.of(context);
final success = await showFutureLoadingDialog(
context: context,
future: () =>
matrix.client.setDisplayName(matrix.client.userID!, input.single),
);
if (success.error == null) {
updateProfile();
}
}
void logoutAction() async {
final noBackup = showChatBackupBanner == true;
2023-02-04 17:32:56 +00:00
if (await showOkCancelAlertDialog(
useRootNavigator: false,
2023-02-04 17:32:56 +00:00
context: context,
title: L10n.of(context)!.areYouSureYouWantToLogout,
message: L10n.of(context)!.noBackupWarning,
isDestructiveAction: noBackup,
okLabel: L10n.of(context)!.logout,
2023-02-04 17:32:56 +00:00
cancelLabel: L10n.of(context)!.cancel,
) ==
OkCancelResult.cancel) {
return;
}
final matrix = Matrix.of(context);
await showFutureLoadingDialog(
context: context,
future: () => matrix.client.logout(),
);
}
2021-04-24 06:22:42 +00:00
void setAvatarAction() async {
2023-02-04 17:32:56 +00:00
final profile = await profileFuture;
2021-11-15 06:43:19 +00:00
final actions = [
if (PlatformInfos.isMobile)
2021-11-13 20:21:13 +00:00
SheetAction(
key: AvatarAction.camera,
2022-01-29 11:35:03 +00:00
label: L10n.of(context)!.openCamera,
2021-11-13 20:21:13 +00:00
isDefaultAction: true,
2021-11-14 12:24:01 +00:00
icon: Icons.camera_alt_outlined,
2021-11-13 20:21:13 +00:00
),
2021-11-15 06:43:19 +00:00
SheetAction(
key: AvatarAction.file,
2022-01-29 11:35:03 +00:00
label: L10n.of(context)!.openGallery,
2021-11-15 06:43:19 +00:00
icon: Icons.photo_outlined,
),
if (profile?.avatarUrl != null)
2021-11-13 20:21:13 +00:00
SheetAction(
2021-11-15 06:43:19 +00:00
key: AvatarAction.remove,
2022-01-29 11:35:03 +00:00
label: L10n.of(context)!.removeYourAvatar,
2021-11-15 06:43:19 +00:00
isDestructiveAction: true,
icon: Icons.delete_outlined,
2021-11-13 20:21:13 +00:00
),
2021-11-15 06:43:19 +00:00
];
final action = actions.length == 1
? actions.single.key
2021-11-15 06:43:19 +00:00
: await showModalActionSheet<AvatarAction>(
context: context,
2022-01-29 11:35:03 +00:00
title: L10n.of(context)!.changeYourAvatar,
2021-11-15 06:43:19 +00:00
actions: actions,
);
2021-05-31 17:33:40 +00:00
if (action == null) return;
final matrix = Matrix.of(context);
if (action == AvatarAction.remove) {
final success = await showFutureLoadingDialog(
context: context,
2021-11-24 06:29:05 +00:00
future: () => matrix.client.setAvatar(null),
2021-05-31 17:33:40 +00:00
);
if (success.error == null) {
updateProfile();
2021-05-31 17:33:40 +00:00
}
return;
}
2021-04-24 06:22:42 +00:00
MatrixFile file;
if (PlatformInfos.isMobile) {
final result = await ImagePicker().pickImage(
source: action == AvatarAction.camera
? ImageSource.camera
: ImageSource.gallery,
imageQuality: 50,
);
2021-04-24 06:22:42 +00:00
if (result == null) return;
file = MatrixFile(
bytes: await result.readAsBytes(),
name: result.path,
);
} else {
final result = await AppLock.of(context).pauseWhile(
FilePicker.platform.pickFiles(
type: FileType.image,
withData: true,
),
);
final pickedFile = result?.files.firstOrNull;
if (pickedFile == null) return;
2021-04-24 06:22:42 +00:00
file = MatrixFile(
bytes: pickedFile.bytes!,
name: pickedFile.name,
2021-04-24 06:22:42 +00:00
);
}
final success = await showFutureLoadingDialog(
context: context,
future: () => matrix.client.setAvatar(file),
);
if (success.error == null) {
updateProfile();
2021-04-24 06:22:42 +00:00
}
}
@override
void initState() {
WidgetsBinding.instance.addPostFrameCallback((_) => checkBootstrap());
super.initState();
}
void checkBootstrap() async {
final client = Matrix.of(context).client;
if (!client.encryptionEnabled) return;
await client.accountDataLoading;
await client.userDeviceKeysLoading;
if (client.prevBatch == null) {
await client.onSync.stream.first;
}
final crossSigning =
await client.encryption?.crossSigning.isCached() ?? false;
final needsBootstrap =
await client.encryption?.keyManager.isCached() == false ||
client.encryption?.crossSigning.enabled == false ||
crossSigning == false;
final isUnknownSession = client.isUnknownSession;
setState(() {
showChatBackupBanner = needsBootstrap || isUnknownSession;
});
}
bool? crossSigningCached;
2023-02-04 17:32:56 +00:00
bool? showChatBackupBanner;
2023-02-04 17:32:56 +00:00
void firstRunBootstrapAction([_]) async {
2023-02-04 20:59:53 +00:00
if (showChatBackupBanner != true) {
showOkAlertDialog(
context: context,
title: L10n.of(context)!.chatBackup,
message: L10n.of(context)!.onlineKeyBackupEnabled,
okLabel: L10n.of(context)!.close,
);
return;
}
await BootstrapDialog(
client: Matrix.of(context).client,
).show(context);
checkBootstrap();
}
2021-04-24 06:22:42 +00:00
@override
Widget build(BuildContext context) {
final client = Matrix.of(context).client;
2023-02-04 17:32:56 +00:00
profileFuture ??= client.getProfileFromUserId(
2022-01-29 11:35:03 +00:00
client.userID!,
cache: !profileUpdated,
getFromRooms: !profileUpdated,
2023-02-04 17:32:56 +00:00
);
2021-05-22 07:13:47 +00:00
return SettingsView(this);
2021-04-24 06:22:42 +00:00
}
}
2021-05-31 17:33:40 +00:00
2021-11-13 20:21:13 +00:00
enum AvatarAction { camera, file, remove }