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

Issue/12563 delete custom fields #12566

Merged
merged 10 commits into from
Sep 12, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class CustomFieldsEditorFragment : BaseFragment() {
companion object {
const val RESULT_KEY = "custom_field_result"
}

override val activityAppBarStatus: AppBarStatus = AppBarStatus.Hidden

private val viewModel: CustomFieldsEditorViewModel by viewModels()
Expand All @@ -36,7 +32,7 @@ class CustomFieldsEditorFragment : BaseFragment() {
private fun handleEvents() {
viewModel.event.observe(viewLifecycleOwner) { event ->
when (event) {
is MultiLiveEvent.Event.ExitWithResult<*> -> navigateBackWithResult(RESULT_KEY, event.data)
is MultiLiveEvent.Event.ExitWithResult<*> -> navigateBackWithResult(event.key!!, event.data)
irfano marked this conversation as resolved.
Show resolved Hide resolved
MultiLiveEvent.Event.Exit -> findNavController().navigateUp()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.LocalContentColor
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Scaffold
import androidx.compose.runtime.Composable
Expand All @@ -18,6 +19,7 @@ import com.woocommerce.android.R
import com.woocommerce.android.ui.compose.component.DiscardChangesDialog
import com.woocommerce.android.ui.compose.component.Toolbar
import com.woocommerce.android.ui.compose.component.WCOutlinedTextField
import com.woocommerce.android.ui.compose.component.WCOverflowMenu
import com.woocommerce.android.ui.compose.component.WCTextButton
import com.woocommerce.android.ui.compose.component.aztec.OutlinedAztecEditor
import com.woocommerce.android.ui.compose.component.getText
Expand All @@ -33,6 +35,7 @@ fun CustomFieldsEditorScreen(viewModel: CustomFieldsEditorViewModel) {
onKeyChanged = viewModel::onKeyChanged,
onValueChanged = viewModel::onValueChanged,
onDoneClicked = viewModel::onDoneClicked,
onDeleteClicked = viewModel::onDeleteClicked,
onBackButtonClick = viewModel::onBackClick,
)
}
Expand All @@ -44,6 +47,7 @@ private fun CustomFieldsEditorScreen(
onKeyChanged: (String) -> Unit,
onValueChanged: (String) -> Unit,
onDoneClicked: () -> Unit,
onDeleteClicked: () -> Unit,
onBackButtonClick: () -> Unit,
) {
BackHandler { onBackButtonClick() }
Expand All @@ -60,6 +64,22 @@ private fun CustomFieldsEditorScreen(
text = stringResource(R.string.done)
)
}
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")
}
}
)
}
)
},
Expand Down Expand Up @@ -117,6 +137,7 @@ private fun CustomFieldsEditorScreenPreview() {
onKeyChanged = {},
onValueChanged = {},
onDoneClicked = {},
onDeleteClicked = {},
onBackButtonClick = {}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class CustomFieldsEditorViewModel @Inject constructor(
savedStateHandle: SavedStateHandle,
private val repository: CustomFieldsRepository
) : ScopedViewModel(savedStateHandle) {
companion object {
const val CUSTOM_FIELD_UPDATED_RESULT_KEY = "custom_field_updated"
const val CUSTOM_FIELD_DELETED_RESULT_KEY = "custom_field_deleted"
}

private val navArgs by savedStateHandle.navArgs<CustomFieldsEditorFragmentArgs>()

private val customFieldDraft = savedStateHandle.getStateFlow(
Expand Down Expand Up @@ -84,15 +89,25 @@ class CustomFieldsEditorViewModel @Inject constructor(
if (existingFields.any { it.key == value.key }) {
keyErrorMessage.value = UiString.UiStringRes(R.string.custom_fields_editor_key_error_duplicate)
} else {
triggerEvent(MultiLiveEvent.Event.ExitWithResult(value))
triggerEvent(
MultiLiveEvent.Event.ExitWithResult(data = value, key = CUSTOM_FIELD_UPDATED_RESULT_KEY)
)
}
}
} else {
// When editing, we don't need to check for duplicate keys
triggerEvent(MultiLiveEvent.Event.ExitWithResult(value))
triggerEvent(
MultiLiveEvent.Event.ExitWithResult(data = value, key = CUSTOM_FIELD_UPDATED_RESULT_KEY)
)
}
}

fun onDeleteClicked() {
triggerEvent(
MultiLiveEvent.Event.ExitWithResult(data = navArgs.customField, key = CUSTOM_FIELD_DELETED_RESULT_KEY)
)
}

fun onBackClick() {
if (state.value?.hasChanges == true) {
showDiscardChangesDialog.value = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.woocommerce.android.ui.base.UIMessageResolver
import com.woocommerce.android.ui.compose.composeView
import com.woocommerce.android.ui.customfields.CustomFieldContentType
import com.woocommerce.android.ui.customfields.CustomFieldUiModel
import com.woocommerce.android.ui.customfields.editor.CustomFieldsEditorFragment
import com.woocommerce.android.ui.customfields.editor.CustomFieldsEditorViewModel
import com.woocommerce.android.ui.main.AppBarStatus
import com.woocommerce.android.util.ActivityUtils
import com.woocommerce.android.util.ChromeCustomTabUtils
Expand Down Expand Up @@ -56,13 +56,16 @@ class CustomFieldsFragment : BaseFragment() {
}

private fun handleResults() {
handleResult<CustomFieldUiModel>(CustomFieldsEditorFragment.RESULT_KEY) { result ->
handleResult<CustomFieldUiModel>(CustomFieldsEditorViewModel.CUSTOM_FIELD_UPDATED_RESULT_KEY) { result ->
if (result.id == null) {
viewModel.onCustomFieldInserted(result)
} else {
viewModel.onCustomFieldUpdated(result)
}
}
handleResult<CustomFieldUiModel>(CustomFieldsEditorViewModel.CUSTOM_FIELD_DELETED_RESULT_KEY) { result ->
viewModel.onCustomFieldDeleted(result)
}
}

private fun openEditor(field: CustomFieldUiModel?) {
Expand Down