From 26c99a1a35a81d7f53689f1cc493f444a17130df Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sat, 25 Jan 2020 11:11:48 -0500 Subject: [PATCH] Improve scroll perform when loading new rows into table views Instead of reloading the whole table view, only insert the rows for statuses/notifications that were added. --- .../BookmarksTableViewController.swift | 19 +++++++++------ .../NotificationsTableViewController.swift | 24 +++++++++++++------ .../TimelineTableViewController.swift | 21 ++++++++++------ 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift b/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift index a823ddec..78092097 100644 --- a/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift +++ b/Tusker/Screens/Bookmarks/BookmarksTableViewController.swift @@ -15,13 +15,7 @@ class BookmarksTableViewController: EnhancedTableViewController { let mastodonController: MastodonController - var statuses: [(id: String, state: StatusState)] = [] { - didSet { - DispatchQueue.main.async { - self.tableView.reloadData() - } - } - } + var statuses: [(id: String, state: StatusState)] = [] var newer: RequestRange? var older: RequestRange? @@ -55,6 +49,10 @@ class BookmarksTableViewController: EnhancedTableViewController { self.statuses.append(contentsOf: statuses.map { ($0.id, .unknown) }) self.newer = pagination?.newer self.older = pagination?.older + + DispatchQueue.main.async { + self.tableView.reloadData() + } } userActivity = UserActivityManager.bookmarksActivity() @@ -90,7 +88,14 @@ class BookmarksTableViewController: EnhancedTableViewController { guard case let .success(newStatuses, pagination) = response else { fatalError() } self.older = pagination?.older self.mastodonController.cache.addAll(statuses: newStatuses) + let newIndexPaths = (self.statuses.count..<(self.statuses.count + newStatuses.count)).map { + IndexPath(row: $0, section: 0) + } self.statuses.append(contentsOf: newStatuses.map { ($0.id, .unknown) }) + + DispatchQueue.main.async { + self.tableView.insertRows(at: newIndexPaths, with: .automatic) + } } } diff --git a/Tusker/Screens/Notifications/NotificationsTableViewController.swift b/Tusker/Screens/Notifications/NotificationsTableViewController.swift index c76a1456..09c22e4a 100644 --- a/Tusker/Screens/Notifications/NotificationsTableViewController.swift +++ b/Tusker/Screens/Notifications/NotificationsTableViewController.swift @@ -21,13 +21,7 @@ class NotificationsTableViewController: EnhancedTableViewController { let excludedTypes: [Pachyderm.Notification.Kind] let groupTypes = [Notification.Kind.favourite, .reblog, .follow] - var groups: [NotificationGroup] = [] { - didSet { - DispatchQueue.main.async { - self.tableView.reloadData() - } - } - } + var groups: [NotificationGroup] = [] var newer: RequestRange? var older: RequestRange? @@ -73,6 +67,10 @@ class NotificationsTableViewController: EnhancedTableViewController { self.newer = pagination?.newer self.older = pagination?.older + + DispatchQueue.main.async { + self.tableView.reloadData() + } } } @@ -133,6 +131,9 @@ class NotificationsTableViewController: EnhancedTableViewController { let groups = NotificationGroup.createGroups(notifications: newNotifications, only: self.groupTypes) + let newIndexPaths = (self.groups.count..<(self.groups.count + groups.count)).map { + IndexPath(row: $0, section: 0) + } self.groups.append(contentsOf: groups) self.mastodonController.cache.addAll(notifications: newNotifications) @@ -140,6 +141,10 @@ class NotificationsTableViewController: EnhancedTableViewController { self.mastodonController.cache.addAll(accounts: newNotifications.map { $0.account }) self.older = pagination?.older + + DispatchQueue.main.async { + self.tableView.insertRows(at: newIndexPaths, with: .automatic) + } } } } @@ -216,6 +221,11 @@ class NotificationsTableViewController: EnhancedTableViewController { } DispatchQueue.main.async { + let newIndexPaths = (0..