2019-12-17 05:22:25 +00:00
|
|
|
//
|
|
|
|
// ExploreViewController.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 12/14/19.
|
|
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Combine
|
2019-12-17 23:48:29 +00:00
|
|
|
import Pachyderm
|
2022-05-11 02:57:46 +00:00
|
|
|
import CoreData
|
2022-07-09 15:45:27 +00:00
|
|
|
import WebURLFoundationExtras
|
2019-12-17 05:22:25 +00:00
|
|
|
|
2022-12-28 20:01:21 +00:00
|
|
|
class ExploreViewController: UIViewController, UICollectionViewDelegate, CollectionViewController {
|
2019-12-17 05:22:25 +00:00
|
|
|
|
2020-05-13 22:57:04 +00:00
|
|
|
weak var mastodonController: MastodonController!
|
2020-01-05 20:25:07 +00:00
|
|
|
|
2022-12-28 20:01:21 +00:00
|
|
|
var collectionView: UICollectionView!
|
2021-02-06 18:48:31 +00:00
|
|
|
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
|
2019-12-17 05:22:25 +00:00
|
|
|
|
2021-02-06 18:48:31 +00:00
|
|
|
private(set) var resultsController: SearchResultsViewController!
|
|
|
|
private(set) var searchController: UISearchController!
|
2019-12-17 05:22:25 +00:00
|
|
|
|
2020-06-30 02:21:03 +00:00
|
|
|
var searchControllerStatusOnAppearance: Bool? = nil
|
|
|
|
|
2022-12-13 03:02:07 +00:00
|
|
|
private var cancellables = Set<AnyCancellable>()
|
2022-11-19 19:08:39 +00:00
|
|
|
|
2020-01-05 20:25:07 +00:00
|
|
|
init(mastodonController: MastodonController) {
|
|
|
|
self.mastodonController = mastodonController
|
|
|
|
|
2021-02-06 18:48:31 +00:00
|
|
|
super.init(nibName: nil, bundle: nil)
|
2020-12-14 23:44:41 +00:00
|
|
|
|
2019-12-17 05:22:25 +00:00
|
|
|
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()
|
2019-12-18 02:18:32 +00:00
|
|
|
|
2021-02-06 18:48:31 +00:00
|
|
|
var configuration = UICollectionLayoutListConfiguration(appearance: .insetGrouped)
|
|
|
|
configuration.trailingSwipeActionsConfigurationProvider = self.trailingSwipeActionsForCell(at:)
|
|
|
|
configuration.headerMode = .supplementary
|
|
|
|
let layout = UICollectionViewCompositionalLayout.list(using: configuration)
|
|
|
|
collectionView = UICollectionView(frame: view.bounds, collectionViewLayout: layout)
|
|
|
|
collectionView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
|
|
collectionView.delegate = self
|
|
|
|
collectionView.dragDelegate = self
|
2023-01-16 22:47:56 +00:00
|
|
|
collectionView.allowsFocus = true
|
2021-02-06 18:48:31 +00:00
|
|
|
view.addSubview(collectionView)
|
2019-12-17 05:22:25 +00:00
|
|
|
|
2021-02-06 18:48:31 +00:00
|
|
|
dataSource = createDataSource()
|
|
|
|
applyInitialSnapshot()
|
2020-01-20 16:48:47 +00:00
|
|
|
|
2021-02-08 00:52:59 +00:00
|
|
|
if mastodonController.instance == nil {
|
|
|
|
mastodonController.getOwnInstance(completion: self.ownInstanceLoaded(_:))
|
|
|
|
}
|
|
|
|
|
2020-01-05 20:25:07 +00:00
|
|
|
resultsController = SearchResultsViewController(mastodonController: mastodonController)
|
2019-12-17 05:22:25 +00:00
|
|
|
resultsController.exploreNavigationController = self.navigationController!
|
|
|
|
searchController = UISearchController(searchResultsController: resultsController)
|
|
|
|
searchController.searchResultsUpdater = resultsController
|
2023-01-22 16:41:38 +00:00
|
|
|
if #available(iOS 16.0, *) {
|
|
|
|
searchController.scopeBarActivation = .onSearchActivation
|
|
|
|
}
|
2019-12-17 05:22:25 +00:00
|
|
|
searchController.searchBar.autocapitalizationType = .none
|
|
|
|
searchController.searchBar.delegate = resultsController
|
2023-01-22 16:41:38 +00:00
|
|
|
searchController.searchBar.scopeButtonTitles = SearchResultsViewController.Scope.allCases.map(\.title)
|
2019-12-17 05:22:25 +00:00
|
|
|
definesPresentationContext = true
|
|
|
|
|
|
|
|
navigationItem.searchController = searchController
|
|
|
|
navigationItem.hidesSearchBarWhenScrolling = false
|
2019-12-17 23:48:29 +00:00
|
|
|
|
2019-12-20 03:41:23 +00:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(savedInstancesChanged), name: .savedInstancesChanged, object: nil)
|
2022-04-01 23:23:49 +00:00
|
|
|
NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil)
|
2022-11-19 19:08:39 +00:00
|
|
|
|
2022-12-13 03:02:07 +00:00
|
|
|
mastodonController.$lists
|
2022-11-19 19:08:39 +00:00
|
|
|
.sink { [unowned self] in self.reloadLists($0) }
|
2022-12-13 03:02:07 +00:00
|
|
|
.store(in: &cancellables)
|
|
|
|
mastodonController.$followedHashtags
|
|
|
|
.merge(with:
|
|
|
|
NotificationCenter.default.publisher(for: .savedHashtagsChanged)
|
|
|
|
.map { [unowned self] _ in self.mastodonController.followedHashtags }
|
|
|
|
)
|
|
|
|
.sink { [unowned self] in self.updateHashtagsSection(followed: $0) }
|
|
|
|
.store(in: &cancellables)
|
|
|
|
|
|
|
|
let a = PassthroughSubject<Int, Never>()
|
|
|
|
let b = PassthroughSubject<Int, Never>()
|
|
|
|
|
|
|
|
a.merge(with: b)
|
|
|
|
.sink(receiveValue: { print($0) })
|
|
|
|
.store(in: &cancellables)
|
2021-02-06 18:48:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
super.viewWillAppear(animated)
|
2019-12-20 02:20:29 +00:00
|
|
|
|
2022-12-28 20:01:21 +00:00
|
|
|
clearSelectionOnAppear(animated: animated)
|
2019-12-18 02:18:32 +00:00
|
|
|
}
|
|
|
|
|
2020-06-30 02:21:03 +00:00
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
|
|
|
|
|
|
|
// this is a workaround for the issue that setting isActive on a search controller that is not visible
|
|
|
|
// does not cause it to automatically become active once it becomes visible
|
|
|
|
// see FB7814561
|
|
|
|
if let active = searchControllerStatusOnAppearance {
|
|
|
|
searchController.isActive = active
|
|
|
|
searchControllerStatusOnAppearance = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 18:48:31 +00:00
|
|
|
private func createDataSource() -> UICollectionViewDiffableDataSource<Section, Item> {
|
|
|
|
let sectionHeaderCell = UICollectionView.SupplementaryRegistration<UICollectionViewListCell>(elementKind: UICollectionView.elementKindSectionHeader) { (headerView, collectionView, indexPath) in
|
|
|
|
let section = self.dataSource.snapshot().sectionIdentifiers[indexPath.section]
|
|
|
|
|
|
|
|
var config = headerView.defaultContentConfiguration()
|
|
|
|
config.text = section.label
|
|
|
|
headerView.contentConfiguration = config
|
|
|
|
}
|
|
|
|
|
|
|
|
let listCell = UICollectionView.CellRegistration<UICollectionViewListCell, Item> { (cell, indexPath, item) in
|
|
|
|
var config = cell.defaultContentConfiguration()
|
|
|
|
config.text = item.label
|
|
|
|
config.image = item.image
|
|
|
|
cell.contentConfiguration = config
|
|
|
|
|
|
|
|
switch item {
|
|
|
|
case .addList, .addSavedHashtag, .findInstance:
|
|
|
|
cell.accessories = []
|
|
|
|
default:
|
|
|
|
cell.accessories = [.disclosureIndicator()]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let dataSource = UICollectionViewDiffableDataSource<Section, Item>(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 applyInitialSnapshot() {
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
2021-02-08 00:52:59 +00:00
|
|
|
snapshot.appendSections(Section.allCases.filter { $0 != .discover })
|
2023-02-04 18:21:58 +00:00
|
|
|
snapshot.appendItems([.bookmarks, .favorites], toSection: .bookmarks)
|
2022-11-15 02:17:08 +00:00
|
|
|
if mastodonController.instanceFeatures.trends,
|
2022-04-01 23:23:49 +00:00
|
|
|
!Preferences.shared.hideDiscover {
|
|
|
|
addDiscoverSection(to: &snapshot)
|
2021-02-08 00:52:59 +00:00
|
|
|
}
|
2021-02-06 18:48:31 +00:00
|
|
|
snapshot.appendItems([.addList], toSection: .lists)
|
2022-12-13 03:02:07 +00:00
|
|
|
let hashtags = fetchHashtagItems(followed: mastodonController.followedHashtags)
|
2022-05-11 02:57:46 +00:00
|
|
|
snapshot.appendItems(hashtags, toSection: .savedHashtags)
|
2021-02-06 18:48:31 +00:00
|
|
|
snapshot.appendItems([.addSavedHashtag], toSection: .savedHashtags)
|
2022-05-11 02:57:46 +00:00
|
|
|
let instances = fetchSavedInstances().map {
|
|
|
|
Item.savedInstance($0.url)
|
|
|
|
}
|
|
|
|
snapshot.appendItems(instances, toSection: .savedInstances)
|
2021-02-06 18:48:31 +00:00
|
|
|
snapshot.appendItems([.findInstance], toSection: .savedInstances)
|
|
|
|
dataSource.apply(snapshot, animatingDifferences: false)
|
|
|
|
|
2022-11-19 19:08:39 +00:00
|
|
|
reloadLists(mastodonController.lists)
|
2021-02-06 18:48:31 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 23:23:49 +00:00
|
|
|
private func addDiscoverSection(to snapshot: inout NSDiffableDataSourceSnapshot<Section, Item>) {
|
|
|
|
snapshot.insertSections([.discover], afterSection: .bookmarks)
|
2023-02-05 19:34:01 +00:00
|
|
|
snapshot.appendItems([.trends], toSection: .discover)
|
2022-04-01 23:23:49 +00:00
|
|
|
}
|
|
|
|
|
2021-02-08 00:52:59 +00:00
|
|
|
private func ownInstanceLoaded(_ instance: Instance) {
|
|
|
|
var snapshot = self.dataSource.snapshot()
|
2022-11-15 02:17:08 +00:00
|
|
|
if mastodonController.instanceFeatures.trends,
|
2022-01-26 02:01:36 +00:00
|
|
|
!snapshot.sectionIdentifiers.contains(.discover) {
|
2023-02-05 19:34:01 +00:00
|
|
|
addDiscoverSection(to: &snapshot)
|
2021-02-08 00:52:59 +00:00
|
|
|
}
|
|
|
|
self.dataSource.apply(snapshot)
|
|
|
|
}
|
|
|
|
|
2022-11-19 19:08:39 +00:00
|
|
|
private func reloadLists(_ lists: [List]) {
|
|
|
|
var snapshot = self.dataSource.snapshot()
|
|
|
|
snapshot.deleteItems(snapshot.itemIdentifiers(inSection: .lists))
|
|
|
|
snapshot.appendItems(lists.map { .list($0) }, toSection: .lists)
|
|
|
|
snapshot.appendItems([.addList], toSection: .lists)
|
|
|
|
|
|
|
|
self.dataSource.apply(snapshot)
|
2022-11-11 23:08:44 +00:00
|
|
|
}
|
|
|
|
|
2022-05-11 02:57:46 +00:00
|
|
|
@MainActor
|
2022-12-13 03:02:07 +00:00
|
|
|
private func fetchHashtagItems(followed: [FollowedHashtag]) -> [Item] {
|
2022-12-19 15:58:14 +00:00
|
|
|
let req = SavedHashtag.fetchRequest(account: mastodonController.accountInfo!)
|
|
|
|
let saved = (try? mastodonController.persistentContainer.viewContext.fetch(req)) ?? []
|
2022-12-13 03:02:07 +00:00
|
|
|
var items = saved.map {
|
|
|
|
Item.savedHashtag(Hashtag(name: $0.name, url: $0.url))
|
2022-05-11 02:57:46 +00:00
|
|
|
}
|
2022-12-13 03:02:07 +00:00
|
|
|
for followed in followed where !saved.contains(where: { $0.name == followed.name }) {
|
|
|
|
items.append(.savedHashtag(Hashtag(name: followed.name, url: followed.url)))
|
|
|
|
}
|
2023-01-01 19:49:04 +00:00
|
|
|
items = items.uniques()
|
2022-12-13 03:02:07 +00:00
|
|
|
items.sort(using: SemiCaseSensitiveComparator.keyPath(\.label))
|
|
|
|
return items
|
2022-05-11 02:57:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@MainActor
|
|
|
|
private func fetchSavedInstances() -> [SavedInstance] {
|
2022-12-19 15:58:14 +00:00
|
|
|
let req = SavedInstance.fetchRequest(account: mastodonController.accountInfo!)
|
2022-05-11 02:57:46 +00:00
|
|
|
req.sortDescriptors = [NSSortDescriptor(key: "url.host", ascending: true)]
|
|
|
|
do {
|
2023-01-01 20:12:31 +00:00
|
|
|
return try mastodonController.persistentContainer.viewContext.fetch(req).uniques(by: \.url)
|
2022-05-11 02:57:46 +00:00
|
|
|
} catch {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-13 03:02:07 +00:00
|
|
|
private func updateHashtagsSection(followed: [FollowedHashtag]) {
|
2019-12-20 02:20:29 +00:00
|
|
|
var snapshot = dataSource.snapshot()
|
|
|
|
snapshot.deleteItems(snapshot.itemIdentifiers(inSection: .savedHashtags))
|
2022-12-13 03:02:07 +00:00
|
|
|
let hashtags = fetchHashtagItems(followed: followed)
|
2022-05-11 02:57:46 +00:00
|
|
|
snapshot.appendItems(hashtags, toSection: .savedHashtags)
|
2021-02-06 18:48:31 +00:00
|
|
|
snapshot.appendItems([.addSavedHashtag], toSection: .savedHashtags)
|
2019-12-20 02:20:29 +00:00
|
|
|
dataSource.apply(snapshot)
|
|
|
|
}
|
|
|
|
|
2021-02-06 18:48:31 +00:00
|
|
|
@objc private func savedInstancesChanged() {
|
2019-12-20 03:41:23 +00:00
|
|
|
var snapshot = dataSource.snapshot()
|
|
|
|
snapshot.deleteItems(snapshot.itemIdentifiers(inSection: .savedInstances))
|
2022-05-11 02:57:46 +00:00
|
|
|
let instances = fetchSavedInstances().map {
|
|
|
|
Item.savedInstance($0.url)
|
|
|
|
}
|
|
|
|
snapshot.appendItems(instances, toSection: .savedInstances)
|
2021-02-06 18:48:31 +00:00
|
|
|
snapshot.appendItems([.findInstance], toSection: .savedInstances)
|
2019-12-20 03:41:23 +00:00
|
|
|
dataSource.apply(snapshot)
|
|
|
|
}
|
|
|
|
|
2022-04-01 23:23:49 +00:00
|
|
|
@objc private func preferencesChanged() {
|
|
|
|
var snapshot = dataSource.snapshot()
|
|
|
|
let hasSection = snapshot.sectionIdentifiers.contains(.discover)
|
|
|
|
let hide = Preferences.shared.hideDiscover
|
|
|
|
if hasSection && hide {
|
|
|
|
snapshot.deleteSections([.discover])
|
|
|
|
} else if !hasSection && !hide {
|
|
|
|
addDiscoverSection(to: &snapshot)
|
|
|
|
} else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dataSource.apply(snapshot)
|
|
|
|
}
|
|
|
|
|
2021-02-06 18:48:31 +00:00
|
|
|
private func deleteList(_ list: List, completion: @escaping (Bool) -> Void) {
|
2022-11-11 23:16:44 +00:00
|
|
|
Task { @MainActor in
|
|
|
|
let service = DeleteListService(list: list, mastodonController: mastodonController, present: { self.present($0, animated: true) })
|
|
|
|
if await service.run() {
|
|
|
|
var snapshot = dataSource.snapshot()
|
2019-12-20 02:20:29 +00:00
|
|
|
snapshot.deleteItems([.list(list)])
|
2022-11-11 23:16:44 +00:00
|
|
|
await dataSource.apply(snapshot)
|
|
|
|
completion(true)
|
|
|
|
} else {
|
|
|
|
completion(false)
|
2019-12-20 02:20:29 +00:00
|
|
|
}
|
2022-11-11 23:16:44 +00:00
|
|
|
}
|
2019-12-20 02:20:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeSavedHashtag(_ hashtag: Hashtag) {
|
2022-05-11 02:57:46 +00:00
|
|
|
let context = mastodonController.persistentContainer.viewContext
|
2022-12-19 15:58:14 +00:00
|
|
|
let req = SavedHashtag.fetchRequest(name: hashtag.name, account: mastodonController.accountInfo!)
|
|
|
|
if let hashtag = try? context.fetch(req).first {
|
2022-05-11 02:57:46 +00:00
|
|
|
context.delete(hashtag)
|
|
|
|
try! context.save()
|
|
|
|
}
|
2019-12-20 02:20:29 +00:00
|
|
|
}
|
|
|
|
|
2019-12-20 03:41:23 +00:00
|
|
|
func removeSavedInstance(_ instanceURL: URL) {
|
2022-05-11 02:57:46 +00:00
|
|
|
let context = mastodonController.persistentContainer.viewContext
|
2022-12-19 15:58:14 +00:00
|
|
|
let req = SavedInstance.fetchRequest(url: instanceURL, account: mastodonController.accountInfo!)
|
|
|
|
if let instance = try? context.fetch(req).first {
|
2022-05-11 02:57:46 +00:00
|
|
|
context.delete(instance)
|
|
|
|
try! context.save()
|
|
|
|
}
|
2019-12-20 03:41:23 +00:00
|
|
|
}
|
|
|
|
|
2021-02-06 18:48:31 +00:00
|
|
|
private func trailingSwipeActionsForCell(at indexPath: IndexPath) -> UISwipeActionsConfiguration? {
|
2022-05-11 02:44:23 +00:00
|
|
|
let title: String
|
2021-02-06 18:48:31 +00:00
|
|
|
let handler: UIContextualAction.Handler
|
|
|
|
switch dataSource.itemIdentifier(for: indexPath) {
|
|
|
|
case let .list(list):
|
2022-05-11 02:44:23 +00:00
|
|
|
title = NSLocalizedString("Delete", comment: "delete swipe action title")
|
2021-02-06 18:48:31 +00:00
|
|
|
handler = { (_, _, completion) in
|
|
|
|
self.deleteList(list, completion: completion)
|
|
|
|
}
|
|
|
|
|
|
|
|
case let .savedHashtag(hashtag):
|
2022-05-11 02:44:23 +00:00
|
|
|
title = NSLocalizedString("Unsave", comment: "unsave swipe action title")
|
2021-02-06 18:48:31 +00:00
|
|
|
handler = { (_, _, completion) in
|
|
|
|
self.removeSavedHashtag(hashtag)
|
|
|
|
completion(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
case let .savedInstance(url):
|
2022-05-11 02:44:23 +00:00
|
|
|
title = NSLocalizedString("Unsave", comment: "unsave swipe action title")
|
2021-02-06 18:48:31 +00:00
|
|
|
handler = { (_, _, completion) in
|
|
|
|
self.removeSavedInstance(url)
|
|
|
|
completion(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return UISwipeActionsConfiguration(actions: [
|
2022-05-11 02:44:23 +00:00
|
|
|
UIContextualAction(style: .destructive, title: title, handler: handler)
|
2021-02-06 18:48:31 +00:00
|
|
|
])
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Collection View Delegate
|
2019-12-17 05:22:25 +00:00
|
|
|
|
2021-02-06 18:48:31 +00:00
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
2019-12-17 05:22:25 +00:00
|
|
|
switch dataSource.itemIdentifier(for: indexPath) {
|
|
|
|
case nil:
|
|
|
|
return
|
|
|
|
|
|
|
|
case .bookmarks:
|
2023-01-28 20:03:13 +00:00
|
|
|
show(BookmarksViewController(mastodonController: mastodonController), sender: nil)
|
2019-12-17 05:22:25 +00:00
|
|
|
|
2023-02-04 18:21:58 +00:00
|
|
|
case .favorites:
|
|
|
|
show(FavoritesViewController(mastodonController: mastodonController), sender: nil)
|
|
|
|
|
2023-02-05 19:34:01 +00:00
|
|
|
case .trends:
|
|
|
|
show(TrendsViewController(mastodonController: mastodonController), sender: nil)
|
2022-04-02 15:36:45 +00:00
|
|
|
|
2019-12-17 23:48:29 +00:00
|
|
|
case let .list(list):
|
2020-01-05 20:25:07 +00:00
|
|
|
show(ListTimelineViewController(for: list, mastodonController: mastodonController), sender: nil)
|
2019-12-18 02:18:32 +00:00
|
|
|
|
|
|
|
case .addList:
|
2021-02-06 18:48:31 +00:00
|
|
|
collectionView.deselectItem(at: indexPath, animated: true)
|
2022-11-11 22:51:23 +00:00
|
|
|
let service = CreateListService(mastodonController: mastodonController, present: { self.present($0, animated: true) }) { list in
|
|
|
|
let listTimelineController = ListTimelineViewController(for: list, mastodonController: self.mastodonController)
|
|
|
|
listTimelineController.presentEditOnAppear = true
|
|
|
|
self.show(listTimelineController, sender: nil)
|
|
|
|
}
|
|
|
|
service.run()
|
2019-12-20 02:20:29 +00:00
|
|
|
|
|
|
|
case let .savedHashtag(hashtag):
|
2020-01-05 20:25:07 +00:00
|
|
|
show(HashtagTimelineViewController(for: hashtag, mastodonController: mastodonController), sender: nil)
|
2019-12-20 02:20:29 +00:00
|
|
|
|
|
|
|
case .addSavedHashtag:
|
2021-02-06 18:48:31 +00:00
|
|
|
collectionView.deselectItem(at: indexPath, animated: true)
|
2020-01-05 20:25:07 +00:00
|
|
|
let navController = UINavigationController(rootViewController: AddSavedHashtagViewController(mastodonController: mastodonController))
|
2019-12-20 02:20:29 +00:00
|
|
|
present(navController, animated: true)
|
2019-12-20 03:41:23 +00:00
|
|
|
|
|
|
|
case let .savedInstance(url):
|
2020-01-20 16:48:47 +00:00
|
|
|
show(InstanceTimelineViewController(for: url, parentMastodonController: mastodonController), sender: nil)
|
2019-12-20 03:41:23 +00:00
|
|
|
|
|
|
|
case .findInstance:
|
2021-02-06 18:48:31 +00:00
|
|
|
collectionView.deselectItem(at: indexPath, animated: true)
|
2020-01-20 16:48:47 +00:00
|
|
|
let findController = FindInstanceViewController(parentMastodonController: mastodonController)
|
2019-12-20 03:41:23 +00:00
|
|
|
findController.instanceTimelineDelegate = self
|
|
|
|
let navController = UINavigationController(rootViewController: findController)
|
|
|
|
present(navController, animated: true)
|
2019-12-17 05:22:25 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-16 03:54:04 +00:00
|
|
|
|
2019-12-17 05:22:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
extension ExploreViewController {
|
|
|
|
enum Section: CaseIterable {
|
|
|
|
case bookmarks
|
2021-02-06 19:54:35 +00:00
|
|
|
case discover
|
2019-12-17 05:22:25 +00:00
|
|
|
case lists
|
2019-12-20 02:20:29 +00:00
|
|
|
case savedHashtags
|
2019-12-20 03:41:23 +00:00
|
|
|
case savedInstances
|
2021-02-06 18:48:31 +00:00
|
|
|
|
|
|
|
var label: String? {
|
|
|
|
switch self {
|
|
|
|
case .bookmarks:
|
|
|
|
return nil
|
2021-02-06 19:54:35 +00:00
|
|
|
case .discover:
|
2023-02-05 19:34:01 +00:00
|
|
|
return nil
|
2021-02-06 18:48:31 +00:00
|
|
|
case .lists:
|
|
|
|
return NSLocalizedString("Lists", comment: "explore lists section title")
|
|
|
|
case .savedHashtags:
|
2022-12-13 03:02:07 +00:00
|
|
|
return NSLocalizedString("Hashtags", comment: "explore saved hashtags section title")
|
2021-02-06 18:48:31 +00:00
|
|
|
case .savedInstances:
|
|
|
|
return NSLocalizedString("Instance Timelines", comment: "explore instance timelines section title")
|
|
|
|
}
|
|
|
|
}
|
2019-12-17 05:22:25 +00:00
|
|
|
}
|
2021-02-06 18:48:31 +00:00
|
|
|
|
2019-12-17 05:22:25 +00:00
|
|
|
enum Item: Hashable {
|
|
|
|
case bookmarks
|
2023-02-04 18:21:58 +00:00
|
|
|
case favorites
|
2023-02-05 19:34:01 +00:00
|
|
|
case trends
|
2019-12-17 23:48:29 +00:00
|
|
|
case list(List)
|
2019-12-18 02:18:32 +00:00
|
|
|
case addList
|
2019-12-20 02:20:29 +00:00
|
|
|
case savedHashtag(Hashtag)
|
|
|
|
case addSavedHashtag
|
2019-12-20 03:41:23 +00:00
|
|
|
case savedInstance(URL)
|
|
|
|
case findInstance
|
2021-02-06 18:48:31 +00:00
|
|
|
|
|
|
|
var label: String {
|
|
|
|
switch self {
|
|
|
|
case .bookmarks:
|
|
|
|
return NSLocalizedString("Bookmarks", comment: "bookmarks nav item title")
|
2023-02-04 18:21:58 +00:00
|
|
|
case .favorites:
|
|
|
|
return NSLocalizedString("Favorites", comment: "favorites nav item title")
|
2023-02-05 19:34:01 +00:00
|
|
|
case .trends:
|
|
|
|
return NSLocalizedString("Trends", comment: "trends nav item title")
|
2021-02-06 18:48:31 +00:00
|
|
|
case let .list(list):
|
|
|
|
return list.title
|
|
|
|
case .addList:
|
|
|
|
return NSLocalizedString("New List...", comment: "new list nav item title")
|
|
|
|
case let .savedHashtag(hashtag):
|
|
|
|
return hashtag.name
|
|
|
|
case .addSavedHashtag:
|
2022-12-13 03:06:55 +00:00
|
|
|
return NSLocalizedString("Add Hashtag...", comment: "save hashtag nav item title")
|
2021-02-06 18:48:31 +00:00
|
|
|
case let .savedInstance(url):
|
|
|
|
return url.host!
|
|
|
|
case .findInstance:
|
|
|
|
return NSLocalizedString("Find An Instance...", comment: "find instance nav item title")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var image: UIImage {
|
|
|
|
let name: String
|
|
|
|
switch self {
|
|
|
|
case .bookmarks:
|
|
|
|
name = "bookmark.fill"
|
2023-02-04 18:21:58 +00:00
|
|
|
case .favorites:
|
|
|
|
name = "star.fill"
|
2023-02-05 19:34:01 +00:00
|
|
|
case .trends:
|
|
|
|
name = "chart.line.uptrend.xyaxis"
|
2021-02-06 18:48:31 +00:00
|
|
|
case .list(_):
|
|
|
|
name = "list.bullet"
|
|
|
|
case .addList, .addSavedHashtag:
|
|
|
|
name = "plus"
|
|
|
|
case .savedHashtag(_):
|
|
|
|
name = "number"
|
|
|
|
case .savedInstance(_):
|
|
|
|
name = "globe"
|
|
|
|
case .findInstance:
|
|
|
|
name = "magnifyingglass"
|
|
|
|
}
|
|
|
|
return UIImage(systemName: name)!
|
|
|
|
}
|
|
|
|
|
|
|
|
static func == (lhs: Item, rhs: Item) -> Bool {
|
2019-12-17 23:48:29 +00:00
|
|
|
switch (lhs, rhs) {
|
|
|
|
case (.bookmarks, .bookmarks):
|
|
|
|
return true
|
2023-02-04 18:21:58 +00:00
|
|
|
case (.favorites, .favorites):
|
|
|
|
return true
|
2023-02-05 19:34:01 +00:00
|
|
|
case (.trends, .trends):
|
2022-04-02 15:36:45 +00:00
|
|
|
return true
|
2019-12-17 23:48:29 +00:00
|
|
|
case let (.list(a), .list(b)):
|
2022-11-11 23:08:44 +00:00
|
|
|
return a.id == b.id && a.title == b.title
|
2019-12-18 02:18:32 +00:00
|
|
|
case (.addList, .addList):
|
|
|
|
return true
|
2019-12-20 02:20:29 +00:00
|
|
|
case let (.savedHashtag(a), .savedHashtag(b)):
|
|
|
|
return a == b
|
|
|
|
case (.addSavedHashtag, .addSavedHashtag):
|
|
|
|
return true
|
2019-12-20 03:41:23 +00:00
|
|
|
case let (.savedInstance(a), .savedInstance(b)):
|
|
|
|
return a == b
|
|
|
|
case (.findInstance, .findInstance):
|
|
|
|
return true
|
2019-12-17 23:48:29 +00:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2021-02-06 18:48:31 +00:00
|
|
|
|
2019-12-17 23:48:29 +00:00
|
|
|
func hash(into hasher: inout Hasher) {
|
|
|
|
switch self {
|
|
|
|
case .bookmarks:
|
|
|
|
hasher.combine("bookmarks")
|
2023-02-04 18:21:58 +00:00
|
|
|
case .favorites:
|
|
|
|
hasher.combine("favorites")
|
2023-02-05 19:34:01 +00:00
|
|
|
case .trends:
|
|
|
|
hasher.combine("trends")
|
2019-12-17 23:48:29 +00:00
|
|
|
case let .list(list):
|
|
|
|
hasher.combine("list")
|
|
|
|
hasher.combine(list.id)
|
2022-11-11 23:08:44 +00:00
|
|
|
hasher.combine(list.title)
|
2019-12-18 02:18:32 +00:00
|
|
|
case .addList:
|
|
|
|
hasher.combine("addList")
|
2019-12-20 02:20:29 +00:00
|
|
|
case let .savedHashtag(hashtag):
|
|
|
|
hasher.combine("savedHashtag")
|
|
|
|
hasher.combine(hashtag.name)
|
|
|
|
case .addSavedHashtag:
|
|
|
|
hasher.combine("addSavedHashtag")
|
2019-12-20 03:41:23 +00:00
|
|
|
case let .savedInstance(url):
|
|
|
|
hasher.combine("savedInstance")
|
|
|
|
hasher.combine(url)
|
|
|
|
case .findInstance:
|
|
|
|
hasher.combine("findInstance")
|
2019-12-17 23:48:29 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-17 05:22:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-20 03:41:23 +00:00
|
|
|
|
|
|
|
extension ExploreViewController: InstanceTimelineViewControllerDelegate {
|
|
|
|
func didSaveInstance(url: URL) {
|
|
|
|
dismiss(animated: true) {
|
2020-01-20 16:48:47 +00:00
|
|
|
self.show(InstanceTimelineViewController(for: url, parentMastodonController: self.mastodonController), sender: nil)
|
2019-12-20 03:41:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func didUnsaveInstance(url: URL) {
|
|
|
|
dismiss(animated: true)
|
|
|
|
}
|
|
|
|
}
|
2020-12-14 23:44:41 +00:00
|
|
|
|
2021-02-06 18:48:31 +00:00
|
|
|
extension ExploreViewController: UICollectionViewDragDelegate {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, itemsForBeginning session: UIDragSession, at indexPath: IndexPath) -> [UIDragItem] {
|
2020-12-14 23:44:41 +00:00
|
|
|
guard let item = dataSource.itemIdentifier(for: indexPath),
|
|
|
|
let accountID = mastodonController.accountInfo?.id else {
|
|
|
|
return []
|
|
|
|
}
|
2021-02-06 18:48:31 +00:00
|
|
|
|
2020-12-14 23:44:41 +00:00
|
|
|
let provider: NSItemProvider
|
|
|
|
switch item {
|
|
|
|
case .bookmarks:
|
2022-05-13 21:10:18 +00:00
|
|
|
let activity = UserActivityManager.bookmarksActivity()
|
|
|
|
activity.displaysAuxiliaryScene = true
|
|
|
|
provider = NSItemProvider(object: activity)
|
2020-12-14 23:44:41 +00:00
|
|
|
case let .list(list):
|
|
|
|
guard let activity = UserActivityManager.showTimelineActivity(timeline: .list(id: list.id), accountID: accountID) else { return [] }
|
2022-05-13 21:10:18 +00:00
|
|
|
activity.displaysAuxiliaryScene = true
|
2020-12-14 23:44:41 +00:00
|
|
|
provider = NSItemProvider(object: activity)
|
|
|
|
case let .savedHashtag(hashtag):
|
2022-07-09 15:45:27 +00:00
|
|
|
guard let url = URL(hashtag.url) else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
provider = NSItemProvider(object: url as NSURL)
|
2020-12-14 23:44:41 +00:00
|
|
|
if let activity = UserActivityManager.showTimelineActivity(timeline: .tag(hashtag: hashtag.name), accountID: accountID) {
|
2022-05-13 21:10:18 +00:00
|
|
|
activity.displaysAuxiliaryScene = true
|
2020-12-14 23:44:41 +00:00
|
|
|
provider.registerObject(activity, visibility: .all)
|
|
|
|
}
|
|
|
|
case let .savedInstance(url):
|
|
|
|
provider = NSItemProvider(object: url as NSURL)
|
|
|
|
// todo: should dragging public timelines into new windows be supported?
|
2022-04-02 14:39:03 +00:00
|
|
|
default:
|
2020-12-14 23:44:41 +00:00
|
|
|
return []
|
|
|
|
}
|
|
|
|
return [UIDragItem(itemProvider: provider)]
|
|
|
|
}
|
|
|
|
}
|