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.
This commit is contained in:
Shadowfacts 2020-01-25 11:11:48 -05:00
parent 6757031dcb
commit 26c99a1a35
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
3 changed files with 43 additions and 21 deletions

View File

@ -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)
}
}
}

View File

@ -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..<groups.count).map {
IndexPath(row: $0, section: 0)
}
self.tableView.insertRows(at: newIndexPaths, with: .automatic)
self.refreshControl?.endRefreshing()
// maintain the current position in the list (don't scroll to top)

View File

@ -14,13 +14,7 @@ class TimelineTableViewController: EnhancedTableViewController {
var timeline: Timeline!
weak var mastodonController: MastodonController!
var timelineSegments: [[(id: String, state: StatusState)]] = [] {
didSet {
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
var timelineSegments: [[(id: String, state: StatusState)]] = []
var newer: RequestRange?
var older: RequestRange?
@ -69,6 +63,9 @@ class TimelineTableViewController: EnhancedTableViewController {
self.timelineSegments.insert(statuses.map { ($0.id, .unknown) }, at: 0)
self.newer = pagination?.newer
self.older = pagination?.older
DispatchQueue.main.async {
self.tableView.reloadData()
}
}
}
@ -105,7 +102,12 @@ class TimelineTableViewController: EnhancedTableViewController {
guard case let .success(newStatuses, pagination) = response else { fatalError() }
self.older = pagination?.older
self.mastodonController.cache.addAll(statuses: newStatuses)
let newRows = self.timelineSegments.last!.count..<(self.timelineSegments.last!.count + newStatuses.count)
let newIndexPaths = newRows.map { IndexPath(row: $0, section: self.timelineSegments.count - 1) }
self.timelineSegments[self.timelineSegments.count - 1].append(contentsOf: newStatuses.map { ($0.id, .unknown) })
DispatchQueue.main.async {
self.tableView.insertRows(at: newIndexPaths, with: .none)
}
}
}
}
@ -137,6 +139,11 @@ class TimelineTableViewController: EnhancedTableViewController {
}
DispatchQueue.main.async {
let newIndexPaths = (0..<newStatuses.count).map {
IndexPath(row: $0, section: 0)
}
self.tableView.insertRows(at: newIndexPaths, with: .automatic)
self.refreshControl?.endRefreshing()
// maintain the current position in the list (don't scroll to the top)