Skip to content

Commit

Permalink
Show Today Card on Traffic tab
Browse files Browse the repository at this point in the history
Adds Today Card for default By day option
  • Loading branch information
ravishanker committed Jan 30, 2024
1 parent ff72121 commit c144d63
Showing 1 changed file with 127 additions and 3 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
)
}
}

0 comments on commit c144d63

Please sign in to comment.