Skip to content

Commit

Permalink
Merge pull request #10645 from woocommerce/issue/10631-interests-sele…
Browse files Browse the repository at this point in the history
…ction

Blaze: Target interest selection
  • Loading branch information
0nko authored Jan 30, 2024
2 parents 35e962c + f7b6701 commit b2b5d2f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ package com.woocommerce.android.extensions
import kotlinx.coroutines.flow.Flow

@Suppress("LongParameterList")
inline fun <T1, T2, T3, T4, T5, T6, R> combine(
inline fun <T1, T2, T3, T4, T5, T6, T7, R> combine(
flow: Flow<T1>,
flow2: Flow<T2>,
flow3: Flow<T3>,
flow4: Flow<T4>,
flow5: Flow<T5>,
flow6: Flow<T6>,
crossinline transform: suspend (T1, T2, T3, T4, T5, T6) -> R
flow7: Flow<T7>,
crossinline transform: suspend (T1, T2, T3, T4, T5, T6, T7) -> R
): Flow<R> {
return kotlinx.coroutines.flow.combine(flow, flow2, flow3, flow4, flow5, flow6) { args: Array<*> ->
return kotlinx.coroutines.flow.combine(flow, flow2, flow3, flow4, flow5, flow6, flow7) { args: Array<*> ->
@Suppress("UNCHECKED_CAST", "MagicNumber")
transform(
args[0] as T1,
Expand All @@ -21,22 +22,24 @@ inline fun <T1, T2, T3, T4, T5, T6, R> combine(
args[3] as T4,
args[4] as T5,
args[5] as T6,
args[6] as T7,
)
}
}

@Suppress("LongParameterList")
inline fun <T1, T2, T3, T4, T5, T6, T7, R> combine(
inline fun <T1, T2, T3, T4, T5, T6, T7, T8, R> combine(
flow: Flow<T1>,
flow2: Flow<T2>,
flow3: Flow<T3>,
flow4: Flow<T4>,
flow5: Flow<T5>,
flow6: Flow<T6>,
flow7: Flow<T7>,
crossinline transform: suspend (T1, T2, T3, T4, T5, T6, T7) -> R
flow8: Flow<T8>,
crossinline transform: suspend (T1, T2, T3, T4, T5, T6, T7, T8) -> R
): Flow<R> {
return kotlinx.coroutines.flow.combine(flow, flow2, flow3, flow4, flow5, flow6, flow7) { args: Array<*> ->
return kotlinx.coroutines.flow.combine(flow, flow2, flow3, flow4, flow5, flow6, flow7, flow8) { args: Array<*> ->
@Suppress("UNCHECKED_CAST", "MagicNumber")
transform(
args[0] as T1,
Expand All @@ -46,6 +49,7 @@ inline fun <T1, T2, T3, T4, T5, T6, T7, R> combine(
args[4] as T5,
args[5] as T6,
args[6] as T7,
args[7] as T8,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,12 @@ class BlazeRepository @Inject constructor(
fun observeDevices() = blazeCampaignsStore.observeBlazeTargetingDevices()
.map { it.map { device -> Device(device.id, device.name) } }

suspend fun fetchDevices() = blazeCampaignsStore.fetchBlazeTargetingDevices()
suspend fun fetchDevices() = blazeCampaignsStore.fetchBlazeTargetingTopics()

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

suspend fun fetchInterests() = blazeCampaignsStore.fetchBlazeTargetingTopics()

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.woocommerce.android.ui.blaze.creation.preview.BlazeCampaignCreationPr
import com.woocommerce.android.ui.blaze.creation.preview.BlazeCampaignCreationPreviewViewModel.AdDetailsUi.Loading
import com.woocommerce.android.ui.blaze.creation.targets.BlazeTargetType
import com.woocommerce.android.ui.blaze.creation.targets.BlazeTargetType.DEVICE
import com.woocommerce.android.ui.blaze.creation.targets.BlazeTargetType.INTEREST
import com.woocommerce.android.ui.blaze.creation.targets.BlazeTargetType.LANGUAGE
import com.woocommerce.android.util.CurrencyFormatter
import com.woocommerce.android.viewmodel.MultiLiveEvent
Expand Down Expand Up @@ -47,6 +48,7 @@ class BlazeCampaignCreationPreviewViewModel @Inject constructor(

private val languages = blazeRepository.observeLanguages()
private val devices = blazeRepository.observeDevices()
private val interests = blazeRepository.observeInterests()
private val selectedLanguages = savedStateHandle.getStateFlow<List<String>>(
scope = viewModelScope,
initialValue = emptyList(),
Expand All @@ -57,22 +59,29 @@ class BlazeCampaignCreationPreviewViewModel @Inject constructor(
initialValue = emptyList(),
key = "selectedDevices"
)
private val selectedInterests = savedStateHandle.getStateFlow<List<String>>(
scope = viewModelScope,
initialValue = emptyList(),
key = "selectedInterests"
)

val viewState = combine(
adDetails,
budget,
languages,
devices,
interests,
selectedLanguages,
selectedDevices
) { adDetails, budget, languages, devices, selectedLanguages, selectedDevices ->
selectedDevices,
selectedInterests
) { adDetails, budget, languages, devices, interests, selectedLanguages, selectedDevices, selectedInterests ->
CampaignPreviewUiState(
adDetails = adDetails,
campaignDetails = campaign.toCampaignDetailsUi(
budget,
languages.filter { it.code in selectedLanguages },
devices.filter { it.id in selectedDevices },
emptyList(),
interests.filter { it.id in selectedInterests },
emptyList()
)
)
Expand Down Expand Up @@ -115,6 +124,7 @@ class BlazeCampaignCreationPreviewViewModel @Inject constructor(
when (targetType) {
LANGUAGE -> selectedLanguages.update { selectedIds }
DEVICE -> selectedDevices.update { selectedIds }
INTEREST -> selectedInterests.update { selectedIds }
else -> Unit
}
}
Expand All @@ -124,6 +134,8 @@ class BlazeCampaignCreationPreviewViewModel @Inject constructor(
launch {
blazeRepository.fetchLanguages()
blazeRepository.fetchDevices()
blazeRepository.fetchInterests()

blazeRepository.getAdSuggestions(navArgs.productId).let { suggestions ->
adDetails.update {
AdDetails(
Expand Down Expand Up @@ -176,7 +188,9 @@ class BlazeCampaignCreationPreviewViewModel @Inject constructor(
displayTitle = resourceProvider.getString(string.blaze_campaign_preview_details_interests),
displayValue = interests.joinToString { it.description }
.ifEmpty { resourceProvider.getString(string.blaze_campaign_preview_target_default_value) },
onItemSelected = { /* TODO Add interests selection */ },
onItemSelected = {
triggerEvent(NavigateToTargetSelectionScreen(INTEREST, interests.map { it.id }))
},
),
),
destinationUrl = CampaignDetailItemUi(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,22 @@ class BlazeCampaignTargetSelectionViewModel @Inject constructor(
)
}
}
else -> blazeRepository.observeDevices().map { devices ->
BlazeTargetType.DEVICE -> blazeRepository.observeDevices().map { devices ->
devices.map { device ->
TargetItem(
id = device.id,
value = device.name
)
}
}
else -> blazeRepository.observeInterests().map { interests ->
interests.map { interest ->
TargetItem(
id = interest.id,
value = interest.description
)
}
}
}

private val selectedIds = savedStateHandle.getStateFlow(viewModelScope, navArgs.selectedIds.toSet())
Expand All @@ -55,7 +63,8 @@ class BlazeCampaignTargetSelectionViewModel @Inject constructor(
selectedItems = selectedIds.map { id -> items.first { it.id == id } },
title = when (navArgs.targetType) {
BlazeTargetType.LANGUAGE -> resourceProvider.getString(R.string.blaze_campaign_preview_details_language)
else -> resourceProvider.getString(R.string.blaze_campaign_preview_details_devices)
BlazeTargetType.DEVICE -> resourceProvider.getString(R.string.blaze_campaign_preview_details_devices)
else -> resourceProvider.getString(R.string.blaze_campaign_preview_details_interests)
}
)
}.asLiveData()
Expand Down

0 comments on commit b2b5d2f

Please sign in to comment.