Add preference to disable Discover

This commit is contained in:
Shadowfacts 2022-04-01 19:23:49 -04:00
parent c6d158a8a3
commit e49859e5ea
4 changed files with 62 additions and 9 deletions

View File

@ -64,6 +64,7 @@ class Preferences: Codable, ObservableObject {
self.defaultNotificationsMode = try container.decode(NotificationsMode.self, forKey: .defaultNotificationsType)
self.grayscaleImages = try container.decodeIfPresent(Bool.self, forKey: .grayscaleImages) ?? false
self.disableInfiniteScrolling = try container.decodeIfPresent(Bool.self, forKey: .disableInfiniteScrolling) ?? false
self.hideDiscover = try container.decodeIfPresent(Bool.self, forKey: .hideDiscover) ?? false
self.silentActions = try container.decode([String: Permission].self, forKey: .silentActions)
self.statusContentType = try container.decode(StatusContentType.self, forKey: .statusContentType)
@ -102,6 +103,7 @@ class Preferences: Codable, ObservableObject {
try container.encode(defaultNotificationsMode, forKey: .defaultNotificationsType)
try container.encode(grayscaleImages, forKey: .grayscaleImages)
try container.encode(disableInfiniteScrolling, forKey: .disableInfiniteScrolling)
try container.encode(hideDiscover, forKey: .hideDiscover)
try container.encode(silentActions, forKey: .silentActions)
try container.encode(statusContentType, forKey: .statusContentType)
@ -142,6 +144,7 @@ class Preferences: Codable, ObservableObject {
@Published var defaultNotificationsMode = NotificationsMode.allNotifications
@Published var grayscaleImages = false
@Published var disableInfiniteScrolling = false
@Published var hideDiscover = false
// MARK: Advanced
@Published var silentActions: [String: Permission] = [:]
@ -179,6 +182,7 @@ class Preferences: Codable, ObservableObject {
case defaultNotificationsType
case grayscaleImages
case disableInfiniteScrolling
case hideDiscover
case silentActions
case statusContentType

View File

@ -68,6 +68,7 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate {
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)
}
override func viewWillAppear(_ animated: Bool) {
@ -138,9 +139,9 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate {
var snapshot = NSDiffableDataSourceSnapshot<Section, Item>()
snapshot.appendSections(Section.allCases.filter { $0 != .discover })
snapshot.appendItems([.bookmarks], toSection: .bookmarks)
if mastodonController.instanceFeatures.instanceType.isMastodon {
snapshot.insertSections([.discover], afterSection: .bookmarks)
snapshot.appendItems([.trendingTags, .profileDirectory], toSection: .discover)
if mastodonController.instanceFeatures.instanceType.isMastodon,
!Preferences.shared.hideDiscover {
addDiscoverSection(to: &snapshot)
}
snapshot.appendItems([.addList], toSection: .lists)
snapshot.appendItems(SavedDataManager.shared.sortedHashtags(for: account).map { .savedHashtag($0) }, toSection: .savedHashtags)
@ -152,6 +153,11 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate {
reloadLists()
}
private func addDiscoverSection(to snapshot: inout NSDiffableDataSourceSnapshot<Section, Item>) {
snapshot.insertSections([.discover], afterSection: .bookmarks)
snapshot.appendItems([.trendingTags, .profileDirectory], toSection: .discover)
}
private func ownInstanceLoaded(_ instance: Instance) {
var snapshot = self.dataSource.snapshot()
if mastodonController.instanceFeatures.instanceType.isMastodon,
@ -198,6 +204,20 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate {
dataSource.apply(snapshot)
}
@objc private func preferencesChanged() {
var snapshot = dataSource.snapshot()
let hasSection = snapshot.sectionIdentifiers.contains(.discover)
let hide = Preferences.shared.hideDiscover
if hasSection && hide {
snapshot.deleteSections([.discover])
} else if !hasSection && !hide {
addDiscoverSection(to: &snapshot)
} else {
return
}
dataSource.apply(snapshot)
}
private func deleteList(_ list: List, completion: @escaping (Bool) -> Void) {
let titleFormat = NSLocalizedString("Are you sure you want to delete the '%@' list?", comment: "delete list alert title")
let title = String(format: titleFormat, list.title)

View File

@ -94,6 +94,7 @@ class MainSidebarViewController: UIViewController {
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)
}
func select(item: Item, animated: Bool) {
@ -145,12 +146,9 @@ class MainSidebarViewController: UIViewController {
snapshot.appendItems([
.tab(.compose)
], toSection: .compose)
if mastodonController.instanceFeatures.instanceType.isMastodon {
snapshot.insertSections([.discover], afterSection: .compose)
snapshot.appendItems([
.trendingTags,
.profileDirectory,
], toSection: .discover)
if mastodonController.instanceFeatures.instanceType.isMastodon,
!Preferences.shared.hideDiscover {
addDiscoverSection(to: &snapshot)
}
dataSource.apply(snapshot, animatingDifferences: false)
@ -159,6 +157,14 @@ class MainSidebarViewController: UIViewController {
reloadSavedInstances()
}
private func addDiscoverSection(to snapshot: inout NSDiffableDataSourceSnapshot<Section, Item>) {
snapshot.insertSections([.discover], afterSection: .compose)
snapshot.appendItems([
.trendingTags,
.profileDirectory,
], toSection: .discover)
}
private func ownInstanceLoaded(_ instance: Instance) {
var snapshot = self.dataSource.snapshot()
if mastodonController.instanceFeatures.instanceType.isMastodon,
@ -231,6 +237,20 @@ class MainSidebarViewController: UIViewController {
}
}
}
@objc private func preferencesChanged() {
var snapshot = dataSource.snapshot()
let hasSection = snapshot.sectionIdentifiers.contains(.discover)
let hide = Preferences.shared.hideDiscover
if hasSection && hide {
snapshot.deleteSections([.discover])
} else if !hasSection && !hide {
addDiscoverSection(to: &snapshot)
} else {
return
}
dataSource.apply(snapshot)
}
// todo: deduplicate with ExploreViewController
private func showAddList() {

View File

@ -19,6 +19,7 @@ struct WellnessPrefsView: View {
if #available(iOS 15.0, *) {
disableInfiniteScrolling
}
hideDiscover
}
.listStyle(InsetGroupedListStyle())
.navigationBarTitle(Text("Digital Wellness"))
@ -57,6 +58,14 @@ struct WellnessPrefsView: View {
}
}
}
private var hideDiscover: some View {
Section(footer: Text("Do not show the Discover section (Trends, Profile Directory) of the Explore screen or sidebar.")) {
Toggle(isOn: $preferences.hideDiscover) {
Text("Hide Discover Section")
}
}
}
}
struct WellnessPrefsView_Previews: PreviewProvider {