forked from shadowfacts/Tusker
Add prefetching for images in table views
This commit is contained in:
parent
37f6a0b4c8
commit
7f3128c958
|
@ -40,6 +40,8 @@ class ConversationTableViewController: EnhancedTableViewController {
|
||||||
tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell")
|
tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell")
|
||||||
tableView.register(UINib(nibName: "ConversationMainStatusTableViewCell", bundle: nil), forCellReuseIdentifier: "mainStatusCell")
|
tableView.register(UINib(nibName: "ConversationMainStatusTableViewCell", bundle: nil), forCellReuseIdentifier: "mainStatusCell")
|
||||||
|
|
||||||
|
tableView.prefetchDataSource = self
|
||||||
|
|
||||||
statusIDs = [mainStatusID]
|
statusIDs = [mainStatusID]
|
||||||
|
|
||||||
guard let mainStatus = MastodonCache.status(for: mainStatusID) else { fatalError("Missing cached status \(mainStatusID!)") }
|
guard let mainStatus = MastodonCache.status(for: mainStatusID) else { fatalError("Missing cached status \(mainStatusID!)") }
|
||||||
|
@ -147,3 +149,25 @@ class ConversationTableViewController: EnhancedTableViewController {
|
||||||
}
|
}
|
||||||
|
|
||||||
extension ConversationTableViewController: StatusTableViewCellDelegate {}
|
extension ConversationTableViewController: StatusTableViewCellDelegate {}
|
||||||
|
|
||||||
|
extension ConversationTableViewController: UITableViewDataSourcePrefetching {
|
||||||
|
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
|
||||||
|
for indexPath in indexPaths {
|
||||||
|
guard let status = MastodonCache.status(for: statusIDs[indexPath.row]) else { continue }
|
||||||
|
ImageCache.avatars.get(status.account.avatar, completion: nil)
|
||||||
|
for attachment in status.attachments {
|
||||||
|
ImageCache.attachments.get(attachment.url, completion: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
|
||||||
|
for indexPath in indexPaths {
|
||||||
|
guard let status = MastodonCache.status(for: statusIDs[indexPath.row]) else { continue }
|
||||||
|
ImageCache.avatars.cancel(status.account.avatar)
|
||||||
|
for attachment in status.attachments {
|
||||||
|
ImageCache.attachments.cancel(attachment.url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -44,6 +44,8 @@ class NotificationsTableViewController: EnhancedTableViewController {
|
||||||
tableView.register(UINib(nibName: "ActionNotificationTableViewCell", bundle: nil), forCellReuseIdentifier: "actionCell")
|
tableView.register(UINib(nibName: "ActionNotificationTableViewCell", bundle: nil), forCellReuseIdentifier: "actionCell")
|
||||||
tableView.register(UINib(nibName: "FollowNotificationTableViewCell", bundle: nil), forCellReuseIdentifier: "followCell")
|
tableView.register(UINib(nibName: "FollowNotificationTableViewCell", bundle: nil), forCellReuseIdentifier: "followCell")
|
||||||
|
|
||||||
|
tableView.prefetchDataSource = self
|
||||||
|
|
||||||
let request = MastodonController.client.getNotifications()
|
let request = MastodonController.client.getNotifications()
|
||||||
MastodonController.client.run(request) { result in
|
MastodonController.client.run(request) { result in
|
||||||
guard case let .success(notifications, pagination) = result else { fatalError() }
|
guard case let .success(notifications, pagination) = result else { fatalError() }
|
||||||
|
@ -150,3 +152,19 @@ class NotificationsTableViewController: EnhancedTableViewController {
|
||||||
}
|
}
|
||||||
|
|
||||||
extension NotificationsTableViewController: StatusTableViewCellDelegate {}
|
extension NotificationsTableViewController: StatusTableViewCellDelegate {}
|
||||||
|
|
||||||
|
extension NotificationsTableViewController: UITableViewDataSourcePrefetching {
|
||||||
|
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
|
||||||
|
for indexPath in indexPaths {
|
||||||
|
let notification = notifications[indexPath.row]
|
||||||
|
ImageCache.avatars.get(notification.account.avatar, completion: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
|
||||||
|
for indexPath in indexPaths {
|
||||||
|
let notification = notifications[indexPath.row]
|
||||||
|
ImageCache.avatars.cancel(notification.account.url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -59,6 +59,8 @@ class ProfileTableViewController: EnhancedTableViewController, PreferencesAdapti
|
||||||
tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell")
|
tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell")
|
||||||
tableView.register(UINib(nibName: "ProfileHeaderTableViewCell", bundle: nil), forCellReuseIdentifier: "headerCell")
|
tableView.register(UINib(nibName: "ProfileHeaderTableViewCell", bundle: nil), forCellReuseIdentifier: "headerCell")
|
||||||
|
|
||||||
|
tableView.prefetchDataSource = self
|
||||||
|
|
||||||
if let accountID = accountID {
|
if let accountID = accountID {
|
||||||
if MastodonCache.account(for: accountID) != nil {
|
if MastodonCache.account(for: accountID) != nil {
|
||||||
updateAccountUI()
|
updateAccountUI()
|
||||||
|
@ -283,3 +285,27 @@ extension ProfileTableViewController: ProfileHeaderTableViewCellDelegate {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension ProfileTableViewController: UITableViewDataSourcePrefetching {
|
||||||
|
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
|
||||||
|
for indexPath in indexPaths {
|
||||||
|
guard indexPath.section == 1,
|
||||||
|
let status = MastodonCache.status(for: statusIDs[indexPath.row]) else { continue }
|
||||||
|
ImageCache.avatars.get(status.account.avatar, completion: nil)
|
||||||
|
for attachment in status.attachments {
|
||||||
|
ImageCache.attachments.get(attachment.url, completion: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
|
||||||
|
for indexPath in indexPaths {
|
||||||
|
guard indexPath.section == 1,
|
||||||
|
let status = MastodonCache.status(for: statusIDs[indexPath.row]) else { continue }
|
||||||
|
ImageCache.avatars.cancel(status.account.avatar)
|
||||||
|
for attachment in status.attachments {
|
||||||
|
ImageCache.attachments.cancel(attachment.url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -71,6 +71,8 @@ class TimelineTableViewController: EnhancedTableViewController {
|
||||||
|
|
||||||
tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell")
|
tableView.register(UINib(nibName: "StatusTableViewCell", bundle: nil), forCellReuseIdentifier: "statusCell")
|
||||||
|
|
||||||
|
tableView.prefetchDataSource = self
|
||||||
|
|
||||||
guard MastodonController.client?.accessToken != nil else { return }
|
guard MastodonController.client?.accessToken != nil else { return }
|
||||||
let request = MastodonController.client.getStatuses(timeline: timeline)
|
let request = MastodonController.client.getStatuses(timeline: timeline)
|
||||||
MastodonController.client.run(request) { response in
|
MastodonController.client.run(request) { response in
|
||||||
|
@ -168,3 +170,25 @@ class TimelineTableViewController: EnhancedTableViewController {
|
||||||
}
|
}
|
||||||
|
|
||||||
extension TimelineTableViewController: StatusTableViewCellDelegate {}
|
extension TimelineTableViewController: StatusTableViewCellDelegate {}
|
||||||
|
|
||||||
|
extension TimelineTableViewController: UITableViewDataSourcePrefetching {
|
||||||
|
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
|
||||||
|
for indexPath in indexPaths {
|
||||||
|
guard let status = MastodonCache.status(for: statusIDs[indexPath.row]) else { continue }
|
||||||
|
ImageCache.avatars.get(status.account.avatar, completion: nil)
|
||||||
|
for attachment in status.attachments {
|
||||||
|
ImageCache.attachments.get(attachment.url, completion: nil)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func tableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
|
||||||
|
for indexPath in indexPaths {
|
||||||
|
guard let status = MastodonCache.status(for: statusIDs[indexPath.row]) else { continue }
|
||||||
|
ImageCache.avatars.cancel(status.account.avatar)
|
||||||
|
for attachment in status.attachments {
|
||||||
|
ImageCache.attachments.cancel(attachment.url)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue