feat: add notification gap fetcher

This commit is contained in:
CMK 2022-02-08 20:07:50 +08:00
parent bdf7114fef
commit 8cd409de86
3 changed files with 47 additions and 2 deletions

View File

@ -49,8 +49,10 @@ extension NotificationSection {
)
}
return cell
case .feedLoader(let record):
return UITableViewCell()
case .feedLoader:
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self), for: indexPath) as! TimelineBottomLoaderTableViewCell
cell.activityIndicatorView.startAnimating()
return cell
case .bottomLoader:
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TimelineBottomLoaderTableViewCell.self), for: indexPath) as! TimelineBottomLoaderTableViewCell
cell.activityIndicatorView.startAnimating()

View File

@ -135,6 +135,18 @@ extension NotificationTimelineViewController: UITableViewDelegate, AutoGenerateT
}
// sourcery:end
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
guard let item = viewModel.diffableDataSource?.itemIdentifier(for: indexPath) else {
return
}
// check item type inside `loadMore`
Task {
await viewModel.loadMore(item: item)
}
}
}
// MARK: - NotificationTableViewCellDelegate

View File

@ -156,4 +156,35 @@ extension NotificationTimelineViewModel {
}
}
// load timeline gap
func loadMore(item: NotificationItem) async {
guard case let .feedLoader(record) = item else { return }
guard let authenticationBox = context.authenticationService.activeMastodonAuthenticationBox.value else { return }
let managedObjectContext = context.managedObjectContext
let key = "LoadMore@\(record.objectID)"
// return when already loading state
guard managedObjectContext.cache(froKey: key) == nil else { return }
guard let feed = record.object(in: managedObjectContext) else { return }
guard let maxID = feed.notification?.id else { return }
// keep transient property live
managedObjectContext.cache(feed, key: key)
defer {
managedObjectContext.cache(nil, key: key)
}
// fetch data
do {
_ = try await context.apiService.notifications(
maxID: maxID,
scope: scope,
authenticationBox: authenticationBox
)
} catch {
logger.log(level: .debug, "\((#file as NSString).lastPathComponent, privacy: .public)[\(#line, privacy: .public)], \(#function, privacy: .public): fetch more failure: \(error.localizedDescription)")
}
}
}