From 1e3d3cbd523e5b917515e6fbe664d23e8eb852be Mon Sep 17 00:00:00 2001 From: Hicham Boushaba Date: Wed, 11 Sep 2024 10:35:51 +0100 Subject: [PATCH 1/3] Show buttons to allow copying key and value --- .../editor/CustomFieldsEditorScreen.kt | 44 ++++++++++++------- .../editor/CustomFieldsEditorViewModel.kt | 8 ++++ WooCommerce/src/main/res/values/strings.xml | 2 + 3 files changed, 37 insertions(+), 17 deletions(-) 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 4c199f8dfc7..1670091a063 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 @@ -36,6 +36,8 @@ fun CustomFieldsEditorScreen(viewModel: CustomFieldsEditorViewModel) { onValueChanged = viewModel::onValueChanged, onDoneClicked = viewModel::onDoneClicked, onDeleteClicked = viewModel::onDeleteClicked, + onCopyKeyClicked = viewModel::onCopyKeyClicked, + onCopyValueClicked = viewModel::onCopyValueClicked, onBackButtonClick = viewModel::onBackClick, ) } @@ -48,6 +50,8 @@ private fun CustomFieldsEditorScreen( onValueChanged: (String) -> Unit, onDoneClicked: () -> Unit, onDeleteClicked: () -> Unit, + onCopyKeyClicked: () -> Unit, + onCopyValueClicked: () -> Unit, onBackButtonClick: () -> Unit, ) { BackHandler { onBackButtonClick() } @@ -64,24 +68,28 @@ private fun CustomFieldsEditorScreen( text = stringResource(R.string.done) ) } - if (!state.isCreatingNewItem) { - WCOverflowMenu( - items = listOf(R.string.delete), - mapper = { stringResource(it) }, - itemColor = { - when (it) { - R.string.delete -> MaterialTheme.colors.error - else -> LocalContentColor.current - } - }, - onSelected = { resourceId -> - when (resourceId) { - R.string.delete -> onDeleteClicked() - else -> error("Unhandled menu item") - } + WCOverflowMenu( + items = listOfNotNull( + R.string.custom_fields_editor_copy_key, + R.string.custom_fields_editor_copy_value, + if (!state.isCreatingNewItem) R.string.delete else null, + ), + mapper = { stringResource(it) }, + itemColor = { + when (it) { + R.string.delete -> MaterialTheme.colors.error + else -> LocalContentColor.current } - ) - } + }, + onSelected = { resourceId -> + when (resourceId) { + R.string.delete -> onDeleteClicked() + R.string.custom_fields_editor_copy_key -> onCopyKeyClicked() + R.string.custom_fields_editor_copy_value -> onCopyValueClicked() + else -> error("Unhandled menu item") + } + } + ) } ) }, @@ -140,6 +148,8 @@ private fun CustomFieldsEditorScreenPreview() { onValueChanged = {}, onDoneClicked = {}, onDeleteClicked = {}, + onCopyKeyClicked = {}, + onCopyValueClicked = {}, onBackButtonClick = {} ) } 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 ee4a4651cba..c5654210f27 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 @@ -109,6 +109,14 @@ class CustomFieldsEditorViewModel @Inject constructor( ) } + fun onCopyKeyClicked() { + TODO() + } + + fun onCopyValueClicked() { + TODO() + } + fun onBackClick() { if (state.value?.hasChanges == true) { showDiscardChangesDialog.value = true diff --git a/WooCommerce/src/main/res/values/strings.xml b/WooCommerce/src/main/res/values/strings.xml index 258ccfa49dd..ad4eb969e84 100644 --- a/WooCommerce/src/main/res/values/strings.xml +++ b/WooCommerce/src/main/res/values/strings.xml @@ -4296,4 +4296,6 @@ Value This key is already used for another custom field.\nThe app currently does not support creating duplicate keys. Please use wp-admin to duplicate a key if needed. Invalid key: please remove the \"_\" character from the beginning. + Copy Key + Copy Value From 5749b6f2396b82434865709fe662c643af92b65b Mon Sep 17 00:00:00 2001 From: Hicham Boushaba Date: Wed, 11 Sep 2024 10:40:17 +0100 Subject: [PATCH 2/3] Add logic to handle the copy to clipboard --- .../customfields/editor/CustomFieldsEditorFragment.kt | 9 +++++++++ .../customfields/editor/CustomFieldsEditorViewModel.kt | 10 ++++++++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/customfields/editor/CustomFieldsEditorFragment.kt b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/customfields/editor/CustomFieldsEditorFragment.kt index 9e6f40d27e9..d871b51c30a 100644 --- a/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/customfields/editor/CustomFieldsEditorFragment.kt +++ b/WooCommerce/src/main/kotlin/com/woocommerce/android/ui/customfields/editor/CustomFieldsEditorFragment.kt @@ -6,12 +6,15 @@ import android.view.View import android.view.ViewGroup import androidx.fragment.app.viewModels import androidx.navigation.fragment.findNavController +import com.woocommerce.android.R +import com.woocommerce.android.extensions.copyToClipboard import com.woocommerce.android.extensions.navigateBackWithResult import com.woocommerce.android.ui.base.BaseFragment import com.woocommerce.android.ui.compose.composeView import com.woocommerce.android.ui.main.AppBarStatus import com.woocommerce.android.viewmodel.MultiLiveEvent import dagger.hilt.android.AndroidEntryPoint +import org.wordpress.android.util.ToastUtils @AndroidEntryPoint class CustomFieldsEditorFragment : BaseFragment() { @@ -32,9 +35,15 @@ class CustomFieldsEditorFragment : BaseFragment() { private fun handleEvents() { viewModel.event.observe(viewLifecycleOwner) { event -> when (event) { + is CustomFieldsEditorViewModel.CopyContentToClipboard -> copyToClipboard(event) is MultiLiveEvent.Event.ExitWithResult<*> -> navigateBackWithResult(event.key!!, event.data) MultiLiveEvent.Event.Exit -> findNavController().navigateUp() } } } + + private fun copyToClipboard(event: CustomFieldsEditorViewModel.CopyContentToClipboard) { + requireContext().copyToClipboard(getString(event.labelResource), event.content) + ToastUtils.showToast(requireContext(), R.string.copied_to_clipboard) + } } 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 c5654210f27..7194bf2078a 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 @@ -1,5 +1,6 @@ package com.woocommerce.android.ui.customfields.editor +import androidx.annotation.StringRes import androidx.lifecycle.SavedStateHandle import androidx.lifecycle.asLiveData import androidx.lifecycle.viewModelScope @@ -110,11 +111,11 @@ class CustomFieldsEditorViewModel @Inject constructor( } fun onCopyKeyClicked() { - TODO() + triggerEvent(CopyContentToClipboard(R.string.custom_fields_editor_key_label, customFieldDraft.value.key)) } fun onCopyValueClicked() { - TODO() + triggerEvent(CopyContentToClipboard(R.string.custom_fields_editor_value_label, customFieldDraft.value.value)) } fun onBackClick() { @@ -152,4 +153,9 @@ class CustomFieldsEditorViewModel @Inject constructor( val onDiscard: () -> Unit, val onCancel: () -> Unit ) + + data class CopyContentToClipboard( + @StringRes val labelResource: Int, + val content: String + ) : MultiLiveEvent.Event() } From 4be88f0b2c41b47c9ea1c084e7522f75c8aa1d04 Mon Sep 17 00:00:00 2001 From: Hicham Boushaba Date: Wed, 11 Sep 2024 11:07:27 +0100 Subject: [PATCH 3/3] Add unit tests --- .../editor/CustomFieldsEditorViewModelTest.kt | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) 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 5274c0dfdcd..ed838f2a466 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 @@ -246,4 +246,36 @@ class CustomFieldsEditorViewModelTest : BaseUnitTest() { .isEqualTo(UiString.UiStringRes(R.string.custom_fields_editor_key_error_underscore)) assertThat(state.showDoneButton).isFalse() } + + @Test + fun `when tapping copy key, then copy key to clipboard`() = testBlocking { + setup(editing = true) + + val event = viewModel.event.runAndCaptureValues { + viewModel.onCopyKeyClicked() + }.last() + + assertThat(event).isEqualTo( + CustomFieldsEditorViewModel.CopyContentToClipboard( + R.string.custom_fields_editor_key_label, + CUSTOM_FIELD.key + ) + ) + } + + @Test + fun `when tapping copy value, then copy value to clipboard`() = testBlocking { + setup(editing = true) + + val event = viewModel.event.runAndCaptureValues { + viewModel.onCopyValueClicked() + }.last() + + assertThat(event).isEqualTo( + CustomFieldsEditorViewModel.CopyContentToClipboard( + R.string.custom_fields_editor_value_label, + CUSTOM_FIELD.valueAsString + ) + ) + } }