diff --git a/AltStore/App Detail/AppViewController.swift b/AltStore/App Detail/AppViewController.swift index 6ea8a81f..aff7a3f2 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 { @@ -530,6 +544,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