Skip to content

Commit

Permalink
* Implements: changes to CardsViewModelSlice from this PR
Browse files Browse the repository at this point in the history
Link: #19877
Updates: the logic to filter the dynamic cards
  • Loading branch information
AjeshRPai committed Jan 15, 2024
1 parent 4d84674 commit 8509c40
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import org.wordpress.android.R
import org.wordpress.android.fluxc.model.SiteModel
import org.wordpress.android.fluxc.model.dashboard.CardModel
import org.wordpress.android.fluxc.model.dashboard.CardModel.Type
import org.wordpress.android.fluxc.network.rest.wpcom.dashboard.CardsRestClient
import org.wordpress.android.fluxc.store.NotificationStore
import org.wordpress.android.fluxc.store.dashboard.CardsStore
import org.wordpress.android.fluxc.utils.PreferenceUtils
import org.wordpress.android.modules.BG_THREAD
import org.wordpress.android.ui.mysite.MySiteCardAndItem
import org.wordpress.android.ui.mysite.SiteNavigationAction
Expand All @@ -26,9 +29,12 @@ import org.wordpress.android.ui.pages.SnackbarMessageHolder
import org.wordpress.android.ui.prefs.AppPrefsWrapper
import org.wordpress.android.ui.utils.ListItemInteraction
import org.wordpress.android.ui.utils.UiString
import org.wordpress.android.util.BuildConfigWrapper
import org.wordpress.android.util.config.DynamicDashboardCardsFeatureConfig
import org.wordpress.android.util.config.FEATURE_FLAG_PLATFORM_PARAMETER
import org.wordpress.android.util.merge
import org.wordpress.android.viewmodel.Event
import java.util.UUID
import javax.inject.Inject
import javax.inject.Named

Expand All @@ -43,6 +49,8 @@ class CardViewModelSlice @Inject constructor(
private val todaysStatsViewModelSlice: TodaysStatsViewModelSlice,
private val postsCardViewModelSlice: PostsCardViewModelSlice,
private val activityLogCardViewModelSlice: ActivityLogCardViewModelSlice,
private val preferences: PreferenceUtils.PreferenceUtilsWrapper,
private val buildConfigWrapper: BuildConfigWrapper,
) {
private lateinit var scope: CoroutineScope

Expand Down Expand Up @@ -102,7 +110,17 @@ class CardViewModelSlice @Inject constructor(
) {
_isRefreshing.postValue(true)
scope.launch(bgDispatcher) {
val result = cardsStore.fetchCards(selectedSite, getCardTypes(selectedSite))
val payload = CardsRestClient.FetchCardsPayload(
selectedSite,
getCardTypes(selectedSite),
buildNumber = buildConfigWrapper.getAppVersionCode().toString(),
deviceId = preferences.getFluxCPreferences().getString(NotificationStore.WPCOM_PUSH_DEVICE_UUID, null)
?: generateAndStoreUUID(),
identifier = buildConfigWrapper.getApplicationId(),
marketingVersion = buildConfigWrapper.getAppVersionName(),
platform = FEATURE_FLAG_PLATFORM_PARAMETER,
)
val result = cardsStore.fetchCards(payload)
val error = result.error
when {
error != null -> postErrorState()
Expand All @@ -111,6 +129,12 @@ class CardViewModelSlice @Inject constructor(
}
}

private fun generateAndStoreUUID(): String {
return UUID.randomUUID().toString().also {
preferences.getFluxCPreferences().edit().putString(NotificationStore.WPCOM_PUSH_DEVICE_UUID, it).apply()
}
}

private fun getCardTypes(selectedSite: SiteModel) = mutableListOf<Type>().apply {
if (shouldRequestStatsCard(selectedSite)) add(Type.TODAYS_STATS)
if (shouldRequestPagesCard(selectedSite)) add(Type.PAGES)
Expand Down Expand Up @@ -166,8 +190,7 @@ class CardViewModelSlice @Inject constructor(

fun postState(cards: List<CardModel>?) {
_isRefreshing.postValue(false)
if (cards.isNullOrEmpty())
{
if (cards.isNullOrEmpty()) {
_uiModel.postValue(CardsState.Success(emptyList()))
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import org.wordpress.android.fluxc.model.dashboard.CardModel.DynamicCardsModel
import org.wordpress.android.fluxc.model.dashboard.CardModel.DynamicCardsModel.CardOrder
import org.wordpress.android.fluxc.model.dashboard.CardModel.DynamicCardsModel.DynamicCardModel
import org.wordpress.android.fluxc.model.dashboard.CardModel.DynamicCardsModel.DynamicCardRowModel
import org.wordpress.android.fluxc.network.rest.wpcom.dashboard.CardsRestClient.FetchCardsPayload
import org.wordpress.android.fluxc.model.dashboard.CardModel.PagesCardModel
import org.wordpress.android.fluxc.model.dashboard.CardModel.PagesCardModel.PageCardModel
import org.wordpress.android.fluxc.model.dashboard.CardModel.PostsCardModel
import org.wordpress.android.fluxc.model.dashboard.CardModel.PostsCardModel.PostCardModel
import org.wordpress.android.fluxc.model.dashboard.CardModel.TodaysStatsCardModel
import org.wordpress.android.fluxc.network.rest.wpcom.dashboard.CardsRestClient.FetchCardsPayload
import org.wordpress.android.fluxc.network.rest.wpcom.dashboard.CardsUtils
import org.wordpress.android.fluxc.store.dashboard.CardsStore
import org.wordpress.android.fluxc.store.dashboard.CardsStore.CardsError
Expand Down Expand Up @@ -247,7 +247,6 @@ class CardsViewModelSliceTest : BaseUnitTest() {
private lateinit var viewModelSlice: CardViewModelSlice

private lateinit var defaultFetchCardsPayload: FetchCardsPayload
private lateinit var cardSource: CardsSource

private val data = CardsResult(
model = CARDS_MODEL
Expand Down Expand Up @@ -275,8 +274,7 @@ class CardsViewModelSliceTest : BaseUnitTest() {
dynamicCardsViewModelSlice,
todaysStatsViewModelSlice,
postsCardViewModelSlice,
activityLogCardViewModelSlice
dynamicDashboardCardsFeatureConfig,
activityLogCardViewModelSlice,
preferenceUtilsWrapper,
buildConfigWrapper,
)
Expand Down

0 comments on commit 8509c40

Please sign in to comment.