From 60b7276ac4d3c30021d4d8ee14e0f5688ec88d8b Mon Sep 17 00:00:00 2001 From: Riley Testut Date: Wed, 16 Nov 2022 18:02:38 -0600 Subject: [PATCH] Supports updating apps from AppViewController Unlike MyAppsViewController, AppViewController will attempt to update to the latest available version, rather than the latest supported version. If the latest version is not supported, it will fall back to asking user to install last supported version. --- AltStore/App Detail/AppViewController.swift | 50 +++++++++++++++++++-- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/AltStore/App Detail/AppViewController.swift b/AltStore/App Detail/AppViewController.swift index 4d3ed3f8..47c8227b 100644 --- a/AltStore/App Detail/AppViewController.swift +++ b/AltStore/App Detail/AppViewController.swift @@ -371,13 +371,20 @@ private extension AppViewController button.tintColor = self.app.tintColor button.isIndicatingActivity = false - if self.app.installedApp == nil + if let installedApp = self.app.installedApp { - button.setTitle(NSLocalizedString("FREE", comment: ""), for: .normal) + if let latestVersion = self.app.latestAvailableVersion, installedApp.version != latestVersion.version + { + button.setTitle(NSLocalizedString("UPDATE", comment: ""), for: .normal) + } + else + { + button.setTitle(NSLocalizedString("OPEN", comment: ""), for: .normal) + } } else { - button.setTitle(NSLocalizedString("OPEN", comment: ""), for: .normal) + button.setTitle(NSLocalizedString("FREE", comment: ""), for: .normal) } let progress = AppManager.shared.installationProgress(for: self.app) @@ -486,7 +493,14 @@ extension AppViewController { if let installedApp = self.app.installedApp { - self.open(installedApp) + if let latestVersion = self.app.latestAvailableVersion, installedApp.version != latestVersion.version + { + self.updateApp(installedApp) + } + else + { + self.open(installedApp) + } } else { @@ -531,6 +545,34 @@ extension AppViewController { UIApplication.shared.open(installedApp.openAppURL) } + + func updateApp(_ installedApp: InstalledApp) + { + let previousProgress = AppManager.shared.installationProgress(for: installedApp) + guard previousProgress == nil else { + //TODO: Handle cancellation + //previousProgress?.cancel() + return + } + + _ = AppManager.shared.update(installedApp, to: .latestAvailableVersionWithFallback, presentingViewController: self) { (result) in + DispatchQueue.main.async { + switch result + { + case .success: print("Updated app from AppViewController:", installedApp.bundleIdentifier) + case .failure(OperationError.cancelled): break + case .failure(let error): + let toastView = ToastView(error: error) + toastView.opensErrorLog = true + toastView.show(in: self) + } + + self.update() + } + } + + self.update() + } } private extension AppViewController