Initial commit
This commit is contained in:
98
lib/pages/settings_password/settings_password.dart
Normal file
98
lib/pages/settings_password/settings_password.dart
Normal file
@@ -0,0 +1,98 @@
|
||||
// SPDX-FileCopyrightText: 2019-Present Christian Kußowski
|
||||
// SPDX-FileCopyrightText: 2019-Present Contributors to FluffyChat
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import 'package:tatchat/l10n/l10n.dart';
|
||||
import 'package:tatchat/pages/settings_password/settings_password_view.dart';
|
||||
import 'package:tatchat/utils/localized_exception_extension.dart';
|
||||
import 'package:tatchat/widgets/matrix.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:material_ui/material_ui.dart';
|
||||
|
||||
class SettingsPassword extends StatefulWidget {
|
||||
const SettingsPassword({super.key});
|
||||
|
||||
@override
|
||||
SettingsPasswordController createState() => SettingsPasswordController();
|
||||
}
|
||||
|
||||
class SettingsPasswordController extends State<SettingsPassword> {
|
||||
final TextEditingController oldPasswordController = TextEditingController();
|
||||
final TextEditingController newPassword1Controller = TextEditingController();
|
||||
final TextEditingController newPassword2Controller = TextEditingController();
|
||||
|
||||
String? oldPasswordError;
|
||||
String? newPassword1Error;
|
||||
String? newPassword2Error;
|
||||
|
||||
bool loading = false;
|
||||
|
||||
Future<void> changePassword() async {
|
||||
final l10n = L10n.of(context);
|
||||
final scaffoldMessenger = ScaffoldMessenger.of(context);
|
||||
final theme = Theme.of(context);
|
||||
setState(() {
|
||||
oldPasswordError = newPassword1Error = newPassword2Error = null;
|
||||
});
|
||||
if (oldPasswordController.text.isEmpty) {
|
||||
setState(() {
|
||||
oldPasswordError = L10n.of(context).pleaseEnterYourPassword;
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (newPassword1Controller.text.isEmpty ||
|
||||
newPassword1Controller.text.length < 6) {
|
||||
setState(() {
|
||||
newPassword1Error = L10n.of(context).pleaseChooseAStrongPassword;
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (newPassword1Controller.text != newPassword2Controller.text) {
|
||||
setState(() {
|
||||
newPassword2Error = L10n.of(context).passwordsDoNotMatch;
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
setState(() {
|
||||
loading = true;
|
||||
});
|
||||
try {
|
||||
await Matrix.of(context).client.changePassword(
|
||||
newPassword1Controller.text,
|
||||
oldPassword: oldPasswordController.text,
|
||||
);
|
||||
if (!mounted) return;
|
||||
scaffoldMessenger.showSnackBar(
|
||||
SnackBar(content: Text(l10n.passwordHasBeenChanged)),
|
||||
);
|
||||
if (mounted) context.pop();
|
||||
} catch (e) {
|
||||
scaffoldMessenger.showSnackBar(
|
||||
SnackBar(
|
||||
backgroundColor: theme.colorScheme.error,
|
||||
content: Text(
|
||||
e.toLocalizedString(context),
|
||||
style: TextStyle(color: theme.colorScheme.onError),
|
||||
),
|
||||
),
|
||||
);
|
||||
} finally {
|
||||
setState(() {
|
||||
loading = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
oldPasswordController.dispose();
|
||||
newPassword1Controller.dispose();
|
||||
newPassword2Controller.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => SettingsPasswordView(this);
|
||||
}
|
||||
93
lib/pages/settings_password/settings_password_view.dart
Normal file
93
lib/pages/settings_password/settings_password_view.dart
Normal file
@@ -0,0 +1,93 @@
|
||||
// SPDX-FileCopyrightText: 2019-Present Christian Kußowski
|
||||
// SPDX-FileCopyrightText: 2019-Present Contributors to FluffyChat
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
|
||||
import 'package:tatchat/l10n/l10n.dart';
|
||||
import 'package:tatchat/pages/settings_password/settings_password.dart';
|
||||
import 'package:tatchat/widgets/layouts/max_width_body.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:material_ui/material_ui.dart';
|
||||
|
||||
class SettingsPasswordView extends StatelessWidget {
|
||||
final SettingsPasswordController controller;
|
||||
const SettingsPasswordView(this.controller, {super.key});
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final theme = Theme.of(context);
|
||||
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text(L10n.of(context).changePassword)),
|
||||
body: ListTileTheme(
|
||||
iconColor: theme.colorScheme.onSurface,
|
||||
child: MaxWidthBody(
|
||||
child: Padding(
|
||||
padding: const EdgeInsets.all(16.0),
|
||||
child: Column(
|
||||
children: [
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: controller.oldPasswordController,
|
||||
obscureText: true,
|
||||
autocorrect: false,
|
||||
autofocus: true,
|
||||
readOnly: controller.loading,
|
||||
decoration: InputDecoration(
|
||||
prefixIcon: const Icon(Icons.lock_outlined),
|
||||
hintText: '********',
|
||||
labelText: L10n.of(context).pleaseEnterYourCurrentPassword,
|
||||
errorText: controller.oldPasswordError,
|
||||
),
|
||||
),
|
||||
const Divider(height: 64),
|
||||
TextField(
|
||||
controller: controller.newPassword1Controller,
|
||||
obscureText: true,
|
||||
autocorrect: false,
|
||||
readOnly: controller.loading,
|
||||
decoration: InputDecoration(
|
||||
prefixIcon: const Icon(Icons.lock_reset_outlined),
|
||||
hintText: '********',
|
||||
labelText: L10n.of(context).newPassword,
|
||||
errorText: controller.newPassword1Error,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextField(
|
||||
controller: controller.newPassword2Controller,
|
||||
obscureText: true,
|
||||
autocorrect: false,
|
||||
readOnly: controller.loading,
|
||||
decoration: InputDecoration(
|
||||
prefixIcon: const Icon(Icons.repeat_outlined),
|
||||
hintText: '********',
|
||||
labelText: L10n.of(context).repeatPassword,
|
||||
errorText: controller.newPassword2Error,
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 32),
|
||||
SizedBox(
|
||||
width: double.infinity,
|
||||
child: ElevatedButton(
|
||||
onPressed: controller.loading
|
||||
? null
|
||||
: controller.changePassword,
|
||||
child: controller.loading
|
||||
? const LinearProgressIndicator()
|
||||
: Text(L10n.of(context).changePassword),
|
||||
),
|
||||
),
|
||||
const SizedBox(height: 16),
|
||||
TextButton(
|
||||
child: Text(L10n.of(context).passwordRecoverySettings),
|
||||
onPressed: () => context.go('/rooms/settings/security/3pid'),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user