2022-01-10 00:23:22 +00:00
|
|
|
//
|
|
|
|
// HomeCollectionViewCell.swift
|
|
|
|
// Reader
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 1/9/22.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2022-06-19 18:56:56 +00:00
|
|
|
import Fervor
|
2022-09-10 04:09:46 +00:00
|
|
|
import Persistence
|
|
|
|
import OSLog
|
|
|
|
|
|
|
|
private let signposter = OSSignposter(subsystem: "net.shadowfacts.Reader", category: "HomeCollectionViewCell")
|
2022-01-10 00:23:22 +00:00
|
|
|
|
|
|
|
class HomeCollectionViewCell: UICollectionViewListCell {
|
|
|
|
|
2022-09-10 18:14:31 +00:00
|
|
|
private var currentItemCountTask: Task<Void, Never>?
|
2022-09-10 04:09:46 +00:00
|
|
|
private var itemCount: Int?
|
|
|
|
|
2022-01-15 19:09:30 +00:00
|
|
|
#if !targetEnvironment(macCatalyst)
|
2022-01-10 00:23:22 +00:00
|
|
|
override func updateConfiguration(using state: UICellConfigurationState) {
|
|
|
|
var backgroundConfig = UIBackgroundConfiguration.listGroupedCell().updated(for: state)
|
2022-01-11 03:36:54 +00:00
|
|
|
if state.isHighlighted || state.isSelected {
|
|
|
|
backgroundConfig.backgroundColor = .appCellHighlightBackground
|
|
|
|
} else {
|
2022-01-10 00:23:22 +00:00
|
|
|
backgroundConfig.backgroundColor = .appBackground
|
|
|
|
}
|
|
|
|
self.backgroundConfiguration = backgroundConfig
|
|
|
|
}
|
2022-01-15 19:09:30 +00:00
|
|
|
#endif
|
2022-01-10 00:23:22 +00:00
|
|
|
|
2022-09-10 04:09:46 +00:00
|
|
|
override func prepareForReuse() {
|
|
|
|
super.prepareForReuse()
|
2022-09-10 18:14:31 +00:00
|
|
|
currentItemCountTask?.cancel()
|
2022-09-10 04:09:46 +00:00
|
|
|
itemCount = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateUI(item: ItemListType, persistentContainer: PersistentContainer) {
|
|
|
|
var config = UIListContentConfiguration.valueCell()
|
|
|
|
config.text = item.title
|
|
|
|
if let itemCount {
|
|
|
|
config.secondaryText = itemCount.formatted(.number)
|
|
|
|
}
|
|
|
|
config.secondaryTextProperties.color = .tintColor
|
|
|
|
self.contentConfiguration = config
|
|
|
|
|
2022-09-10 18:14:31 +00:00
|
|
|
currentItemCountTask = Task(priority: .userInitiated) {
|
|
|
|
let state = signposter.beginInterval("fetch count", id: signposter.makeSignpostID(), "\(String(item.hashValue, radix: 16), privacy: .public)")
|
2022-09-10 04:09:46 +00:00
|
|
|
if let count = await fetchCount(item: item, in: persistentContainer),
|
2022-09-10 18:14:31 +00:00
|
|
|
!Task.isCancelled {
|
2022-09-10 04:09:46 +00:00
|
|
|
self.itemCount = count
|
|
|
|
config.secondaryText = count.formatted(.number)
|
|
|
|
self.contentConfiguration = config
|
|
|
|
}
|
|
|
|
signposter.endInterval("fetch count", state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func fetchCount(item: ItemListType, in persistentContainer: PersistentContainer) async -> Int? {
|
|
|
|
guard let request = item.countFetchRequest else {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return await withCheckedContinuation({ continuation in
|
2022-09-10 18:14:31 +00:00
|
|
|
let state = signposter.beginInterval("waiting to perform", id: signposter.makeSignpostID(), "\(String(item.hashValue, radix: 16), privacy: .public)")
|
|
|
|
persistentContainer.performBackgroundTask { context in
|
|
|
|
signposter.endInterval("waiting to perform", state)
|
|
|
|
let state = signposter.beginInterval("count", id: signposter.makeSignpostID(), "\(String(item.hashValue, radix: 16), privacy: .public)")
|
2022-09-10 04:09:46 +00:00
|
|
|
let count = try? context.count(for: request)
|
2022-09-10 18:14:31 +00:00
|
|
|
signposter.endInterval("count", state)
|
2022-09-10 04:09:46 +00:00
|
|
|
continuation.resume(returning: count)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-10 00:23:22 +00:00
|
|
|
}
|