Add counts to home cells

This commit is contained in:
Shadowfacts 2022-01-09 18:01:19 -05:00
parent eded49b266
commit 8d385e61f5
2 changed files with 83 additions and 20 deletions

View File

@ -20,6 +20,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene)
window!.tintColor = .systemRed
if let account = LocalData.account {
fervorController = FervorController(account: account)

View File

@ -32,16 +32,8 @@ class HomeViewController: UIViewController {
view.backgroundColor = .systemBackground
// let label = UILabel()
// label.translatesAutoresizingMaskIntoConstraints = false
// label.text = "Logged in to \(fervorController.instanceURL.host!)"
// view.addSubview(label)
// NSLayoutConstraint.activate([
// label.centerXAnchor.constraint(equalTo: view.centerXAnchor),
// label.centerYAnchor.constraint(equalTo: view.centerYAnchor),
// ])
let configuration = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
var configuration = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
configuration.headerMode = .supplementary
let layout = UICollectionViewCompositionalLayout.list(using: configuration)
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
@ -51,7 +43,8 @@ class HomeViewController: UIViewController {
dataSource = createDataSource()
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections([.groups, .feeds])
snapshot.appendSections([.all, .groups, .feeds])
snapshot.appendItems([.unread, .all], toSection: .all)
dataSource.apply(snapshot, animatingDifferences: false)
let groupReq = Group.fetchRequest()
@ -67,10 +60,31 @@ class HomeViewController: UIViewController {
try! feedResultsController.performFetch()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let indexPaths = collectionView.indexPathsForSelectedItems {
for indexPath in indexPaths {
collectionView.deselectItem(at: indexPath, animated: true)
}
}
}
private func createDataSource() -> UICollectionViewDiffableDataSource<Section, Item> {
let sectionHeaderCell = UICollectionView.SupplementaryRegistration<UICollectionViewListCell>(elementKind: UICollectionView.elementKindSectionHeader) { supplementaryView, elementKind, indexPath in
let section = self.dataSource.sectionIdentifier(for: indexPath.section)!
var config = supplementaryView.defaultContentConfiguration()
config.text = section.title
supplementaryView.contentConfiguration = config
}
let listCell = UICollectionView.CellRegistration<UICollectionViewListCell, Item> { cell, indexPath, item in
var config = cell.defaultContentConfiguration()
var config = UIListContentConfiguration.valueCell()
config.text = item.title
if let req = item.countFetchRequest,
let count = try? self.fervorController.persistentContainer.viewContext.count(for: req) {
config.secondaryText = "\(count)"
config.secondaryTextProperties.color = .tintColor
}
cell.contentConfiguration = config
cell.accessories = [.disclosureIndicator()]
@ -78,6 +92,13 @@ class HomeViewController: UIViewController {
let dataSource = UICollectionViewDiffableDataSource<Section, Item>(collectionView: collectionView) { collectionView, indexPath, item in
return collectionView.dequeueConfiguredReusableCell(using: listCell, for: indexPath, item: item)
}
dataSource.supplementaryViewProvider = { collectionView, elementKind, indexPath in
if elementKind == UICollectionView.elementKindSectionHeader {
return collectionView.dequeueConfiguredReusableSupplementary(using: sectionHeaderCell, for: indexPath)
} else {
return nil
}
}
return dataSource
}
@ -85,21 +106,69 @@ class HomeViewController: UIViewController {
extension HomeViewController {
enum Section: Hashable {
case all
case groups
case feeds
var title: String {
switch self {
case .all:
return ""
case .groups:
return "Groups"
case .feeds:
return "Feeds"
}
}
}
enum Item: Hashable {
case unread
case all
case group(Group)
case feed(Feed)
var title: String {
switch self {
case .unread:
return "Unread Articles"
case .all:
return "All Articles"
case let .group(group):
return group.title
case let .feed(feed):
return feed.title!
}
}
var fetchRequest: NSFetchRequest<Reader.Item> {
let req = Reader.Item.fetchRequest()
switch self {
case .unread:
req.predicate = NSPredicate(format: "read = NO")
case .all:
break
case .group(let group):
req.predicate = NSPredicate(format: "feed in %@", group.feeds!)
case .feed(let feed):
req.predicate = NSPredicate(format: "feed = %@", feed)
}
return req
}
var countFetchRequest: NSFetchRequest<Reader.Item>? {
let req = Reader.Item.fetchRequest()
switch self {
case .unread:
req.predicate = NSPredicate(format: "read = NO")
case .all:
return nil
case .group(let group):
req.predicate = NSPredicate(format: "read = NO AND feed in %@", group.feeds!)
case .feed(let feed):
req.predicate = NSPredicate(format: "read = NO AND feed = %@", feed)
}
return req
}
}
}
@ -123,17 +192,10 @@ extension HomeViewController: NSFetchedResultsControllerDelegate {
extension HomeViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.deselectItem(at: indexPath, animated: true)
guard let item = dataSource.itemIdentifier(for: indexPath) else {
return
}
let req = Reader.Item.fetchRequest()
switch item {
case .group(let group):
req.predicate = NSPredicate(format: "feed in %@", group.feeds!)
case .feed(let feed):
req.predicate = NSPredicate(format: "feed = %@", feed)
}
let req = item.fetchRequest
show(ItemsViewController(fervorController: fervorController, fetchRequest: req), sender: nil)
}
}