Hides source detail screens after adding/removing source

Fixes various issues due to saving/deleting source while viewing source details.
This commit is contained in:
Riley Testut
2024-01-23 16:43:05 -06:00
committed by Magesh K
parent afb393b80b
commit 6d7d06a85e
4 changed files with 137 additions and 44 deletions

View File

@@ -391,61 +391,50 @@ private extension SourceDetailContentViewController
sender.isIndicatingActivity = true
Task<Void, Never> {
await self.addSourceThenDownloadApp(storeApp)
await self.downloadApp(storeApp)
sender.isIndicatingActivity = false
}
}
}
func addSourceThenDownloadApp(_ storeApp: StoreApp) async
@MainActor
func downloadApp(_ storeApp: StoreApp) async
{
do
{
let isAdded = try await self.source.isAdded
if !isAdded
{
let message = String(format: NSLocalizedString("You must add this source before you can install apps from it.\n\n“%@” will begin downloading once it has been added.", comment: ""), storeApp.name)
try await AppManager.shared.add(self.source, message: message, presentingViewController: self)
}
do
{
try await self.downloadApp(storeApp)
}
catch is CancellationError {}
catch
{
let toastView = ToastView(error: error)
toastView.opensErrorLog = true
toastView.show(in: self)
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
if let installedApp = storeApp.installedApp, installedApp.isUpdateAvailable
{
AppManager.shared.update(installedApp, presentingViewController: self) { result in
continuation.resume(with: result.map { _ in () })
}
reload()
}
else
{
Task<Void, Never> { @MainActor in
await AppManager.shared.installAsync(storeApp, presentingViewController: self) { result in
continuation.resume(with: result.map { _ in () })
}
reload()
}
}
}
}
catch is CancellationError {}
catch
{
await self.presentAlert(title: NSLocalizedString("Unable to Add Source", comment: ""), message: error.localizedDescription)
let toastView = ToastView(error: error)
toastView.opensErrorLog = true
toastView.show(in: self)
}
self.collectionView.reloadSections([Section.featuredApps.rawValue])
}
@MainActor
func downloadApp(_ storeApp: StoreApp) async throws
{
try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<Void, Error>) in
if let installedApp = storeApp.installedApp, installedApp.isUpdateAvailable
{
AppManager.shared.update(installedApp, presentingViewController: self) { result in
continuation.resume(with: result.map { _ in () })
}
}
else
{
AppManager.shared.install(storeApp, presentingViewController: self) { result in
continuation.resume(with: result.map { _ in () })
}
}
func reload()
{
UIView.performWithoutAnimation {
guard let index = self.appsDataSource.items.firstIndex(of: storeApp) else {
self.collectionView.reloadSections([Section.featuredApps.rawValue])

View File

@@ -80,7 +80,7 @@ class SourceDetailViewController: HeaderContentViewController<SourceHeaderView,
private var addButton: VibrantButton!
private var previousBounds: CGRect?
private var cancellable: AnyCancellable?
private var cancellables = Set<AnyCancellable>()
init?(source: Source, coder: NSCoder)
{
@@ -307,11 +307,41 @@ private extension SourceDetailViewController
{
func preparePipeline()
{
self.cancellable = Publishers
.CombineLatest(self.viewModel.$isSourceAdded, self.viewModel.$isAddingSource)
Publishers.CombineLatest(self.viewModel.$isSourceAdded, self.viewModel.$isAddingSource)
.receive(on: RunLoop.main)
.sink { [weak self] _ in
self?.update()
}
.store(in: &self.cancellables)
// Adding or removing a source while viewing source details is currently broken,
// so for now we just dismiss the view whenever the source is added or removed.
self.viewModel.$isSourceAdded
.compactMap { $0 }
.dropFirst() // Ignore first non-nil value.
.removeDuplicates()
.receive(on: RunLoop.main)
.sink { [weak self] isAdded in
if isAdded
{
self?.didAddSource()
}
else
{
self?.didRemoveSource()
}
}
.store(in: &self.cancellables)
}
func didAddSource()
{
guard let presentingViewController = self.navigationController?.presentingViewController else { return }
presentingViewController.dismiss(animated: true)
}
func didRemoveSource()
{
self.navigationController?.popToRootViewController(animated: true)
}
}