Skip to content
This repository was archived by the owner on Feb 4, 2025. It is now read-only.

Commit f30d1b6

Browse files
Introduce MetadataChanges class to improve usage in other stores
1 parent b2002e5 commit f30d1b6

File tree

6 files changed

+80
-48
lines changed

6 files changed

+80
-48
lines changed

example/src/main/java/org/wordpress/android/fluxc/example/ui/metadata/CustomFieldsViewModel.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import kotlinx.coroutines.flow.update
1313
import kotlinx.coroutines.launch
1414
import org.wordpress.android.fluxc.model.SiteModel
1515
import org.wordpress.android.fluxc.model.metadata.MetaDataParentItemType
16+
import org.wordpress.android.fluxc.model.metadata.MetadataChanges
1617
import org.wordpress.android.fluxc.model.metadata.UpdateMetadataRequest
1718
import org.wordpress.android.fluxc.model.metadata.WCMetaData
1819
import org.wordpress.android.fluxc.store.MetaDataStore
@@ -43,6 +44,7 @@ class CustomFieldsViewModel(
4344
loadCustomFields()
4445
}
4546

47+
@Suppress("LongMethod")
4648
private fun observeLoadingState() {
4749
val customFields = combine(
4850
metaDataStore.observeDisplayableMetaData(
@@ -63,8 +65,8 @@ class CustomFieldsViewModel(
6365
customFields,
6466
pendingUpdateRequest.map {
6567
it.insertedMetadata.isNotEmpty() ||
66-
it.updatedMetadata.isNotEmpty() ||
67-
it.deletedMetadataIds.isNotEmpty()
68+
it.updatedMetadata.isNotEmpty() ||
69+
it.deletedMetadataIds.isNotEmpty()
6870
}
6971
) { loadingState, metaData, hasChanges ->
7072
when (loadingState) {
@@ -74,21 +76,27 @@ class CustomFieldsViewModel(
7476
onDelete = { field ->
7577
pendingUpdateRequest.update {
7678
it.copy(
77-
deletedMetadataIds = it.deletedMetadataIds + field.id
79+
metadataChanges = it.metadataChanges.copy(
80+
deletedMetadataIds = it.deletedMetadataIds + field.id
81+
)
7882
)
7983
}
8084
},
8185
onEdit = { field ->
8286
pendingUpdateRequest.update {
8387
it.copy(
84-
updatedMetadata = it.updatedMetadata + field
88+
metadataChanges = it.metadataChanges.copy(
89+
updatedMetadata = it.updatedMetadata + field
90+
)
8591
)
8692
}
8793
},
8894
onAdd = { field ->
8995
pendingUpdateRequest.update {
9096
it.copy(
91-
insertedMetadata = it.insertedMetadata + field
97+
metadataChanges = it.metadataChanges.copy(
98+
insertedMetadata = it.insertedMetadata + field
99+
)
92100
)
93101
}
94102
},
@@ -134,9 +142,7 @@ class CustomFieldsViewModel(
134142
} else {
135143
pendingUpdateRequest.update {
136144
it.copy(
137-
insertedMetadata = emptyList(),
138-
updatedMetadata = emptyList(),
139-
deletedMetadataIds = emptyList()
145+
metadataChanges = MetadataChanges()
140146
)
141147
}
142148
loadingState.value = LoadingState.Loaded
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.wordpress.android.fluxc.model.metadata
2+
3+
import com.google.gson.JsonArray
4+
import com.google.gson.JsonNull
5+
import com.google.gson.JsonObject
6+
7+
data class MetadataChanges(
8+
val insertedMetadata: List<WCMetaData> = emptyList(),
9+
val updatedMetadata: List<WCMetaData> = emptyList(),
10+
val deletedMetadataIds: List<Long> = emptyList(),
11+
) {
12+
init {
13+
// The ID of inserted metadata is ignored, so to ensure that there is no data loss here,
14+
// we require that all inserted metadata have an ID of 0.
15+
require(insertedMetadata.all { it.id == 0L }) {
16+
"Inserted metadata must have an ID of 0"
17+
}
18+
}
19+
20+
internal fun toJsonArray() = JsonArray().apply {
21+
insertedMetadata.forEach {
22+
add(
23+
JsonObject().apply {
24+
addProperty(WCMetaData.KEY, it.key)
25+
add(WCMetaData.VALUE, it.value.jsonValue)
26+
}
27+
)
28+
}
29+
updatedMetadata.forEach {
30+
add(it.toJson())
31+
}
32+
deletedMetadataIds.forEach {
33+
add(
34+
JsonObject().apply {
35+
addProperty(WCMetaData.ID, it)
36+
add(WCMetaData.VALUE, JsonNull.INSTANCE)
37+
}
38+
)
39+
}
40+
}
41+
}
Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,28 @@
11
package org.wordpress.android.fluxc.model.metadata
22

3-
import com.google.gson.JsonArray
4-
import com.google.gson.JsonNull
5-
import com.google.gson.JsonObject
6-
73
data class UpdateMetadataRequest(
84
val parentItemId: Long,
95
val parentItemType: MetaDataParentItemType,
10-
val insertedMetadata: List<WCMetaData> = emptyList(),
11-
val updatedMetadata: List<WCMetaData> = emptyList(),
12-
val deletedMetadataIds: List<Long> = emptyList(),
6+
val metadataChanges: MetadataChanges,
137
) {
14-
init {
15-
// The ID of inserted metadata is ignored, so to ensure that there is no data loss here,
16-
// we require that all inserted metadata have an ID of 0.
17-
require(insertedMetadata.all { it.id == 0L }) {
18-
"Inserted metadata must have an ID of 0"
19-
}
20-
}
8+
val insertedMetadata: List<WCMetaData> get() = metadataChanges.insertedMetadata
9+
val updatedMetadata: List<WCMetaData> get() = metadataChanges.updatedMetadata
10+
val deletedMetadataIds: List<Long> get() = metadataChanges.deletedMetadataIds
2111

22-
internal fun toJsonArray() = JsonArray().apply {
23-
insertedMetadata.forEach {
24-
add(
25-
JsonObject().apply {
26-
addProperty(WCMetaData.KEY, it.key)
27-
add(WCMetaData.VALUE, it.value.jsonValue)
28-
}
29-
)
30-
}
31-
updatedMetadata.forEach {
32-
add(it.toJson())
33-
}
34-
deletedMetadataIds.forEach {
35-
add(
36-
JsonObject().apply {
37-
addProperty(WCMetaData.ID, it)
38-
add(WCMetaData.VALUE, JsonNull.INSTANCE)
39-
}
40-
)
41-
}
42-
}
12+
constructor(
13+
parentItemId: Long,
14+
parentItemType: MetaDataParentItemType,
15+
insertedMetadata: List<WCMetaData> = emptyList(),
16+
updatedMetadata: List<WCMetaData> = emptyList(),
17+
deletedMetadataIds: List<Long> = emptyList(),
18+
) : this(
19+
parentItemId,
20+
parentItemType,
21+
MetadataChanges(
22+
insertedMetadata,
23+
updatedMetadata,
24+
deletedMetadataIds,
25+
)
26+
)
4327
}
28+

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/metadata/MetaDataRestClient.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ internal class MetaDataRestClient @Inject internal constructor(
4949
site = site,
5050
path = path,
5151
body = mapOf(
52-
"meta_data" to request.toJsonArray(),
52+
"meta_data" to request.metadataChanges.toJsonArray(),
5353
"_fields" to "meta_data"
5454
),
5555
clazz = JsonObject::class.java

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/network/rest/wpcom/wc/product/ProductRestClient.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import org.wordpress.android.fluxc.model.WCProductReviewModel
2020
import org.wordpress.android.fluxc.model.WCProductShippingClassModel
2121
import org.wordpress.android.fluxc.model.WCProductTagModel
2222
import org.wordpress.android.fluxc.model.WCProductVariationModel
23-
import org.wordpress.android.fluxc.model.metadata.UpdateMetadataRequest
23+
import org.wordpress.android.fluxc.model.metadata.MetadataChanges
2424
import org.wordpress.android.fluxc.model.metadata.WCMetaData
2525
import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType
2626
import org.wordpress.android.fluxc.network.BaseRequest.GenericErrorType.PARSE_ERROR
@@ -944,7 +944,7 @@ class ProductRestClient @Inject constructor(
944944
site: SiteModel,
945945
storedWCProductModel: WCProductModel?,
946946
updatedProductModel: WCProductModel,
947-
metadataChanges: UpdateMetadataRequest? = null
947+
metadataChanges: MetadataChanges? = null
948948
) {
949949
coroutineEngine.launch(AppLog.T.API, this, "updateProduct") {
950950
val remoteProductId = updatedProductModel.remoteProductId

plugins/woocommerce/src/main/kotlin/org/wordpress/android/fluxc/store/WCProductStore.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import org.wordpress.android.fluxc.model.WCProductTagModel
2525
import org.wordpress.android.fluxc.model.WCProductVariationModel
2626
import org.wordpress.android.fluxc.model.WCProductVariationModel.ProductVariantOption
2727
import org.wordpress.android.fluxc.model.addons.RemoteAddonDto
28-
import org.wordpress.android.fluxc.model.metadata.UpdateMetadataRequest
28+
import org.wordpress.android.fluxc.model.metadata.MetadataChanges
2929
import org.wordpress.android.fluxc.model.metadata.WCMetaData
3030
import org.wordpress.android.fluxc.model.metadata.get
3131
import org.wordpress.android.fluxc.network.BaseRequest.BaseNetworkError
@@ -181,7 +181,7 @@ class WCProductStore @Inject constructor(
181181
class UpdateProductPayload(
182182
var site: SiteModel,
183183
val product: WCProductModel,
184-
val metadataChanges: UpdateMetadataRequest? = null
184+
val metadataChanges: MetadataChanges? = null
185185
) : Payload<BaseNetworkError>()
186186

187187
class BatchUpdateProductsPayload(

0 commit comments

Comments
 (0)