Skip to content

Commit aa4775f

Browse files
Merge pull request #10711 from woocommerce/issue/10689-blaze-network-implementations
[Blaze] Network implementations
2 parents 421e573 + 1f7fa7b commit aa4775f

File tree

5 files changed

+68
-16
lines changed

5 files changed

+68
-16
lines changed

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/blaze/BlazeRepository.kt

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.woocommerce.android.ui.blaze
22

33
import android.os.Parcelable
4+
import com.woocommerce.android.OnChangedException
45
import com.woocommerce.android.tools.SelectedSite
56
import com.woocommerce.android.ui.products.ProductDetailRepository
67
import com.woocommerce.android.util.TimezoneProvider
8+
import com.woocommerce.android.util.WooLog
79
import kotlinx.coroutines.flow.map
810
import kotlinx.parcelize.Parcelize
9-
import org.wordpress.android.fluxc.persistence.blaze.BlazeCampaignsDao.BlazeAdSuggestionEntity
11+
import org.wordpress.android.fluxc.model.blaze.BlazeAdSuggestion
1012
import org.wordpress.android.fluxc.store.blaze.BlazeCampaignsStore
1113
import java.util.Date
1214
import javax.inject.Inject
@@ -30,33 +32,82 @@ class BlazeRepository @Inject constructor(
3032
fun observeLanguages() = blazeCampaignsStore.observeBlazeTargetingLanguages()
3133
.map { it.map { language -> Language(language.id, language.name) } }
3234

33-
suspend fun fetchLanguages() = blazeCampaignsStore.fetchBlazeTargetingLanguages()
35+
suspend fun fetchLanguages(): Result<Unit> {
36+
val result = blazeCampaignsStore.fetchBlazeTargetingLanguages(selectedSite.get())
37+
38+
return when {
39+
result.isError -> {
40+
WooLog.w(WooLog.T.BLAZE, "Failed to fetch languages: ${result.error}")
41+
Result.failure(OnChangedException(result.error))
42+
}
43+
else -> Result.success(Unit)
44+
}
45+
}
3446

3547
fun observeDevices() = blazeCampaignsStore.observeBlazeTargetingDevices()
3648
.map { it.map { device -> Device(device.id, device.name) } }
3749

38-
suspend fun fetchDevices() = blazeCampaignsStore.fetchBlazeTargetingDevices()
50+
suspend fun fetchDevices(): Result<Unit> {
51+
val result = blazeCampaignsStore.fetchBlazeTargetingDevices(selectedSite.get())
52+
53+
return when {
54+
result.isError -> {
55+
WooLog.w(WooLog.T.BLAZE, "Failed to fetch devices: ${result.error}")
56+
Result.failure(OnChangedException(result.error))
57+
}
58+
else -> Result.success(Unit)
59+
}
60+
}
3961

4062
fun observeInterests() = blazeCampaignsStore.observeBlazeTargetingTopics()
4163
.map { it.map { interest -> Interest(interest.id, interest.description) } }
4264

43-
suspend fun fetchInterests() = blazeCampaignsStore.fetchBlazeTargetingTopics()
65+
suspend fun fetchInterests(): Result<Unit> {
66+
val result = blazeCampaignsStore.fetchBlazeTargetingTopics(selectedSite.get())
67+
68+
return when {
69+
result.isError -> {
70+
WooLog.w(WooLog.T.BLAZE, "Failed to fetch interests: ${result.error}")
71+
Result.failure(OnChangedException(result.error))
72+
}
73+
else -> Result.success(Unit)
74+
}
75+
}
4476

45-
suspend fun fetchLocations(query: String) = blazeCampaignsStore.fetchBlazeTargetingLocations(query).model
46-
?.map { location -> Location(location.id, location.name, location.parent?.name, location.type) }
77+
suspend fun fetchLocations(query: String): Result<List<Location>> {
78+
val result = blazeCampaignsStore.fetchBlazeTargetingLocations(
79+
selectedSite.get(),
80+
query
81+
)
82+
83+
return when {
84+
result.isError -> {
85+
WooLog.w(WooLog.T.BLAZE, "Failed to fetch locations: ${result.error}")
86+
Result.failure(OnChangedException(result.error))
87+
}
88+
else -> Result.success(
89+
result.model?.map { location ->
90+
Location(location.id, location.name, location.parent?.name, location.type)
91+
} ?: emptyList()
92+
)
93+
}
94+
}
4795

4896
suspend fun getMostRecentCampaign() = blazeCampaignsStore.getMostRecentBlazeCampaign(selectedSite.get())
4997

50-
suspend fun getAdSuggestions(productId: Long): List<AiSuggestionForAd>? {
51-
fun List<BlazeAdSuggestionEntity>.mapToUiModel(): List<AiSuggestionForAd> {
98+
suspend fun fetchAdSuggestions(productId: Long): Result<List<AiSuggestionForAd>> {
99+
fun List<BlazeAdSuggestion>.mapToUiModel(): List<AiSuggestionForAd> {
52100
return map { AiSuggestionForAd(it.tagLine, it.description) }
53101
}
54102

55-
val suggestions = blazeCampaignsStore.getBlazeAdSuggestions(selectedSite.get(), productId)
56-
return if (suggestions.isNotEmpty()) {
57-
suggestions.mapToUiModel()
58-
} else {
59-
blazeCampaignsStore.fetchBlazeAdSuggestions(selectedSite.get(), productId).model?.mapToUiModel()
103+
val result = blazeCampaignsStore.fetchBlazeAdSuggestions(selectedSite.get(), productId)
104+
105+
return when {
106+
result.isError -> {
107+
WooLog.w(WooLog.T.BLAZE, "Failed to fetch ad suggestions: ${result.error}")
108+
Result.failure(OnChangedException(result.error))
109+
}
110+
else -> Result.success(result.model?.mapToUiModel() ?: emptyList())
60111
}
61112
}
62113

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/blaze/creation/ad/BlazeCampaignCreationEditAdViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class BlazeCampaignCreationEditAdViewModel @Inject constructor(
4343

4444
private fun loadSuggestions() {
4545
viewModelScope.launch {
46-
blazeRepository.getAdSuggestions(navArgs.productId)?.let { list ->
46+
blazeRepository.fetchAdSuggestions(navArgs.productId).getOrNull()?.let { list ->
4747
val index = list.indexOfFirst { it.tagLine == navArgs.tagline && it.description == navArgs.description }
4848
val suggestions = list.map { AiSuggestionForAd(it.tagLine, it.description) }
4949
if (index != -1) {

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/blaze/creation/preview/BlazeCampaignCreationPreviewViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ class BlazeCampaignCreationPreviewViewModel @Inject constructor(
158158
blazeRepository.fetchDevices()
159159
blazeRepository.fetchInterests()
160160

161-
blazeRepository.getAdSuggestions(navArgs.productId).let { suggestions ->
161+
blazeRepository.fetchAdSuggestions(navArgs.productId).getOrNull().let { suggestions ->
162162
adDetails.update {
163163
AdDetails(
164164
productId = navArgs.productId,

WooCommerce/src/main/kotlin/com/woocommerce/android/ui/blaze/creation/targets/BlazeCampaignTargetLocationSelectionViewModel.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class BlazeCampaignTargetLocationSelectionViewModel @Inject constructor(
7272
}
7373

7474
private suspend fun fetchLocations(query: String) = blazeRepository.fetchLocations(query)
75+
.getOrNull()
7576
?.asSequence()
7677
?.filterNot { location ->
7778
location.id in items.value.map { it.location.id }

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ tasks.register("installGitHooks", Copy) {
9696
}
9797

9898
ext {
99-
fluxCVersion = '2.65.0'
99+
fluxCVersion = 'trunk-6b2c6fc5022e2f14570bd5e126da1f8409965830'
100100
glideVersion = '4.13.2'
101101
coilVersion = '2.1.0'
102102
constraintLayoutVersion = '1.2.0'

0 commit comments

Comments
 (0)