Skip to content

Commit 1b8bc1c

Browse files
committed
Create StatsRowsCell with default child stack view rows and ability to configure more
1 parent 89fd816 commit 1b8bc1c

File tree

5 files changed

+101
-101
lines changed

5 files changed

+101
-101
lines changed

WordPress/Classes/ViewRelated/Stats/Extensions/UITableViewCell+Stats.swift

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -70,89 +70,4 @@ extension UITableViewCell {
7070
row.configure(statSection: statSection, delegate: delegate)
7171
rowsStackView.addArrangedSubview(row)
7272
}
73-
74-
}
75-
76-
extension UITableViewCell {
77-
struct StatsTotalRowConfiguration {
78-
let limitRowsDisplayed: Bool
79-
let rowDelegate: StatsTotalRowDelegate?
80-
let referrerDelegate: StatsTotalRowReferrerDelegate?
81-
let viewMoreDelegate: ViewMoreRowDelegate?
82-
}
83-
84-
func addDefaultTotalRows(toStackView rowsStackView: UIStackView) {
85-
for _ in 0..<StatsDataHelper.maxRowsToDisplay {
86-
let row = StatsTotalRow.loadFromNib()
87-
rowsStackView.addArrangedSubview(row)
88-
}
89-
90-
let emptyRow = StatsNoDataRow.loadFromNib()
91-
rowsStackView.addArrangedSubview(emptyRow)
92-
93-
let viewMoreRow = ViewMoreRow.loadFromNib()
94-
rowsStackView.addArrangedSubview(viewMoreRow)
95-
}
96-
97-
func configureTotalRows(_ dataRows: [StatsTotalRowData],
98-
inStackView rowsStackView: UIStackView,
99-
forType statType: StatType,
100-
configuration: StatsTotalRowConfiguration) {
101-
if rowsStackView.arrangedSubviews.isEmpty {
102-
addDefaultTotalRows(toStackView: rowsStackView)
103-
}
104-
105-
guard !dataRows.isEmpty else {
106-
configureForNoData(inStackView: rowsStackView, forType: statType)
107-
return
108-
}
109-
110-
let numberOfRowsToAdd = calculateNumberOfRowsToAdd(from: dataRows, withConfiguration: configuration)
111-
112-
rowsStackView.arrangedSubviews.enumerated().forEach { index, view in
113-
configure(view: view, at: index, in: dataRows, numberOfRowsToAdd: numberOfRowsToAdd, configuration: configuration)
114-
}
115-
}
116-
117-
private func configureForNoData(inStackView rowsStackView: UIStackView, forType statType: StatType) {
118-
rowsStackView.arrangedSubviews.forEach { view in
119-
if let emptyRow = view as? StatsNoDataRow {
120-
emptyRow.isHidden = false
121-
emptyRow.configure(forType: statType)
122-
} else {
123-
view.isHidden = true
124-
}
125-
}
126-
}
127-
128-
private func calculateNumberOfRowsToAdd(from dataRows: [StatsTotalRowData], withConfiguration configuration: StatsTotalRowConfiguration) -> Int {
129-
if configuration.limitRowsDisplayed {
130-
return min(dataRows.count, StatsDataHelper.maxRowsToDisplay)
131-
}
132-
return dataRows.count
133-
}
134-
135-
private func configure(view: UIView, at index: Int, in dataRows: [StatsTotalRowData], numberOfRowsToAdd: Int, configuration: StatsTotalRowConfiguration) {
136-
switch view {
137-
case let view as StatsNoDataRow:
138-
view.isHidden = true
139-
case let view as ViewMoreRow:
140-
configureViewMoreRow(view, at: index, in: dataRows, withConfiguration: configuration)
141-
case let view as StatsTotalRow where index < dataRows.count:
142-
view.isHidden = false
143-
let dataRow = dataRows[index]
144-
view.configure(rowData: dataRow, delegate: configuration.rowDelegate, referrerDelegate: configuration.referrerDelegate)
145-
view.showSeparator = index != (numberOfRowsToAdd - 1)
146-
default:
147-
view.isHidden = true
148-
}
149-
}
150-
151-
private func configureViewMoreRow(_ viewMoreRow: ViewMoreRow, at index: Int, in dataRows: [StatsTotalRowData], withConfiguration configuration: StatsTotalRowConfiguration) {
152-
let shouldShowViewMore = configuration.limitRowsDisplayed && dataRows.count > StatsDataHelper.maxRowsToDisplay
153-
viewMoreRow.isHidden = !shouldShowViewMore
154-
if shouldShowViewMore {
155-
viewMoreRow.configure(statSection: dataRows.first?.statSection, delegate: configuration.viewMoreDelegate)
156-
}
157-
}
15873
}

WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import UIKit
22

3-
class CountriesCell: StatsBaseCell, NibLoadable {
3+
final class CountriesCell: StatsRowsCell, NibLoadable {
44

55
// MARK: - Properties
66

77
@IBOutlet weak var topSeparatorLine: UIView!
88
@IBOutlet weak var subtitleStackView: UIStackView!
9-
@IBOutlet weak var rowsStackView: UIStackView!
109
@IBOutlet weak var itemSubtitleLabel: UILabel!
1110
@IBOutlet weak var dataSubtitleLabel: UILabel!
1211
@IBOutlet weak var bottomSeparatorLine: UIView!
@@ -21,12 +20,6 @@ class CountriesCell: StatsBaseCell, NibLoadable {
2120
private typealias Style = WPStyleGuide.Stats
2221
private var forDetails = false
2322

24-
override func awakeFromNib() {
25-
super.awakeFromNib()
26-
27-
addDefaultTotalRows(toStackView: rowsStackView)
28-
}
29-
3023
// MARK: - Configure
3124

3225
func configure(itemSubtitle: String,
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import Foundation
2+
3+
class StatsRowsCell: StatsBaseCell {
4+
struct StatsTotalRowConfiguration {
5+
let limitRowsDisplayed: Bool
6+
let rowDelegate: StatsTotalRowDelegate?
7+
let referrerDelegate: StatsTotalRowReferrerDelegate?
8+
let viewMoreDelegate: ViewMoreRowDelegate?
9+
}
10+
11+
@IBOutlet weak var rowsStackView: UIStackView!
12+
13+
override func awakeFromNib() {
14+
super.awakeFromNib()
15+
16+
addDefaultTotalRows(toStackView: rowsStackView)
17+
}
18+
19+
func configureTotalRows(_ dataRows: [StatsTotalRowData],
20+
inStackView rowsStackView: UIStackView,
21+
forType statType: StatType,
22+
configuration: StatsTotalRowConfiguration) {
23+
if rowsStackView.arrangedSubviews.isEmpty {
24+
addDefaultTotalRows(toStackView: rowsStackView)
25+
}
26+
27+
guard !dataRows.isEmpty else {
28+
configureForNoData(inStackView: rowsStackView, forType: statType)
29+
return
30+
}
31+
32+
let numberOfRowsToAdd = calculateNumberOfRowsToAdd(from: dataRows, withConfiguration: configuration)
33+
34+
rowsStackView.arrangedSubviews.enumerated().forEach { index, view in
35+
configure(view: view, at: index, in: dataRows, numberOfRowsToAdd: numberOfRowsToAdd, configuration: configuration)
36+
}
37+
}
38+
39+
private func addDefaultTotalRows(toStackView rowsStackView: UIStackView) {
40+
for _ in 0..<StatsDataHelper.maxRowsToDisplay {
41+
let row = StatsTotalRow.loadFromNib()
42+
rowsStackView.addArrangedSubview(row)
43+
}
44+
45+
let emptyRow = StatsNoDataRow.loadFromNib()
46+
rowsStackView.addArrangedSubview(emptyRow)
47+
48+
let viewMoreRow = ViewMoreRow.loadFromNib()
49+
rowsStackView.addArrangedSubview(viewMoreRow)
50+
}
51+
52+
private func configureForNoData(inStackView rowsStackView: UIStackView, forType statType: StatType) {
53+
rowsStackView.arrangedSubviews.forEach { view in
54+
if let emptyRow = view as? StatsNoDataRow {
55+
emptyRow.isHidden = false
56+
emptyRow.configure(forType: statType)
57+
} else {
58+
view.isHidden = true
59+
}
60+
}
61+
}
62+
63+
private func calculateNumberOfRowsToAdd(from dataRows: [StatsTotalRowData], withConfiguration configuration: StatsTotalRowConfiguration) -> Int {
64+
if configuration.limitRowsDisplayed {
65+
return min(dataRows.count, StatsDataHelper.maxRowsToDisplay)
66+
}
67+
return dataRows.count
68+
}
69+
70+
private func configure(view: UIView, at index: Int, in dataRows: [StatsTotalRowData], numberOfRowsToAdd: Int, configuration: StatsTotalRowConfiguration) {
71+
switch view {
72+
case let view as StatsNoDataRow:
73+
view.isHidden = true
74+
case let view as ViewMoreRow:
75+
configureViewMoreRow(view, at: index, in: dataRows, withConfiguration: configuration)
76+
case let view as StatsTotalRow where index < dataRows.count:
77+
view.isHidden = false
78+
let dataRow = dataRows[index]
79+
view.configure(rowData: dataRow, delegate: configuration.rowDelegate, referrerDelegate: configuration.referrerDelegate)
80+
view.showSeparator = index != (numberOfRowsToAdd - 1)
81+
default:
82+
view.isHidden = true
83+
}
84+
}
85+
86+
private func configureViewMoreRow(_ viewMoreRow: ViewMoreRow, at index: Int, in dataRows: [StatsTotalRowData], withConfiguration configuration: StatsTotalRowConfiguration) {
87+
let shouldShowViewMore = configuration.limitRowsDisplayed && dataRows.count > StatsDataHelper.maxRowsToDisplay
88+
viewMoreRow.isHidden = !shouldShowViewMore
89+
if shouldShowViewMore {
90+
viewMoreRow.configure(statSection: dataRows.first?.statSection, delegate: configuration.viewMoreDelegate)
91+
}
92+
}
93+
}

WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@ import UIKit
77
/// If the row has child rows, those child rows are added to the stack view below the selected row.
88
///
99

10-
class TopTotalsCell: StatsBaseCell, NibLoadable {
10+
final class TopTotalsCell: StatsRowsCell, NibLoadable {
1111

1212
// MARK: - Properties
1313

1414
@IBOutlet weak var outerStackView: UIStackView!
1515
@IBOutlet weak var subtitleStackView: UIStackView!
16-
@IBOutlet weak var rowsStackView: UIStackView!
1716
@IBOutlet weak var itemSubtitleLabel: UILabel!
1817
@IBOutlet weak var dataSubtitleLabel: UILabel!
1918
@IBOutlet weak var dataSubtitleLabelWidthConstraint: NSLayoutConstraint!
@@ -44,12 +43,6 @@ class TopTotalsCell: StatsBaseCell, NibLoadable {
4443
private weak var postStatsDelegate: PostStatsDelegate?
4544
private typealias Style = WPStyleGuide.Stats
4645

47-
override func awakeFromNib() {
48-
super.awakeFromNib()
49-
50-
addDefaultTotalRows(toStackView: rowsStackView)
51-
}
52-
5346
// MARK: - Configure
5447

5548
func configure(itemSubtitle: String? = nil,

WordPress/WordPress.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,8 @@
255255
01E70EBC2BB5CCCF000BFE45 /* NumberFormatter+Stats.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01E70EBA2BB5CCCF000BFE45 /* NumberFormatter+Stats.swift */; };
256256
01E70EBD2BB5D035000BFE45 /* NumberFormatter+Stats.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01E70EBA2BB5CCCF000BFE45 /* NumberFormatter+Stats.swift */; };
257257
01E78D1D296EA54F00FB6863 /* StatsPeriodHelperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01E78D1C296EA54F00FB6863 /* StatsPeriodHelperTests.swift */; };
258+
01FB42F42C25651000F5069E /* StatsRowsCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01FB42F32C25651000F5069E /* StatsRowsCell.swift */; };
259+
01FB42F52C25651000F5069E /* StatsRowsCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01FB42F32C25651000F5069E /* StatsRowsCell.swift */; };
258260
02761EC02270072F009BAF0F /* BlogDetailsViewController+SectionHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02761EBF2270072F009BAF0F /* BlogDetailsViewController+SectionHelpers.swift */; };
259261
02761EC222700A9C009BAF0F /* BlogDetailsSubsectionToSectionCategoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02761EC122700A9C009BAF0F /* BlogDetailsSubsectionToSectionCategoryTests.swift */; };
260262
02761EC4227010BC009BAF0F /* BlogDetailsSectionIndexTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02761EC3227010BC009BAF0F /* BlogDetailsSectionIndexTests.swift */; };
@@ -6058,6 +6060,7 @@
60586060
01E2580D2ACDC88100F09666 /* PlanWizardContentViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlanWizardContentViewModelTests.swift; sourceTree = "<group>"; };
60596061
01E70EBA2BB5CCCF000BFE45 /* NumberFormatter+Stats.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NumberFormatter+Stats.swift"; sourceTree = "<group>"; };
60606062
01E78D1C296EA54F00FB6863 /* StatsPeriodHelperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatsPeriodHelperTests.swift; sourceTree = "<group>"; };
6063+
01FB42F32C25651000F5069E /* StatsRowsCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatsRowsCell.swift; sourceTree = "<group>"; };
60616064
02761EBF2270072F009BAF0F /* BlogDetailsViewController+SectionHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BlogDetailsViewController+SectionHelpers.swift"; sourceTree = "<group>"; };
60626065
02761EC122700A9C009BAF0F /* BlogDetailsSubsectionToSectionCategoryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlogDetailsSubsectionToSectionCategoryTests.swift; sourceTree = "<group>"; };
60636066
02761EC3227010BC009BAF0F /* BlogDetailsSectionIndexTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlogDetailsSectionIndexTests.swift; sourceTree = "<group>"; };
@@ -14672,6 +14675,7 @@
1467214675
987535622282682D001661B4 /* DetailDataCell.xib */,
1467314676
DC9AF768285DF8A300EA2A0D /* StatsFollowersChartViewModel.swift */,
1467414677
1762B6DB2845510400F270A5 /* StatsReferrersChartViewModel.swift */,
14678+
01FB42F32C25651000F5069E /* StatsRowsCell.swift */,
1467514679
);
1467614680
path = "Stats Detail";
1467714681
sourceTree = "<group>";
@@ -23358,6 +23362,7 @@
2335823362
01B7590B2B3ED63B00179AE6 /* DomainDetailsWebViewControllerWrapper.swift in Sources */,
2335923363
0CB424F12ADEE52A0080B807 /* PostSearchToken.swift in Sources */,
2336023364
98AA6D1126B8CE7200920C8B /* Comment+CoreDataClass.swift in Sources */,
23365+
01FB42F42C25651000F5069E /* StatsRowsCell.swift in Sources */,
2336123366
7E4A773720F802A8001C706D /* ActivityRangesFactory.swift in Sources */,
2336223367
E1AC282D18282423004D394C /* SFHFKeychainUtils.m in Sources */,
2336323368
7EDAB3F420B046FE002D1A76 /* CircularProgressView+ActivityIndicatorType.swift in Sources */,
@@ -25356,6 +25361,7 @@
2535625361
F41E32FF287B47A500F89082 /* SuggestionsListViewModel.swift in Sources */,
2535725362
F4EDAA5129A795C600622D3D /* BlockedAuthor.swift in Sources */,
2535825363
FABB238E2602FC2C00C8785C /* NotificationSettingsService.swift in Sources */,
25364+
01FB42F52C25651000F5069E /* StatsRowsCell.swift in Sources */,
2535925365
019105872BE8BD6000CDFB16 /* StatsGhostSingleValueCell.swift in Sources */,
2536025366
8091019429078CFE00FCB4EA /* JetpackFullscreenOverlayViewController.swift in Sources */,
2536125367
FA347AEE26EB6E300096604B /* GrowAudienceCell.swift in Sources */,

0 commit comments

Comments
 (0)