mastodon-ios/Mastodon/Protocol/StatusProvider/StatusProvider+UITableViewD...

59 lines
2.5 KiB
Swift
Raw Normal View History

2021-03-10 12:12:53 +01:00
//
// StatusProvider+UITableViewDataSourcePrefetching.swift
// Mastodon
//
// Created by MainasuK Cirno on 2021-3-10.
//
import UIKit
import CoreData
import CoreDataStack
extension StatusTableViewCellDelegate where Self: StatusProvider {
func handleTableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
let statusObjectItems = self.statusObjectItems(indexPaths: indexPaths)
self.context.statusPrefetchingService.prefetch(statusObjectItems: statusObjectItems)
2021-04-01 08:39:15 +02:00
// prefetch reply status
2021-03-10 12:12:53 +01:00
guard let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return }
let domain = activeMastodonAuthenticationBox.domain
2021-07-02 13:48:56 +02:00
let items = self.items(indexPaths: indexPaths)
let managedObjectContext = context.managedObjectContext
managedObjectContext.perform { [weak self] in
2021-03-10 12:12:53 +01:00
guard let self = self else { return }
2021-07-02 13:48:56 +02:00
var statuses: [Status] = []
for item in items {
switch item {
case .homeTimelineIndex(let objectID, _):
guard let homeTimelineIndex = try? managedObjectContext.existingObject(with: objectID) as? HomeTimelineIndex else { continue }
statuses.append(homeTimelineIndex.status)
case .status(let objectID, _):
guard let status = try? managedObjectContext.existingObject(with: objectID) as? Status else { continue }
statuses.append(status)
default:
continue
}
}
for status in statuses {
if let replyToID = status.inReplyToID, status.replyTo == nil {
self.context.statusPrefetchingService.prefetchReplyTo(
domain: domain,
statusObjectID: status.objectID,
statusID: status.id,
replyToStatusID: replyToID,
authorizationBox: activeMastodonAuthenticationBox
)
2021-03-10 12:12:53 +01:00
}
2021-07-02 13:48:56 +02:00
} // end for in
} // end context.perform
} // end func
func handleTableView(_ tableView: UITableView, cancelPrefetchingForRowsAt indexPaths: [IndexPath]) {
let statusObjectItems = self.statusObjectItems(indexPaths: indexPaths)
self.context.statusPrefetchingService.cancelPrefetch(statusObjectItems: statusObjectItems)
}
2021-03-10 12:12:53 +01:00
}