redesign for settings page
This commit is contained in:
@@ -3,8 +3,8 @@ import 'package:flutter/material.dart';
|
||||
import 'package:pshared/provider/account.dart';
|
||||
import 'package:pshared/api/responses/error/server.dart';
|
||||
|
||||
import 'package:pweb/models/state/edit_state.dart';
|
||||
import 'package:pweb/models/auth/password_field_type.dart';
|
||||
import 'package:pweb/models/state/controller_lifecycle.dart';
|
||||
import 'package:pweb/models/state/control_state.dart';
|
||||
import 'package:pweb/models/state/visibility.dart';
|
||||
|
||||
|
||||
@@ -14,32 +14,17 @@ class PasswordFormController extends ChangeNotifier {
|
||||
final newPasswordController = TextEditingController();
|
||||
final confirmPasswordController = TextEditingController();
|
||||
|
||||
final Map<PasswordFieldType, VisibilityState> _visibility = {
|
||||
PasswordFieldType.old: VisibilityState.hidden,
|
||||
PasswordFieldType.newPassword: VisibilityState.hidden,
|
||||
PasswordFieldType.confirmPassword: VisibilityState.hidden,
|
||||
};
|
||||
EditState _state = EditState.view;
|
||||
ControlState _formState = ControlState.enabled;
|
||||
String _errorText = '';
|
||||
bool _disposed = false;
|
||||
VisibilityState _oldPasswordVisibility = VisibilityState.hidden;
|
||||
ControllerLifecycleState _lifecycleState = ControllerLifecycleState.active;
|
||||
|
||||
bool get isExpanded => _state != EditState.view;
|
||||
bool get isSaving => _state == EditState.saving;
|
||||
bool get isSaving => _formState == ControlState.loading;
|
||||
String get errorText => _errorText;
|
||||
EditState get state => _state;
|
||||
bool isPasswordVisible(PasswordFieldType type) =>
|
||||
_visibility[type] == VisibilityState.visible;
|
||||
VisibilityState get oldPasswordVisibility => _oldPasswordVisibility;
|
||||
|
||||
void toggleExpanded() {
|
||||
if (_state == EditState.saving) return;
|
||||
_setState(_state == EditState.view ? EditState.edit : EditState.view);
|
||||
_setError('');
|
||||
}
|
||||
|
||||
void togglePasswordVisibility(PasswordFieldType type) {
|
||||
final current = _visibility[type];
|
||||
if (current == null) return;
|
||||
_visibility[type] = current == VisibilityState.hidden
|
||||
void toggleOldPasswordVisibility() {
|
||||
_oldPasswordVisibility = _oldPasswordVisibility == VisibilityState.hidden
|
||||
? VisibilityState.visible
|
||||
: VisibilityState.hidden;
|
||||
notifyListeners();
|
||||
@@ -52,7 +37,7 @@ class PasswordFormController extends ChangeNotifier {
|
||||
final currentForm = formKey.currentState;
|
||||
if (currentForm == null || !currentForm.validate()) return false;
|
||||
|
||||
_setState(EditState.saving);
|
||||
_setState(ControlState.loading);
|
||||
_setError('');
|
||||
|
||||
try {
|
||||
@@ -64,11 +49,11 @@ class PasswordFormController extends ChangeNotifier {
|
||||
oldPasswordController.clear();
|
||||
newPasswordController.clear();
|
||||
confirmPasswordController.clear();
|
||||
_setState(EditState.view);
|
||||
_setState(ControlState.enabled);
|
||||
return true;
|
||||
} catch (e) {
|
||||
_setError(_errorMessageForException(e, errorText));
|
||||
_setState(EditState.edit);
|
||||
_setState(ControlState.enabled);
|
||||
rethrow;
|
||||
}
|
||||
}
|
||||
@@ -80,21 +65,23 @@ class PasswordFormController extends ChangeNotifier {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
void _setState(EditState value) {
|
||||
if (_state == value || _disposed) return;
|
||||
_state = value;
|
||||
void _setState(ControlState value) {
|
||||
if (_formState == value || _isDisposed) return;
|
||||
_formState = value;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void _setError(String value) {
|
||||
if (_disposed) return;
|
||||
if (_isDisposed) return;
|
||||
_errorText = value;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
bool get _isDisposed => _lifecycleState == ControllerLifecycleState.disposed;
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
_lifecycleState = ControllerLifecycleState.disposed;
|
||||
oldPasswordController.dispose();
|
||||
newPasswordController.dispose();
|
||||
confirmPasswordController.dispose();
|
||||
|
||||
62
frontend/pweb/lib/controllers/settings/profile_actions.dart
Normal file
62
frontend/pweb/lib/controllers/settings/profile_actions.dart
Normal file
@@ -0,0 +1,62 @@
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import 'package:pweb/controllers/auth/account_name.dart';
|
||||
import 'package:pweb/models/settings/profile_action_section.dart';
|
||||
|
||||
|
||||
class ProfileActionsController extends ChangeNotifier {
|
||||
AccountNameController? _accountNameController;
|
||||
ProfileActionSection? _expandedSection;
|
||||
|
||||
ProfileActionSection? get expandedSection => _expandedSection;
|
||||
bool get isEditingName => _accountNameController?.isEditing ?? false;
|
||||
|
||||
bool isExpanded(ProfileActionSection section) => _expandedSection == section;
|
||||
|
||||
void updateAccountNameController(AccountNameController controller) {
|
||||
if (identical(_accountNameController, controller)) {
|
||||
return;
|
||||
}
|
||||
_accountNameController?.removeListener(_handleAccountNameChanged);
|
||||
_accountNameController = controller;
|
||||
_accountNameController?.addListener(_handleAccountNameChanged);
|
||||
}
|
||||
|
||||
void toggle(ProfileActionSection section) {
|
||||
final isOpeningSection = !isExpanded(section);
|
||||
if (isOpeningSection) {
|
||||
if (_accountNameController?.isBusy ?? false) {
|
||||
return;
|
||||
}
|
||||
_accountNameController?.cancelEditing();
|
||||
}
|
||||
_expandedSection = isExpanded(section) ? null : section;
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void toggleNameEditing() {
|
||||
final accountNameController = _accountNameController;
|
||||
if (accountNameController == null || accountNameController.isBusy) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (accountNameController.isEditing) {
|
||||
accountNameController.cancelEditing();
|
||||
return;
|
||||
}
|
||||
|
||||
_expandedSection = null;
|
||||
accountNameController.startEditing();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void _handleAccountNameChanged() {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_accountNameController?.removeListener(_handleAccountNameChanged);
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user