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 } guard let windowScene = (scene as? UIWindowScene) else { return }
window = UIWindow(windowScene: windowScene) window = UIWindow(windowScene: windowScene)
window!.tintColor = .systemRed
if let account = LocalData.account { if let account = LocalData.account {
fervorController = FervorController(account: account) fervorController = FervorController(account: account)

View File

@ -32,16 +32,8 @@ class HomeViewController: UIViewController {
view.backgroundColor = .systemBackground view.backgroundColor = .systemBackground
// let label = UILabel() var configuration = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
// label.translatesAutoresizingMaskIntoConstraints = false configuration.headerMode = .supplementary
// 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)
let layout = UICollectionViewCompositionalLayout.list(using: configuration) let layout = UICollectionViewCompositionalLayout.list(using: configuration)
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout) collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight] collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
@ -51,7 +43,8 @@ class HomeViewController: UIViewController {
dataSource = createDataSource() dataSource = createDataSource()
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>() 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) dataSource.apply(snapshot, animatingDifferences: false)
let groupReq = Group.fetchRequest() let groupReq = Group.fetchRequest()
@ -67,10 +60,31 @@ class HomeViewController: UIViewController {
try! feedResultsController.performFetch() 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> { 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 let listCell = UICollectionView.CellRegistration<UICollectionViewListCell, Item> { cell, indexPath, item in
var config = cell.defaultContentConfiguration() var config = UIListContentConfiguration.valueCell()
config.text = item.title 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.contentConfiguration = config
cell.accessories = [.disclosureIndicator()] cell.accessories = [.disclosureIndicator()]
@ -78,6 +92,13 @@ class HomeViewController: UIViewController {
let dataSource = UICollectionViewDiffableDataSource<Section, Item>(collectionView: collectionView) { collectionView, indexPath, item in let dataSource = UICollectionViewDiffableDataSource<Section, Item>(collectionView: collectionView) { collectionView, indexPath, item in
return collectionView.dequeueConfiguredReusableCell(using: listCell, for: indexPath, item: item) 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 return dataSource
} }
@ -85,21 +106,69 @@ class HomeViewController: UIViewController {
extension HomeViewController { extension HomeViewController {
enum Section: Hashable { enum Section: Hashable {
case all
case groups case groups
case feeds case feeds
var title: String {
switch self {
case .all:
return ""
case .groups:
return "Groups"
case .feeds:
return "Feeds"
}
}
} }
enum Item: Hashable { enum Item: Hashable {
case unread
case all
case group(Group) case group(Group)
case feed(Feed) case feed(Feed)
var title: String { var title: String {
switch self { switch self {
case .unread:
return "Unread Articles"
case .all:
return "All Articles"
case let .group(group): case let .group(group):
return group.title return group.title
case let .feed(feed): case let .feed(feed):
return feed.title! 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 { extension HomeViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
collectionView.deselectItem(at: indexPath, animated: true)
guard let item = dataSource.itemIdentifier(for: indexPath) else { guard let item = dataSource.itemIdentifier(for: indexPath) else {
return return
} }
let req = Reader.Item.fetchRequest() let req = 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)
}
show(ItemsViewController(fervorController: fervorController, fetchRequest: req), sender: nil) show(ItemsViewController(fervorController: fervorController, fetchRequest: req), sender: nil)
} }
} }