From 3d07c284b957f0ae43dab1c250313ebb5e15a231 Mon Sep 17 00:00:00 2001 From: darken Date: Tue, 24 Feb 2026 18:48:32 +0100 Subject: [PATCH] fix(profiles): Don't show unsaved changes dialog when nothing was edited In create mode, the init block pre-filled _currentState with a default name but left _initialState empty. The hasUnsavedChanges() check saw the non-blank name as a change, triggering the dialog on back press even without user edits. Set _initialState to match _currentState defaults in create mode and unify the comparison logic to always use current != initial. --- .../DeviceProfileCreationViewModel.kt | 30 ++++--------------- 1 file changed, 5 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/eu/darken/capod/profiles/ui/creation/DeviceProfileCreationViewModel.kt b/app/src/main/java/eu/darken/capod/profiles/ui/creation/DeviceProfileCreationViewModel.kt index 60308ae0..f0c806eb 100644 --- a/app/src/main/java/eu/darken/capod/profiles/ui/creation/DeviceProfileCreationViewModel.kt +++ b/app/src/main/java/eu/darken/capod/profiles/ui/creation/DeviceProfileCreationViewModel.kt @@ -65,7 +65,9 @@ class DeviceProfileCreationViewModel @Inject constructor( loadProfile(profileId) } else { val defaultName = context.getString(R.string.profiles_name_default) - _currentState.value = _currentState.value.copy(name = defaultName) + val defaultState = ProfileEditorState(name = defaultName) + _initialState.value = defaultState + _currentState.value = defaultState } } @@ -76,16 +78,7 @@ class DeviceProfileCreationViewModel @Inject constructor( private val hasUnsavedChangesFlow = combine( _currentState, _initialState ) { current, initial -> - if (isEditMode) { - current != initial - } else { - current.name.isNotBlank() || - current.selectedModel != null || - current.identityKeyHex != null || - current.encryptionKeyHex != null || - current.selectedDeviceAddress != null || - current.minimumSignalQuality != DeviceProfile.DEFAULT_MINIMUM_SIGNAL_QUALITY - } + current != initial } private val isFormValid = combine( @@ -166,20 +159,7 @@ class DeviceProfileCreationViewModel @Inject constructor( log(TAG) { "Minimum signal quality updated: $quality" } } - fun hasUnsavedChanges(): Boolean { - val current = _currentState.value - val initial = _initialState.value - return if (isEditMode) { - current != initial - } else { - current.name.isNotBlank() || - current.selectedModel != null || - current.identityKeyHex != null || - current.encryptionKeyHex != null || - current.selectedDeviceAddress != null || - current.minimumSignalQuality != DeviceProfile.DEFAULT_MINIMUM_SIGNAL_QUALITY - } - } + fun hasUnsavedChanges(): Boolean = _currentState.value != _initialState.value fun onBackPressed() { log(TAG) { "onBackPressed()" }