fluffychat/lib/pages/settings_security/settings_security.dart

105 lines
3.2 KiB
Dart
Raw Normal View History

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';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/config/setting_keys.dart';
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(
2022-01-29 12:35:03 +01:00
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
}
}
}
void showBootstrapDialog(BuildContext context) async {
2021-06-23 11:26:12 +02:00
await BootstrapDialog(
client: Matrix.of(context).client,
).show(context);
}
@override
Widget build(BuildContext context) => SettingsSecurityView(this);
}