feat: add prefetching feaature for reporting

This commit is contained in:
ihugo 2021-04-27 17:44:01 +08:00
parent 36a6b8b8cc
commit d66491b726
2 changed files with 48 additions and 0 deletions

View File

@ -68,6 +68,7 @@ class ReportViewController: UIViewController, NeedsDependency {
tableView.backgroundColor = .clear
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.delegate = self
tableView.prefetchDataSource = self
tableView.allowsMultipleSelection = true
return tableView
}()
@ -310,6 +311,7 @@ class ReportViewController: UIViewController, NeedsDependency {
}
}
// MARK: - UITableViewDelegate
extension ReportViewController: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard let item = viewModel.diffableDataSource?.itemIdentifier(for: indexPath) else {
@ -326,6 +328,14 @@ extension ReportViewController: UITableViewDelegate {
}
}
// MARK: - UITableViewDataSourcePrefetching
extension ReportViewController: UITableViewDataSourcePrefetching {
func tableView(_ tableView: UITableView, prefetchRowsAt indexPaths: [IndexPath]) {
viewModel.prefetchData(prefetchRowsAt: indexPaths)
}
}
// MARK: - UITextViewDelegate
extension ReportViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
self.comment.send(textView.text)

View File

@ -97,4 +97,42 @@ extension ReportViewModel {
}
.store(in: &disposeBag)
}
func prefetchData(prefetchRowsAt indexPaths: [IndexPath]) {
guard let diffableDataSource = diffableDataSource else { return }
// prefetch reply status
guard let activeMastodonAuthenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return }
let domain = activeMastodonAuthenticationBox.domain
var statusObjectIDs: [NSManagedObjectID] = []
for indexPath in indexPaths {
let item = diffableDataSource.itemIdentifier(for: indexPath)
switch item {
case .reportStatus(let objectID, _):
statusObjectIDs.append(objectID)
default:
continue
}
}
let backgroundManagedObjectContext = context.backgroundManagedObjectContext
backgroundManagedObjectContext.perform { [weak self] in
guard let self = self else { return }
for objectID in statusObjectIDs {
let status = backgroundManagedObjectContext.object(with: objectID) as! Status
guard let replyToID = status.inReplyToID, status.replyTo == nil else {
// skip
continue
}
self.context.statusPrefetchingService.prefetchReplyTo(
domain: domain,
statusObjectID: status.objectID,
statusID: status.id,
replyToStatusID: replyToID,
authorizationBox: activeMastodonAuthenticationBox
)
}
}
}
}