fluffychat/lib/pages/settings/settings.dart

139 lines
3.9 KiB
Dart
Raw Normal View History

2021-04-24 08:22:42 +02:00
import 'dart:async';
2021-10-26 18:50:34 +02:00
import 'package:flutter/material.dart';
2021-05-23 13:11:55 +02:00
2021-10-26 18:50:34 +02:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
2021-04-24 08:22:42 +02:00
import 'package:file_picker_cross/file_picker_cross.dart';
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-10-26 18:50:34 +02:00
import 'package:future_loading_dialog/future_loading_dialog.dart';
2021-04-24 08:22:42 +02:00
import 'package:image_picker/image_picker.dart';
2021-10-26 18:50:34 +02:00
import 'package:matrix/matrix.dart';
2021-04-24 08:22:42 +02:00
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/utils/platform_infos.dart';
2021-11-09 21:32:16 +01:00
import '../../widgets/matrix.dart';
import 'settings_view.dart';
2021-04-24 08:22:42 +02:00
class Settings extends StatefulWidget {
2022-01-29 12:35:03 +01:00
const Settings({Key? key}) : super(key: key);
2021-10-14 18:09:30 +02:00
2021-04-24 08:22:42 +02:00
@override
SettingsController createState() => SettingsController();
}
class SettingsController extends State<Settings> {
2022-01-29 12:35:03 +01:00
Future<bool>? crossSigningCachedFuture;
bool? crossSigningCached;
Future<bool>? megolmBackupCachedFuture;
bool? megolmBackupCached;
Future<dynamic>? profileFuture;
Profile? profile;
bool profileUpdated = false;
void updateProfile() => setState(() {
profileUpdated = true;
profile = profileFuture = null;
});
2021-04-24 08:22:42 +02:00
void setAvatarAction() async {
2021-11-15 07:43:19 +01:00
final actions = [
if (PlatformInfos.isMobile)
2021-11-13 21:21:13 +01:00
SheetAction(
key: AvatarAction.camera,
2022-01-29 12:35:03 +01:00
label: L10n.of(context)!.openCamera,
2021-11-13 21:21:13 +01:00
isDefaultAction: true,
2021-11-14 13:24:01 +01:00
icon: Icons.camera_alt_outlined,
2021-11-13 21:21:13 +01:00
),
2021-11-15 07:43:19 +01:00
SheetAction(
key: AvatarAction.file,
2022-01-29 12:35:03 +01:00
label: L10n.of(context)!.openGallery,
2021-11-15 07:43:19 +01:00
icon: Icons.photo_outlined,
),
if (profile?.avatarUrl != null)
2021-11-13 21:21:13 +01:00
SheetAction(
2021-11-15 07:43:19 +01:00
key: AvatarAction.remove,
2022-01-29 12:35:03 +01:00
label: L10n.of(context)!.removeYourAvatar,
2021-11-15 07:43:19 +01:00
isDestructiveAction: true,
icon: Icons.delete_outlined,
2021-11-13 21:21:13 +01:00
),
2021-11-15 07:43:19 +01:00
];
final action = actions.length == 1
? actions.single
: await showModalActionSheet<AvatarAction>(
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.changeYourAvatar,
2021-11-15 07:43:19 +01:00
actions: actions,
);
2021-05-31 19:33:40 +02:00
if (action == null) return;
final matrix = Matrix.of(context);
if (action == AvatarAction.remove) {
final success = await showFutureLoadingDialog(
context: context,
2021-11-24 07:29:05 +01:00
future: () => matrix.client.setAvatar(null),
2021-05-31 19:33:40 +02:00
);
if (success.error == null) {
updateProfile();
2021-05-31 19:33:40 +02:00
}
return;
}
2021-04-24 08:22:42 +02:00
MatrixFile file;
if (PlatformInfos.isMobile) {
final result = await ImagePicker().pickImage(
source: action == AvatarAction.camera
? ImageSource.camera
: ImageSource.gallery,
imageQuality: 50,
);
2021-04-24 08:22:42 +02:00
if (result == null) return;
file = MatrixFile(
bytes: await result.readAsBytes(),
name: result.path,
);
} else {
final result =
await FilePickerCross.importFromStorage(type: FileTypeCross.image);
2022-01-29 12:35:03 +01:00
if (result.fileName == null) return;
2021-04-24 08:22:42 +02:00
file = MatrixFile(
bytes: result.toUint8List(),
2022-01-29 12:35:03 +01:00
name: result.fileName!,
2021-04-24 08:22:42 +02:00
);
}
final success = await showFutureLoadingDialog(
context: context,
future: () => matrix.client.setAvatar(file),
);
if (success.error == null) {
updateProfile();
2021-04-24 08:22:42 +02:00
}
}
@override
Widget build(BuildContext context) {
final client = Matrix.of(context).client;
2021-05-31 19:33:40 +02:00
profileFuture ??= client
.getProfileFromUserId(
2022-01-29 12:35:03 +01:00
client.userID!,
cache: !profileUpdated,
getFromRooms: !profileUpdated,
2021-05-31 19:33:40 +02:00
)
.then((p) {
2021-04-24 08:22:42 +02:00
if (mounted) setState(() => profile = p);
return p;
});
if (client.encryption != null) {
crossSigningCachedFuture ??=
2022-01-29 12:35:03 +01:00
client.encryption?.crossSigning.isCached().then((c) {
2021-04-24 08:22:42 +02:00
if (mounted) setState(() => crossSigningCached = c);
return c;
});
megolmBackupCachedFuture ??=
2022-01-29 12:35:03 +01:00
client.encryption?.keyManager.isCached().then((c) {
2021-04-24 08:22:42 +02:00
if (mounted) setState(() => megolmBackupCached = c);
return c;
});
}
2021-05-22 09:13:47 +02:00
return SettingsView(this);
2021-04-24 08:22:42 +02:00
}
}
2021-05-31 19:33:40 +02:00
2021-11-13 21:21:13 +01:00
enum AvatarAction { camera, file, remove }