Skip to content

Commit e961d83

Browse files
authored
[Stats Revamp]: Rebuild Views & Visitors card when selected segment changes (#19818)
* Notify SiteStatsInsightsDelegate when Views and Visitors segment changes * Save views and visitors segment in the memory after segment is changed * Update Views and Visitors selectedSegment and refreshTableView when segment changed Changing Views and Visitors segment changes content inside a cell that requires cell to be rebuilt to be displayed properly * Update SiteStatsImmuTableRowsTests.swift
1 parent 6298d3b commit e961d83

File tree

9 files changed

+58
-14
lines changed

9 files changed

+58
-14
lines changed

WordPress/Classes/ViewRelated/Stats/Helpers/SiteStatsImmuTableRows.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class SiteStatsImmuTableRows {
88
/// Helper method to create the rows for the Views and Visitors section
99
///
1010
static func viewVisitorsImmuTableRows(_ statsSummaryTimeIntervalData: StatsSummaryTimeIntervalData?,
11+
selectedSegment: StatsSegmentedControlData.Segment,
1112
periodDate: Date,
1213
periodEndDate: Date? = nil,
1314
statsLineChartViewDelegate: StatsLineChartViewDelegate?,
@@ -62,12 +63,13 @@ class SiteStatsImmuTableRows {
6263
}
6364

6465
let row = ViewsVisitorsRow(
65-
segmentsData: [viewsSegmentData, visitorsSegmentData],
66-
chartData: lineChartData,
67-
chartStyling: lineChartStyling,
68-
period: StatsPeriodUnit.day,
69-
statsLineChartViewDelegate: statsLineChartViewDelegate,
70-
siteStatsInsightsDelegate: siteStatsInsightsDelegate, xAxisDates: xAxisDates
66+
segmentsData: [viewsSegmentData, visitorsSegmentData],
67+
selectedSegment: selectedSegment,
68+
chartData: lineChartData,
69+
chartStyling: lineChartStyling,
70+
period: StatsPeriodUnit.day,
71+
statsLineChartViewDelegate: statsLineChartViewDelegate,
72+
siteStatsInsightsDelegate: siteStatsInsightsDelegate, xAxisDates: xAxisDates
7173
)
7274
tableRows.append(row)
7375
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ import Foundation
1919
@objc optional func addInsightSelected(_ insight: StatSection)
2020
@objc optional func addInsightDismissed()
2121
@objc optional func manageInsightSelected(_ insight: StatSection, fromButton: UIButton)
22+
@objc optional func viewsAndVisitorsSegmendChanged(to selectedSegmentIndex: Int)
2223
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,13 @@ extension SiteStatsInsightsTableViewController: SiteStatsInsightsDelegate {
607607
alert.popoverPresentationController?.sourceView = fromButton
608608
present(alert, animated: true)
609609
}
610+
611+
func viewsAndVisitorsSegmendChanged(to selectedSegmentIndex: Int) {
612+
if let selectedSegment = StatsSegmentedControlData.Segment(rawValue: selectedSegmentIndex) {
613+
viewModel?.updateViewsAndVisitorsSegment(selectedSegment)
614+
refreshTableView()
615+
}
616+
}
610617
}
611618

612619
// MARK: - StatsInsightsManagementDelegate

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class SiteStatsInsightsViewModel: Observable {
4242

4343
private var mostRecentChartData: StatsSummaryTimeIntervalData?
4444

45+
private var selectedViewsVisitorsSegment: StatsSegmentedControlData.Segment = .views
46+
4547
// MARK: - Constructor
4648

4749
init(insightsToShow: [InsightType],
@@ -399,6 +401,10 @@ class SiteStatsInsightsViewModel: Observable {
399401
pinnedItemStore?.markPinnedItemAsHidden(item)
400402
}
401403

404+
func updateViewsAndVisitorsSegment(_ selectedSegment: StatsSegmentedControlData.Segment) {
405+
selectedViewsVisitorsSegment = selectedSegment
406+
}
407+
402408
static func intervalData(_ statsSummaryTimeIntervalData: StatsSummaryTimeIntervalData?, summaryType: StatsSummaryType, periodEndDate: Date? = nil) -> (count: Int, prevCount: Int, difference: Int, percentage: Int) {
403409
guard let statsSummaryTimeIntervalData = statsSummaryTimeIntervalData else {
404410
return (0, 0, 0, 0)
@@ -510,8 +516,11 @@ private extension SiteStatsInsightsViewModel {
510516

511517
let periodDate = self.lastRequestedDate
512518

513-
return SiteStatsImmuTableRows.viewVisitorsImmuTableRows(mostRecentChartData, periodDate: periodDate,
514-
statsLineChartViewDelegate: statsLineChartViewDelegate, siteStatsInsightsDelegate: siteStatsInsightsDelegate)
519+
return SiteStatsImmuTableRows.viewVisitorsImmuTableRows(mostRecentChartData,
520+
selectedSegment: selectedViewsVisitorsSegment,
521+
periodDate: periodDate,
522+
statsLineChartViewDelegate: statsLineChartViewDelegate,
523+
siteStatsInsightsDelegate: siteStatsInsightsDelegate)
515524
}
516525

517526
func createAllTimeStatsRows() -> [StatsTwoColumnRowData] {

WordPress/Classes/ViewRelated/Stats/Insights/ViewsVisitors/ViewsVisitorsLineChartCell.swift

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ struct StatsSegmentedControlData {
1212
var period: StatsPeriodUnit?
1313
var analyticsStat: WPAnalyticsStat?
1414

15+
enum Segment: Int {
16+
case views
17+
case visitors
18+
}
19+
1520
private(set) var accessibilityHint: String?
1621

1722
init(segmentTitle: String, segmentData: Int, segmentPrevData: Int, difference: Int, differenceText: String, segmentDataStub: String? = nil, date: Date? = nil, period: StatsPeriodUnit? = nil, analyticsStat: WPAnalyticsStat? = nil, accessibilityHint: String? = nil, differencePercent: Int) {
@@ -162,6 +167,7 @@ class ViewsVisitorsLineChartCell: StatsBaseCell, NibLoadable {
162167
}
163168

164169
func configure(segmentsData: [StatsSegmentedControlData],
170+
selectedSegment: StatsSegmentedControlData.Segment,
165171
lineChartData: [LineChartDataConvertible] = [],
166172
lineChartStyling: [LineChartStyling] = [],
167173
period: StatsPeriodUnit? = nil,
@@ -180,7 +186,7 @@ class ViewsVisitorsLineChartCell: StatsBaseCell, NibLoadable {
180186
self.period = period
181187
self.xAxisDates = xAxisDates
182188

183-
setupSegmentedControl()
189+
setupSegmentedControl(selectedSegment: selectedSegment)
184190
configureChartView()
185191
updateLabels()
186192
}
@@ -191,8 +197,9 @@ class ViewsVisitorsLineChartCell: StatsBaseCell, NibLoadable {
191197

192198
configureChartView()
193199
updateLabels()
194-
}
195200

201+
siteStatsInsightsDelegate?.viewsAndVisitorsSegmendChanged?(to: selectedSegmentIndex)
202+
}
196203
}
197204

198205

@@ -205,12 +212,13 @@ private extension ViewsVisitorsLineChartCell {
205212
styleLabels()
206213
}
207214

208-
func setupSegmentedControl() {
215+
func setupSegmentedControl(selectedSegment: StatsSegmentedControlData.Segment) {
209216
segmentedControl.selectedSegmentTintColor = UIColor.white
210217
segmentedControl.setTitleTextAttributes([.font: UIFont.preferredFont(forTextStyle: .subheadline).bold()], for: .normal)
211218
segmentedControl.setTitleTextAttributes([.foregroundColor: UIColor.black], for: .selected)
212219
segmentedControl.setTitle(segmentsData[0].segmentTitle, forSegmentAt: 0)
213220
segmentedControl.setTitle(segmentsData[1].segmentTitle, forSegmentAt: 1)
221+
segmentedControl.selectedSegmentIndex = selectedSegment.rawValue
214222
}
215223

216224
func styleLabels() {

WordPress/Classes/ViewRelated/Stats/Shared Views/Stats Detail/SiteStatsInsightsDetailsTableViewController.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,13 @@ extension SiteStatsInsightsDetailsTableViewController: SiteStatsInsightsDelegate
334334
selectedPeriod: .week)
335335
navigationController?.pushViewController(detailTableViewController, animated: true)
336336
}
337+
338+
func viewsAndVisitorsSegmendChanged(to selectedSegmentIndex: Int) {
339+
if let selectedSegment = StatsSegmentedControlData.Segment(rawValue: selectedSegmentIndex) {
340+
viewModel?.updateViewsAndVisitorsSegment(selectedSegment)
341+
refreshTableView()
342+
}
343+
}
337344
}
338345

339346
// MARK: - SiteStatsReferrerDelegate

WordPress/Classes/ViewRelated/Stats/Shared Views/Stats Detail/SiteStatsInsightsDetailsViewModel.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ class SiteStatsInsightsDetailsViewModel: Observable {
3535

3636
private var allAnnualInsights = [StatsAnnualInsight]()
3737

38+
private var selectedViewsVisitorsSegment: StatsSegmentedControlData.Segment = .views
39+
3840
// MARK: - Init
3941

4042
init(insightsDetailsDelegate: SiteStatsInsightsDelegate,
@@ -256,10 +258,11 @@ class SiteStatsInsightsDetailsViewModel: Observable {
256258
// Views Visitors
257259
let weekEnd = futureEndOfWeekDate(for: periodSummary)
258260
rows.append(contentsOf: SiteStatsImmuTableRows.viewVisitorsImmuTableRows(periodSummary,
261+
selectedSegment: selectedViewsVisitorsSegment,
259262
periodDate: selectedDate!,
260263
periodEndDate: weekEnd,
261264
statsLineChartViewDelegate: nil,
262-
siteStatsInsightsDelegate: nil))
265+
siteStatsInsightsDelegate: insightsDetailsDelegate))
263266

264267
// Referrers
265268
if let referrers = viewsAndVisitorsData.topReferrers {
@@ -625,6 +628,12 @@ class SiteStatsInsightsDetailsViewModel: Observable {
625628

626629
ActionDispatcher.dispatch(PeriodAction.refreshPostStats(postID: postID))
627630
}
631+
632+
// MARK: - Views & Visitors
633+
634+
func updateViewsAndVisitorsSegment(_ selectedSegment: StatsSegmentedControlData.Segment) {
635+
selectedViewsVisitorsSegment = selectedSegment
636+
}
628637
}
629638

630639
// MARK: - Private Extension

WordPress/Classes/ViewRelated/Stats/SiteStatsTableViewCells.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ struct ViewsVisitorsRow: ImmuTableRow {
3838
}()
3939

4040
let segmentsData: [StatsSegmentedControlData]
41+
let selectedSegment: StatsSegmentedControlData.Segment
4142
let action: ImmuTableAction? = nil
4243
let chartData: [LineChartDataConvertible]
4344
let chartStyling: [LineChartStyling]
@@ -52,7 +53,7 @@ struct ViewsVisitorsRow: ImmuTableRow {
5253
return
5354
}
5455

55-
cell.configure(segmentsData: segmentsData, lineChartData: chartData, lineChartStyling: chartStyling, period: period, statsLineChartViewDelegate: statsLineChartViewDelegate, xAxisDates: xAxisDates, delegate: siteStatsInsightsDelegate)
56+
cell.configure(segmentsData: segmentsData, selectedSegment: selectedSegment, lineChartData: chartData, lineChartStyling: chartStyling, period: period, statsLineChartViewDelegate: statsLineChartViewDelegate, xAxisDates: xAxisDates, delegate: siteStatsInsightsDelegate)
5657
}
5758
}
5859

WordPress/WordPressTest/ViewRelated/Stats/Helpers/SiteStatsImmuTableRowsTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class SiteStatsImmuTableRowsTests: XCTestCase {
1515
let date = Calendar.autoupdatingCurrent.date(from: feb21)!
1616

1717
// When creating rows from statsSummaryTimeIntervalData
18-
let rows = SiteStatsImmuTableRows.viewVisitorsImmuTableRows(statsSummaryTimeIntervalData, periodDate: date, statsLineChartViewDelegate: nil, siteStatsInsightsDelegate: nil)
18+
let rows = SiteStatsImmuTableRows.viewVisitorsImmuTableRows(statsSummaryTimeIntervalData, selectedSegment: .views, periodDate: date, statsLineChartViewDelegate: nil, siteStatsInsightsDelegate: nil)
1919

2020
// Then
2121
XCTAssertTrue(rows.count == 1)

0 commit comments

Comments
 (0)