384 lines
14 KiB
Swift
384 lines
14 KiB
Swift
//
|
|
// MainSidebarViewController.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 6/24/20.
|
|
// Copyright © 2020 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Pachyderm
|
|
|
|
#if SDK_IOS_14
|
|
@available(iOS 14.0, *)
|
|
protocol MainSidebarViewControllerDelegate: class {
|
|
func sidebarRequestPresentCompose(_ sidebarViewController: MainSidebarViewController)
|
|
func sidebar(_ sidebarViewController: MainSidebarViewController, didSelectItem item: MainSidebarViewController.Item)
|
|
}
|
|
|
|
@available(iOS 14.0, *)
|
|
class MainSidebarViewController: UIViewController {
|
|
|
|
private weak var mastodonController: MastodonController!
|
|
|
|
weak var sidebarDelegate: MainSidebarViewControllerDelegate?
|
|
|
|
private var collectionView: UICollectionView!
|
|
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
|
|
|
|
var allItems: [Item] {
|
|
[
|
|
.tab(.timelines),
|
|
.tab(.notifications),
|
|
.tab(.myProfile),
|
|
] + exploreTabItems
|
|
}
|
|
|
|
var exploreTabItems: [Item] {
|
|
var items: [Item] = [.search, .bookmarks]
|
|
let snapshot = dataSource.snapshot()
|
|
for case let .list(list) in snapshot.itemIdentifiers(inSection: .lists) {
|
|
items.append(.list(list))
|
|
}
|
|
for case let .savedHashtag(hashtag) in snapshot.itemIdentifiers(inSection: .savedHashtags) {
|
|
items.append(.savedHashtag(hashtag))
|
|
}
|
|
for case let .savedInstance(instance) in snapshot.itemIdentifiers(inSection: .savedInstances) {
|
|
items.append(.savedInstance(instance))
|
|
}
|
|
return items
|
|
}
|
|
|
|
private(set) var previouslySelectedItem: Item?
|
|
var selectedItem: Item? {
|
|
guard let indexPath = collectionView.indexPathsForSelectedItems?.first else {
|
|
return nil
|
|
}
|
|
return dataSource.itemIdentifier(for: indexPath)
|
|
}
|
|
|
|
private(set) var itemLastSelectedTimestamps = [Item: Date]()
|
|
|
|
init(mastodonController: MastodonController) {
|
|
self.mastodonController = mastodonController
|
|
|
|
super.init(nibName: nil, bundle: nil)
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
navigationItem.title = "Tusker"
|
|
navigationItem.largeTitleDisplayMode = .always
|
|
navigationController!.navigationBar.prefersLargeTitles = true
|
|
|
|
let layout = UICollectionViewCompositionalLayout.list(using: .init(appearance: .sidebar))
|
|
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
|
|
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
collectionView.backgroundColor = .systemGroupedBackground
|
|
collectionView.delegate = self
|
|
view.addSubview(collectionView)
|
|
|
|
dataSource = createDataSource()
|
|
|
|
applyInitialSnapshot()
|
|
|
|
select(item: .tab(.timelines), animated: false)
|
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(reloadSavedHashtags), name: .savedHashtagsChanged, object: nil)
|
|
NotificationCenter.default.addObserver(self, selector: #selector(reloadSavedInstances), name: .savedInstancesChanged, object: nil)
|
|
}
|
|
|
|
func select(item: Item, animated: Bool) {
|
|
guard let indexPath = dataSource.indexPath(for: item) else { return }
|
|
collectionView.selectItem(at: indexPath, animated: animated, scrollPosition: .top)
|
|
itemLastSelectedTimestamps[item] = Date()
|
|
}
|
|
|
|
private func createDataSource() -> UICollectionViewDiffableDataSource<Section, Item> {
|
|
let listCell = UICollectionView.CellRegistration<UICollectionViewListCell, Item> { (cell, indexPath, item) in
|
|
var config = cell.defaultContentConfiguration()
|
|
config.text = item.title
|
|
if let imageName = item.imageName {
|
|
config.image = UIImage(systemName: imageName)
|
|
}
|
|
cell.contentConfiguration = config
|
|
}
|
|
|
|
let outlineHeaderCell = UICollectionView.CellRegistration<UICollectionViewListCell, Item> { (cell, indexPath, item) in
|
|
var config = cell.defaultContentConfiguration()
|
|
config.attributedText = NSAttributedString(string: item.title, attributes: [
|
|
.font: UIFont.boldSystemFont(ofSize: 21)
|
|
])
|
|
cell.contentConfiguration = config
|
|
cell.accessories = [.outlineDisclosure(options: .init(style: .header))]
|
|
}
|
|
|
|
return UICollectionViewDiffableDataSource<Section, Item>(collectionView: collectionView, cellProvider: { (collectionView, indexPath, item) -> UICollectionViewCell? in
|
|
if item.hasChildren {
|
|
return collectionView.dequeueConfiguredReusableCell(using: outlineHeaderCell, for: indexPath, item: item)
|
|
} else {
|
|
return collectionView.dequeueConfiguredReusableCell(using: listCell, for: indexPath, item: item)
|
|
}
|
|
})
|
|
}
|
|
|
|
private func applyInitialSnapshot() {
|
|
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
|
snapshot.appendSections(Section.allCases)
|
|
snapshot.appendItems([
|
|
.tab(.timelines),
|
|
.tab(.notifications),
|
|
.search,
|
|
.bookmarks,
|
|
.tab(.myProfile)
|
|
], toSection: .tabs)
|
|
snapshot.appendItems([
|
|
.tab(.compose)
|
|
], toSection: .compose)
|
|
dataSource.apply(snapshot, animatingDifferences: false)
|
|
|
|
reloadLists()
|
|
reloadSavedHashtags()
|
|
reloadSavedInstances()
|
|
}
|
|
|
|
private func reloadLists() {
|
|
let request = Client.getLists()
|
|
mastodonController.run(request) { [weak self] (response) in
|
|
guard let self = self, case let .success(lists, _) = response else { return }
|
|
|
|
var exploreSnapshot = NSDiffableDataSourceSectionSnapshot<Item>()
|
|
exploreSnapshot.append([.listsHeader])
|
|
exploreSnapshot.expand([.listsHeader])
|
|
exploreSnapshot.append(lists.map { .list($0) }, to: .listsHeader)
|
|
exploreSnapshot.append([.addList], to: .listsHeader)
|
|
DispatchQueue.main.async {
|
|
self.dataSource.apply(exploreSnapshot, to: .lists)
|
|
}
|
|
}
|
|
}
|
|
|
|
@objc private func reloadSavedHashtags() {
|
|
var hashtagsSnapshot = NSDiffableDataSourceSectionSnapshot<Item>()
|
|
hashtagsSnapshot.append([.savedHashtagsHeader])
|
|
hashtagsSnapshot.expand([.savedHashtagsHeader])
|
|
let sortedHashtags = SavedDataManager.shared.sortedHashtags(for: mastodonController.accountInfo!)
|
|
hashtagsSnapshot.append(sortedHashtags.map { .savedHashtag($0) }, to: .savedHashtagsHeader)
|
|
hashtagsSnapshot.append([.addSavedHashtag], to: .savedHashtagsHeader)
|
|
self.dataSource.apply(hashtagsSnapshot, to: .savedHashtags, animatingDifferences: false)
|
|
}
|
|
|
|
@objc private func reloadSavedInstances() {
|
|
var instancesSnapshot = NSDiffableDataSourceSectionSnapshot<Item>()
|
|
instancesSnapshot.append([.savedInstancesHeader])
|
|
instancesSnapshot.expand([.savedInstancesHeader])
|
|
let sortedInstances = SavedDataManager.shared.savedInstances(for: mastodonController.accountInfo!)
|
|
instancesSnapshot.append(sortedInstances.map { .savedInstance($0) }, to: .savedInstancesHeader)
|
|
instancesSnapshot.append([.addSavedInstance], to: .savedInstancesHeader)
|
|
self.dataSource.apply(instancesSnapshot, to: .savedInstances, animatingDifferences: false)
|
|
}
|
|
|
|
// todo: deduplicate with ExploreViewController
|
|
private func showAddList() {
|
|
let alert = UIAlertController(title: "New List", message: "Choose a title for your new list", preferredStyle: .alert)
|
|
alert.addTextField(configurationHandler: nil)
|
|
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
|
|
alert.addAction(UIAlertAction(title: "Create List", style: .default, handler: { (_) in
|
|
guard let title = alert.textFields?.first?.text else {
|
|
fatalError()
|
|
}
|
|
|
|
let request = Client.createList(title: title)
|
|
self.mastodonController.run(request) { (response) in
|
|
guard case let .success(list, _) = response else { fatalError() }
|
|
|
|
self.reloadLists()
|
|
|
|
DispatchQueue.main.async {
|
|
self.sidebarDelegate?.sidebar(self, didSelectItem: .list(list))
|
|
}
|
|
}
|
|
}))
|
|
present(alert, animated: true)
|
|
}
|
|
|
|
// todo: deduplicate with ExploreViewController
|
|
private func showAddSavedHashtag() {
|
|
let navController = EnhancedNavigationViewController(rootViewController: AddSavedHashtagViewController(mastodonController: mastodonController))
|
|
present(navController, animated: true)
|
|
}
|
|
|
|
// todo: deduplicate with ExploreViewController
|
|
private func showAddSavedInstance() {
|
|
let findController = FindInstanceViewController(parentMastodonController: mastodonController)
|
|
findController.instanceTimelineDelegate = self
|
|
let navController = EnhancedNavigationViewController(rootViewController: findController)
|
|
present(navController, animated: true)
|
|
}
|
|
|
|
}
|
|
|
|
@available(iOS 14.0, *)
|
|
extension MainSidebarViewController {
|
|
enum Section: Int, Hashable, CaseIterable {
|
|
case tabs
|
|
case compose
|
|
case lists
|
|
case savedHashtags
|
|
case savedInstances
|
|
}
|
|
enum Item: Hashable {
|
|
case tab(MainTabBarViewController.Tab)
|
|
case search, bookmarks
|
|
case listsHeader, list(List), addList
|
|
case savedHashtagsHeader, savedHashtag(Hashtag), addSavedHashtag
|
|
case savedInstancesHeader, savedInstance(URL), addSavedInstance
|
|
|
|
var title: String {
|
|
switch self {
|
|
case let .tab(tab):
|
|
return tab.title
|
|
case .search:
|
|
return "Search"
|
|
case .bookmarks:
|
|
return "Bookmarks"
|
|
case .listsHeader:
|
|
return "Lists"
|
|
case let .list(list):
|
|
return list.title
|
|
case .addList:
|
|
return "New List..."
|
|
case .savedHashtagsHeader:
|
|
return "Saved Hashtags"
|
|
case let .savedHashtag(hashtag):
|
|
return hashtag.name
|
|
case .addSavedHashtag:
|
|
return "Save Hashtag..."
|
|
case .savedInstancesHeader:
|
|
return "Saved Instances"
|
|
case let .savedInstance(url):
|
|
return url.host!
|
|
case .addSavedInstance:
|
|
return "Find An Instance..."
|
|
}
|
|
}
|
|
|
|
var imageName: String? {
|
|
switch self {
|
|
case let .tab(tab):
|
|
return tab.imageName
|
|
case .search:
|
|
return "magnifyingglass"
|
|
case .bookmarks:
|
|
return "bookmark"
|
|
case .list(_):
|
|
return "list.bullet"
|
|
case .savedHashtag(_):
|
|
return "number"
|
|
case .savedInstance(_):
|
|
return "globe"
|
|
case .listsHeader, .savedHashtagsHeader, .savedInstancesHeader:
|
|
return nil
|
|
case .addList, .addSavedHashtag, .addSavedInstance:
|
|
return "plus"
|
|
}
|
|
}
|
|
|
|
var hasChildren: Bool {
|
|
switch self {
|
|
case .listsHeader, .savedHashtagsHeader, .savedInstancesHeader:
|
|
return true
|
|
default:
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fileprivate extension MainTabBarViewController.Tab {
|
|
var title: String {
|
|
switch self {
|
|
case .timelines:
|
|
return "Home"
|
|
case .notifications:
|
|
return "Notifications"
|
|
case .compose:
|
|
return "Compose"
|
|
case .explore:
|
|
return "Explore"
|
|
case .myProfile:
|
|
return "My Profile"
|
|
}
|
|
}
|
|
|
|
var imageName: String? {
|
|
switch self {
|
|
case .timelines:
|
|
return "house"
|
|
case .notifications:
|
|
return "bell"
|
|
case .compose:
|
|
return "pencil"
|
|
case .explore:
|
|
return "magnifyingglass"
|
|
case .myProfile:
|
|
// todo: use user avatar image
|
|
return "person"
|
|
}
|
|
}
|
|
}
|
|
|
|
@available(iOS 14.0, *)
|
|
extension MainSidebarViewController: UICollectionViewDelegate {
|
|
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
|
|
previouslySelectedItem = selectedItem
|
|
return true
|
|
}
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
|
guard let item = dataSource.itemIdentifier(for: indexPath) else {
|
|
collectionView.deselectItem(at: indexPath, animated: true)
|
|
return
|
|
}
|
|
itemLastSelectedTimestamps[item] = Date()
|
|
if [MainSidebarViewController.Item.tab(.compose), .addList, .addSavedHashtag, .addSavedInstance].contains(item) {
|
|
if let previous = previouslySelectedItem, let indexPath = dataSource.indexPath(for: previous) {
|
|
collectionView.selectItem(at: indexPath, animated: false, scrollPosition: .centeredVertically)
|
|
}
|
|
switch item {
|
|
case .tab(.compose):
|
|
sidebarDelegate?.sidebarRequestPresentCompose(self)
|
|
case .addList:
|
|
showAddList()
|
|
case .addSavedHashtag:
|
|
showAddSavedHashtag()
|
|
case .addSavedInstance:
|
|
showAddSavedInstance()
|
|
default:
|
|
fatalError("unreachable")
|
|
}
|
|
} else {
|
|
sidebarDelegate?.sidebar(self, didSelectItem: item)
|
|
}
|
|
}
|
|
}
|
|
|
|
@available(iOS 14.0, *)
|
|
extension MainSidebarViewController: InstanceTimelineViewControllerDelegate {
|
|
func didSaveInstance(url: URL) {
|
|
dismiss(animated: true) {
|
|
self.sidebarDelegate?.sidebar(self, didSelectItem: .savedInstance(url))
|
|
}
|
|
}
|
|
|
|
func didUnsaveInstance(url: URL) {
|
|
dismiss(animated: true)
|
|
}
|
|
}
|
|
#endif
|