Skip to content

Commit

Permalink
Merge pull request #20074 from wordpress-mobile/Stats-Traffic-Today-Card
Browse files Browse the repository at this point in the history
Stats Traffic: Today card
  • Loading branch information
irfano authored Jan 30, 2024
2 parents 5de2df4 + 8842976 commit f09894e
Show file tree
Hide file tree
Showing 2 changed files with 139 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,23 @@ import org.wordpress.android.modules.BG_THREAD
import org.wordpress.android.modules.UI_THREAD
import org.wordpress.android.ui.stats.refresh.lists.sections.BaseStatsUseCase
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.QuickScanItem
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.QuickScanItem.Column
import org.wordpress.android.ui.stats.refresh.lists.sections.BlockListItem.ValueItem
import org.wordpress.android.ui.stats.refresh.lists.sections.granular.GranularUseCaseFactory
import org.wordpress.android.ui.stats.refresh.lists.sections.granular.SelectedDateProvider
import org.wordpress.android.ui.stats.refresh.lists.sections.granular.usecases.OverviewUseCase.UiState
import org.wordpress.android.ui.stats.refresh.lists.widget.WidgetUpdater.StatsWidgetUpdaters
import org.wordpress.android.ui.stats.refresh.utils.StatsDateFormatter
import org.wordpress.android.ui.stats.refresh.utils.StatsSiteProvider
import org.wordpress.android.ui.stats.refresh.utils.StatsUtils
import org.wordpress.android.ui.stats.refresh.utils.toStatsSection
import org.wordpress.android.ui.stats.refresh.utils.trackGranular
import org.wordpress.android.util.AppLog
import org.wordpress.android.util.AppLog.T
import org.wordpress.android.util.LocaleManagerWrapper
import org.wordpress.android.util.analytics.AnalyticsTrackerWrapper
import org.wordpress.android.util.config.StatsTrafficTabFeatureConfig
import org.wordpress.android.viewmodel.ResourceProvider
import java.util.Calendar
import javax.inject.Inject
Expand All @@ -48,7 +52,9 @@ class OverviewUseCase constructor(
private val analyticsTracker: AnalyticsTrackerWrapper,
private val statsWidgetUpdaters: StatsWidgetUpdaters,
private val localeManagerWrapper: LocaleManagerWrapper,
private val resourceProvider: ResourceProvider
private val resourceProvider: ResourceProvider,
private val statsUtils: StatsUtils,
private val trafficTabFeatureConfig: StatsTrafficTabFeatureConfig
) : BaseStatsUseCase<VisitsAndViewsModel, UiState>(
OVERVIEW,
mainDispatcher,
Expand Down Expand Up @@ -144,6 +150,120 @@ class OverviewUseCase constructor(
domainModel: VisitsAndViewsModel,
uiState: UiState
): List<BlockListItem> {
return if (trafficTabFeatureConfig.isEnabled()) {
buildTrafficOverview(domainModel, uiState)
} else {
buildGranularOverview(domainModel, uiState)
}
}

private fun buildTrafficOverview(domainModel: VisitsAndViewsModel, uiState: UiState): List<BlockListItem> {
val items = mutableListOf<BlockListItem>()
if (domainModel.dates.isNotEmpty()) {
val dateFromProvider = selectedDateProvider.getSelectedDate(statsGranularity)
val visibleBarCount = uiState.visibleBarCount ?: domainModel.dates.size
val availableDates = domainModel.dates.map {
statsDateFormatter.parseStatsDate(
statsGranularity,
it.period
)
}
val selectedDate = dateFromProvider ?: availableDates.last()
val index = availableDates.indexOf(selectedDate)

selectedDateProvider.selectDate(
selectedDate,
availableDates.takeLast(visibleBarCount),
statsGranularity
)
val selectedItem = domainModel.dates.getOrNull(index) ?: domainModel.dates.last()
val previousItem = domainModel.dates.getOrNull(domainModel.dates.indexOf(selectedItem) - 1)

if (statsGranularity == StatsGranularity.DAYS) {
buildTodayCard(selectedItem,items)
} else {
buildGranularChart(domainModel, uiState, items, selectedItem, previousItem)
}
} else {
selectedDateProvider.onDateLoadingFailed(statsGranularity)
AppLog.e(T.STATS, "There is no data to be shown in the overview block")
}
return items
}

private fun buildTodayCard(
selectedItem: VisitsAndViewsModel.PeriodData?,
items: MutableList<BlockListItem>
) {
val views = selectedItem?.views ?: 0
val visitors = selectedItem?.visitors ?: 0
val likes = selectedItem?.likes ?: 0
val comments = selectedItem?.comments ?: 0

items.add(BlockListItem.Title(R.string.stats_timeframe_today))
items.add(
QuickScanItem(
Column(
R.string.stats_views,
statsUtils.toFormattedString(views)
),
Column(
R.string.stats_visitors,
statsUtils.toFormattedString(visitors)
)
)
)

items.add(
QuickScanItem(
Column(
R.string.stats_likes,
statsUtils.toFormattedString(likes)
),
Column(
R.string.stats_comments,
statsUtils.toFormattedString(comments)
)
)
)
}

private fun buildGranularChart(
domainModel: VisitsAndViewsModel,
uiState: UiState,
items: MutableList<BlockListItem>,
selectedItem: VisitsAndViewsModel.PeriodData,
previousItem: VisitsAndViewsModel.PeriodData?
) {
items.add(
overviewMapper.buildTitle(
selectedItem,
previousItem,
uiState.selectedPosition,
isLast = selectedItem == domainModel.dates.last(),
statsGranularity = statsGranularity
)
)
items.addAll(
overviewMapper.buildChart(
domainModel.dates,
statsGranularity,
this::onBarSelected,
this::onBarChartDrawn,
uiState.selectedPosition,
selectedItem.period
)
)
items.add(
overviewMapper.buildColumns(
selectedItem,
this::onColumnSelected,
uiState.selectedPosition
)
)
}

private fun buildGranularOverview(domainModel: VisitsAndViewsModel, uiState: UiState): List<BlockListItem> {
val items = mutableListOf<BlockListItem>()
if (domainModel.dates.isNotEmpty()) {
val dateFromProvider = selectedDateProvider.getSelectedDate(statsGranularity)
Expand Down Expand Up @@ -237,7 +357,9 @@ class OverviewUseCase constructor(
private val analyticsTracker: AnalyticsTrackerWrapper,
private val statsWidgetUpdaters: StatsWidgetUpdaters,
private val localeManagerWrapper: LocaleManagerWrapper,
private val resourceProvider: ResourceProvider
private val resourceProvider: ResourceProvider,
private val statsUtils: StatsUtils,
private val trafficTabFeatureConfig: StatsTrafficTabFeatureConfig
) : GranularUseCaseFactory {
override fun build(granularity: StatsGranularity, useCaseMode: UseCaseMode) =
OverviewUseCase(
Expand All @@ -252,7 +374,9 @@ class OverviewUseCase constructor(
analyticsTracker,
statsWidgetUpdaters,
localeManagerWrapper,
resourceProvider
resourceProvider,
statsUtils,
trafficTabFeatureConfig
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ import org.wordpress.android.ui.stats.refresh.lists.sections.granular.SelectedDa
import org.wordpress.android.ui.stats.refresh.lists.widget.WidgetUpdater.StatsWidgetUpdaters
import org.wordpress.android.ui.stats.refresh.utils.StatsDateFormatter
import org.wordpress.android.ui.stats.refresh.utils.StatsSiteProvider
import org.wordpress.android.ui.stats.refresh.utils.StatsUtils
import org.wordpress.android.util.LocaleManagerWrapper
import org.wordpress.android.util.analytics.AnalyticsTrackerWrapper
import org.wordpress.android.util.config.StatsTrafficTabFeatureConfig
import org.wordpress.android.viewmodel.ResourceProvider
import java.util.Calendar

Expand Down Expand Up @@ -77,6 +79,13 @@ class OverviewUseCaseTest : BaseUnitTest() {

@Mock
lateinit var localeManagerWrapper: LocaleManagerWrapper

@Mock
lateinit var statsUtils: StatsUtils

@Mock
lateinit var trafficTabFeatureConfig: StatsTrafficTabFeatureConfig

private lateinit var useCase: OverviewUseCase
private val site = SiteModel()
private val siteId = 1L
Expand All @@ -100,7 +109,9 @@ class OverviewUseCaseTest : BaseUnitTest() {
analyticsTrackerWrapper,
statsWidgetUpdaters,
localeManagerWrapper,
resourceProvider
resourceProvider,
statsUtils,
trafficTabFeatureConfig
)
site.siteId = siteId
whenever(statsSiteProvider.siteModel).thenReturn(site)
Expand Down

0 comments on commit f09894e

Please sign in to comment.