Skip to content

Commit

Permalink
Merge pull request #10711 from woocommerce/issue/10689-blaze-network-…
Browse files Browse the repository at this point in the history
…implementations

[Blaze] Network implementations
  • Loading branch information
hichamboushaba authored Feb 8, 2024
2 parents 421e573 + 1f7fa7b commit aa4775f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.woocommerce.android.ui.blaze

import android.os.Parcelable
import com.woocommerce.android.OnChangedException
import com.woocommerce.android.tools.SelectedSite
import com.woocommerce.android.ui.products.ProductDetailRepository
import com.woocommerce.android.util.TimezoneProvider
import com.woocommerce.android.util.WooLog
import kotlinx.coroutines.flow.map
import kotlinx.parcelize.Parcelize
import org.wordpress.android.fluxc.persistence.blaze.BlazeCampaignsDao.BlazeAdSuggestionEntity
import org.wordpress.android.fluxc.model.blaze.BlazeAdSuggestion
import org.wordpress.android.fluxc.store.blaze.BlazeCampaignsStore
import java.util.Date
import javax.inject.Inject
Expand All @@ -30,33 +32,82 @@ class BlazeRepository @Inject constructor(
fun observeLanguages() = blazeCampaignsStore.observeBlazeTargetingLanguages()
.map { it.map { language -> Language(language.id, language.name) } }

suspend fun fetchLanguages() = blazeCampaignsStore.fetchBlazeTargetingLanguages()
suspend fun fetchLanguages(): Result<Unit> {
val result = blazeCampaignsStore.fetchBlazeTargetingLanguages(selectedSite.get())

return when {
result.isError -> {
WooLog.w(WooLog.T.BLAZE, "Failed to fetch languages: ${result.error}")
Result.failure(OnChangedException(result.error))
}
else -> Result.success(Unit)
}
}

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

suspend fun fetchDevices() = blazeCampaignsStore.fetchBlazeTargetingDevices()
suspend fun fetchDevices(): Result<Unit> {
val result = blazeCampaignsStore.fetchBlazeTargetingDevices(selectedSite.get())

return when {
result.isError -> {
WooLog.w(WooLog.T.BLAZE, "Failed to fetch devices: ${result.error}")
Result.failure(OnChangedException(result.error))
}
else -> Result.success(Unit)
}
}

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

suspend fun fetchInterests() = blazeCampaignsStore.fetchBlazeTargetingTopics()
suspend fun fetchInterests(): Result<Unit> {
val result = blazeCampaignsStore.fetchBlazeTargetingTopics(selectedSite.get())

return when {
result.isError -> {
WooLog.w(WooLog.T.BLAZE, "Failed to fetch interests: ${result.error}")
Result.failure(OnChangedException(result.error))
}
else -> Result.success(Unit)
}
}

suspend fun fetchLocations(query: String) = blazeCampaignsStore.fetchBlazeTargetingLocations(query).model
?.map { location -> Location(location.id, location.name, location.parent?.name, location.type) }
suspend fun fetchLocations(query: String): Result<List<Location>> {
val result = blazeCampaignsStore.fetchBlazeTargetingLocations(
selectedSite.get(),
query
)

return when {
result.isError -> {
WooLog.w(WooLog.T.BLAZE, "Failed to fetch locations: ${result.error}")
Result.failure(OnChangedException(result.error))
}
else -> Result.success(
result.model?.map { location ->
Location(location.id, location.name, location.parent?.name, location.type)
} ?: emptyList()
)
}
}

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

suspend fun getAdSuggestions(productId: Long): List<AiSuggestionForAd>? {
fun List<BlazeAdSuggestionEntity>.mapToUiModel(): List<AiSuggestionForAd> {
suspend fun fetchAdSuggestions(productId: Long): Result<List<AiSuggestionForAd>> {
fun List<BlazeAdSuggestion>.mapToUiModel(): List<AiSuggestionForAd> {
return map { AiSuggestionForAd(it.tagLine, it.description) }
}

val suggestions = blazeCampaignsStore.getBlazeAdSuggestions(selectedSite.get(), productId)
return if (suggestions.isNotEmpty()) {
suggestions.mapToUiModel()
} else {
blazeCampaignsStore.fetchBlazeAdSuggestions(selectedSite.get(), productId).model?.mapToUiModel()
val result = blazeCampaignsStore.fetchBlazeAdSuggestions(selectedSite.get(), productId)

return when {
result.isError -> {
WooLog.w(WooLog.T.BLAZE, "Failed to fetch ad suggestions: ${result.error}")
Result.failure(OnChangedException(result.error))
}
else -> Result.success(result.model?.mapToUiModel() ?: emptyList())
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class BlazeCampaignCreationEditAdViewModel @Inject constructor(

private fun loadSuggestions() {
viewModelScope.launch {
blazeRepository.getAdSuggestions(navArgs.productId)?.let { list ->
blazeRepository.fetchAdSuggestions(navArgs.productId).getOrNull()?.let { list ->
val index = list.indexOfFirst { it.tagLine == navArgs.tagline && it.description == navArgs.description }
val suggestions = list.map { AiSuggestionForAd(it.tagLine, it.description) }
if (index != -1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class BlazeCampaignCreationPreviewViewModel @Inject constructor(
blazeRepository.fetchDevices()
blazeRepository.fetchInterests()

blazeRepository.getAdSuggestions(navArgs.productId).let { suggestions ->
blazeRepository.fetchAdSuggestions(navArgs.productId).getOrNull().let { suggestions ->
adDetails.update {
AdDetails(
productId = navArgs.productId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class BlazeCampaignTargetLocationSelectionViewModel @Inject constructor(
}

private suspend fun fetchLocations(query: String) = blazeRepository.fetchLocations(query)
.getOrNull()
?.asSequence()
?.filterNot { location ->
location.id in items.value.map { it.location.id }
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ tasks.register("installGitHooks", Copy) {
}

ext {
fluxCVersion = '2.65.0'
fluxCVersion = 'trunk-6b2c6fc5022e2f14570bd5e126da1f8409965830'
glideVersion = '4.13.2'
coilVersion = '2.1.0'
constraintLayoutVersion = '1.2.0'
Expand Down

0 comments on commit aa4775f

Please sign in to comment.