@@ -26,10 +26,15 @@ private const val HOURLY_QUANTITY = 24
2626private const val DAILY_QUANTITY = 1
2727private const val WEEKLY_QUANTITY = 7
2828private const val DAYS_BEFORE_END_DATE = - 6
29- private const val DAYS_IN_30_DAYS = 30
3029private const val DAYS_IN_7_DAYS = 7
30+ private const val DAYS_IN_30_DAYS = 30
31+ private const val DAYS_IN_6_MONTHS = 182
32+ private const val DAYS_IN_12_MONTHS = 365
3133private const val MONTHS_IN_6_MONTHS = 6
3234private const val MONTHS_IN_12_MONTHS = 12
35+ private const val PERCENTAGE_MULTIPLIER = 100.0
36+ private const val PERCENTAGE_NO_CHANGE = 0.0
37+ private const val NUM_DAYS_TODAY = 1
3338
3439/* *
3540 * Repository for fetching stats data using the wordpress-rs API.
@@ -533,8 +538,8 @@ class StatsRepository @Inject constructor(
533538 val previousTotalViews = previousItemsMap.values.sumOf { it.views }
534539 val totalChange = totalViews - previousTotalViews
535540 val totalChangePercent = if (previousTotalViews > 0 ) {
536- (totalChange.toDouble() / previousTotalViews.toDouble()) * 100.0
537- } else if (totalViews > 0 ) 100.0 else 0.0
541+ (totalChange.toDouble() / previousTotalViews.toDouble()) * PERCENTAGE_MULTIPLIER
542+ } else if (totalViews > 0 ) PERCENTAGE_MULTIPLIER else PERCENTAGE_NO_CHANGE
538543
539544 MostViewedResult .Success (
540545 items = currentResult.items.mapIndexed { index, item ->
@@ -580,8 +585,8 @@ class StatsRepository @Inject constructor(
580585 val previousTotalViews = previousItemsMap.values.sumOf { it.views }
581586 val totalChange = totalViews - previousTotalViews
582587 val totalChangePercent = if (previousTotalViews > 0 ) {
583- (totalChange.toDouble() / previousTotalViews.toDouble()) * 100.0
584- } else if (totalViews > 0 ) 100.0 else 0.0
588+ (totalChange.toDouble() / previousTotalViews.toDouble()) * PERCENTAGE_MULTIPLIER
589+ } else if (totalViews > 0 ) PERCENTAGE_MULTIPLIER else PERCENTAGE_NO_CHANGE
585590
586591 MostViewedResult .Success (
587592 items = currentResult.items.mapIndexed { index, item ->
@@ -611,29 +616,29 @@ class StatsRepository @Inject constructor(
611616
612617 return when (period) {
613618 is StatsPeriod .Today -> {
614- val yesterdayString = today.minusDays(1 ).format(dateFormatter)
615- StatsDateRange .Preset (num = 1 , date = todayString) to
616- StatsDateRange .Preset (num = 1 , date = yesterdayString)
619+ val yesterdayString = today.minusDays(NUM_DAYS_TODAY .toLong() ).format(dateFormatter)
620+ StatsDateRange .Preset (num = NUM_DAYS_TODAY , date = todayString) to
621+ StatsDateRange .Preset (num = NUM_DAYS_TODAY , date = yesterdayString)
617622 }
618623 is StatsPeriod .Last7Days -> {
619- val previousEndString = today.minusDays(7 ).format(dateFormatter)
620- StatsDateRange .Preset (num = 7 , date = todayString) to
621- StatsDateRange .Preset (num = 7 , date = previousEndString)
624+ val previousEndString = today.minusDays(DAYS_IN_7_DAYS .toLong() ).format(dateFormatter)
625+ StatsDateRange .Preset (num = DAYS_IN_7_DAYS , date = todayString) to
626+ StatsDateRange .Preset (num = DAYS_IN_7_DAYS , date = previousEndString)
622627 }
623628 is StatsPeriod .Last30Days -> {
624- val previousEndString = today.minusDays(30 ).format(dateFormatter)
625- StatsDateRange .Preset (num = 30 , date = todayString) to
626- StatsDateRange .Preset (num = 30 , date = previousEndString)
629+ val previousEndString = today.minusDays(DAYS_IN_30_DAYS .toLong() ).format(dateFormatter)
630+ StatsDateRange .Preset (num = DAYS_IN_30_DAYS , date = todayString) to
631+ StatsDateRange .Preset (num = DAYS_IN_30_DAYS , date = previousEndString)
627632 }
628633 is StatsPeriod .Last6Months -> {
629- val previousEndString = today.minusDays(182 ).format(dateFormatter)
630- StatsDateRange .Preset (num = 182 , date = todayString) to
631- StatsDateRange .Preset (num = 182 , date = previousEndString)
634+ val previousEndString = today.minusDays(DAYS_IN_6_MONTHS .toLong() ).format(dateFormatter)
635+ StatsDateRange .Preset (num = DAYS_IN_6_MONTHS , date = todayString) to
636+ StatsDateRange .Preset (num = DAYS_IN_6_MONTHS , date = previousEndString)
632637 }
633638 is StatsPeriod .Last12Months -> {
634- val previousEndString = today.minusDays(365 ).format(dateFormatter)
635- StatsDateRange .Preset (num = 365 , date = todayString) to
636- StatsDateRange .Preset (num = 365 , date = previousEndString)
639+ val previousEndString = today.minusDays(DAYS_IN_12_MONTHS .toLong() ).format(dateFormatter)
640+ StatsDateRange .Preset (num = DAYS_IN_12_MONTHS , date = todayString) to
641+ StatsDateRange .Preset (num = DAYS_IN_12_MONTHS , date = previousEndString)
637642 }
638643 is StatsPeriod .Custom -> {
639644 val daysBetween = ChronoUnit .DAYS .between(period.startDate, period.endDate).toInt() + 1
@@ -649,26 +654,6 @@ class StatsRepository @Inject constructor(
649654 }
650655 }
651656 }
652-
653- /* *
654- * Maps a StatsPeriod to the appropriate StatsDateRange for the API.
655- */
656- private fun mapStatsPeriodToDateRange (period : StatsPeriod ): StatsDateRange {
657- val today = LocalDate .now()
658- val todayString = today.format(dateFormatter)
659-
660- return when (period) {
661- is StatsPeriod .Today -> StatsDateRange .Preset (num = 1 , date = todayString)
662- is StatsPeriod .Last7Days -> StatsDateRange .Preset (num = 7 , date = todayString)
663- is StatsPeriod .Last30Days -> StatsDateRange .Preset (num = 30 , date = todayString)
664- is StatsPeriod .Last6Months -> StatsDateRange .Preset (num = 182 , date = todayString)
665- is StatsPeriod .Last12Months -> StatsDateRange .Preset (num = 365 , date = todayString)
666- is StatsPeriod .Custom -> StatsDateRange .Custom (
667- startDate = period.startDate.format(dateFormatter),
668- date = period.endDate.format(dateFormatter)
669- )
670- }
671- }
672657}
673658
674659/* *
@@ -793,10 +778,10 @@ data class MostViewedItemData(
793778) {
794779 val viewsChange: Long get() = views - previousViews
795780 val viewsChangePercent: Double get() = if (previousViews > 0 ) {
796- (viewsChange.toDouble() / previousViews.toDouble()) * 100.0
781+ (viewsChange.toDouble() / previousViews.toDouble()) * PERCENTAGE_MULTIPLIER
797782 } else if (views > 0 ) {
798- 100.0
783+ PERCENTAGE_MULTIPLIER
799784 } else {
800- 0.0
785+ PERCENTAGE_NO_CHANGE
801786 }
802787}
0 commit comments