Skip to content

Commit

Permalink
Stats Period Details: Optimize table view updates by integrating diff…
Browse files Browse the repository at this point in the history
…able data source (#22823)
  • Loading branch information
staskus authored Apr 14, 2024
2 parents a8940bd + 47988e5 commit 199d402
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class PostStatsTableViewController: UITableViewController, StoryboardLoadable {
private let store = StatsPeriodStore()
private var changeReceipt: Receipt?

private lazy var tableHandler: ImmuTableViewHandler = {
return ImmuTableViewHandler(takeOver: self, with: self)
private lazy var tableHandler: ImmuTableDiffableViewHandler = {
return ImmuTableDiffableViewHandler(takeOver: self, with: self)
}()

// MARK: - View
Expand Down Expand Up @@ -152,7 +152,7 @@ private extension PostStatsTableViewController {
return
}

tableHandler.viewModel = viewModel.tableViewModel()
tableHandler.diffableDataSource.apply(viewModel.tableViewSnapshot(), animatingDifferences: false)
refreshControl?.endRefreshing()

if viewModel.fetchDataHasFailed() {
Expand All @@ -173,13 +173,16 @@ private extension PostStatsTableViewController {

viewModel.refreshPostStats(postID: postID, selectedDate: selectedDate)
if forceUpdate {
tableHandler.viewModel = viewModel.tableViewModel()
tableHandler.diffableDataSource.apply(viewModel.tableViewSnapshot(), animatingDifferences: false)
}
}

func applyTableUpdates() {
tableView.performBatchUpdates({
})
guard let viewModel = viewModel else {
return
}

tableHandler.diffableDataSource.apply(viewModel.tableViewSnapshot(), animatingDifferences: false)
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ class PostStatsViewModel: Observable {

// MARK: - Table View

func tableViewModel() -> ImmuTable {
func tableViewSnapshot() -> ImmuTableDiffableDataSourceSnapshot {
if let postId = postID, store.fetchingFailed(for: .postStats(postID: postId)) {
return ImmuTable.Empty
return ImmuTableDiffableDataSourceSnapshot()
}

postStats = store.getPostStats(for: postID)

return blocks(for: store.postStatsFetchingStatuses(for: postID)) {
var tableRows = [ImmuTableRow]()
var tableRows = [any HashableImmutableRow]()
tableRows.append(titleTableRow())
tableRows.append(contentsOf: overviewTableRows())
tableRows.append(contentsOf: yearsTableRows())
Expand Down Expand Up @@ -111,14 +111,14 @@ private extension PostStatsViewModel {

// MARK: - Create Table Rows

func titleTableRow() -> ImmuTableRow {
func titleTableRow() -> any HashableImmutableRow {
return PostStatsTitleRow(postTitle: postTitle ?? StatSection.noPostTitle,
postURL: postURL,
postStatsDelegate: postStatsDelegate)
}

func overviewTableRows() -> [ImmuTableRow] {
var tableRows = [ImmuTableRow]()
func overviewTableRows() -> [any HashableImmutableRow] {
var tableRows = [any HashableImmutableRow]()
tableRows.append(PostStatsEmptyCellHeaderRow())

let lastTwoWeeks = postStats?.lastTwoWeeks ?? []
Expand All @@ -145,8 +145,8 @@ private extension PostStatsViewModel {
return tableRows
}

func yearsTableRows(forAverages: Bool = false) -> [ImmuTableRow] {
var tableRows = [ImmuTableRow]()
func yearsTableRows(forAverages: Bool = false) -> [any HashableImmutableRow] {
var tableRows = [any HashableImmutableRow]()

let statSection = forAverages ? StatSection.postStatsAverageViews :
StatSection.postStatsMonthsYears
Expand Down Expand Up @@ -199,8 +199,8 @@ private extension PostStatsViewModel {
return yearRows
}

func recentWeeksTableRows() -> [ImmuTableRow] {
var tableRows = [ImmuTableRow]()
func recentWeeksTableRows() -> [any HashableImmutableRow] {
var tableRows = [any HashableImmutableRow]()

tableRows.append(CellHeaderRow(statSection: StatSection.postStatsRecentWeeks))
tableRows.append(TopTotalsPostStatsRow(itemSubtitle: StatSection.postStatsRecentWeeks.itemSubtitle,
Expand Down Expand Up @@ -289,15 +289,12 @@ private extension PostStatsViewModel {
return (currentCount, difference, roundedPercentage)
}

func blocks(for state: StoreFetchingStatus, block: () -> [ImmuTableRow]) -> ImmuTable {
func blocks(for state: StoreFetchingStatus, block: () -> [any HashableImmutableRow]) -> ImmuTableDiffableDataSourceSnapshot {
if postStats != nil {
return ImmuTable(sections: [
ImmuTableSection(
rows: block())
])
return .singleSectionSnapshot(block())
}

var rows = [ImmuTableRow]()
var rows = [any HashableImmutableRow]()
let sections: [StatSection] = [.postStatsMonthsYears,
.postStatsAverageViews,
.postStatsRecentWeeks]
Expand All @@ -311,15 +308,12 @@ private extension PostStatsViewModel {
rows.append(StatsGhostChartImmutableRow())
sections.forEach {
rows.append(CellHeaderRow(statSection: $0))
rows.append(StatsGhostTopImmutableRow())
rows.append(StatsGhostTopImmutableRow(statSection: $0))
}
case .error:
return ImmuTable.Empty
return ImmuTableDiffableDataSourceSnapshot()
}

return ImmuTable(sections: [
ImmuTableSection(
rows: rows)
])
return .singleSectionSnapshot(rows)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class SiteStatsDetailTableViewController: UITableViewController, StoryboardLoada
private let insightsStore = StatsInsightsStore()
private let periodStore = StatsPeriodStore()

private lazy var tableHandler: ImmuTableViewHandler = {
return ImmuTableViewHandler(takeOver: self)
private lazy var tableHandler: ImmuTableDiffableViewHandler = {
return ImmuTableDiffableViewHandler(takeOver: self, with: nil)
}()

private var postID: Int?
Expand Down Expand Up @@ -174,7 +174,7 @@ private extension SiteStatsDetailTableViewController {
return
}

tableHandler.viewModel = viewModel.tableViewModel()
tableHandler.diffableDataSource.apply(viewModel.tableViewSnapshot(), animatingDifferences: false)
refreshControl?.endRefreshing()

if viewModel.fetchDataHasFailed() {
Expand Down
Loading

0 comments on commit 199d402

Please sign in to comment.