Compare commits

...

3 Commits

2 changed files with 71 additions and 4 deletions

View File

@ -9,14 +9,14 @@
import UIKit import UIKit
import Pachyderm import Pachyderm
class AddSavedHashtagViewController: UIViewController { class AddSavedHashtagViewController: UIViewController, CollectionViewController {
weak var mastodonController: MastodonController! weak var mastodonController: MastodonController!
var resultsController: SearchResultsViewController! var resultsController: SearchResultsViewController!
var searchController: UISearchController! var searchController: UISearchController!
private var collectionView: UICollectionView! private(set) var collectionView: UICollectionView!
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>! private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
init(mastodonController: MastodonController) { init(mastodonController: MastodonController) {
@ -91,6 +91,12 @@ class AddSavedHashtagViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) { override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated) super.viewWillAppear(animated)
if searchController.isActive {
resultsController.clearSelectionOnAppear(animated: animated)
}
clearSelectionOnAppear(animated: animated)
let request = Client.getTrendingHashtags(limit: 10) let request = Client.getTrendingHashtags(limit: 10)
mastodonController.run(request) { (response) in mastodonController.run(request) { (response) in
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>() var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
@ -108,7 +114,63 @@ class AddSavedHashtagViewController: UIViewController {
} }
private func selectHashtag(_ hashtag: Hashtag) { 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 // MARK: - Interaction
@ -144,3 +206,7 @@ extension AddSavedHashtagViewController: SearchResultsViewControllerDelegate {
selectHashtag(hashtag) selectHashtag(hashtag)
} }
} }
extension AddSavedHashtagViewController: TuskerNavigationDelegate {
var apiController: MastodonController! { mastodonController }
}

View File

@ -96,7 +96,7 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate, Collect
// so we manually propagate this down to the results controller // so we manually propagate this down to the results controller
// so that it can deselect on appear // so that it can deselect on appear
if searchController.isActive { if searchController.isActive {
resultsController.viewWillAppear(animated) resultsController.clearSelectionOnAppear(animated: animated)
} }
clearSelectionOnAppear(animated: animated) clearSelectionOnAppear(animated: animated)
@ -308,6 +308,7 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate, Collect
actions.append(UIContextualAction(style: .destructive, title: "Unsave", handler: { _, _, completion in actions.append(UIContextualAction(style: .destructive, title: "Unsave", handler: { _, _, completion in
context.delete(existing) context.delete(existing)
try! context.save() try! context.save()
completion(true)
})) }))
} }
if mastodonController.instanceFeatures.canFollowHashtags, if mastodonController.instanceFeatures.canFollowHashtags,