Skip to content

Commit

Permalink
Merge pull request #20475 from wordpress-mobile/fix/bar-count-issue-w…
Browse files Browse the repository at this point in the history
…ith-traffic-chart

Fix bar count issue with traffic chart
  • Loading branch information
ravishanker authored Mar 14, 2024
2 parents b3c7fcf + bb577fd commit 22edd92
Showing 1 changed file with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import org.wordpress.android.ui.stats.refresh.utils.trackWithGranularity
import org.wordpress.android.util.AppLog
import org.wordpress.android.util.analytics.AnalyticsTrackerWrapper
import org.wordpress.android.viewmodel.ResourceProvider
import java.util.Calendar
import java.util.Date
import javax.inject.Inject
import javax.inject.Named
Expand Down Expand Up @@ -58,9 +59,9 @@ class TrafficOverviewUseCase(
else -> statsGranularity
}

private val barCount = when (lowerGranularity) {
private val itemsToLoad = when (lowerGranularity) {
StatsGranularity.DAYS -> 7
StatsGranularity.WEEKS -> 5
StatsGranularity.WEEKS -> 6
StatsGranularity.MONTHS -> 12
else -> OVERVIEW_ITEMS_TO_LOAD
}
Expand Down Expand Up @@ -122,7 +123,7 @@ class TrafficOverviewUseCase(
// Fetch lower granularity model for chart values
val lowerGranularityResponse = if (statsGranularity != StatsGranularity.DAYS) {
val selectedDate = getLastDate(model)
selectedDate?.let { fetchVisit(lowerGranularity, OVERVIEW_ITEMS_TO_LOAD, forced, it) }
selectedDate?.let { fetchVisit(lowerGranularity, itemsToLoad, forced, it) }
} else {
null
}
Expand Down Expand Up @@ -201,6 +202,10 @@ class TrafficOverviewUseCase(
if (statsGranularity == StatsGranularity.DAYS) {
buildTodayCard(selectedItem, items)
} else {
val lowerGranularityDates = domainModel.lowerGranularityDates.map {
statsDateFormatter.parseStatsDate(lowerGranularity, it.period)
}
val barCount = getBarCount(lowerGranularityDates, selectedDate)
buildGranularChart(
domainModel.lowerGranularityDates.takeLast(barCount),
uiState,
Expand All @@ -215,6 +220,31 @@ class TrafficOverviewUseCase(
return items
}

private fun getBarCount(dates: List<Date>, selectedDate: Date) = if (statsGranularity == StatsGranularity.MONTHS) {
val selectedCalendar = Calendar.getInstance().apply { time = selectedDate }
val selectedYear = selectedCalendar.get(Calendar.YEAR)
val selectedMonth = selectedCalendar.get(Calendar.MONTH)

// Count the weeks that are in the selected month.
dates.count {
val weekEndCalendar = Calendar.getInstance().apply { time = it }
val weekEndYear = weekEndCalendar.get(Calendar.YEAR)
val weekEndMonth = weekEndCalendar.get(Calendar.MONTH)

val weekStartCalendar = Calendar.getInstance().apply {
time = it
add(Calendar.DAY_OF_YEAR, @Suppress("MagicNumber") -6)
}
val weekStartYear = weekStartCalendar.get(Calendar.YEAR)
val weekStartMonth = weekStartCalendar.get(Calendar.MONTH)

(weekEndYear == selectedYear && weekEndMonth == selectedMonth) ||
(weekStartYear == selectedYear && weekStartMonth == selectedMonth)
}
} else {
itemsToLoad
}

private fun buildTodayCard(
selectedItem: VisitsAndViewsModel.PeriodData?,
items: MutableList<BlockListItem>
Expand Down

0 comments on commit 22edd92

Please sign in to comment.