Skip to content

Commit

Permalink
Merge pull request #3091 from wordpress-mobile/woo/metadata-few-impro…
Browse files Browse the repository at this point in the history
…vements

[Woo] Simplify MetaData delete API
  • Loading branch information
hichamboushaba authored Sep 11, 2024
2 parents a77c116 + b4061b6 commit c35b836
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CustomFieldsViewModel(
parentItemType = parentItemType,
insertedMetadata = emptyList(),
updatedMetadata = emptyList(),
deletedMetadata = emptyList()
deletedMetadataIds = emptyList()
)
)

Expand All @@ -51,7 +51,7 @@ class CustomFieldsViewModel(
),
pendingUpdateRequest
) { customFields, pendingUpdateRequest ->
customFields.filterNot { it in pendingUpdateRequest.deletedMetadata }
customFields.filterNot { it.id in pendingUpdateRequest.deletedMetadataIds }
.map { field ->
pendingUpdateRequest.updatedMetadata.find { it.key == field.key }
?: field
Expand All @@ -64,7 +64,7 @@ class CustomFieldsViewModel(
pendingUpdateRequest.map {
it.insertedMetadata.isNotEmpty() ||
it.updatedMetadata.isNotEmpty() ||
it.deletedMetadata.isNotEmpty()
it.deletedMetadataIds.isNotEmpty()
}
) { loadingState, metaData, hasChanges ->
when (loadingState) {
Expand All @@ -74,7 +74,7 @@ class CustomFieldsViewModel(
onDelete = { field ->
pendingUpdateRequest.update {
it.copy(
deletedMetadata = it.deletedMetadata + field
deletedMetadataIds = it.deletedMetadataIds + field.id
)
}
},
Expand Down Expand Up @@ -136,7 +136,7 @@ class CustomFieldsViewModel(
it.copy(
insertedMetadata = emptyList(),
updatedMetadata = emptyList(),
deletedMetadata = emptyList()
deletedMetadataIds = emptyList()
)
}
loadingState.value = LoadingState.Loaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ data class UpdateMetadataRequest(
val parentItemType: MetaDataParentItemType,
val insertedMetadata: List<WCMetaData> = emptyList(),
val updatedMetadata: List<WCMetaData> = emptyList(),
val deletedMetadata: List<WCMetaData> = emptyList(),
val deletedMetadataIds: List<Long> = emptyList(),
) {
init {
// The ID of inserted metadata is ignored, so to ensure that there is no data loss here,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.wordpress.android.fluxc.model.metadata.WCMetaDataValue.JsonArrayValue
import org.wordpress.android.fluxc.model.metadata.WCMetaDataValue.JsonObjectValue
import org.wordpress.android.fluxc.model.metadata.WCMetaDataValue.WCMetaDataValueJsonAdapter
import org.wordpress.android.util.AppLog
import org.wordpress.android.util.HtmlUtils

data class WCMetaData(
@SerializedName(ID)
Expand Down Expand Up @@ -39,10 +40,10 @@ data class WCMetaData(
get() = value.stringValue.orEmpty()

val valueStrippedHtml: String
get() = valueAsString.replace(htmlRegex, "")
get() = HtmlUtils.fastStripHtml(valueAsString)

val isHtml: Boolean
get() = valueAsString.contains(htmlRegex)
get() = valueAsString != valueStrippedHtml

val isJson: Boolean
get() = value is JsonObjectValue || value is JsonArrayValue
Expand All @@ -64,10 +65,6 @@ data class WCMetaData(
private const val DISPLAY_KEY = "display_key"
private const val DISPLAY_VALUE = "display_value"

private val htmlRegex by lazy {
Regex("<[^>]+>")
}

val SUPPORTED_KEYS: Set<String> = buildSet {
add(SubscriptionMetadataKeys.SUBSCRIPTION_RENEWAL)
add(BundleMetadataKeys.BUNDLED_ITEM_ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ internal class MetaDataRestClient @Inject internal constructor(
request.updatedMetadata.forEach {
metaDataJson.add(it.toJson())
}
request.deletedMetadata.forEach {
request.deletedMetadataIds.forEach {
metaDataJson.add(
JsonObject().apply {
addProperty(WCMetaData.ID, it.id)
addProperty(WCMetaData.ID, it)
add(WCMetaData.VALUE, JsonNull.INSTANCE)
}
)
Expand Down

0 comments on commit c35b836

Please sign in to comment.