// // ExploreViewController.swift // Tusker // // Created by Shadowfacts on 12/14/19. // Copyright © 2019 Shadowfacts. All rights reserved. // import UIKit import Combine class ExploreViewController: EnhancedTableViewController { var dataSource: UITableViewDiffableDataSource! var resultsController: SearchResultsViewController! var searchController: UISearchController! let searchSubject = PassthroughSubject() init() { super.init(style: .insetGrouped) title = NSLocalizedString("Explore", comment: "explore tab title") tabBarItem.image = UIImage(systemName: "magnifyingglass") } required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") } override func viewDidLoad() { super.viewDidLoad() tableView.register(UINib(nibName: "BasicTableViewCell", bundle: .main), forCellReuseIdentifier: "basicCell") dataSource = DataSource(tableView: tableView, cellProvider: { (tableView, indexPath, item) -> UITableViewCell? in switch item { case .bookmarks: let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath) cell.imageView!.image = UIImage(systemName: "bookmark.fill") cell.textLabel!.text = NSLocalizedString("Bookmarks", comment: "bookmarks nav item title") cell.accessoryType = .disclosureIndicator return cell case let .list(id): let cell = tableView.dequeueReusableCell(withIdentifier: "basicCell", for: indexPath) cell.imageView!.image = nil cell.textLabel!.text = id cell.accessoryType = .disclosureIndicator return cell } }) var snapshot = NSDiffableDataSourceSnapshot() snapshot.appendSections([.bookmarks, .lists]) snapshot.appendItems([.bookmarks], toSection: .bookmarks) // the initial, static items should not be displayed with an animation UIView.performWithoutAnimation { dataSource.apply(snapshot) } resultsController = SearchResultsViewController() resultsController.exploreNavigationController = self.navigationController! searchController = UISearchController(searchResultsController: resultsController) searchController.searchResultsUpdater = resultsController searchController.searchBar.autocapitalizationType = .none searchController.searchBar.delegate = resultsController definesPresentationContext = true navigationItem.searchController = searchController navigationItem.hidesSearchBarWhenScrolling = false } // MARK: - Table view delegate override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { switch dataSource.itemIdentifier(for: indexPath) { case nil: return case .bookmarks: show(BookmarksTableViewController(), sender: nil) case let .list(id): show(TimelineTableViewController(for: .list(id: id)), sender: nil) } } } extension ExploreViewController { enum Section: CaseIterable { case bookmarks case lists } enum Item: Hashable { case bookmarks case list(id: String) } class DataSource: UITableViewDiffableDataSource { override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? { switch section { case 1: return NSLocalizedString("Lists", comment: "explore lists section title") default: return nil } } } }