diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mysite/MySiteCardAndItem.kt b/WordPress/src/main/java/org/wordpress/android/ui/mysite/MySiteCardAndItem.kt index 2a85e3c69c03..87ee810e74c8 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mysite/MySiteCardAndItem.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mysite/MySiteCardAndItem.kt @@ -33,7 +33,9 @@ sealed class MySiteCardAndItem(open val type: Type, open val activeQuickStartIte enum class DashboardCardType { ERROR_CARD, + TODAYS_STATS_CARD_ERROR, TODAYS_STATS_CARD, + POST_CARD_ERROR, POST_CARD_WITHOUT_POST_ITEMS, POST_CARD_WITH_POST_ITEMS } @@ -97,20 +99,35 @@ sealed class MySiteCardAndItem(open val type: Type, open val activeQuickStartIte open val dashboardCardType: DashboardCardType ) { data class ErrorCard( - override val dashboardCardType: DashboardCardType = DashboardCardType.ERROR_CARD, val onRetryClick: ListItemInteraction - ) : DashboardCard(dashboardCardType) + ) : DashboardCard(dashboardCardType = DashboardCardType.ERROR_CARD) - data class TodaysStatsCard( - val views: UiString, - val visitors: UiString, - val likes: UiString - ) : DashboardCard(DashboardCardType.TODAYS_STATS_CARD) + interface ErrorWithinCard { + val title: UiString + } + + sealed class TodaysStatsCard( + override val dashboardCardType: DashboardCardType + ) : DashboardCard(dashboardCardType) { + data class Error( + override val title: UiString + ) : TodaysStatsCard(dashboardCardType = DashboardCardType.TODAYS_STATS_CARD_ERROR), ErrorWithinCard + + data class TodaysStatsCardWithData( + val views: UiString, + val visitors: UiString, + val likes: UiString + ) : TodaysStatsCard(dashboardCardType = DashboardCardType.TODAYS_STATS_CARD) + } sealed class PostCard( override val dashboardCardType: DashboardCardType, - open val footerLink: FooterLink + open val footerLink: FooterLink? = null ) : DashboardCard(dashboardCardType) { + data class Error( + override val title: UiString + ) : PostCard(dashboardCardType = DashboardCardType.POST_CARD_ERROR), ErrorWithinCard + data class PostCardWithoutPostItems( val postCardType: PostCardType, val title: UiString, diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/CardsAdapter.kt b/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/CardsAdapter.kt index 586175954957..527206aa689f 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/CardsAdapter.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/CardsAdapter.kt @@ -7,12 +7,14 @@ import androidx.recyclerview.widget.RecyclerView.Adapter import org.apache.commons.lang3.NotImplementedException import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.ErrorCard +import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.ErrorWithinCard import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.PostCard import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.PostCard.PostCardWithPostItems import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.PostCard.PostCardWithoutPostItems -import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.TodaysStatsCard +import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.TodaysStatsCard.TodaysStatsCardWithData import org.wordpress.android.ui.mysite.MySiteCardAndItem.DashboardCardType import org.wordpress.android.ui.mysite.cards.dashboard.error.ErrorCardViewHolder +import org.wordpress.android.ui.mysite.cards.dashboard.error.ErrorWithinCardViewHolder import org.wordpress.android.ui.mysite.cards.dashboard.posts.PostCardViewHolder import org.wordpress.android.ui.mysite.cards.dashboard.todaysstats.TodaysStatsCardViewHolder @@ -27,6 +29,8 @@ class CardsAdapter( override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder<*> { return when (viewType) { DashboardCardType.ERROR_CARD.ordinal -> ErrorCardViewHolder(parent) + DashboardCardType.TODAYS_STATS_CARD_ERROR.ordinal, + DashboardCardType.POST_CARD_ERROR.ordinal -> ErrorWithinCardViewHolder(parent, uiHelpers) DashboardCardType.TODAYS_STATS_CARD.ordinal -> TodaysStatsCardViewHolder(parent, uiHelpers) DashboardCardType.POST_CARD_WITHOUT_POST_ITEMS.ordinal -> PostCardViewHolder.PostCardWithoutPostItemsViewHolder(parent, imageManager, uiHelpers) @@ -41,7 +45,8 @@ class CardsAdapter( override fun onBindViewHolder(holder: CardViewHolder<*>, position: Int) { when (holder) { is ErrorCardViewHolder -> holder.bind(items[position] as ErrorCard) - is TodaysStatsCardViewHolder -> holder.bind(items[position] as TodaysStatsCard) + is ErrorWithinCardViewHolder -> holder.bind(items[position] as ErrorWithinCard) + is TodaysStatsCardViewHolder -> holder.bind(items[position] as TodaysStatsCardWithData) is PostCardViewHolder<*> -> holder.bind(items[position] as PostCard) } } @@ -65,7 +70,8 @@ class CardsAdapter( return oldItem.dashboardCardType == newItem.dashboardCardType && when { oldItem is ErrorCard && newItem is ErrorCard -> true - oldItem is TodaysStatsCard && newItem is TodaysStatsCard -> true + oldItem is ErrorWithinCard && newItem is ErrorWithinCard -> true + oldItem is TodaysStatsCardWithData && newItem is TodaysStatsCardWithData -> true oldItem is PostCardWithPostItems && newItem is PostCardWithPostItems -> true oldItem is PostCardWithoutPostItems && newItem is PostCardWithoutPostItems -> true else -> throw NotImplementedException("Diff not implemented yet") diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/CardsShownTracker.kt b/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/CardsShownTracker.kt index 71b9e486eeb5..68a687d2b004 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/CardsShownTracker.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/CardsShownTracker.kt @@ -4,11 +4,12 @@ import org.wordpress.android.analytics.AnalyticsTracker.Stat import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.ErrorCard +import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.PostCard import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.PostCard.PostCardWithPostItems import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.PostCard.PostCardWithoutPostItems import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.TodaysStatsCard -import org.wordpress.android.ui.mysite.cards.dashboard.CardsTracker.Type.ERROR -import org.wordpress.android.ui.mysite.cards.dashboard.CardsTracker.Type.TODAYS_STATS +import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.TodaysStatsCard.TodaysStatsCardWithData +import org.wordpress.android.ui.mysite.cards.dashboard.CardsTracker.Type import org.wordpress.android.util.analytics.AnalyticsTrackerWrapper import javax.inject.Inject @@ -31,13 +32,25 @@ class CardsShownTracker @Inject constructor( is ErrorCard -> trackCardShown( Pair( card.dashboardCardType.toTypeValue().label, - ERROR.label + Type.ERROR.label ) ) - is TodaysStatsCard -> trackCardShown( + is TodaysStatsCard.Error -> trackCardShown( Pair( card.dashboardCardType.toTypeValue().label, - TODAYS_STATS.label + Type.TODAYS_STATS.label + ) + ) + is TodaysStatsCardWithData -> trackCardShown( + Pair( + card.dashboardCardType.toTypeValue().label, + Type.TODAYS_STATS.label + ) + ) + is PostCard.Error -> trackCardShown( + Pair( + card.dashboardCardType.toTypeValue().label, + Type.POST.label ) ) is PostCardWithoutPostItems -> trackCardShown( diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/CardsTracker.kt b/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/CardsTracker.kt index a62cb0b0153b..ba3941736373 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/CardsTracker.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/CardsTracker.kt @@ -71,7 +71,9 @@ class CardsTracker @Inject constructor( fun DashboardCardType.toTypeValue(): Type { return when (this) { DashboardCardType.ERROR_CARD -> Type.ERROR + DashboardCardType.TODAYS_STATS_CARD_ERROR -> Type.ERROR DashboardCardType.TODAYS_STATS_CARD -> Type.TODAYS_STATS + DashboardCardType.POST_CARD_ERROR -> Type.ERROR DashboardCardType.POST_CARD_WITHOUT_POST_ITEMS -> Type.POST DashboardCardType.POST_CARD_WITH_POST_ITEMS -> Type.POST } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/error/ErrorWithinCardViewHolder.kt b/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/error/ErrorWithinCardViewHolder.kt new file mode 100644 index 000000000000..72b7433b80db --- /dev/null +++ b/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/error/ErrorWithinCardViewHolder.kt @@ -0,0 +1,25 @@ +package org.wordpress.android.ui.mysite.cards.dashboard.error + +import android.view.ViewGroup +import org.wordpress.android.databinding.MySiteCardToolbarBinding +import org.wordpress.android.databinding.MySiteErrorWithinCardBinding +import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.ErrorWithinCard +import org.wordpress.android.ui.mysite.cards.dashboard.CardViewHolder +import org.wordpress.android.ui.utils.UiHelpers +import org.wordpress.android.ui.utils.UiString +import org.wordpress.android.util.viewBinding + +class ErrorWithinCardViewHolder( + parent: ViewGroup, + private val uiHelpers: UiHelpers +) : CardViewHolder( + parent.viewBinding(MySiteErrorWithinCardBinding::inflate) +) { + fun bind(card: ErrorWithinCard) = with(binding) { + mySiteToolbar.update(card.title) + } + + private fun MySiteCardToolbarBinding.update(title: UiString) { + uiHelpers.setTextOrHide(mySiteCardToolbarTitle, title) + } +} diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/posts/PostCardBuilder.kt b/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/posts/PostCardBuilder.kt index b79e4e299b3d..b240a82a94f1 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/posts/PostCardBuilder.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/posts/PostCardBuilder.kt @@ -3,6 +3,9 @@ package org.wordpress.android.ui.mysite.cards.dashboard.posts import org.wordpress.android.R import org.wordpress.android.fluxc.model.dashboard.CardModel.PostsCardModel import org.wordpress.android.fluxc.model.dashboard.CardModel.PostsCardModel.PostCardModel +import org.wordpress.android.fluxc.store.dashboard.CardsStore.PostCardError +import org.wordpress.android.fluxc.store.dashboard.CardsStore.PostCardErrorType +import org.wordpress.android.fluxc.utils.AppLogWrapper import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.PostCard import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.PostCard.FooterLink import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.PostCard.PostCardWithPostItems @@ -13,6 +16,7 @@ import org.wordpress.android.ui.mysite.MySiteCardAndItemBuilderParams.PostCardBu import org.wordpress.android.ui.utils.ListItemInteraction import org.wordpress.android.ui.utils.UiString.UiStringRes import org.wordpress.android.ui.utils.UiString.UiStringText +import org.wordpress.android.util.AppLog import org.wordpress.android.util.LocaleManagerWrapper import java.text.SimpleDateFormat import java.util.Date @@ -20,22 +24,42 @@ import javax.inject.Inject @Suppress("TooManyFunctions") class PostCardBuilder @Inject constructor( - private val localeManagerWrapper: LocaleManagerWrapper + private val localeManagerWrapper: LocaleManagerWrapper, + private val appLogWrapper: AppLogWrapper ) { - fun build(params: PostCardBuilderParams): List = mutableListOf().apply { - val posts = params.posts - posts?.hasPublished?.takeIf { !posts.hasDraftsOrScheduledPosts() } - ?.let { hasPublished -> - if (hasPublished) { - add(createNextPostCard(params.onFooterLinkClick)) - } else { - add(createFirstPostCard(params.onFooterLinkClick)) - } - } - posts?.draft?.takeIf { it.isNotEmpty() }?.let { add(it.createDraftPostsCard(params)) } - posts?.scheduled?.takeIf { it.isNotEmpty() }?.let { add(it.createScheduledPostsCard(params)) } + fun build(params: PostCardBuilderParams): List { + val error = params.posts?.error + return if (error != null) { + buildPostCardWithError(error) + } else { + buildPostCardsWithData(params) + } } + private fun buildPostCardWithError(error: PostCardError): List { + error.message?.let { appLogWrapper.e(AppLog.T.MY_SITE_DASHBOARD, "Post Card Error: $it") } + return if (shouldShowError(error)) listOf(createPostErrorCard()) else emptyList() + } + + private fun buildPostCardsWithData(params: PostCardBuilderParams) = + mutableListOf().apply { + val posts = params.posts + posts?.hasPublished?.takeIf { !posts.hasDraftsOrScheduledPosts() } + ?.let { hasPublished -> + if (hasPublished) { + add(createNextPostCard(params.onFooterLinkClick)) + } else { + add(createFirstPostCard(params.onFooterLinkClick)) + } + } + posts?.draft?.takeIf { it.isNotEmpty() }?.let { add(it.createDraftPostsCard(params)) } + posts?.scheduled?.takeIf { it.isNotEmpty() }?.let { add(it.createScheduledPostsCard(params)) } + }.toList() + + private fun createPostErrorCard() = PostCard.Error( + title = UiStringRes(R.string.posts) + ) + private fun createFirstPostCard(onFooterLinkClick: (postCardType: PostCardType) -> Unit) = PostCardWithoutPostItems( postCardType = PostCardType.CREATE_FIRST, @@ -122,6 +146,8 @@ class PostCardBuilder @Inject constructor( private fun constructPostDate(date: Date) = SimpleDateFormat(MONTH_DAY_FORMAT, localeManagerWrapper.getLocale()).format(date) + private fun shouldShowError(error: PostCardError) = error.type == PostCardErrorType.GENERIC_ERROR + companion object { private const val MONTH_DAY_FORMAT = "MMM d" } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/todaysstats/TodaysStatsCardBuilder.kt b/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/todaysstats/TodaysStatsCardBuilder.kt index 1e7751865653..743cfed699cd 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/todaysstats/TodaysStatsCardBuilder.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/todaysstats/TodaysStatsCardBuilder.kt @@ -1,21 +1,48 @@ package org.wordpress.android.ui.mysite.cards.dashboard.todaysstats +import org.wordpress.android.R +import org.wordpress.android.fluxc.model.dashboard.CardModel.TodaysStatsCardModel +import org.wordpress.android.fluxc.store.dashboard.CardsStore.TodaysStatsCardError +import org.wordpress.android.fluxc.store.dashboard.CardsStore.TodaysStatsCardErrorType +import org.wordpress.android.fluxc.utils.AppLogWrapper import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.TodaysStatsCard +import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.TodaysStatsCard.TodaysStatsCardWithData import org.wordpress.android.ui.mysite.MySiteCardAndItemBuilderParams.TodaysStatsCardBuilderParams import org.wordpress.android.ui.stats.refresh.utils.StatsUtils +import org.wordpress.android.ui.utils.UiString.UiStringRes import org.wordpress.android.ui.utils.UiString.UiStringText +import org.wordpress.android.util.AppLog import javax.inject.Inject class TodaysStatsCardBuilder @Inject constructor( - private val statsUtils: StatsUtils + private val statsUtils: StatsUtils, + private val appLogWrapper: AppLogWrapper ) { fun build(params: TodaysStatsCardBuilderParams) = params.todaysStatsCard?.let { - TodaysStatsCard( - views = statToUiString(it.views), - visitors = statToUiString(it.visitors), - likes = statToUiString(it.likes) - ) + val error = it.error + if (error != null) { + handleError(error) + } else { + createTodaysStatsCardWithData(it) + } } + private fun handleError(error: TodaysStatsCardError): TodaysStatsCard.Error? { + error.message?.let { appLogWrapper.e(AppLog.T.MY_SITE_DASHBOARD, "Today's Stats Error: $it") } + return if (shouldShowError(error)) createTodaysStatsCardWithError() else null + } + + private fun createTodaysStatsCardWithError() = TodaysStatsCard.Error( + title = UiStringRes(R.string.my_site_todays_stat_card_title) + ) + + private fun createTodaysStatsCardWithData(model: TodaysStatsCardModel) = TodaysStatsCardWithData( + views = statToUiString(model.views), + visitors = statToUiString(model.visitors), + likes = statToUiString(model.likes) + ) + + private fun shouldShowError(error: TodaysStatsCardError) = error.type == TodaysStatsCardErrorType.GENERIC_ERROR + private fun statToUiString(stat: Int) = UiStringText(statsUtils.toFormattedString(stat)) } diff --git a/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/todaysstats/TodaysStatsCardViewHolder.kt b/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/todaysstats/TodaysStatsCardViewHolder.kt index f498ff332712..6fe4cddccfbf 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/todaysstats/TodaysStatsCardViewHolder.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/mysite/cards/dashboard/todaysstats/TodaysStatsCardViewHolder.kt @@ -2,7 +2,7 @@ package org.wordpress.android.ui.mysite.cards.dashboard.todaysstats import android.view.ViewGroup import org.wordpress.android.databinding.MySiteTodaysStatsCardBinding -import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.TodaysStatsCard +import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.TodaysStatsCard.TodaysStatsCardWithData import org.wordpress.android.ui.mysite.cards.dashboard.CardViewHolder import org.wordpress.android.ui.utils.UiHelpers import org.wordpress.android.util.viewBinding @@ -13,7 +13,7 @@ class TodaysStatsCardViewHolder( ) : CardViewHolder( parent.viewBinding(MySiteTodaysStatsCardBinding::inflate) ) { - fun bind(card: TodaysStatsCard) = with(binding) { + fun bind(card: TodaysStatsCardWithData) = with(binding) { uiHelpers.setTextOrHide(viewsCount, card.views) uiHelpers.setTextOrHide(visitorsCount, card.visitors) uiHelpers.setTextOrHide(likesCount, card.likes) diff --git a/WordPress/src/main/res/layout/my_site_error_within_card.xml b/WordPress/src/main/res/layout/my_site_error_within_card.xml new file mode 100644 index 000000000000..1cfce291da8e --- /dev/null +++ b/WordPress/src/main/res/layout/my_site_error_within_card.xml @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + diff --git a/WordPress/src/main/res/values/strings.xml b/WordPress/src/main/res/values/strings.xml index cef95fe0bf27..6cb6dfd486f2 100644 --- a/WordPress/src/main/res/values/strings.xml +++ b/WordPress/src/main/res/values/strings.xml @@ -2237,6 +2237,7 @@ Some data hasn\'t loaded We\'re having trouble loading your site\'s data at the moment. + Check your internet connection and refresh the page. Choose site diff --git a/WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/dashboard/CardsBuilderTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/dashboard/CardsBuilderTest.kt index a1699b613b3f..116ddd0b0ac1 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/dashboard/CardsBuilderTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/dashboard/CardsBuilderTest.kt @@ -16,7 +16,7 @@ import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.Das import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.PostCard import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.PostCard.FooterLink import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.PostCard.PostCardWithPostItems -import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.TodaysStatsCard +import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.TodaysStatsCard.TodaysStatsCardWithData import org.wordpress.android.ui.mysite.MySiteCardAndItemBuilderParams.DashboardCardsBuilderParams import org.wordpress.android.ui.mysite.MySiteCardAndItemBuilderParams.PostCardBuilderParams import org.wordpress.android.ui.mysite.MySiteCardAndItemBuilderParams.TodaysStatsCardBuilderParams @@ -82,13 +82,14 @@ class CardsBuilderTest : BaseUnitTest() { assertThat(cards.findErrorCard()).isNotNull } - private fun DashboardCards.findTodaysStatsCard() = this.cards.find { it is TodaysStatsCard } as? TodaysStatsCard + private fun DashboardCards.findTodaysStatsCard() = + this.cards.find { it is TodaysStatsCardWithData } as? TodaysStatsCardWithData private fun DashboardCards.findPostCard() = this.cards.find { it is PostCard } as? PostCard private fun DashboardCards.findErrorCard() = this.cards.find { it is ErrorCard } as? ErrorCard - private val todaysStatsCard = mock() + private val todaysStatsCard = mock() private fun createPostCards() = listOf( PostCardWithPostItems( diff --git a/WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/dashboard/posts/PostCardBuilderTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/dashboard/posts/PostCardBuilderTest.kt index 78dd3c704b48..632eee8a56b0 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/dashboard/posts/PostCardBuilderTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/dashboard/posts/PostCardBuilderTest.kt @@ -10,11 +10,15 @@ import org.wordpress.android.BaseUnitTest import org.wordpress.android.R import org.wordpress.android.fluxc.model.dashboard.CardModel.PostsCardModel import org.wordpress.android.fluxc.model.dashboard.CardModel.PostsCardModel.PostCardModel +import org.wordpress.android.fluxc.store.dashboard.CardsStore.PostCardError +import org.wordpress.android.fluxc.store.dashboard.CardsStore.PostCardErrorType +import org.wordpress.android.fluxc.utils.AppLogWrapper import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.PostCard import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.PostCard.FooterLink import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.PostCard.PostCardWithPostItems import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.PostCard.PostCardWithoutPostItems import org.wordpress.android.ui.mysite.MySiteCardAndItem.DashboardCardType +import org.wordpress.android.ui.mysite.MySiteCardAndItem.DashboardCardType.POST_CARD_ERROR import org.wordpress.android.ui.mysite.MySiteCardAndItemBuilderParams.PostCardBuilderParams import org.wordpress.android.ui.mysite.MySiteCardAndItemBuilderParams.PostCardBuilderParams.PostItemClickParams import org.wordpress.android.ui.utils.UiString.UiStringRes @@ -33,6 +37,7 @@ private val POST_DATE = SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse("2021-12-0 @InternalCoroutinesApi class PostCardBuilderTest : BaseUnitTest() { @Mock private lateinit var localeManagerWrapper: LocaleManagerWrapper + @Mock private lateinit var appLogWrapper: AppLogWrapper private lateinit var builder: PostCardBuilder private val post = PostCardModel( @@ -48,7 +53,7 @@ class PostCardBuilderTest : BaseUnitTest() { @Before fun setUp() { - builder = PostCardBuilder(localeManagerWrapper) + builder = PostCardBuilder(localeManagerWrapper, appLogWrapper) setUpMocks() } @@ -56,6 +61,26 @@ class PostCardBuilderTest : BaseUnitTest() { whenever(localeManagerWrapper.getLocale()).thenReturn(Locale.US) } + /* POST CARD ERROR */ + + @Test + fun `given post unauth error, when card is built, then posts card not exist`() { + val posts = PostsCardModel(error = PostCardError(PostCardErrorType.UNAUTHORIZED)) + + val postsCard = buildPostsCard(posts) + + assertThat(postsCard).isEmpty() + } + + @Test + fun `given post generic error, when card is built, then error card exists`() { + val posts = PostsCardModel(error = PostCardError(PostCardErrorType.GENERIC_ERROR)) + + val postsCard = buildPostsCard(posts) + + assertThat(postsCard.filterPostErrorCard()).isInstanceOf(PostCard.Error::class.java) + } + /* CREATE FIRST POST CARD */ @Test @@ -321,6 +346,8 @@ class PostCardBuilderTest : BaseUnitTest() { assertThat((postsCard.filterScheduledPostCard())?.postItems?.first()?.isTimeIconVisible).isTrue } + private fun List.filterPostErrorCard() = firstOrNull { it.dashboardCardType == POST_CARD_ERROR } + @Suppress("UNCHECKED_CAST") private fun List.filterCreateFirstPostCard() = ( filter { diff --git a/WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/dashboard/todaysstats/TodaysStatsCardBuilderTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/dashboard/todaysstats/TodaysStatsCardBuilderTest.kt index 5cd3ab66a7a9..de9a6333b13a 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/dashboard/todaysstats/TodaysStatsCardBuilderTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/mysite/cards/dashboard/todaysstats/TodaysStatsCardBuilderTest.kt @@ -9,7 +9,11 @@ import org.mockito.Mock import org.mockito.junit.MockitoJUnitRunner import org.wordpress.android.BaseUnitTest import org.wordpress.android.fluxc.model.dashboard.CardModel.TodaysStatsCardModel +import org.wordpress.android.fluxc.store.dashboard.CardsStore.TodaysStatsCardError +import org.wordpress.android.fluxc.store.dashboard.CardsStore.TodaysStatsCardErrorType +import org.wordpress.android.fluxc.utils.AppLogWrapper import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.TodaysStatsCard +import org.wordpress.android.ui.mysite.MySiteCardAndItem.Card.DashboardCards.DashboardCard.TodaysStatsCard.TodaysStatsCardWithData import org.wordpress.android.ui.mysite.MySiteCardAndItemBuilderParams.TodaysStatsCardBuilderParams import org.wordpress.android.ui.stats.refresh.utils.StatsUtils import org.wordpress.android.ui.utils.UiString.UiStringText @@ -26,6 +30,7 @@ private const val TODAYS_STATS_LIKES_FORMATTED_STRING = "100" @RunWith(MockitoJUnitRunner::class) class TodaysStatsCardBuilderTest : BaseUnitTest() { @Mock private lateinit var statsUtils: StatsUtils + @Mock private lateinit var appLogWrapper: AppLogWrapper private lateinit var builder: TodaysStatsCardBuilder private val todaysStatsCardModel = TodaysStatsCardModel( @@ -37,7 +42,7 @@ class TodaysStatsCardBuilderTest : BaseUnitTest() { @Before fun setUp() { - builder = TodaysStatsCardBuilder(statsUtils) + builder = TodaysStatsCardBuilder(statsUtils, appLogWrapper) setUpMocks() } @@ -47,6 +52,52 @@ class TodaysStatsCardBuilderTest : BaseUnitTest() { whenever(statsUtils.toFormattedString(TODAYS_STATS_LIKES)).thenReturn(TODAYS_STATS_LIKES_FORMATTED_STRING) } + /* TODAY'S STATS CARD ERROR */ + + @Test + fun `given jetpack disconnected error, when card is built, then card not exists`() { + val todaysStatsCardModel = TodaysStatsCardModel( + error = TodaysStatsCardError(TodaysStatsCardErrorType.JETPACK_DISCONNECTED) + ) + + val todaysStatsCard = buildTodaysStatsCard(todaysStatsCardModel) + + assertThat(todaysStatsCard).isNull() + } + + @Test + fun `given jetpack disabled error, when card is built, then card not exists`() { + val todaysStatsCardModel = TodaysStatsCardModel( + error = TodaysStatsCardError(TodaysStatsCardErrorType.JETPACK_DISABLED) + ) + + val todaysStatsCard = buildTodaysStatsCard(todaysStatsCardModel) + + assertThat(todaysStatsCard).isNull() + } + + @Test + fun `given today's stats unauth error, when card is built, then card not exists`() { + val todaysStatsCardModel = TodaysStatsCardModel( + error = TodaysStatsCardError(TodaysStatsCardErrorType.UNAUTHORIZED) + ) + + val todaysStatsCard = buildTodaysStatsCard(todaysStatsCardModel) + + assertThat(todaysStatsCard).isNull() + } + + @Test + fun `given today's stats generic error, when card is built, then error card exists`() { + val todaysStatsCardModel = TodaysStatsCardModel( + error = TodaysStatsCardError(TodaysStatsCardErrorType.GENERIC_ERROR) + ) + + val todaysStatsCard = buildTodaysStatsCard(todaysStatsCardModel) + + assertThat(todaysStatsCard).isInstanceOf(TodaysStatsCard.Error::class.java) + } + @Test fun `given no todays stats, when card is built, then return null`() { val statCard = buildTodaysStatsCard(null) @@ -72,7 +123,7 @@ class TodaysStatsCardBuilderTest : BaseUnitTest() { TodaysStatsCardBuilderParams(todaysStatsCardModel) ) - private val todaysStatsCard = TodaysStatsCard( + private val todaysStatsCard = TodaysStatsCardWithData( UiStringText(TODAYS_STATS_VIEWS_FORMATTED_STRING), UiStringText(TODAYS_STATS_VISITORS_FORMATTED_STRING), UiStringText(TODAYS_STATS_LIKES_FORMATTED_STRING) diff --git a/build.gradle b/build.gradle index 50cd11c02ae8..827295852698 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,6 @@ ext { coroutinesVersion = '1.5.2' - wordPressUtilsVersion = '2.3.0' + wordPressUtilsVersion = 'trunk-9fc3b0acf0cfa44b743f3c5f8a65279c80cc00a3' wordPressLoginVersion = 'trunk-3f8bfa7e2c5af8daa9ae1cfc391cd6c5fa0ca4d5' gutenbergMobileVersion = 'v1.71.1' storiesVersion = '1.2.1' @@ -14,7 +14,7 @@ ext { androidxWorkVersion = "2.4.0" daggerVersion = '2.29.1' - fluxCVersion = 'trunk-3159174b9758e16d347be10588f1928a14a1f93b' + fluxCVersion = 'trunk-71f2665a3974bec7e3a1ffe1437186d31e770145' appCompatVersion = '1.0.2' coreVersion = '1.3.2'