parent
3a3af77907
commit
814f64b3e2
|
@ -114,7 +114,63 @@ class AddSavedHashtagViewController: UIViewController, CollectionViewController
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
@ -150,3 +206,7 @@ extension AddSavedHashtagViewController: SearchResultsViewControllerDelegate {
|
||||||
selectHashtag(hashtag)
|
selectHashtag(hashtag)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension AddSavedHashtagViewController: TuskerNavigationDelegate {
|
||||||
|
var apiController: MastodonController! { mastodonController }
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue