65 lines
2.0 KiB
Swift
65 lines
2.0 KiB
Swift
//
|
|
// HashtagTimelineViewController.swift
|
|
// Tusker
|
|
//
|
|
// Created by Shadowfacts on 12/19/19.
|
|
// Copyright © 2019 Shadowfacts. All rights reserved.
|
|
//
|
|
|
|
import UIKit
|
|
import Pachyderm
|
|
|
|
class HashtagTimelineViewController: TimelineViewController {
|
|
|
|
let hashtag: Hashtag
|
|
|
|
var toggleSaveButton: UIBarButtonItem!
|
|
|
|
private var isHashtagSaved: Bool {
|
|
mastodonController.persistentContainer.viewContext.objectExists(for: SavedHashtag.fetchRequest(name: hashtag.name))
|
|
}
|
|
|
|
private var isHashtagFollowed: Bool {
|
|
mastodonController.followedHashtags.contains(where: { $0.name == hashtag.name })
|
|
}
|
|
|
|
init(for hashtag: Hashtag, mastodonController: MastodonController) {
|
|
self.hashtag = hashtag
|
|
|
|
super.init(for: .tag(hashtag: hashtag.name), mastodonController: mastodonController)
|
|
}
|
|
|
|
required init?(coder aDecoder: NSCoder) {
|
|
fatalError("init(coder:) has not been implemented")
|
|
}
|
|
|
|
override func viewDidLoad() {
|
|
super.viewDidLoad()
|
|
|
|
let menu = UIMenu(children: [
|
|
// uncached so that the saved/followed updates every time
|
|
UIDeferredMenuElement.uncached({ [unowned self] elementHandler in
|
|
elementHandler(actionsForHashtag(hashtag, source: .barButtonItem(self.navigationItem.rightBarButtonItem!)))
|
|
})
|
|
])
|
|
navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIImage(systemName: "ellipsis"), menu: menu)
|
|
}
|
|
|
|
private func toggleSave() {
|
|
let context = mastodonController.persistentContainer.viewContext
|
|
if let existing = try? context.fetch(SavedHashtag.fetchRequest(name: hashtag.name)).first {
|
|
context.delete(existing)
|
|
} else {
|
|
_ = SavedHashtag(hashtag: hashtag, context: context)
|
|
}
|
|
mastodonController.persistentContainer.save(context: context)
|
|
}
|
|
|
|
private func toggleFollow() {
|
|
Task {
|
|
await ToggleFollowHashtagService(hashtag: hashtag, presenter: self).toggleFollow()
|
|
}
|
|
}
|
|
|
|
}
|