diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/customfields/editor/CustomFieldsEditorScreen.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/customfields/editor/CustomFieldsEditorScreen.kt index 0e7fb9ec6b5..bc5c13473e5 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/customfields/editor/CustomFieldsEditorScreen.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/customfields/editor/CustomFieldsEditorScreen.kt @@ -103,12 +103,11 @@ private fun CustomFieldsEditorScreen( title = "Custom Field", onNavigationButtonClick = onBackButtonClick, actions = { - if (state.showDoneButton) { - WCTextButton( - onClick = onDoneClicked, - text = stringResource(R.string.done) - ) - } + WCTextButton( + onClick = onDoneClicked, + text = stringResource(R.string.done), + enabled = state.enableDoneButton + ) WCOverflowMenu( items = listOfNotNull( R.string.custom_fields_editor_copy_key, diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/customfields/editor/CustomFieldsEditorViewModel.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/customfields/editor/CustomFieldsEditorViewModel.kt index 088e11dd937..c6aa229b6e4 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/customfields/editor/CustomFieldsEditorViewModel.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/customfields/editor/CustomFieldsEditorViewModel.kt @@ -176,7 +176,7 @@ class CustomFieldsEditorViewModel @Inject constructor( val keyErrorMessage: UiString? = null, val isCreatingNewItem: Boolean = false, ) { - val showDoneButton + val enableDoneButton get() = customField.key.isNotEmpty() && hasChanges && keyErrorMessage == null } diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/customfields/list/CustomFieldsScreen.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/customfields/list/CustomFieldsScreen.kt index f2e60b578e3..a260613073a 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/customfields/list/CustomFieldsScreen.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/customfields/list/CustomFieldsScreen.kt @@ -122,12 +122,11 @@ private fun CustomFieldsScreen( title = stringResource(id = R.string.custom_fields_list_title), onNavigationButtonClick = onBackClick, actions = { - if (state.hasChanges) { - WCTextButton( - text = stringResource(id = R.string.save), - onClick = onSaveClicked - ) - } + WCTextButton( + text = stringResource(id = R.string.save), + onClick = onSaveClicked, + enabled = state.hasChanges + ) } ) }, diff --git a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/customfields/editor/CustomFieldsEditorViewModelTest.kt b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/customfields/editor/CustomFieldsEditorViewModelTest.kt index 180f01c0290..8bc379b70e9 100644 --- a/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/customfields/editor/CustomFieldsEditorViewModelTest.kt +++ b/WooCommerce/src/test/kotlin/com/woocommerce/android/ui/customfields/editor/CustomFieldsEditorViewModelTest.kt @@ -119,47 +119,47 @@ class CustomFieldsEditorViewModelTest : BaseUnitTest() { } @Test - fun `given editing an existing field, when key is changed, then show done button`() = testBlocking { + fun `given editing an existing field, when key is changed, then enable done button`() = testBlocking { setup(editing = true) val state = viewModel.state.runAndCaptureValues { viewModel.onKeyChanged("new key") }.last() - assertThat(state.showDoneButton).isTrue() + assertThat(state.enableDoneButton).isTrue() } @Test - fun `given editing an existing field, when value is changed, then show done button`() = testBlocking { + fun `given editing an existing field, when value is changed, then enable done button`() = testBlocking { setup(editing = true) val state = viewModel.state.runAndCaptureValues { viewModel.onValueChanged("new value") }.last() - assertThat(state.showDoneButton).isTrue() + assertThat(state.enableDoneButton).isTrue() } @Test - fun `given creating a new field, when the key is not empty, then show done button`() = testBlocking { + fun `given creating a new field, when the key is not empty, then enable done button`() = testBlocking { setup(editing = false) val state = viewModel.state.runAndCaptureValues { viewModel.onKeyChanged("key") }.last() - assertThat(state.showDoneButton).isTrue() + assertThat(state.enableDoneButton).isTrue() } @Test - fun `when key is empty, then hide done button`() = testBlocking { + fun `when key is empty, then disable done button`() = testBlocking { setup(editing = false) val state = viewModel.state.runAndCaptureValues { viewModel.onKeyChanged("") }.last() - assertThat(state.showDoneButton).isFalse() + assertThat(state.enableDoneButton).isFalse() } @Test @@ -249,7 +249,7 @@ class CustomFieldsEditorViewModelTest : BaseUnitTest() { assertThat(state.keyErrorMessage) .isEqualTo(UiString.UiStringRes(R.string.custom_fields_editor_key_error_duplicate)) - assertThat(state.showDoneButton).isFalse() + assertThat(state.enableDoneButton).isFalse() } @Test @@ -291,7 +291,7 @@ class CustomFieldsEditorViewModelTest : BaseUnitTest() { assertThat(state.keyErrorMessage) .isEqualTo(UiString.UiStringRes(R.string.custom_fields_editor_key_error_underscore)) - assertThat(state.showDoneButton).isFalse() + assertThat(state.enableDoneButton).isFalse() } @Test