|
|
|
@ -9,14 +9,14 @@
|
|
|
|
|
import UIKit
|
|
|
|
|
import Pachyderm
|
|
|
|
|
|
|
|
|
|
class AddSavedHashtagViewController: UIViewController {
|
|
|
|
|
class AddSavedHashtagViewController: UIViewController, CollectionViewController {
|
|
|
|
|
|
|
|
|
|
weak var mastodonController: MastodonController!
|
|
|
|
|
|
|
|
|
|
var resultsController: SearchResultsViewController!
|
|
|
|
|
var searchController: UISearchController!
|
|
|
|
|
|
|
|
|
|
private var collectionView: UICollectionView!
|
|
|
|
|
private(set) var collectionView: UICollectionView!
|
|
|
|
|
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
|
|
|
|
|
|
|
|
|
|
init(mastodonController: MastodonController) {
|
|
|
|
@ -91,6 +91,12 @@ class AddSavedHashtagViewController: UIViewController {
|
|
|
|
|
override func viewWillAppear(_ animated: Bool) {
|
|
|
|
|
super.viewWillAppear(animated)
|
|
|
|
|
|
|
|
|
|
if searchController.isActive {
|
|
|
|
|
resultsController.clearSelectionOnAppear(animated: animated)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
clearSelectionOnAppear(animated: animated)
|
|
|
|
|
|
|
|
|
|
let request = Client.getTrendingHashtags(limit: 10)
|
|
|
|
|
mastodonController.run(request) { (response) in
|
|
|
|
|
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
|
|
|
|
@ -108,7 +114,63 @@ class AddSavedHashtagViewController: UIViewController {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private func selectHashtag(_ hashtag: Hashtag) {
|
|
|
|
|
show(HashtagTimelineViewController(for: hashtag, mastodonController: mastodonController), sender: nil)
|
|
|
|
|
let vc = HashtagTimelineViewController(for: hashtag, mastodonController: mastodonController)
|
|
|
|
|
vc.loadViewIfNeeded()
|
|
|
|
|
|
|
|
|
|
let mastodonController = mastodonController!
|
|
|
|
|
let context = mastodonController.persistentContainer.viewContext
|
|
|
|
|
let existingSaved = try? context.fetch(SavedHashtag.fetchRequest(name: hashtag.name, account: mastodonController.accountInfo!)).first
|
|
|
|
|
let saveItem = UIBarButtonItem()
|
|
|
|
|
func updateSaveItem(saved: Bool) {
|
|
|
|
|
saveItem.title = saved ? "Unsave Hashag" : "Save Hashtag"
|
|
|
|
|
saveItem.image = UIImage(systemName: saved ? "minus" : "plus")
|
|
|
|
|
}
|
|
|
|
|
saveItem.primaryAction = UIAction(handler: { [unowned self] _ in
|
|
|
|
|
// re-fetch this in case the button's been tapped before and the captured var would be out of date
|
|
|
|
|
let existingSaved = try? context.fetch(SavedHashtag.fetchRequest(name: hashtag.name, account: mastodonController.accountInfo!)).first
|
|
|
|
|
if let existingSaved {
|
|
|
|
|
context.delete(existingSaved)
|
|
|
|
|
} else {
|
|
|
|
|
_ = SavedHashtag(hashtag: hashtag, account: mastodonController.accountInfo!, context: context)
|
|
|
|
|
}
|
|
|
|
|
mastodonController.persistentContainer.save(context: context)
|
|
|
|
|
updateSaveItem(saved: existingSaved == nil)
|
|
|
|
|
if existingSaved == nil {
|
|
|
|
|
self.presentingViewController?.dismiss(animated: true)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
// setting primaryAction replace's the bar button's title/image with the action, so do this after
|
|
|
|
|
updateSaveItem(saved: existingSaved != nil)
|
|
|
|
|
|
|
|
|
|
vc.navigationItem.rightBarButtonItems = [
|
|
|
|
|
saveItem,
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
if mastodonController.instanceFeatures.canFollowHashtags {
|
|
|
|
|
let existingFollowed = mastodonController.followedHashtags.first(where: { $0.name.lowercased() == hashtag.name })
|
|
|
|
|
let followItem = UIBarButtonItem()
|
|
|
|
|
func updateFollowItem(followed: Bool) {
|
|
|
|
|
followItem.title = followed ? "Unfollow Hashtag" : "Follow Hashtag"
|
|
|
|
|
followItem.image = UIImage(systemName: "person.badge.\(followed ? "minus" : "plus")")
|
|
|
|
|
}
|
|
|
|
|
followItem.primaryAction = UIAction(handler: { [unowned self] _ in
|
|
|
|
|
Task {
|
|
|
|
|
let success = await ToggleFollowHashtagService(hashtagName: hashtag.name, presenter: self).toggleFollow()
|
|
|
|
|
if success {
|
|
|
|
|
let followed = mastodonController.followedHashtags.contains(where: { $0.name.lowercased() == hashtag.name })
|
|
|
|
|
updateFollowItem(followed: followed)
|
|
|
|
|
if followed {
|
|
|
|
|
self.presentingViewController?.dismiss(animated: true)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
updateFollowItem(followed: existingFollowed != nil)
|
|
|
|
|
|
|
|
|
|
vc.navigationItem.rightBarButtonItems!.append(followItem)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
show(vc, sender: self)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MARK: - Interaction
|
|
|
|
@ -144,3 +206,7 @@ extension AddSavedHashtagViewController: SearchResultsViewControllerDelegate {
|
|
|
|
|
selectHashtag(hashtag)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension AddSavedHashtagViewController: TuskerNavigationDelegate {
|
|
|
|
|
var apiController: MastodonController! { mastodonController }
|
|
|
|
|
}
|
|
|
|
|