From e49859e5ea679e196366f37aa0bf94ce2a4a7a51 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Fri, 1 Apr 2022 19:23:49 -0400 Subject: [PATCH] Add preference to disable Discover --- Tusker/Preferences/Preferences.swift | 4 +++ .../Explore/ExploreViewController.swift | 26 +++++++++++++-- .../Main/MainSidebarViewController.swift | 32 +++++++++++++++---- .../Preferences/WellnessPrefsView.swift | 9 ++++++ 4 files changed, 62 insertions(+), 9 deletions(-) diff --git a/Tusker/Preferences/Preferences.swift b/Tusker/Preferences/Preferences.swift index 233d559582..8b52ff0a19 100644 --- a/Tusker/Preferences/Preferences.swift +++ b/Tusker/Preferences/Preferences.swift @@ -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 diff --git a/Tusker/Screens/Explore/ExploreViewController.swift b/Tusker/Screens/Explore/ExploreViewController.swift index 6413e58053..fe4cb070c6 100644 --- a/Tusker/Screens/Explore/ExploreViewController.swift +++ b/Tusker/Screens/Explore/ExploreViewController.swift @@ -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() 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) { + 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) diff --git a/Tusker/Screens/Main/MainSidebarViewController.swift b/Tusker/Screens/Main/MainSidebarViewController.swift index c2cfe6fd2b..251ad9dc4e 100644 --- a/Tusker/Screens/Main/MainSidebarViewController.swift +++ b/Tusker/Screens/Main/MainSidebarViewController.swift @@ -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) { + 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() { diff --git a/Tusker/Screens/Preferences/WellnessPrefsView.swift b/Tusker/Screens/Preferences/WellnessPrefsView.swift index b950b5be94..d96b3a6066 100644 --- a/Tusker/Screens/Preferences/WellnessPrefsView.swift +++ b/Tusker/Screens/Preferences/WellnessPrefsView.swift @@ -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 {