From 98d5b7719b648da5a67260f80de33ac99d9235cd Mon Sep 17 00:00:00 2001 From: Povilas Staskus Date: Mon, 18 Mar 2024 16:45:44 +0200 Subject: [PATCH 01/12] Optimize TopTotalsCell to add rows only when the cell loads TopTotalsCell was calling addRows on every configuration of cell which in turn created and added a hierarchy of UIStackView-based views. Optimizing TopTotalsCell to only add rows once and then make manipulations on existing rows. --- .../Extensions/UITableViewCell+Stats.swift | 81 +++++++++++++++++++ .../Stats/Shared Views/TopTotalsCell.swift | 43 +++++----- 2 files changed, 100 insertions(+), 24 deletions(-) diff --git a/WordPress/Classes/ViewRelated/Stats/Extensions/UITableViewCell+Stats.swift b/WordPress/Classes/ViewRelated/Stats/Extensions/UITableViewCell+Stats.swift index 5403422edb55..c0e0a67046a3 100644 --- a/WordPress/Classes/ViewRelated/Stats/Extensions/UITableViewCell+Stats.swift +++ b/WordPress/Classes/ViewRelated/Stats/Extensions/UITableViewCell+Stats.swift @@ -71,3 +71,84 @@ extension UITableViewCell { } } + + extension UITableViewCell { + struct StatsTotalRowConfiguration { + let limitRowsDisplayed: Bool + let rowDelegate: StatsTotalRowDelegate? + let referrerDelegate: StatsTotalRowReferrerDelegate? + let viewMoreDelegate: ViewMoreRowDelegate? + } + + func addDefaultTotalRows(toStackView rowsStackView: UIStackView) { + for _ in 0.. 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) + } + } +} diff --git a/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift b/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift index b2e3e24ca838..353d485e9809 100644 --- a/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift +++ b/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift @@ -42,6 +42,12 @@ 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, @@ -71,15 +77,23 @@ class TopTotalsCell: StatsBaseCell, NibLoadable { self.forDetails = forDetails if !forDetails { - addRows(dataRows, - toStackView: rowsStackView, - forType: siteStatsPeriodDelegate != nil ? .period : .insights, + if rowsStackView.arrangedSubviews.isEmpty { + addDefaultTotalRows(toStackView: rowsStackView) + } + configureTotalRows( + dataRows, + inStackView: rowsStackView, + forType: siteStatsPeriodDelegate != nil ? .period : .insights, + configuration: .init( limitRowsDisplayed: limitRowsDisplayed, rowDelegate: self, referrerDelegate: self, - viewMoreDelegate: self) - + viewMoreDelegate: self + ) + ) initChildRows() + } else { + rowsStackView.arrangedSubviews.forEach { $0.removeFromSuperview() } } setSubtitleVisibility() @@ -87,25 +101,6 @@ class TopTotalsCell: StatsBaseCell, NibLoadable { prepareForVoiceOver() } - override func prepareForReuse() { - super.prepareForReuse() - - rowsStackView.arrangedSubviews.forEach { subview in - - // Remove granchild rows - if let row = subview as? StatsTotalRow { - removeChildRowsForRow(row) - } - - // Remove child rows - if let childView = subview as? StatsChildRowsView { - removeRowsFromStackView(childView.rowsStackView) - } - } - - removeRowsFromStackView(rowsStackView) - } - private enum Metrics { static let topAccessoryViewSpacing: CGFloat = 32.0 } From 9880cb715e724674636c280635ed53b2798f7d26 Mon Sep 17 00:00:00 2001 From: Povilas Staskus Date: Mon, 18 Mar 2024 17:05:02 +0200 Subject: [PATCH 02/12] Optimize CountriesCell to add rows only when the cell loads --- .../Countries/CountriesCell.swift | 30 +++++++++++++------ 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift b/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift index e0324c0c65ce..0a630e219cfe 100644 --- a/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift +++ b/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift @@ -21,6 +21,12 @@ 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, @@ -38,21 +44,27 @@ class CountriesCell: StatsBaseCell, NibLoadable { bottomSeparatorLine.isHidden = forDetails if !forDetails { - addRows(dataRows, - toStackView: rowsStackView, + if rowsStackView.arrangedSubviews.isEmpty { + addDefaultTotalRows(toStackView: rowsStackView) + } + configureTotalRows( + dataRows, + inStackView: rowsStackView, forType: .period, - limitRowsDisplayed: true, - viewMoreDelegate: self) + configuration: .init( + limitRowsDisplayed: true, + rowDelegate: nil, + referrerDelegate: nil, + viewMoreDelegate: self + ) + ) + } else { + removeRowsFromStackView(rowsStackView) } setSubtitleVisibility() applyStyles() } - - override func prepareForReuse() { - super.prepareForReuse() - removeRowsFromStackView(rowsStackView) - } } private extension CountriesCell { From 9252c6fb127c2af7d902044ccf9f69b880e13b7d Mon Sep 17 00:00:00 2001 From: Povilas Staskus Date: Mon, 18 Mar 2024 17:05:19 +0200 Subject: [PATCH 03/12] Do not track StatsTraffic tableView scrolling --- .../Stats/Period Stats/SiteStatsPeriodTableViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPress/Classes/ViewRelated/Stats/Period Stats/SiteStatsPeriodTableViewController.swift b/WordPress/Classes/ViewRelated/Stats/Period Stats/SiteStatsPeriodTableViewController.swift index 53d11ee54d6f..7ede31860c2d 100644 --- a/WordPress/Classes/ViewRelated/Stats/Period Stats/SiteStatsPeriodTableViewController.swift +++ b/WordPress/Classes/ViewRelated/Stats/Period Stats/SiteStatsPeriodTableViewController.swift @@ -37,7 +37,7 @@ final class SiteStatsPeriodTableViewController: SiteStatsBaseTableViewController private let analyticsTracker = BottomScrollAnalyticsTracker() private lazy var tableHandler: ImmuTableDiffableViewHandler = { - return ImmuTableDiffableViewHandler(takeOver: self, with: analyticsTracker) + return ImmuTableDiffableViewHandler(takeOver: self, with: nil) }() init(date: Date, period: StatsPeriodUnit) { From d0af997137fcc64eade21e101fdc134e5f88521d Mon Sep 17 00:00:00 2001 From: Povilas Staskus Date: Wed, 10 Apr 2024 11:40:38 +0300 Subject: [PATCH 04/12] Update RELEASE-NOTES.txt --- RELEASE-NOTES.txt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt index 6e12cc861e54..ee9525587bbb 100644 --- a/RELEASE-NOTES.txt +++ b/RELEASE-NOTES.txt @@ -1,8 +1,7 @@ 24.7 ----- -* [*] [Jetpack-only] Stats: Optimized the Insights tab to enhance loading and scrolling performance. [#22592] - +* [*] [Jetpack-only] Stats: Made optimizations to enhance loading and scrolling performance. [#22592, #22847] 24.6 ----- From eb7c14247c314081fff7b7ac902481bc87e6be43 Mon Sep 17 00:00:00 2001 From: Povilas Staskus Date: Thu, 11 Apr 2024 15:45:56 +0300 Subject: [PATCH 05/12] Update TopTotalsCell to use setNeedsLayout for more efficiency --- .../Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift b/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift index 353d485e9809..1b5122d6fd1b 100644 --- a/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift +++ b/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift @@ -124,7 +124,7 @@ private extension TopTotalsCell { /// - Hide the stack view. /// func setSubtitleVisibility() { - subtitleStackView.layoutIfNeeded() + subtitleStackView.setNeedsLayout() if forDetails { bottomSeparatorLine.isHidden = true From 2674401d14e07a1823d27e5bce65600d00f0342a Mon Sep 17 00:00:00 2001 From: Povilas Staskus Date: Thu, 11 Apr 2024 15:57:07 +0300 Subject: [PATCH 06/12] Update CountriesCell to use setNeedsLayout for more efficiency --- .../Stats/Period Stats/Countries/CountriesCell.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift b/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift index 0a630e219cfe..98c82b454ce2 100644 --- a/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift +++ b/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift @@ -78,7 +78,7 @@ private extension CountriesCell { } func setSubtitleVisibility() { - subtitleStackView.layoutIfNeeded() + subtitleStackView.setNeedsLayout() let subtitleHeight = subtitlesStackViewTopConstraint.constant * 2 + subtitleStackView.frame.height if forDetails { From 25bf46df9a20def57836770e1e95c6f6fb0d7697 Mon Sep 17 00:00:00 2001 From: Povilas Staskus Date: Wed, 19 Jun 2024 13:47:26 +0300 Subject: [PATCH 07/12] Update RELEASE-NOTES --- RELEASE-NOTES.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RELEASE-NOTES.txt b/RELEASE-NOTES.txt index 8576f70309bd..e50a23c3ceba 100644 --- a/RELEASE-NOTES.txt +++ b/RELEASE-NOTES.txt @@ -1,6 +1,7 @@ 25.2 ----- +* [*] [Jetpack-only] Stats: Made optimizations to enhance scrolling performance. [#22847] 25.1 ----- @@ -61,7 +62,6 @@ 24.7 ----- -* [*] [Jetpack-only] Stats: Made optimizations to enhance loading and scrolling performance. [#22592, #22847] * [**] [Jetpack-only] Reader: Introducing Reading Preferences, an experimental feature that allows users to customize their Reader post content screen with the color, font, and size that they like the most. [#22999] * [*] [Jetpack-only] Stats: Optimized the Insights tab to enhance loading and scrolling performance. [#22592] From 7684a35d1694b29d9ce33a9c3c68ca3238cf075c Mon Sep 17 00:00:00 2001 From: Povilas Staskus Date: Thu, 20 Jun 2024 10:48:25 +0300 Subject: [PATCH 08/12] Move additional checks for adding default rows into the extension --- .../ViewRelated/Stats/Extensions/UITableViewCell+Stats.swift | 3 +++ .../Stats/Period Stats/Countries/CountriesCell.swift | 3 --- .../Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift | 3 --- 3 files changed, 3 insertions(+), 6 deletions(-) diff --git a/WordPress/Classes/ViewRelated/Stats/Extensions/UITableViewCell+Stats.swift b/WordPress/Classes/ViewRelated/Stats/Extensions/UITableViewCell+Stats.swift index a54eb87c6b5a..ecbc67c82219 100644 --- a/WordPress/Classes/ViewRelated/Stats/Extensions/UITableViewCell+Stats.swift +++ b/WordPress/Classes/ViewRelated/Stats/Extensions/UITableViewCell+Stats.swift @@ -98,6 +98,9 @@ extension UITableViewCell { inStackView rowsStackView: UIStackView, forType statType: StatType, configuration: StatsTotalRowConfiguration) { + if rowsStackView.arrangedSubviews.isEmpty { + addDefaultTotalRows(toStackView: rowsStackView) + } guard !dataRows.isEmpty else { configureForNoData(inStackView: rowsStackView, forType: statType) diff --git a/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift b/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift index 98c82b454ce2..6ac6462deffe 100644 --- a/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift +++ b/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift @@ -44,9 +44,6 @@ class CountriesCell: StatsBaseCell, NibLoadable { bottomSeparatorLine.isHidden = forDetails if !forDetails { - if rowsStackView.arrangedSubviews.isEmpty { - addDefaultTotalRows(toStackView: rowsStackView) - } configureTotalRows( dataRows, inStackView: rowsStackView, diff --git a/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift b/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift index 61923bb348c1..dd22fc9e52c1 100644 --- a/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift +++ b/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift @@ -83,9 +83,6 @@ class TopTotalsCell: StatsBaseCell, NibLoadable { self.forDetails = forDetails if !forDetails { - if rowsStackView.arrangedSubviews.isEmpty { - addDefaultTotalRows(toStackView: rowsStackView) - } configureTotalRows( dataRows, inStackView: rowsStackView, From 51992ab27d31886305ebaba6a5fa127ff23cd1eb Mon Sep 17 00:00:00 2001 From: Povilas Staskus Date: Fri, 21 Jun 2024 10:28:01 +0300 Subject: [PATCH 09/12] Remove force layout calls when setting subtitle visibility These calls were added together with dynamic type support, however, they slow down layout process of the cell --- .../Stats/Period Stats/Countries/CountriesCell.swift | 1 - .../Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift | 2 -- 2 files changed, 3 deletions(-) diff --git a/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift b/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift index 6ac6462deffe..d5159c9a5081 100644 --- a/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift +++ b/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift @@ -75,7 +75,6 @@ private extension CountriesCell { } func setSubtitleVisibility() { - subtitleStackView.setNeedsLayout() let subtitleHeight = subtitlesStackViewTopConstraint.constant * 2 + subtitleStackView.frame.height if forDetails { diff --git a/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift b/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift index dd22fc9e52c1..b600b132089c 100644 --- a/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift +++ b/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift @@ -128,8 +128,6 @@ private extension TopTotalsCell { /// - Hide the stack view. /// func setSubtitleVisibility() { - subtitleStackView.setNeedsLayout() - if forDetails { bottomSeparatorLine.isHidden = true From 89fd81614f24fe5298ad31c97e63a060de7e182a Mon Sep 17 00:00:00 2001 From: Povilas Staskus Date: Fri, 21 Jun 2024 10:28:28 +0300 Subject: [PATCH 10/12] Make maximum content size category smaller for stats cell subtitles --- .../ViewRelated/Stats/Extensions/WPStyleGuide+Stats.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPress/Classes/ViewRelated/Stats/Extensions/WPStyleGuide+Stats.swift b/WordPress/Classes/ViewRelated/Stats/Extensions/WPStyleGuide+Stats.swift index a3498ecd2148..dc176cc818f2 100644 --- a/WordPress/Classes/ViewRelated/Stats/Extensions/WPStyleGuide+Stats.swift +++ b/WordPress/Classes/ViewRelated/Stats/Extensions/WPStyleGuide+Stats.swift @@ -73,7 +73,7 @@ extension WPStyleGuide { label.textColor = .DS.Foreground.secondary label.font = .DS.font(.footnote) label.adjustsFontSizeToFitWidth = true - label.maximumContentSizeCategory = .accessibilityLarge + label.maximumContentSizeCategory = .extraExtraExtraLarge } static func configureLabelAsLink(_ label: UILabel) { From 1b8bc1cfa46e87bad24a0ed43738d92c6ba52d92 Mon Sep 17 00:00:00 2001 From: Povilas Staskus Date: Fri, 21 Jun 2024 10:52:21 +0300 Subject: [PATCH 11/12] Create StatsRowsCell with default child stack view rows and ability to configure more --- .../Extensions/UITableViewCell+Stats.swift | 85 ----------------- .../Countries/CountriesCell.swift | 9 +- .../Stats Detail/StatsRowsCell.swift | 93 +++++++++++++++++++ .../Stats/Shared Views/TopTotalsCell.swift | 9 +- WordPress/WordPress.xcodeproj/project.pbxproj | 6 ++ 5 files changed, 101 insertions(+), 101 deletions(-) create mode 100644 WordPress/Classes/ViewRelated/Stats/Shared Views/Stats Detail/StatsRowsCell.swift diff --git a/WordPress/Classes/ViewRelated/Stats/Extensions/UITableViewCell+Stats.swift b/WordPress/Classes/ViewRelated/Stats/Extensions/UITableViewCell+Stats.swift index ecbc67c82219..4f7e3363a16d 100644 --- a/WordPress/Classes/ViewRelated/Stats/Extensions/UITableViewCell+Stats.swift +++ b/WordPress/Classes/ViewRelated/Stats/Extensions/UITableViewCell+Stats.swift @@ -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.. 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) - } - } } diff --git a/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift b/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift index d5159c9a5081..ea6ac16fc325 100644 --- a/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift +++ b/WordPress/Classes/ViewRelated/Stats/Period Stats/Countries/CountriesCell.swift @@ -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! @@ -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, diff --git a/WordPress/Classes/ViewRelated/Stats/Shared Views/Stats Detail/StatsRowsCell.swift b/WordPress/Classes/ViewRelated/Stats/Shared Views/Stats Detail/StatsRowsCell.swift new file mode 100644 index 000000000000..43852efc6614 --- /dev/null +++ b/WordPress/Classes/ViewRelated/Stats/Shared Views/Stats Detail/StatsRowsCell.swift @@ -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.. 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) + } + } +} diff --git a/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift b/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift index b600b132089c..669d11678739 100644 --- a/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift +++ b/WordPress/Classes/ViewRelated/Stats/Shared Views/TopTotalsCell.swift @@ -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! @@ -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, diff --git a/WordPress/WordPress.xcodeproj/project.pbxproj b/WordPress/WordPress.xcodeproj/project.pbxproj index e2a87025bc77..3731f1c8d9c6 100644 --- a/WordPress/WordPress.xcodeproj/project.pbxproj +++ b/WordPress/WordPress.xcodeproj/project.pbxproj @@ -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 */; }; @@ -6058,6 +6060,7 @@ 01E2580D2ACDC88100F09666 /* PlanWizardContentViewModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PlanWizardContentViewModelTests.swift; sourceTree = ""; }; 01E70EBA2BB5CCCF000BFE45 /* NumberFormatter+Stats.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NumberFormatter+Stats.swift"; sourceTree = ""; }; 01E78D1C296EA54F00FB6863 /* StatsPeriodHelperTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatsPeriodHelperTests.swift; sourceTree = ""; }; + 01FB42F32C25651000F5069E /* StatsRowsCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StatsRowsCell.swift; sourceTree = ""; }; 02761EBF2270072F009BAF0F /* BlogDetailsViewController+SectionHelpers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "BlogDetailsViewController+SectionHelpers.swift"; sourceTree = ""; }; 02761EC122700A9C009BAF0F /* BlogDetailsSubsectionToSectionCategoryTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlogDetailsSubsectionToSectionCategoryTests.swift; sourceTree = ""; }; 02761EC3227010BC009BAF0F /* BlogDetailsSectionIndexTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BlogDetailsSectionIndexTests.swift; sourceTree = ""; }; @@ -14672,6 +14675,7 @@ 987535622282682D001661B4 /* DetailDataCell.xib */, DC9AF768285DF8A300EA2A0D /* StatsFollowersChartViewModel.swift */, 1762B6DB2845510400F270A5 /* StatsReferrersChartViewModel.swift */, + 01FB42F32C25651000F5069E /* StatsRowsCell.swift */, ); path = "Stats Detail"; sourceTree = ""; @@ -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 */, @@ -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 */, From bc88b72471faf126dcd072016c5966954e684288 Mon Sep 17 00:00:00 2001 From: Povilas Staskus Date: Fri, 21 Jun 2024 10:57:00 +0300 Subject: [PATCH 12/12] Put analyticsTracker back since it's used by JetpackBanner --- .../Stats/Period Stats/SiteStatsPeriodTableViewController.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/WordPress/Classes/ViewRelated/Stats/Period Stats/SiteStatsPeriodTableViewController.swift b/WordPress/Classes/ViewRelated/Stats/Period Stats/SiteStatsPeriodTableViewController.swift index 028aa59aaa18..9e5119cc562b 100644 --- a/WordPress/Classes/ViewRelated/Stats/Period Stats/SiteStatsPeriodTableViewController.swift +++ b/WordPress/Classes/ViewRelated/Stats/Period Stats/SiteStatsPeriodTableViewController.swift @@ -36,7 +36,7 @@ final class SiteStatsPeriodTableViewController: SiteStatsBaseTableViewController private let analyticsTracker = BottomScrollAnalyticsTracker() private lazy var tableHandler: ImmuTableDiffableViewHandler = { - return ImmuTableDiffableViewHandler(takeOver: self, with: nil) + return ImmuTableDiffableViewHandler(takeOver: self, with: analyticsTracker) }() init(date: Date, period: StatsPeriodUnit) {