Include followed hashtags in Explore and sidebar

This commit is contained in:
Shadowfacts 2022-12-12 22:02:07 -05:00
parent f96cd1b5e2
commit 478ba3db28
2 changed files with 55 additions and 36 deletions

View File

@ -24,7 +24,7 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate {
var searchControllerStatusOnAppearance: Bool? = nil
private var listsCancellable: AnyCancellable?
private var cancellables = Set<AnyCancellable>()
init(mastodonController: MastodonController) {
self.mastodonController = mastodonController
@ -70,12 +70,26 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate {
navigationItem.searchController = searchController
navigationItem.hidesSearchBarWhenScrolling = false
NotificationCenter.default.addObserver(self, selector: #selector(savedHashtagsChanged), name: .savedHashtagsChanged, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(savedInstancesChanged), name: .savedInstancesChanged, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil)
listsCancellable = mastodonController.$lists
mastodonController.$lists
.sink { [unowned self] in self.reloadLists($0) }
.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)
}
override func viewWillAppear(_ animated: Bool) {
@ -149,9 +163,7 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate {
addDiscoverSection(to: &snapshot)
}
snapshot.appendItems([.addList], toSection: .lists)
let hashtags = fetchSavedHashtags().map {
Item.savedHashtag(Hashtag(name: $0.name, url: $0.url))
}
let hashtags = fetchHashtagItems(followed: mastodonController.followedHashtags)
snapshot.appendItems(hashtags, toSection: .savedHashtags)
snapshot.appendItems([.addSavedHashtag], toSection: .savedHashtags)
let instances = fetchSavedInstances().map {
@ -193,14 +205,16 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate {
}
@MainActor
private func fetchSavedHashtags() -> [SavedHashtag] {
let req = SavedHashtag.fetchRequest()
req.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true, selector: #selector(NSString.localizedCompare(_:)))]
do {
return try mastodonController.persistentContainer.viewContext.fetch(req)
} catch {
return []
private func fetchHashtagItems(followed: [FollowedHashtag]) -> [Item] {
let saved = (try? mastodonController.persistentContainer.viewContext.fetch(SavedHashtag.fetchRequest())) ?? []
var items = saved.map {
Item.savedHashtag(Hashtag(name: $0.name, url: $0.url))
}
for followed in followed where !saved.contains(where: { $0.name == followed.name }) {
items.append(.savedHashtag(Hashtag(name: followed.name, url: followed.url)))
}
items.sort(using: SemiCaseSensitiveComparator.keyPath(\.label))
return items
}
@MainActor
@ -214,12 +228,10 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate {
}
}
@objc private func savedHashtagsChanged() {
private func updateHashtagsSection(followed: [FollowedHashtag]) {
var snapshot = dataSource.snapshot()
snapshot.deleteItems(snapshot.itemIdentifiers(inSection: .savedHashtags))
let hashtags = fetchSavedHashtags().map {
Item.savedHashtag(Hashtag(name: $0.name, url: $0.url))
}
let hashtags = fetchHashtagItems(followed: followed)
snapshot.appendItems(hashtags, toSection: .savedHashtags)
snapshot.appendItems([.addSavedHashtag], toSection: .savedHashtags)
dataSource.apply(snapshot)
@ -386,7 +398,7 @@ extension ExploreViewController {
case .lists:
return NSLocalizedString("Lists", comment: "explore lists section title")
case .savedHashtags:
return NSLocalizedString("Saved Hashtags", comment: "explore saved hashtags section title")
return NSLocalizedString("Hashtags", comment: "explore saved hashtags section title")
case .savedInstances:
return NSLocalizedString("Instance Timelines", comment: "explore instance timelines section title")
}

View File

@ -30,7 +30,7 @@ class MainSidebarViewController: UIViewController {
private var collectionView: UICollectionView!
private var dataSource: UICollectionViewDiffableDataSource<Section, Item>!
private var listsCancellable: AnyCancellable?
private var cancellables = Set<AnyCancellable>()
var allItems: [Item] {
[
@ -101,12 +101,19 @@ class MainSidebarViewController: UIViewController {
select(item: .tab(.timelines), animated: false)
NotificationCenter.default.addObserver(self, selector: #selector(reloadSavedHashtags), name: .savedHashtagsChanged, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(reloadSavedInstances), name: .savedInstancesChanged, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil)
listsCancellable = mastodonController.$lists
mastodonController.$lists
.sink { [unowned self] in self.reloadLists($0) }
.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)
onViewDidLoad?()
}
@ -176,7 +183,7 @@ class MainSidebarViewController: UIViewController {
applyDiscoverSectionSnapshot()
reloadLists(mastodonController.lists)
reloadSavedHashtags()
updateHashtagsSection(followed: mastodonController.followedHashtags)
reloadSavedInstances()
}
@ -224,14 +231,16 @@ class MainSidebarViewController: UIViewController {
}
@MainActor
private func fetchSavedHashtags() -> [SavedHashtag] {
let req = SavedHashtag.fetchRequest()
req.sortDescriptors = [NSSortDescriptor(key: "name", ascending: true, selector: #selector(NSString.localizedCompare(_:)))]
do {
return try mastodonController.persistentContainer.viewContext.fetch(req)
} catch {
return []
private func fetchHashtagItems(followed: [FollowedHashtag]) -> [Item] {
let saved = (try? mastodonController.persistentContainer.viewContext.fetch(SavedHashtag.fetchRequest())) ?? []
var items = saved.map {
Item.savedHashtag(Hashtag(name: $0.name, url: $0.url))
}
for followed in followed where !saved.contains(where: { $0.name == followed.name }) {
items.append(.savedHashtag(Hashtag(name: followed.name, url: followed.url)))
}
items.sort(using: SemiCaseSensitiveComparator.keyPath(\.title))
return items
}
@MainActor
@ -245,10 +254,8 @@ class MainSidebarViewController: UIViewController {
}
}
@objc private func reloadSavedHashtags() {
let hashtags = fetchSavedHashtags().map {
Item.savedHashtag(Hashtag(name: $0.name, url: $0.url))
}
private func updateHashtagsSection(followed: [FollowedHashtag]) {
let hashtags = fetchHashtagItems(followed: followed)
if let selectedItem,
case .savedHashtag(_) = selectedItem,
!hashtags.contains(selectedItem) {
@ -403,13 +410,13 @@ extension MainSidebarViewController {
case .addList:
return "New List..."
case .savedHashtagsHeader:
return "Saved Hashtags"
return "Hashtags"
case let .savedHashtag(hashtag):
return hashtag.name
case .addSavedHashtag:
return "Save Hashtag..."
return "Add Hashtag..."
case .savedInstancesHeader:
return "Saved Instances"
return "Instance Timelines"
case let .savedInstance(url):
return url.host!
case .addSavedInstance: