// // HomeViewController.swift // Reader // // Created by Shadowfacts on 11/25/21. // import UIKit import CoreData import Combine import Persistence protocol HomeViewControllerDelegate: AnyObject { func switchToAccount(_ account: LocalData.Account) } class HomeViewController: UIViewController { weak var delegate: HomeViewControllerDelegate? weak var itemsDelegate: ItemsViewControllerDelegate? let fervorController: FervorController var enableStretchyMenu = true private var collectionView: UICollectionView! private var dataSource: UICollectionViewDiffableDataSource! private var groupResultsController: NSFetchedResultsController! private var feedResultsController: NSFetchedResultsController! private var lastSyncState = FervorController.SyncState.done // weak so that when it's removed from the superview, this becomes nil private weak var syncStateView: SyncStateView? private var cancellables = Set() init(fervorController: FervorController) { self.fervorController = fervorController super.init(nibName: nil, bundle: nil) fervorController.syncState .buffer(size: 25, prefetch: .byRequest, whenFull: .dropOldest) .receive(on: RunLoop.main) .sink { [unowned self] in self.syncStateChanged($0) } .store(in: &cancellables) } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() // todo: account info title = "Reader" if enableStretchyMenu { view.addInteraction(StretchyMenuInteraction(delegate: self)) } if UIDevice.current.userInterfaceIdiom != .mac { view.backgroundColor = .appBackground } var config = UICollectionLayoutListConfiguration(appearance: UIDevice.current.userInterfaceIdiom == .mac ? .sidebar : .grouped) config.headerMode = .supplementary config.backgroundColor = .clear config.separatorConfiguration.topSeparatorVisibility = .visible config.separatorConfiguration.topSeparatorInsets = NSDirectionalEdgeInsets(top: 0, leading: 20, bottom: 0, trailing: 0) config.separatorConfiguration.bottomSeparatorVisibility = .hidden config.itemSeparatorHandler = { indexPath, defaultConfig in var config = defaultConfig if indexPath.section == 0 && indexPath.row == 0 { config.topSeparatorVisibility = .hidden } return config } let layout = UICollectionViewCompositionalLayout.list(using: config) collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout) collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight] collectionView.delegate = self view.addSubview(collectionView) dataSource = createDataSource() var snapshot = NSDiffableDataSourceSnapshot() snapshot.appendSections([.all, .groups, .feeds]) snapshot.appendItems([.unread, .all], toSection: .all) dataSource.apply(snapshot, animatingDifferences: false) let groupReq = Group.fetchRequest() groupReq.sortDescriptors = [NSSortDescriptor(key: "title", ascending: true)] groupResultsController = NSFetchedResultsController(fetchRequest: groupReq, managedObjectContext: fervorController.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil) groupResultsController.delegate = self try! groupResultsController.performFetch() let feedReq = Feed.fetchRequest() feedReq.sortDescriptors = [NSSortDescriptor(key: "title", ascending: true)] feedResultsController = NSFetchedResultsController(fetchRequest: feedReq, managedObjectContext: fervorController.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil) feedResultsController.delegate = self 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) } } var snapshot = dataSource.snapshot() // reconfigure so that unread counts update snapshot.reconfigureItems(snapshot.itemIdentifiers) dataSource.apply(snapshot) } 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 { [unowned self] cell, indexPath, item in cell.updateUI(item: item, persistentContainer: self.fervorController.persistentContainer) cell.accessories = [.disclosureIndicator()] } 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 } private func syncStateChanged(_ newState: FervorController.SyncState) { if case .done = newState { // update unread counts for visible items var snapshot = dataSource.snapshot() snapshot.reconfigureItems(snapshot.itemIdentifiers) dataSource.apply(snapshot, animatingDifferences: false) if syncStateView == nil { // no sync state view, nothing further to update return } } func updateView(_ syncStateView: SyncStateView, isFirstUpdate: Bool) { switch newState { case .groupsAndFeeds: syncStateView.label.text = "Syncing groups and feeds" case .items: syncStateView.label.text = "Syncing items" case .updateItems(current: let current, total: let total): syncStateView.label.text = "Updating \(current + 1) of \(total) item\(total == 1 ? "" : "s")" case .excerpts(current: let current, total: let total): syncStateView.label.text = "Generating \(current) of \(total) excerpt\(total == 1 ? "" : "s")" case .error(let error): syncStateView.label.text = "Error syncing" syncStateView.subtitleLabel.isHidden = false syncStateView.subtitleLabel.text = error.localizedDescription syncStateView.removeAfterDelay(delay: isFirstUpdate ? 2 : 1) case .done: syncStateView.label.text = "Done syncing" syncStateView.removeAfterDelay(delay: isFirstUpdate ? 2 : 1) } } if let syncStateView = self.syncStateView { updateView(syncStateView, isFirstUpdate: false) } else { let syncStateView = SyncStateView() self.syncStateView = syncStateView syncStateView.translatesAutoresizingMaskIntoConstraints = false view.addSubview(syncStateView) NSLayoutConstraint.activate([ syncStateView.leadingAnchor.constraint(equalTo: view.leadingAnchor), syncStateView.trailingAnchor.constraint(equalTo: view.trailingAnchor), syncStateView.bottomAnchor.constraint(equalTo: view.bottomAnchor), ]) updateView(syncStateView, isFirstUpdate: true) view.layoutIfNeeded() syncStateView.transform = CGAffineTransform(translationX: 0, y: syncStateView.bounds.height) UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseOut) { syncStateView.transform = .identity } } } private func itemsViewController(for item: ItemListType) -> ItemsViewController { let vc = ItemsViewController(type: item, fervorController: fervorController) vc.delegate = itemsDelegate return vc } func selectItem(_ item: ItemListType) { guard let navigationController = navigationController else { return } if navigationController.viewControllers.count >= 2, let second = navigationController.viewControllers[1] as? ItemsViewController, second.type == item { while navigationController.viewControllers.count > 2 { navigationController.popViewController(animated: false) } } else { navigationController.popToRootViewController(animated: false) navigationController.pushViewController(itemsViewController(for: item), animated: false) } } } 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" } } } } extension HomeViewController: NSFetchedResultsControllerDelegate { func controller(_ controller: NSFetchedResultsController, didChangeContentWith snapshot: NSDiffableDataSourceSnapshotReference) { var snapshot = dataSource.snapshot() if controller == groupResultsController { if snapshot.sectionIdentifiers.contains(.groups) { snapshot.deleteItems(snapshot.itemIdentifiers(inSection: .groups)) } snapshot.appendItems(controller.fetchedObjects!.map { .group($0 as! Group) }, toSection: .groups) } else if controller == feedResultsController { if snapshot.sectionIdentifiers.contains(.feeds) { snapshot.deleteItems(snapshot.itemIdentifiers(inSection: .feeds)) } snapshot.appendItems(controller.fetchedObjects!.map { .feed($0 as! Feed) }, toSection: .feeds) } dataSource.apply(snapshot, animatingDifferences: false) } } extension HomeViewController: UICollectionViewDelegate { func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { guard let item = dataSource.itemIdentifier(for: indexPath) else { return } show(itemsViewController(for: item), sender: nil) UISelectionFeedbackGenerator().selectionChanged() } func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? { guard let item = dataSource.itemIdentifier(for: indexPath) else { return nil } return UIContextMenuConfiguration(identifier: nil, previewProvider: { [unowned self] in return self.itemsViewController(for: item) }, actionProvider: nil) } func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) { guard let vc = animator.previewViewController else { return } animator.preferredCommitStyle = .pop animator.addCompletion { self.show(vc, sender: nil) } } } extension HomeViewController: StretchyMenuInteractionDelegate { func stretchyMenuTitle() -> String? { return "Switch Accounts" } func stretchyMenuItems() -> [StretchyMenuItem] { var items: [StretchyMenuItem] = LocalData.accounts.map { account in var title = account.instanceURL.host! if let port = account.instanceURL.port, port != 80 && port != 443 { title += ":\(port)" } let subtitle = account.id == fervorController.account!.id ? "Currently logged in" : nil let menu = UIMenu(children: [ UIAction(title: "Log Out", image: UIImage(systemName: "door.left.hand.open"), handler: { _ in guard let index = LocalData.accounts.firstIndex(where: { $0.id == account.id }) else { return } LocalData.accounts.remove(at: index) NotificationCenter.default.post(name: .logoutAccount, object: account) }) ]) return StretchyMenuItem(title: title, subtitle: subtitle, menu: menu) { [unowned self] in guard account.id != self.fervorController.account!.id else { return } self.delegate?.switchToAccount(account) } } items.append(StretchyMenuItem(title: "Add Account", action: { [unowned self] in let login = LoginViewController() login.delegate = self login.navigationItem.leftBarButtonItem = UIBarButtonItem(systemItem: .cancel, primaryAction: UIAction(handler: { (_) in self.dismiss(animated: true) }), menu: nil) let nav = UINavigationController(rootViewController: login) self.present(nav, animated: true) })) return items } } extension HomeViewController: LoginViewControllerDelegate { func didLogin(with controller: FervorController) { self.dismiss(animated: true) (view.window!.windowScene!.delegate as! SceneDelegate).didLogin(with: controller) } } private class SyncStateView: UIView { let label = UILabel() let subtitleLabel = UILabel() init() { super.init(frame: .zero) let blur = UIBlurEffect(style: .regular) let blurView = UIVisualEffectView(effect: blur) blurView.translatesAutoresizingMaskIntoConstraints = false addSubview(blurView) label.font = .preferredFont(forTextStyle: .callout) subtitleLabel.font = .preferredFont(forTextStyle: .caption1) subtitleLabel.isHidden = true subtitleLabel.numberOfLines = 2 let stack = UIStackView(arrangedSubviews: [ label, subtitleLabel, ]) stack.translatesAutoresizingMaskIntoConstraints = false stack.axis = .vertical stack.alignment = .center stack.spacing = 4 addSubview(stack) NSLayoutConstraint.activate([ blurView.leadingAnchor.constraint(equalTo: leadingAnchor), blurView.trailingAnchor.constraint(equalTo: trailingAnchor), blurView.topAnchor.constraint(equalTo: topAnchor), blurView.bottomAnchor.constraint(equalTo: bottomAnchor), stack.centerXAnchor.constraint(equalTo: safeAreaLayoutGuide.centerXAnchor), stack.topAnchor.constraint(greaterThanOrEqualToSystemSpacingBelow: safeAreaLayoutGuide.topAnchor, multiplier: 1), stack.bottomAnchor.constraint(lessThanOrEqualToSystemSpacingBelow: safeAreaLayoutGuide.bottomAnchor, multiplier: 1), stack.centerYAnchor.constraint(equalTo: safeAreaLayoutGuide.centerYAnchor), topAnchor.constraint(lessThanOrEqualTo: safeAreaLayoutGuide.bottomAnchor, constant: -50), ]) layer.shadowColor = UIColor.black.cgColor layer.shadowOpacity = 0.1 layer.shadowRadius = 10 } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } func removeAfterDelay(delay: Int) { // can't use UIView.animate's delay b/c it may clash with the appearance animation DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(delay)) { UIView.animate(withDuration: 0.25, delay: 0, options: .curveEaseIn) { self.transform = CGAffineTransform(translationX: 0, y: self.bounds.height) } completion: { _ in self.removeFromSuperview() } } } }