Skip to content

Commit

Permalink
Calculate visible bar count for "By Month" granularity
Browse files Browse the repository at this point in the history
  • Loading branch information
irfano committed Mar 14, 2024
1 parent 73b17ea commit bb577fd
Showing 1 changed file with 30 additions and 0 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 @@ -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 bb577fd

Please sign in to comment.