Skip to content

Commit

Permalink
Create StatsRowsCell with default child stack view rows and ability t…
Browse files Browse the repository at this point in the history
…o configure more
  • Loading branch information
staskus committed Jun 21, 2024
1 parent 89fd816 commit 1b8bc1c
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,89 +70,4 @@ extension UITableViewCell {
row.configure(statSection: statSection, delegate: delegate)
rowsStackView.addArrangedSubview(row)
}

}

extension UITableViewCell {
struct StatsTotalRowConfiguration {
let limitRowsDisplayed: Bool
let rowDelegate: StatsTotalRowDelegate?
let referrerDelegate: StatsTotalRowReferrerDelegate?
let viewMoreDelegate: ViewMoreRowDelegate?
}

func addDefaultTotalRows(toStackView rowsStackView: UIStackView) {
for _ in 0..<StatsDataHelper.maxRowsToDisplay {
let row = StatsTotalRow.loadFromNib()
rowsStackView.addArrangedSubview(row)
}

let emptyRow = StatsNoDataRow.loadFromNib()
rowsStackView.addArrangedSubview(emptyRow)

let viewMoreRow = ViewMoreRow.loadFromNib()
rowsStackView.addArrangedSubview(viewMoreRow)
}

func configureTotalRows(_ dataRows: [StatsTotalRowData],
inStackView rowsStackView: UIStackView,
forType statType: StatType,
configuration: StatsTotalRowConfiguration) {
if rowsStackView.arrangedSubviews.isEmpty {
addDefaultTotalRows(toStackView: rowsStackView)
}

guard !dataRows.isEmpty else {
configureForNoData(inStackView: rowsStackView, forType: statType)
return
}

let numberOfRowsToAdd = calculateNumberOfRowsToAdd(from: dataRows, withConfiguration: configuration)

rowsStackView.arrangedSubviews.enumerated().forEach { index, view in
configure(view: view, at: index, in: dataRows, numberOfRowsToAdd: numberOfRowsToAdd, configuration: configuration)
}
}

private func configureForNoData(inStackView rowsStackView: UIStackView, forType statType: StatType) {
rowsStackView.arrangedSubviews.forEach { view in
if let emptyRow = view as? StatsNoDataRow {
emptyRow.isHidden = false
emptyRow.configure(forType: statType)
} else {
view.isHidden = true
}
}
}

private func calculateNumberOfRowsToAdd(from dataRows: [StatsTotalRowData], withConfiguration configuration: StatsTotalRowConfiguration) -> Int {
if configuration.limitRowsDisplayed {
return min(dataRows.count, StatsDataHelper.maxRowsToDisplay)
}
return dataRows.count
}

private func configure(view: UIView, at index: Int, in dataRows: [StatsTotalRowData], numberOfRowsToAdd: Int, configuration: StatsTotalRowConfiguration) {
switch view {
case let view as StatsNoDataRow:
view.isHidden = true
case let view as ViewMoreRow:
configureViewMoreRow(view, at: index, in: dataRows, withConfiguration: configuration)
case let view as StatsTotalRow where index < dataRows.count:
view.isHidden = false
let dataRow = dataRows[index]
view.configure(rowData: dataRow, delegate: configuration.rowDelegate, referrerDelegate: configuration.referrerDelegate)
view.showSeparator = index != (numberOfRowsToAdd - 1)
default:
view.isHidden = true
}
}

private func configureViewMoreRow(_ viewMoreRow: ViewMoreRow, at index: Int, in dataRows: [StatsTotalRowData], withConfiguration configuration: StatsTotalRowConfiguration) {
let shouldShowViewMore = configuration.limitRowsDisplayed && dataRows.count > StatsDataHelper.maxRowsToDisplay
viewMoreRow.isHidden = !shouldShowViewMore
if shouldShowViewMore {
viewMoreRow.configure(statSection: dataRows.first?.statSection, delegate: configuration.viewMoreDelegate)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import UIKit

class CountriesCell: StatsBaseCell, NibLoadable {
final class CountriesCell: StatsRowsCell, NibLoadable {

// MARK: - Properties

@IBOutlet weak var topSeparatorLine: UIView!
@IBOutlet weak var subtitleStackView: UIStackView!
@IBOutlet weak var rowsStackView: UIStackView!
@IBOutlet weak var itemSubtitleLabel: UILabel!
@IBOutlet weak var dataSubtitleLabel: UILabel!
@IBOutlet weak var bottomSeparatorLine: UIView!
Expand All @@ -21,12 +20,6 @@ class CountriesCell: StatsBaseCell, NibLoadable {
private typealias Style = WPStyleGuide.Stats
private var forDetails = false

override func awakeFromNib() {
super.awakeFromNib()

addDefaultTotalRows(toStackView: rowsStackView)
}

// MARK: - Configure

func configure(itemSubtitle: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import Foundation

class StatsRowsCell: StatsBaseCell {
struct StatsTotalRowConfiguration {
let limitRowsDisplayed: Bool
let rowDelegate: StatsTotalRowDelegate?
let referrerDelegate: StatsTotalRowReferrerDelegate?
let viewMoreDelegate: ViewMoreRowDelegate?
}

@IBOutlet weak var rowsStackView: UIStackView!

override func awakeFromNib() {
super.awakeFromNib()

addDefaultTotalRows(toStackView: rowsStackView)
}

func configureTotalRows(_ dataRows: [StatsTotalRowData],
inStackView rowsStackView: UIStackView,
forType statType: StatType,
configuration: StatsTotalRowConfiguration) {
if rowsStackView.arrangedSubviews.isEmpty {
addDefaultTotalRows(toStackView: rowsStackView)
}

guard !dataRows.isEmpty else {
configureForNoData(inStackView: rowsStackView, forType: statType)
return
}

let numberOfRowsToAdd = calculateNumberOfRowsToAdd(from: dataRows, withConfiguration: configuration)

rowsStackView.arrangedSubviews.enumerated().forEach { index, view in
configure(view: view, at: index, in: dataRows, numberOfRowsToAdd: numberOfRowsToAdd, configuration: configuration)
}
}

private func addDefaultTotalRows(toStackView rowsStackView: UIStackView) {
for _ in 0..<StatsDataHelper.maxRowsToDisplay {
let row = StatsTotalRow.loadFromNib()
rowsStackView.addArrangedSubview(row)
}

let emptyRow = StatsNoDataRow.loadFromNib()
rowsStackView.addArrangedSubview(emptyRow)

let viewMoreRow = ViewMoreRow.loadFromNib()
rowsStackView.addArrangedSubview(viewMoreRow)
}

private func configureForNoData(inStackView rowsStackView: UIStackView, forType statType: StatType) {
rowsStackView.arrangedSubviews.forEach { view in
if let emptyRow = view as? StatsNoDataRow {
emptyRow.isHidden = false
emptyRow.configure(forType: statType)
} else {
view.isHidden = true
}
}
}

private func calculateNumberOfRowsToAdd(from dataRows: [StatsTotalRowData], withConfiguration configuration: StatsTotalRowConfiguration) -> Int {
if configuration.limitRowsDisplayed {
return min(dataRows.count, StatsDataHelper.maxRowsToDisplay)
}
return dataRows.count
}

private func configure(view: UIView, at index: Int, in dataRows: [StatsTotalRowData], numberOfRowsToAdd: Int, configuration: StatsTotalRowConfiguration) {
switch view {
case let view as StatsNoDataRow:
view.isHidden = true
case let view as ViewMoreRow:
configureViewMoreRow(view, at: index, in: dataRows, withConfiguration: configuration)
case let view as StatsTotalRow where index < dataRows.count:
view.isHidden = false
let dataRow = dataRows[index]
view.configure(rowData: dataRow, delegate: configuration.rowDelegate, referrerDelegate: configuration.referrerDelegate)
view.showSeparator = index != (numberOfRowsToAdd - 1)
default:
view.isHidden = true
}
}

private func configureViewMoreRow(_ viewMoreRow: ViewMoreRow, at index: Int, in dataRows: [StatsTotalRowData], withConfiguration configuration: StatsTotalRowConfiguration) {
let shouldShowViewMore = configuration.limitRowsDisplayed && dataRows.count > StatsDataHelper.maxRowsToDisplay
viewMoreRow.isHidden = !shouldShowViewMore
if shouldShowViewMore {
viewMoreRow.configure(statSection: dataRows.first?.statSection, delegate: configuration.viewMoreDelegate)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import UIKit
/// If the row has child rows, those child rows are added to the stack view below the selected row.
///

class TopTotalsCell: StatsBaseCell, NibLoadable {
final class TopTotalsCell: StatsRowsCell, NibLoadable {

// MARK: - Properties

@IBOutlet weak var outerStackView: UIStackView!
@IBOutlet weak var subtitleStackView: UIStackView!
@IBOutlet weak var rowsStackView: UIStackView!
@IBOutlet weak var itemSubtitleLabel: UILabel!
@IBOutlet weak var dataSubtitleLabel: UILabel!
@IBOutlet weak var dataSubtitleLabelWidthConstraint: NSLayoutConstraint!
Expand Down Expand Up @@ -44,12 +43,6 @@ class TopTotalsCell: StatsBaseCell, NibLoadable {
private weak var postStatsDelegate: PostStatsDelegate?
private typealias Style = WPStyleGuide.Stats

override func awakeFromNib() {
super.awakeFromNib()

addDefaultTotalRows(toStackView: rowsStackView)
}

// MARK: - Configure

func configure(itemSubtitle: String? = nil,
Expand Down
6 changes: 6 additions & 0 deletions WordPress/WordPress.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@
01E70EBC2BB5CCCF000BFE45 /* NumberFormatter+Stats.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01E70EBA2BB5CCCF000BFE45 /* NumberFormatter+Stats.swift */; };
01E70EBD2BB5D035000BFE45 /* NumberFormatter+Stats.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01E70EBA2BB5CCCF000BFE45 /* NumberFormatter+Stats.swift */; };
01E78D1D296EA54F00FB6863 /* StatsPeriodHelperTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01E78D1C296EA54F00FB6863 /* StatsPeriodHelperTests.swift */; };
01FB42F42C25651000F5069E /* StatsRowsCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01FB42F32C25651000F5069E /* StatsRowsCell.swift */; };
01FB42F52C25651000F5069E /* StatsRowsCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 01FB42F32C25651000F5069E /* StatsRowsCell.swift */; };
02761EC02270072F009BAF0F /* BlogDetailsViewController+SectionHelpers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02761EBF2270072F009BAF0F /* BlogDetailsViewController+SectionHelpers.swift */; };
02761EC222700A9C009BAF0F /* BlogDetailsSubsectionToSectionCategoryTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02761EC122700A9C009BAF0F /* BlogDetailsSubsectionToSectionCategoryTests.swift */; };
02761EC4227010BC009BAF0F /* BlogDetailsSectionIndexTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 02761EC3227010BC009BAF0F /* BlogDetailsSectionIndexTests.swift */; };
Expand Down Expand Up @@ -6058,6 +6060,7 @@
01E2580D2ACDC88100F09666 /* PlanWizardContentViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlanWizardContentViewModelTests.swift; sourceTree = "<group>"; };
01E70EBA2BB5CCCF000BFE45 /* NumberFormatter+Stats.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NumberFormatter+Stats.swift"; sourceTree = "<group>"; };
01E78D1C296EA54F00FB6863 /* StatsPeriodHelperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatsPeriodHelperTests.swift; sourceTree = "<group>"; };
01FB42F32C25651000F5069E /* StatsRowsCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatsRowsCell.swift; sourceTree = "<group>"; };
02761EBF2270072F009BAF0F /* BlogDetailsViewController+SectionHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BlogDetailsViewController+SectionHelpers.swift"; sourceTree = "<group>"; };
02761EC122700A9C009BAF0F /* BlogDetailsSubsectionToSectionCategoryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlogDetailsSubsectionToSectionCategoryTests.swift; sourceTree = "<group>"; };
02761EC3227010BC009BAF0F /* BlogDetailsSectionIndexTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlogDetailsSectionIndexTests.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -14672,6 +14675,7 @@
987535622282682D001661B4 /* DetailDataCell.xib */,
DC9AF768285DF8A300EA2A0D /* StatsFollowersChartViewModel.swift */,
1762B6DB2845510400F270A5 /* StatsReferrersChartViewModel.swift */,
01FB42F32C25651000F5069E /* StatsRowsCell.swift */,
);
path = "Stats Detail";
sourceTree = "<group>";
Expand Down Expand Up @@ -23358,6 +23362,7 @@
01B7590B2B3ED63B00179AE6 /* DomainDetailsWebViewControllerWrapper.swift in Sources */,
0CB424F12ADEE52A0080B807 /* PostSearchToken.swift in Sources */,
98AA6D1126B8CE7200920C8B /* Comment+CoreDataClass.swift in Sources */,
01FB42F42C25651000F5069E /* StatsRowsCell.swift in Sources */,
7E4A773720F802A8001C706D /* ActivityRangesFactory.swift in Sources */,
E1AC282D18282423004D394C /* SFHFKeychainUtils.m in Sources */,
7EDAB3F420B046FE002D1A76 /* CircularProgressView+ActivityIndicatorType.swift in Sources */,
Expand Down Expand Up @@ -25356,6 +25361,7 @@
F41E32FF287B47A500F89082 /* SuggestionsListViewModel.swift in Sources */,
F4EDAA5129A795C600622D3D /* BlockedAuthor.swift in Sources */,
FABB238E2602FC2C00C8785C /* NotificationSettingsService.swift in Sources */,
01FB42F52C25651000F5069E /* StatsRowsCell.swift in Sources */,
019105872BE8BD6000CDFB16 /* StatsGhostSingleValueCell.swift in Sources */,
8091019429078CFE00FCB4EA /* JetpackFullscreenOverlayViewController.swift in Sources */,
FA347AEE26EB6E300096604B /* GrowAudienceCell.swift in Sources */,
Expand Down

0 comments on commit 1b8bc1c

Please sign in to comment.