From 65485ecdf50c565acdd255f6073a876bdec9f364 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 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