Merge pull request #740 from krille-chan/krille/fix-change-password
feat: New change password page with server capabilities checkpull/742/head
commit
3b0c06bc32
@ -0,0 +1,83 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||||
|
import 'package:go_router/go_router.dart';
|
||||||
|
|
||||||
|
import 'package:fluffychat/pages/settings_password/settings_password_view.dart';
|
||||||
|
import 'package:fluffychat/utils/localized_exception_extension.dart';
|
||||||
|
import 'package:fluffychat/widgets/matrix.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;
|
||||||
|
|
||||||
|
void changePassword() async {
|
||||||
|
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 {
|
||||||
|
final scaffoldMessenger = ScaffoldMessenger.of(context);
|
||||||
|
await Matrix.of(context).client.changePassword(
|
||||||
|
newPassword1Controller.text,
|
||||||
|
oldPassword: oldPasswordController.text,
|
||||||
|
);
|
||||||
|
scaffoldMessenger.showSnackBar(
|
||||||
|
SnackBar(
|
||||||
|
content: Text(L10n.of(context)!.passwordHasBeenChanged),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
if (mounted) context.pop();
|
||||||
|
} catch (e) {
|
||||||
|
setState(() {
|
||||||
|
newPassword2Error = e.toLocalizedString(
|
||||||
|
context,
|
||||||
|
ExceptionContext.changePassword,
|
||||||
|
);
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
setState(() {
|
||||||
|
loading = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) => SettingsPasswordView(this);
|
||||||
|
}
|
@ -0,0 +1,75 @@
|
|||||||
|
import 'package:flutter/material.dart';
|
||||||
|
|
||||||
|
import 'package:flutter_gen/gen_l10n/l10n.dart';
|
||||||
|
|
||||||
|
import 'package:fluffychat/pages/settings_password/settings_password.dart';
|
||||||
|
import 'package:fluffychat/widgets/layouts/max_width_body.dart';
|
||||||
|
|
||||||
|
class SettingsPasswordView extends StatelessWidget {
|
||||||
|
final SettingsPasswordController controller;
|
||||||
|
const SettingsPasswordView(this.controller, {super.key});
|
||||||
|
|
||||||
|
@override
|
||||||
|
Widget build(BuildContext context) {
|
||||||
|
return Scaffold(
|
||||||
|
appBar: AppBar(title: Text(L10n.of(context)!.changePassword)),
|
||||||
|
body: ListTileTheme(
|
||||||
|
iconColor: Theme.of(context).colorScheme.onBackground,
|
||||||
|
child: MaxWidthBody(
|
||||||
|
child: Padding(
|
||||||
|
padding: const EdgeInsets.all(16.0),
|
||||||
|
child: Column(
|
||||||
|
children: [
|
||||||
|
TextField(
|
||||||
|
controller: controller.oldPasswordController,
|
||||||
|
obscureText: true,
|
||||||
|
autocorrect: false,
|
||||||
|
autofocus: true,
|
||||||
|
readOnly: controller.loading,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
hintText: L10n.of(context)!.pleaseEnterYourCurrentPassword,
|
||||||
|
errorText: controller.oldPasswordError,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const Divider(height: 32),
|
||||||
|
TextField(
|
||||||
|
controller: controller.newPassword1Controller,
|
||||||
|
obscureText: true,
|
||||||
|
autocorrect: false,
|
||||||
|
readOnly: controller.loading,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
hintText: L10n.of(context)!.newPassword,
|
||||||
|
errorText: controller.newPassword1Error,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
TextField(
|
||||||
|
controller: controller.newPassword2Controller,
|
||||||
|
obscureText: true,
|
||||||
|
autocorrect: false,
|
||||||
|
readOnly: controller.loading,
|
||||||
|
decoration: InputDecoration(
|
||||||
|
hintText: L10n.of(context)!.repeatPassword,
|
||||||
|
errorText: controller.newPassword2Error,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
const SizedBox(height: 16),
|
||||||
|
SizedBox(
|
||||||
|
width: double.infinity,
|
||||||
|
child: ElevatedButton.icon(
|
||||||
|
onPressed:
|
||||||
|
controller.loading ? null : controller.changePassword,
|
||||||
|
icon: const Icon(Icons.send_outlined),
|
||||||
|
label: controller.loading
|
||||||
|
? const LinearProgressIndicator()
|
||||||
|
: Text(L10n.of(context)!.changePassword),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue