From 8d385e61f5a6a3230edaf2c945d9a7d83dbe5582 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Sun, 9 Jan 2022 18:01:19 -0500 Subject: [PATCH] Add counts to home cells --- Reader/SceneDelegate.swift | 1 + Reader/Screens/Home/HomeViewController.swift | 102 +++++++++++++++---- 2 files changed, 83 insertions(+), 20 deletions(-) diff --git a/Reader/SceneDelegate.swift b/Reader/SceneDelegate.swift index b826843..9ddb981 100644 --- a/Reader/SceneDelegate.swift +++ b/Reader/SceneDelegate.swift @@ -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) diff --git a/Reader/Screens/Home/HomeViewController.swift b/Reader/Screens/Home/HomeViewController.swift index 4bb5a48..a8521b4 100644 --- a/Reader/Screens/Home/HomeViewController.swift +++ b/Reader/Screens/Home/HomeViewController.swift @@ -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() - 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 { + let sectionHeaderCell = UICollectionView.SupplementaryRegistration(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 { 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(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 { + 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? { + 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) } }