Skip to content

Commit

Permalink
Merge pull request #2871 from wordpress-mobile/observe-blaze-campaigns
Browse files Browse the repository at this point in the history
Observe blaze campaigns
  • Loading branch information
JorgeMucientes authored Oct 17, 2023
2 parents 21b9cb5 + 0eff506 commit 3caf105
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ import org.mockito.kotlin.verify
import org.mockito.kotlin.verifyNoInteractions
import org.mockito.kotlin.whenever
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.model.blaze.BlazeCampaignModel
import org.wordpress.android.fluxc.model.blaze.BlazeCampaignsModel
import org.wordpress.android.fluxc.network.rest.wpcom.blaze.AudienceList
import org.wordpress.android.fluxc.network.rest.wpcom.blaze.BlazeCampaignsError
import org.wordpress.android.fluxc.network.rest.wpcom.blaze.BlazeCampaignsErrorType.GENERIC_ERROR
import org.wordpress.android.fluxc.network.rest.wpcom.blaze.BlazeCampaignsFetchedPayload
import org.wordpress.android.fluxc.network.rest.wpcom.blaze.BlazeCampaignsResponse
import org.wordpress.android.fluxc.network.rest.wpcom.blaze.BlazeCampaignsRestClient
import org.wordpress.android.fluxc.network.rest.wpcom.blaze.BlazeCampaignsErrorType.GENERIC_ERROR
import org.wordpress.android.fluxc.network.rest.wpcom.blaze.BlazeCampaignsUtils
import org.wordpress.android.fluxc.network.rest.wpcom.blaze.Campaign
import org.wordpress.android.fluxc.network.rest.wpcom.blaze.CampaignStats
import org.wordpress.android.fluxc.network.rest.wpcom.blaze.ContentConfig
import org.wordpress.android.fluxc.persistence.blaze.BlazeCampaignsDao
import org.wordpress.android.fluxc.persistence.blaze.BlazeCampaignsDao.BlazeCampaignEntity
import org.wordpress.android.fluxc.test
import org.wordpress.android.fluxc.tools.initCoroutineEngine

Expand Down Expand Up @@ -84,7 +84,8 @@ private val BLAZE_CAMPAIGNS_RESPONSE = BlazeCampaignsResponse(
totalPages = TOTAL_PAGES
)

private val BLAZE_CAMPAIGN_MODEL = BlazeCampaignModel(
private val BLAZE_CAMPAIGN_MODEL = BlazeCampaignEntity(
siteId = SITE_ID,
campaignId = CAMPAIGN_ID,
title = TITLE,
imageUrl = IMAGE_URL,
Expand All @@ -96,7 +97,7 @@ private val BLAZE_CAMPAIGN_MODEL = BlazeCampaignModel(
clicks = CLICKS
)
private val BLAZE_CAMPAIGNS_MODEL = BlazeCampaignsModel(
campaigns = listOf(BLAZE_CAMPAIGN_MODEL),
campaigns = listOf(BLAZE_CAMPAIGN_MODEL.toDomainModel()),
page = PAGE,
totalItems = TOTAL_ITEMS,
totalPages = TOTAL_PAGES
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import androidx.room.TypeConverters
import kotlinx.coroutines.flow.Flow
import org.wordpress.android.fluxc.model.blaze.BlazeCampaignModel
import org.wordpress.android.fluxc.model.blaze.BlazeCampaignsModel
import org.wordpress.android.fluxc.persistence.coverters.BlazeCampaignsDateConverter
Expand All @@ -20,24 +21,27 @@ abstract class BlazeCampaignsDao {
val campaigns = getCampaigns(siteId)
val pagination = getCampaignsPagination(siteId)
return BlazeCampaignsModel(
campaigns = campaigns?.map { it.toDomainModel() } ?: emptyList(),
campaigns = campaigns.map { it.toDomainModel() },
page = pagination?.page ?: 1,
totalItems = pagination?.totalItems ?: 0,
totalPages = pagination?.totalPages ?: 0
)
}

@Query("SELECT * from BlazeCampaigns WHERE `siteId` = :siteId ORDER BY createdAt DESC")
abstract fun getCampaigns(siteId: Long): List<BlazeCampaignEntity>?
abstract fun getCampaigns(siteId: Long): List<BlazeCampaignEntity>

@Query("SELECT * from BlazeCampaigns WHERE `siteId` = :siteId ORDER BY createdAt DESC")
abstract fun observeCampaigns(siteId: Long): Flow<List<BlazeCampaignEntity>>

@Query("SELECT * from BlazeCampaignsPagination WHERE `siteId` = :siteId")
abstract fun getCampaignsPagination(siteId: Long): BlazeCampaignsPaginationEntity?

@Transaction
@Query("SELECT * from BlazeCampaigns WHERE `siteId` = :siteId ORDER BY createdAt DESC")
fun getMostRecentCampaignForSite(siteId: Long): BlazeCampaignModel? {
return getCampaigns(siteId)?.firstOrNull()?.toDomainModel()
}
@Query("SELECT * from BlazeCampaigns WHERE `siteId` = :siteId ORDER BY createdAt DESC LIMIT 1")
abstract fun getMostRecentCampaignForSite(siteId: Long): BlazeCampaignEntity?

@Query("SELECT * from BlazeCampaigns WHERE `siteId` = :siteId ORDER BY createdAt DESC LIMIT 1")
abstract fun observeMostRecentCampaignForSite(siteId: Long): Flow<BlazeCampaignEntity?>

@Transaction
open suspend fun insertCampaignsAndPageInfoForSite(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.wordpress.android.fluxc.store.blaze

import kotlinx.coroutines.flow.map
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.model.blaze.BlazeCampaignModel
import org.wordpress.android.fluxc.model.blaze.BlazeCampaignsModel
Expand Down Expand Up @@ -38,16 +39,21 @@ class BlazeCampaignsStore @Inject constructor(
}
}

fun observeBlazeCampaigns(site: SiteModel) = campaignsDao.observeCampaigns(site.siteId)

suspend fun getMostRecentBlazeCampaign(site: SiteModel): BlazeCampaignModel? {
return coroutineEngine.withDefaultContext(
AppLog.T.API,
this,
"get most recent blaze campaign"
) {
campaignsDao.getMostRecentCampaignForSite(site.siteId)
campaignsDao.getMostRecentCampaignForSite(site.siteId)?.toDomainModel()
}
}

fun observeMostRecentBlazeCampaign(site: SiteModel) = campaignsDao.observeMostRecentCampaignForSite(site.siteId)
.map { it?.toDomainModel() }

private suspend fun storeBlazeCampaigns(
site: SiteModel,
payload: BlazeCampaignsFetchedPayload<BlazeCampaignsResponse>
Expand Down

0 comments on commit 3caf105

Please sign in to comment.