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

56 lines
2.2 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]) {
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
var statusObjectIDs: [NSManagedObjectID] = []
for item in items(indexPaths: indexPaths) {
switch item {
case .homeTimelineIndex(let objectID, _):
let homeTimelineIndex = managedObjectContext.object(with: objectID) as! HomeTimelineIndex
2021-04-01 08:39:15 +02:00
statusObjectIDs.append(homeTimelineIndex.status.objectID)
case .status(let objectID, _):
2021-03-10 12:12:53 +01:00
statusObjectIDs.append(objectID)
default:
continue
}
}
let backgroundManagedObjectContext = context.backgroundManagedObjectContext
backgroundManagedObjectContext.perform { [weak self] in
guard let self = self else { return }
for objectID in statusObjectIDs {
2021-04-01 08:39:15 +02:00
let status = backgroundManagedObjectContext.object(with: objectID) as! Status
// fetch in-reply info if needs
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
}
// self.context.statusContentCacheService.prefetch(
// content: (status.reblog ?? status).content,
// emojiDict: (status.reblog ?? status).emojiDict
// )
2021-03-10 12:12:53 +01:00
}
}
}
}