fluffychat/lib/pages/settings_security/settings_security.dart

104 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 {
const SettingsSecurity({Key key}) : super(key: key);
@override
SettingsSecurityController createState() => SettingsSecurityController();
}
class SettingsSecurityController extends State<SettingsSecurity> {
void changePasswordAccountAction() async {
final input = await showTextInputDialog(
useRootNavigator: false,
context: context,
title: L10n.of(context).changePassword,
okLabel: L10n.of(context).ok,
cancelLabel: L10n.of(context).cancel,
textFields: [
DialogTextField(
hintText: L10n.of(context).pleaseEnterYourPassword,
obscureText: true,
minLines: 1,
maxLines: 1,
),
DialogTextField(
hintText: L10n.of(context).chooseAStrongPassword,
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)));
}
}
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) {
await AppLock.of(context).showLockScreen();
}
final newLock = await showTextInputDialog(
useRootNavigator: false,
context: context,
title: L10n.of(context).pleaseChooseAPasscode,
message: L10n.of(context).pleaseEnter4Digits,
cancelLabel: L10n.of(context).cancel,
textFields: [
DialogTextField(
validator: (text) {
if (text.isEmpty || (text.length == 4 && int.tryParse(text) >= 0)) {
return null;
}
return L10n.of(context).pleaseEnter4Digits;
},
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) {
AppLock.of(context).disable();
} else {
AppLock.of(context).enable();
}
}
}
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);
}