Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Custom Fields] Support for copying key and value #12579

Merged
merged 3 commits into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}
Expand All @@ -48,6 +50,8 @@ private fun CustomFieldsEditorScreen(
onValueChanged: (String) -> Unit,
onDoneClicked: () -> Unit,
onDeleteClicked: () -> Unit,
onCopyKeyClicked: () -> Unit,
onCopyValueClicked: () -> Unit,
onBackButtonClick: () -> Unit,
) {
BackHandler { onBackButtonClick() }
Expand All @@ -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,
Comment on lines +73 to +74
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These entries will be shown even when the key or value are empty, I thought about hiding them when they are, but I didn't implement it because I think it'll complicate the logic for not much added value, WDYT?

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")
}
}
)
}
)
},
Expand Down Expand Up @@ -140,6 +148,8 @@ private fun CustomFieldsEditorScreenPreview() {
onValueChanged = {},
onDoneClicked = {},
onDeleteClicked = {},
onCopyKeyClicked = {},
onCopyValueClicked = {},
onBackButtonClick = {}
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -109,6 +110,14 @@ class CustomFieldsEditorViewModel @Inject constructor(
)
}

fun onCopyKeyClicked() {
triggerEvent(CopyContentToClipboard(R.string.custom_fields_editor_key_label, customFieldDraft.value.key))
}

fun onCopyValueClicked() {
triggerEvent(CopyContentToClipboard(R.string.custom_fields_editor_value_label, customFieldDraft.value.value))
}

fun onBackClick() {
if (state.value?.hasChanges == true) {
showDiscardChangesDialog.value = true
Expand Down Expand Up @@ -144,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()
}
2 changes: 2 additions & 0 deletions WooCommerce/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4296,4 +4296,6 @@
<string name="custom_fields_editor_value_label">Value</string>
<string name="custom_fields_editor_key_error_duplicate">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.</string>
<string name="custom_fields_editor_key_error_underscore">Invalid key: please remove the \"_\" character from the beginning.</string>
<string name="custom_fields_editor_copy_key">Copy Key</string>
<string name="custom_fields_editor_copy_value">Copy Value</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
}
}
Loading