feat: Implement change detection and conditional enabling of save/cancel buttons for profile and notification settings.
This commit is contained in:
@@ -26,6 +26,17 @@ class _NotificationsTabState extends ConsumerState<NotificationsTab> {
|
||||
Map<String, Map<String, bool>> _originalPrefs = {};
|
||||
String _originalFrequency = 'instant';
|
||||
|
||||
bool get _hasNotificationChanges {
|
||||
if (_digestFrequency != _originalFrequency) return true;
|
||||
for (final key in _notificationPrefs.keys) {
|
||||
final current = _notificationPrefs[key];
|
||||
final original = _originalPrefs[key];
|
||||
if (current == null || original == null) return true;
|
||||
if (current['email'] != original['email'] || current['inApp'] != original['inApp']) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -154,6 +165,7 @@ class _NotificationsTabState extends ConsumerState<NotificationsTab> {
|
||||
ProfileActionButtons(
|
||||
onSave: _saveNotificationSettings,
|
||||
onCancel: () {
|
||||
if (!_hasNotificationChanges) return;
|
||||
setState(() {
|
||||
for (final key in _originalPrefs.keys) {
|
||||
_notificationPrefs[key] = Map<String, bool>.from(_originalPrefs[key]!);
|
||||
@@ -162,6 +174,7 @@ class _NotificationsTabState extends ConsumerState<NotificationsTab> {
|
||||
});
|
||||
},
|
||||
isSaving: _isSavingNotifications,
|
||||
hasChanges: _hasNotificationChanges,
|
||||
),
|
||||
],
|
||||
);
|
||||
@@ -180,6 +193,12 @@ class _NotificationsTabState extends ConsumerState<NotificationsTab> {
|
||||
},
|
||||
'digestFrequency': _digestFrequency,
|
||||
});
|
||||
// Update saved snapshot so Cancel won't revert after successful save
|
||||
_originalPrefs = {
|
||||
for (final e in _notificationPrefs.entries)
|
||||
e.key: Map<String, bool>.from(e.value),
|
||||
};
|
||||
_originalFrequency = _digestFrequency;
|
||||
_showSnackBar('Notification settings saved');
|
||||
} catch (e) {
|
||||
_showSnackBar('Failed to save: $e', isError: true);
|
||||
|
||||
Reference in New Issue
Block a user