mirror of
https://gitlab.com/famedly/fluffychat.git
synced 2024-11-19 18:49:28 +01:00
161 lines
4.5 KiB
Dart
161 lines
4.5 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:adaptive_dialog/adaptive_dialog.dart';
|
|
import 'package:file_picker_cross/file_picker_cross.dart';
|
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
|
import 'package:future_loading_dialog/future_loading_dialog.dart';
|
|
import 'package:image_picker/image_picker.dart';
|
|
import 'package:matrix/matrix.dart';
|
|
|
|
import 'package:fluffychat/utils/platform_infos.dart';
|
|
import '../../widgets/matrix.dart';
|
|
import '../bootstrap/bootstrap_dialog.dart';
|
|
import 'settings_view.dart';
|
|
|
|
class Settings extends StatefulWidget {
|
|
const Settings({Key? key}) : super(key: key);
|
|
|
|
@override
|
|
SettingsController createState() => SettingsController();
|
|
}
|
|
|
|
class SettingsController extends State<Settings> {
|
|
Future<dynamic>? profileFuture;
|
|
Profile? profile;
|
|
bool profileUpdated = false;
|
|
|
|
void updateProfile() => setState(() {
|
|
profileUpdated = true;
|
|
profile = profileFuture = null;
|
|
});
|
|
|
|
void setAvatarAction() async {
|
|
final actions = [
|
|
if (PlatformInfos.isMobile)
|
|
SheetAction(
|
|
key: AvatarAction.camera,
|
|
label: L10n.of(context)!.openCamera,
|
|
isDefaultAction: true,
|
|
icon: Icons.camera_alt_outlined,
|
|
),
|
|
SheetAction(
|
|
key: AvatarAction.file,
|
|
label: L10n.of(context)!.openGallery,
|
|
icon: Icons.photo_outlined,
|
|
),
|
|
if (profile?.avatarUrl != null)
|
|
SheetAction(
|
|
key: AvatarAction.remove,
|
|
label: L10n.of(context)!.removeYourAvatar,
|
|
isDestructiveAction: true,
|
|
icon: Icons.delete_outlined,
|
|
),
|
|
];
|
|
final action = actions.length == 1
|
|
? actions.single.key
|
|
: await showModalActionSheet<AvatarAction>(
|
|
context: context,
|
|
title: L10n.of(context)!.changeYourAvatar,
|
|
actions: actions,
|
|
);
|
|
if (action == null) return;
|
|
final matrix = Matrix.of(context);
|
|
if (action == AvatarAction.remove) {
|
|
final success = await showFutureLoadingDialog(
|
|
context: context,
|
|
future: () => matrix.client.setAvatar(null),
|
|
);
|
|
if (success.error == null) {
|
|
updateProfile();
|
|
}
|
|
return;
|
|
}
|
|
MatrixFile file;
|
|
if (PlatformInfos.isMobile) {
|
|
final result = await ImagePicker().pickImage(
|
|
source: action == AvatarAction.camera
|
|
? ImageSource.camera
|
|
: ImageSource.gallery,
|
|
imageQuality: 50,
|
|
);
|
|
if (result == null) return;
|
|
file = MatrixFile(
|
|
bytes: await result.readAsBytes(),
|
|
name: result.path,
|
|
);
|
|
} else {
|
|
final result =
|
|
await FilePickerCross.importFromStorage(type: FileTypeCross.image);
|
|
if (result.fileName == null) return;
|
|
file = MatrixFile(
|
|
bytes: result.toUint8List(),
|
|
name: result.fileName!,
|
|
);
|
|
}
|
|
final success = await showFutureLoadingDialog(
|
|
context: context,
|
|
future: () => matrix.client.setAvatar(file),
|
|
);
|
|
if (success.error == null) {
|
|
updateProfile();
|
|
}
|
|
}
|
|
|
|
@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;
|
|
bool showChatBackupBanner = false;
|
|
|
|
void firstRunBootstrapAction() async {
|
|
await BootstrapDialog(
|
|
client: Matrix.of(context).client,
|
|
).show(context);
|
|
checkBootstrap();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final client = Matrix.of(context).client;
|
|
profileFuture ??= client
|
|
.getProfileFromUserId(
|
|
client.userID!,
|
|
cache: !profileUpdated,
|
|
getFromRooms: !profileUpdated,
|
|
)
|
|
.then((p) {
|
|
if (mounted) setState(() => profile = p);
|
|
return p;
|
|
});
|
|
return SettingsView(this);
|
|
}
|
|
}
|
|
|
|
enum AvatarAction { camera, file, remove }
|