Remove direct accesses of MastodonController.instance

Fixes potential race conditions
This commit is contained in:
Shadowfacts 2023-05-28 22:04:41 -07:00
parent 2b5b749dc8
commit cb5b70a23a
3 changed files with 9 additions and 20 deletions

View File

@ -57,10 +57,6 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate, Collect
dataSource = createDataSource() dataSource = createDataSource()
applyInitialSnapshot() applyInitialSnapshot()
if mastodonController.instance == nil {
mastodonController.getOwnInstance(completion: self.ownInstanceLoaded(_:))
}
resultsController = SearchResultsViewController(mastodonController: mastodonController) resultsController = SearchResultsViewController(mastodonController: mastodonController)
resultsController.exploreNavigationController = self.navigationController! resultsController.exploreNavigationController = self.navigationController!
searchController = UISearchController(searchResultsController: resultsController) searchController = UISearchController(searchResultsController: resultsController)
@ -79,6 +75,9 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate, Collect
NotificationCenter.default.addObserver(self, selector: #selector(savedInstancesChanged), name: .savedInstancesChanged, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(savedInstancesChanged), name: .savedInstancesChanged, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(preferencesChanged), name: .preferencesChanged, object: nil)
mastodonController.instanceFeatures.featuresUpdated
.sink { [unowned self] in self.instanceFeaturesChanged() }
.store(in: &cancellables)
mastodonController.$lists mastodonController.$lists
.sink { [unowned self] in self.reloadLists($0) } .sink { [unowned self] in self.reloadLists($0) }
.store(in: &cancellables) .store(in: &cancellables)
@ -194,7 +193,7 @@ class ExploreViewController: UIViewController, UICollectionViewDelegate, Collect
snapshot.appendItems([.trends], toSection: .discover) snapshot.appendItems([.trends], toSection: .discover)
} }
private func ownInstanceLoaded(_ instance: Instance) { private func instanceFeaturesChanged() {
var snapshot = self.dataSource.snapshot() var snapshot = self.dataSource.snapshot()
if mastodonController.instanceFeatures.trends, if mastodonController.instanceFeatures.trends,
!snapshot.sectionIdentifiers.contains(.discover) { !snapshot.sectionIdentifiers.contains(.discover) {

View File

@ -97,10 +97,6 @@ class MainSidebarViewController: UIViewController {
applyInitialSnapshot() applyInitialSnapshot()
if mastodonController.instance == nil {
mastodonController.getOwnInstance(completion: self.ownInstanceLoaded(_:))
}
select(item: .tab(.timelines), animated: false) select(item: .tab(.timelines), animated: false)
NotificationCenter.default.addObserver(self, selector: #selector(reloadSavedInstances), name: .savedInstancesChanged, object: nil) NotificationCenter.default.addObserver(self, selector: #selector(reloadSavedInstances), name: .savedInstancesChanged, object: nil)
@ -189,14 +185,6 @@ class MainSidebarViewController: UIViewController {
reloadSavedInstances() reloadSavedInstances()
} }
private func ownInstanceLoaded(_ instance: Instance) {
let prevSelected = collectionView.indexPathsForSelectedItems
if let prevSelected = prevSelected?.first {
collectionView.selectItem(at: prevSelected, animated: false, scrollPosition: .top)
}
}
private func reloadLists(_ lists: [List], animated: Bool) { private func reloadLists(_ lists: [List], animated: Bool) {
if let selectedItem, if let selectedItem,
case .list(let list) = selectedItem, case .list(let list) = selectedItem,

View File

@ -24,9 +24,6 @@ struct ReportView: View {
self.account = mastodonController.persistentContainer.account(for: report.accountID)! self.account = mastodonController.persistentContainer.account(for: report.accountID)!
self.mastodonController = mastodonController self.mastodonController = mastodonController
self._report = StateObject(wrappedValue: report) self._report = StateObject(wrappedValue: report)
if mastodonController.instance?.rules == nil {
report.reason = .spam
}
} }
var body: some View { var body: some View {
@ -173,6 +170,11 @@ struct ReportView: View {
} }
} }
} }
.onReceive(mastodonController.$instance) { instance in
if instance?.rules == nil {
report.reason = .spam
}
}
} }
private func sendReport() { private func sendReport() {