From bb577fd838352699163c5b8e6024af5cebf9b736 Mon Sep 17 00:00:00 2001 From: Irfan Omur Date: Thu, 14 Mar 2024 20:41:03 +0300 Subject: [PATCH] Calculate visible bar count for "By Month" granularity --- .../traffic/TrafficOverviewUseCase.kt | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/sections/traffic/TrafficOverviewUseCase.kt b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/sections/traffic/TrafficOverviewUseCase.kt index 5db233f07bea..00395defc845 100644 --- a/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/sections/traffic/TrafficOverviewUseCase.kt +++ b/WordPress/src/main/java/org/wordpress/android/ui/stats/refresh/lists/sections/traffic/TrafficOverviewUseCase.kt @@ -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 @@ -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, @@ -215,6 +220,31 @@ class TrafficOverviewUseCase( return items } + private fun getBarCount(dates: List, 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