fluffychat/lib/pages/settings_security/settings_security.dart

202 lines
6.2 KiB
Dart
Raw Normal View History

2023-02-04 18:32:56 +01:00
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';
2021-06-23 11:26:12 +02:00
import 'package:flutter_app_lock/flutter_app_lock.dart';
2021-10-26 18:50:34 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
2021-06-23 11:26:12 +02:00
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
2023-02-04 18:32:56 +01:00
import 'package:intl/intl.dart';
import 'package:matrix/matrix.dart';
2021-06-23 11:26:12 +02:00
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/config/setting_keys.dart';
import 'package:fluffychat/utils/matrix_sdk_extensions/matrix_file_extension.dart';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/widgets/matrix.dart';
2021-11-09 21:32:16 +01:00
import '../bootstrap/bootstrap_dialog.dart';
import 'settings_security_view.dart';
2021-06-23 11:26:12 +02:00
class SettingsSecurity extends StatefulWidget {
2022-01-29 12:35:03 +01:00
const SettingsSecurity({Key? key}) : super(key: key);
2021-06-23 11:26:12 +02:00
@override
SettingsSecurityController createState() => SettingsSecurityController();
}
class SettingsSecurityController extends State<SettingsSecurity> {
void changePasswordAccountAction() async {
final input = await showTextInputDialog(
useRootNavigator: false,
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.changePassword,
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
2021-06-23 11:26:12 +02:00
textFields: [
DialogTextField(
2022-12-30 17:45:58 +01:00
hintText: L10n.of(context)!.chooseAStrongPassword,
2021-06-23 11:26:12 +02:00
obscureText: true,
minLines: 1,
maxLines: 1,
),
DialogTextField(
2022-12-30 17:45:58 +01:00
hintText: L10n.of(context)!.repeatPassword,
2021-06-23 11:26:12 +02:00
obscureText: true,
minLines: 1,
maxLines: 1,
),
],
);
if (input == null) return;
final success = await showFutureLoadingDialog(
context: context,
future: () => Matrix.of(context)
.client
.changePassword(input.last, oldPassword: input.first),
);
if (success.error == null) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(L10n.of(context)!.passwordHasBeenChanged)),
);
2021-06-23 11:26:12 +02:00
}
}
void setAppLockAction() async {
final currentLock =
2021-10-14 18:09:30 +02:00
await const FlutterSecureStorage().read(key: SettingKeys.appLockKey);
2021-06-23 11:26:12 +02:00
if (currentLock?.isNotEmpty ?? false) {
2022-01-29 12:35:03 +01:00
await AppLock.of(context)!.showLockScreen();
2021-06-23 11:26:12 +02:00
}
final newLock = await showTextInputDialog(
useRootNavigator: false,
context: context,
2022-01-29 12:35:03 +01:00
title: L10n.of(context)!.pleaseChooseAPasscode,
message: L10n.of(context)!.pleaseEnter4Digits,
cancelLabel: L10n.of(context)!.cancel,
2021-06-23 11:26:12 +02:00
textFields: [
DialogTextField(
validator: (text) {
2022-01-29 12:35:03 +01:00
if (text!.isEmpty ||
(text.length == 4 && int.tryParse(text)! >= 0)) {
2021-06-23 11:26:12 +02:00
return null;
}
2022-01-29 12:35:03 +01:00
return L10n.of(context)!.pleaseEnter4Digits;
2021-06-23 11:26:12 +02:00
},
keyboardType: TextInputType.number,
obscureText: true,
maxLines: 1,
minLines: 1,
)
],
);
if (newLock != null) {
2021-10-14 18:09:30 +02:00
await const FlutterSecureStorage()
2021-06-23 11:26:12 +02:00
.write(key: SettingKeys.appLockKey, value: newLock.single);
if (newLock.single.isEmpty) {
2022-01-29 12:35:03 +01:00
AppLock.of(context)!.disable();
2021-06-23 11:26:12 +02:00
} else {
2022-01-29 12:35:03 +01:00
AppLock.of(context)!.enable();
2021-06-23 11:26:12 +02:00
}
}
}
2023-02-04 18:32:56 +01:00
void deleteAccountAction() async {
if (await showOkCancelAlertDialog(
useRootNavigator: false,
context: context,
title: L10n.of(context)!.warning,
message: L10n.of(context)!.deactivateAccountWarning,
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
) ==
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) {
return;
}
final input = await showTextInputDialog(
useRootNavigator: false,
context: context,
title: L10n.of(context)!.pleaseEnterYourPassword,
okLabel: L10n.of(context)!.ok,
cancelLabel: L10n.of(context)!.cancel,
textFields: [
const DialogTextField(
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(
user: Matrix.of(context).client.userID!,
),
2023-02-04 18:32:56 +01:00
),
),
);
}
void showBootstrapDialog(BuildContext context) async {
2021-06-23 11:26:12 +02:00
await BootstrapDialog(
client: Matrix.of(context).client,
).show(context);
}
2023-02-04 18:32:56 +01:00
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;
}
final file = await showFutureLoadingDialog(
2023-02-04 18:32:56 +01:00
context: context,
future: () async {
final export = await Matrix.of(context).client.exportDump();
if (export == null) throw Exception('Export data is null.');
final exportBytes = Uint8List.fromList(
const Utf8Codec().encode(export),
);
final exportFileName =
'fluffychat-export-${DateFormat(DateFormat.YEAR_MONTH_DAY).format(DateTime.now())}.fluffybackup';
return MatrixFile(bytes: exportBytes, name: exportFileName);
2023-02-04 18:32:56 +01:00
},
);
file.result?.save(context);
2023-02-04 18:32:56 +01:00
}
2021-06-23 11:26:12 +02:00
@override
Widget build(BuildContext context) => SettingsSecurityView(this);
}