Skip to content

Commit 10c5667

Browse files
committed
refactor for constants and cleanup of unused variable.
1 parent 138933f commit 10c5667

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

WordPress/Classes/ViewRelated/Stats/Insights/SiteStatsInsightsViewModel.swift

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,10 @@ class SiteStatsInsightsViewModel: Observable {
4646
guard let mostRecentChartData = mostRecentChartData else {
4747
return
4848
}
49-
50-
currentEntryIndex = mostRecentChartData.summaryData.lastIndex(where: { $0.periodStartDate <= self.lastRequestedDate })
51-
?? max(mostRecentChartData.summaryData.count - 1, 0)
5249
}
5350
}
5451
}
5552

56-
private var currentEntryIndex: Int = 0
57-
5853
// MARK: - Constructor
5954

6055
init(insightsToShow: [InsightType],
@@ -437,6 +432,7 @@ private extension SiteStatsInsightsViewModel {
437432
segmentData: viewsData.count,
438433
segmentPrevData: viewsData.prevCount,
439434
difference: viewsData.difference,
435+
differenceText: viewsData.difference < 0 ? Constants.viewsLower : Constants.viewsHigher,
440436
date: periodDate,
441437
period: period,
442438
analyticsStat: .statsOverviewTypeTappedViews,
@@ -448,6 +444,7 @@ private extension SiteStatsInsightsViewModel {
448444
segmentData: visitorsData.count,
449445
segmentPrevData: visitorsData.prevCount,
450446
difference: visitorsData.difference,
447+
differenceText: visitorsData.difference < 0 ? Constants.visitorsLower : Constants.visitorsHigher,
451448
date: periodDate,
452449
period: period,
453450
analyticsStat: .statsOverviewTypeTappedViews,
@@ -845,15 +842,15 @@ extension SiteStatsInsightsViewModel: AsyncBlocksLoadable {
845842
public static func splitStatsSummaryTimeIntervalData(_ statsSummaryTimeIntervalData: StatsSummaryTimeIntervalData) ->
846843
[StatsSummaryTimeIntervalDataAsAWeek] {
847844
switch statsSummaryTimeIntervalData.summaryData.count {
848-
case let count where count == 14:
845+
case let count where count == Constants.fourteenDays:
849846
// normal case api returns 14 rows
850-
let summaryData = statsSummaryTimeIntervalData.summaryData[0..<14]
847+
let summaryData = statsSummaryTimeIntervalData.summaryData[0..<Constants.fourteenDays]
851848
return createStatsSummaryTimeIntervalDataAsAWeeks(summaryData: Array(summaryData))
852-
case let count where count > 14:
849+
case let count where count > Constants.fourteenDays:
853850
// when more than 14 rows we take the last 14 rows for most recent data
854-
let summaryData = statsSummaryTimeIntervalData.summaryData[count-14..<count]
851+
let summaryData = statsSummaryTimeIntervalData.summaryData[count-Constants.fourteenDays..<count]
855852
return createStatsSummaryTimeIntervalDataAsAWeeks(summaryData: Array(summaryData))
856-
case let count where count < 14:
853+
case let count where count < Constants.fourteenDays:
857854
// when 0 to 14 rows presume the user could be new / doesn't have enough data. Pad 0's to prev week
858855
var summaryData = statsSummaryTimeIntervalData.summaryData
859856
summaryData.reverse()
@@ -862,7 +859,7 @@ extension SiteStatsInsightsViewModel: AsyncBlocksLoadable {
862859
return []
863860
}
864861

865-
while summaryData.count < 14 {
862+
while summaryData.count < Constants.fourteenDays {
866863
if let newPeriodStartDate = Calendar.autoupdatingCurrent.date(byAdding: .day, value: -1, to: date) {
867864
date = newPeriodStartDate
868865
summaryData.append(StatsSummaryData(period: .day,
@@ -889,12 +886,20 @@ extension SiteStatsInsightsViewModel: AsyncBlocksLoadable {
889886
summaryData: Array(prevWeekData))
890887

891888

892-
let thisWeekData = summaryData[half ..< 14]
889+
let thisWeekData = summaryData[half ..< Constants.fourteenDays]
893890
let thisWeekTimeIntervalData = StatsSummaryTimeIntervalData(period: .day,
894891
periodEndDate: thisWeekData.last!.periodStartDate,
895892
summaryData: Array(thisWeekData))
896893

897894
return [StatsSummaryTimeIntervalDataAsAWeek.thisWeek(data: thisWeekTimeIntervalData),
898895
StatsSummaryTimeIntervalDataAsAWeek.prevWeek(data: prevWeekTimeIntervalData)]
899896
}
897+
898+
enum Constants {
899+
static let fourteenDays = 14
900+
static let viewsHigher = NSLocalizedString("Your views this week are %@ higher than the previous week.\n", comment: "Stats insights views higher than previous week")
901+
static let viewsLower = NSLocalizedString("Your views this week are %@ lower than the previous week.\n", comment: "Stats insights views lower than previous week")
902+
static let visitorsHigher = NSLocalizedString("Your visitors this week are %@ higher than the previous week.\n", comment: "Stats insights visitors higher than previous week")
903+
static let visitorsLower = NSLocalizedString("Your visitors this week are %@ lower than the previous week.\n", comment: "Stats insights visitors lower than previous week")
904+
}
900905
}

0 commit comments

Comments
 (0)