fluffychat/lib/pages/settings_account/settings_account.dart

186 lines
5.5 KiB
Dart
Raw Normal View History

import 'dart:convert';
import 'dart:typed_data';
2021-06-23 11:26:12 +02:00
import 'package:flutter/material.dart';
2021-10-26 18:50:34 +02:00
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:file_picker_cross/file_picker_cross.dart';
2021-06-23 11:26:12 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
import 'package:intl/intl.dart';
2021-06-23 11:26:12 +02:00
import 'package:matrix/matrix.dart';
import 'package:vrouter/vrouter.dart';
2021-06-23 11:26:12 +02:00
2021-11-09 21:32:16 +01:00
import 'package:fluffychat/pages/settings_account/settings_account_view.dart';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/widgets/matrix.dart';
2021-06-23 11:26:12 +02:00
class SettingsAccount extends StatefulWidget {
2022-01-29 12:35:03 +01:00
const SettingsAccount({Key? key}) : super(key: key);
2021-06-23 11:26:12 +02:00
@override
SettingsAccountController createState() => SettingsAccountController();
}
class SettingsAccountController extends State<SettingsAccount> {
2022-01-29 12:35:03 +01:00
Future<dynamic>? profileFuture;
Profile? profile;
2021-06-23 11:26:12 +02:00
bool profileUpdated = false;
void updateProfile() => setState(() {
profileUpdated = true;
profile = profileFuture = null;
});
void setDisplaynameAction() async {
final input = await showTextInputDialog(
useRootNavigator: false,
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.editDisplayname,
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
2021-06-23 11:26:12 +02:00
textFields: [
DialogTextField(
initialText: profile?.displayName ??
2022-01-29 12:35:03 +01:00
Matrix.of(context).client.userID!.localpart,
2021-06-23 11:26:12 +02:00
)
],
);
if (input == null) return;
final matrix = Matrix.of(context);
final success = await showFutureLoadingDialog(
context: context,
future: () =>
2022-01-29 12:35:03 +01:00
matrix.client.setDisplayName(matrix.client.userID!, input.single),
2021-06-23 11:26:12 +02:00
);
if (success.error == null) {
updateProfile();
}
}
void logoutAction() async {
if (await showOkCancelAlertDialog(
useRootNavigator: false,
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.areYouSureYouWantToLogout,
okLabel: L10n.of(context)!.yes,
cancelLabel: L10n.of(context)!.cancel,
2021-06-23 11:26:12 +02:00
) ==
OkCancelResult.cancel) {
return;
}
final matrix = Matrix.of(context);
await showFutureLoadingDialog(
context: context,
future: () => matrix.client.logout(),
);
}
void deleteAccountAction() async {
if (await showOkCancelAlertDialog(
useRootNavigator: false,
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.warning,
message: L10n.of(context)!.deactivateAccountWarning,
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
2021-06-23 11:26:12 +02:00
) ==
OkCancelResult.cancel) {
return;
}
final supposedMxid = Matrix.of(context).client.userID!;
final mxids = await showTextInputDialog(
useRootNavigator: false,
context: context,
title: L10n.of(context)!.confirmMatrixId,
textFields: [
DialogTextField(
validator: (text) => text == supposedMxid
? null
: L10n.of(context)!.supposedMxid(supposedMxid),
),
],
okLabel: L10n.of(context)!.delete,
cancelLabel: L10n.of(context)!.cancel,
);
if (mxids == null || mxids.length != 1 || mxids.single != supposedMxid) {
2021-06-23 11:26:12 +02:00
return;
}
final input = await showTextInputDialog(
useRootNavigator: false,
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.pleaseEnterYourPassword,
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
2021-06-23 11:26:12 +02:00
textFields: [
2021-10-14 18:09:30 +02:00
const DialogTextField(
2021-06-23 11:26:12 +02:00
obscureText: true,
hintText: '******',
minLines: 1,
maxLines: 1,
)
],
);
if (input == null) return;
await showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context).client.deactivateAccount(
auth: AuthenticationPassword(
password: input.single,
identifier: AuthenticationUserIdentifier(
2022-01-29 12:35:03 +01:00
user: Matrix.of(context).client.userID!),
2021-06-23 11:26:12 +02:00
),
),
);
}
void addAccountAction() => VRouter.of(context).to('add');
2021-06-23 11:26:12 +02:00
@override
Widget build(BuildContext context) {
final client = Matrix.of(context).client;
profileFuture ??= client
.getProfileFromUserId(
2022-01-29 12:35:03 +01:00
client.userID!,
2021-06-23 11:26:12 +02:00
cache: !profileUpdated,
getFromRooms: !profileUpdated,
)
.then((p) {
if (mounted) setState(() => profile = p);
return p;
});
return SettingsAccountView(this);
}
Future<void> dehydrateAction() => dehydrateDevice(context);
static Future<void> dehydrateDevice(BuildContext context) async {
final response = await showOkCancelAlertDialog(
context: context,
isDestructiveAction: true,
title: L10n.of(context)!.dehydrate,
message: L10n.of(context)!.dehydrateWarning,
);
if (response != OkCancelResult.ok) {
return;
}
await showFutureLoadingDialog(
context: context,
future: () async {
try {
final export = await Matrix.of(context).client.exportDump();
final filePickerCross = FilePickerCross(
Uint8List.fromList(const Utf8Codec().encode(export!)),
path:
'/fluffychat-export-${DateFormat(DateFormat.YEAR_MONTH_DAY).format(DateTime.now())}.fluffybackup',
fileExtension: 'fluffybackup');
await filePickerCross.exportToStorage(
subject: L10n.of(context)!.dehydrateShare,
);
} catch (e, s) {
Logs().e('Export error', e, s);
}
},
);
}
2021-06-23 11:26:12 +02:00
}