@capgo/capacitor-updater 9.0.0 → 9.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CapgoCapacitorUpdater.podspec +3 -6
- package/Package.swift +30 -0
- package/README.md +2406 -291
- package/android/build.gradle +37 -17
- package/android/consumer-rules.pro +48 -0
- package/android/proguard-rules.pro +23 -0
- package/android/src/main/AndroidManifest.xml +0 -1
- package/android/src/main/java/ee/forgr/capacitor_updater/AndroidAppExitReporter.java +92 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/AppLifecycleObserver.java +115 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleInfo.java +227 -195
- package/android/src/main/java/ee/forgr/capacitor_updater/BundleStatus.java +24 -23
- package/android/src/main/java/ee/forgr/capacitor_updater/Callback.java +13 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +7927 -1243
- package/android/src/main/java/ee/forgr/capacitor_updater/CapgoUpdater.java +5785 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CryptoCipher.java +564 -121
- package/android/src/main/java/ee/forgr/capacitor_updater/DataManager.java +44 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayCondition.java +43 -49
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUntilNext.java +4 -4
- package/android/src/main/java/ee/forgr/capacitor_updater/DelayUpdateUtils.java +427 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DeviceIdHelper.java +230 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadService.java +2041 -118
- package/android/src/main/java/ee/forgr/capacitor_updater/DownloadWorkerManager.java +301 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/HeadlessUpdateWorker.java +569 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Http3.java +436 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/HttpEngineTransport.java +604 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/InternalUtils.java +79 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Logger.java +419 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/NativeUpdatePrompt.java +303 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeDetector.java +72 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ShakeMenu.java +963 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/StatsBatchFailureDiagnostics.java +40 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/ThreeFingerPinchDetector.java +323 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/Version.java +306 -0
- package/dist/docs.json +4431 -840
- package/dist/esm/definitions.d.ts +2641 -225
- package/dist/esm/definitions.js +103 -1
- package/dist/esm/definitions.js.map +1 -1
- package/dist/esm/history.d.ts +1 -0
- package/dist/esm/history.js +283 -0
- package/dist/esm/history.js.map +1 -0
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/index.js +5 -4
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/web.d.ts +53 -42
- package/dist/esm/web.js +180 -37
- package/dist/esm/web.js.map +1 -1
- package/dist/plugin.cjs.js +570 -37
- package/dist/plugin.cjs.js.map +1 -1
- package/dist/plugin.js +570 -37
- package/dist/plugin.js.map +1 -1
- package/ios/Sources/CapacitorUpdaterPlugin/AES.swift +262 -0
- package/ios/Sources/CapacitorUpdaterPlugin/AppHealthTracker.swift +82 -0
- package/ios/Sources/CapacitorUpdaterPlugin/BundleInfo.swift +185 -0
- package/ios/Sources/CapacitorUpdaterPlugin/BundleStatus.swift +127 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapacitorUpdaterPlugin.swift +6813 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoRawRsa.swift +36 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoSemanticVersion.swift +200 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CapgoUpdater.swift +5099 -0
- package/ios/Sources/CapacitorUpdaterPlugin/CryptoCipher.swift +387 -0
- package/ios/Sources/CapacitorUpdaterPlugin/DelayUpdateUtils.swift +343 -0
- package/ios/Sources/CapacitorUpdaterPlugin/DeviceIdHelper.swift +120 -0
- package/ios/Sources/CapacitorUpdaterPlugin/InternalUtils.swift +452 -0
- package/ios/Sources/CapacitorUpdaterPlugin/Logger.swift +380 -0
- package/ios/Sources/CapacitorUpdaterPlugin/NativeUpdatePrompt.swift +183 -0
- package/ios/Sources/CapacitorUpdaterPlugin/RSA.swift +96 -0
- package/ios/Sources/CapacitorUpdaterPlugin/RedirectPolicyDelegate.swift +97 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ShakeMenu.swift +876 -0
- package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/UserDefaultsExtension.swift +0 -2
- package/ios/Sources/CapacitorUpdaterPlugin/WKWebView+CapgoObjcBridge.swift +20 -0
- package/ios/Sources/CapacitorUpdaterPlugin/WebViewStatsReporter.swift +304 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ZipArchiveReader.swift +247 -0
- package/ios/Sources/CapacitorUpdaterPlugin/ZipCentralDirectory.swift +280 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/BuiltinHashesTests.swift +210 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/BundleHardeningTests.swift +292 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CapacitorUpdaterPluginTests.swift +6533 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CapgoSemanticVersionTests.swift +64 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ChecksumRequiredTests.swift +156 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/CliCryptoFixtureTests.swift +314 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/HttpsOnlyBuiltinMinimumTests.swift +653 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/InstallPerfTests.swift +174 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ListChannelsTests.swift +74 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/LoggerSecurityTests.swift +149 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/NativeContractTests.swift +160 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/NetworkTransportTests.swift +1109 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/PopulateDeltaCacheTests.swift +573 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/RedirectPolicyTests.swift +63 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/RsaContractTests.swift +281 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/SessionKeyRequiredTests.swift +196 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/TestZipWriter.swift +235 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/UpdateLifecycleTests.swift +470 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/WebKitOverlayGuardTests.swift +32 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ZipArchiveReaderTests.swift +302 -0
- package/ios/Tests/CapacitorUpdaterPluginTests/ZipBundleExtractionTests.swift +122 -0
- package/package.json +80 -43
- package/scripts/builtin-hashes-lib.mjs +148 -0
- package/scripts/capacitor-builtin-hashes-hook.mjs +65 -0
- package/scripts/resolve-real-path.mjs +25 -0
- package/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java +0 -1130
- package/ios/Plugin/BundleInfo.swift +0 -113
- package/ios/Plugin/BundleStatus.swift +0 -48
- package/ios/Plugin/CapacitorUpdater.swift +0 -858
- package/ios/Plugin/CapacitorUpdaterPlugin.h +0 -10
- package/ios/Plugin/CapacitorUpdaterPlugin.m +0 -27
- package/ios/Plugin/CapacitorUpdaterPlugin.swift +0 -677
- package/ios/Plugin/CryptoCipher.swift +0 -240
- /package/{LICENCE → LICENSE} +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayCondition.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/DelayUntilNext.swift +0 -0
- /package/ios/{Plugin → Sources/CapacitorUpdaterPlugin}/Info.plist +0 -0
|
@@ -0,0 +1,876 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
3
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import UIKit
|
|
8
|
+
import Capacitor
|
|
9
|
+
|
|
10
|
+
private var lastShakeMenuShownAt: TimeInterval = 0
|
|
11
|
+
private let shakeMenuCooldownSeconds: TimeInterval = 1.2
|
|
12
|
+
private let threeFingerPinchScaleDelta: CGFloat = 0.12
|
|
13
|
+
|
|
14
|
+
final class ThreeFingerPinchGestureRecognizer: UIGestureRecognizer {
|
|
15
|
+
private var initialSpan: CGFloat = 0
|
|
16
|
+
private(set) var scale: CGFloat = 1
|
|
17
|
+
var onTrackingStarted: (() -> Void)?
|
|
18
|
+
|
|
19
|
+
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent) {
|
|
20
|
+
super.touchesBegan(touches, with: event)
|
|
21
|
+
guard let view = self.view, let activeTouches = activeTouches(with: event), activeTouches.count <= 3 else {
|
|
22
|
+
self.state = .failed
|
|
23
|
+
return
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if activeTouches.count == 3 {
|
|
27
|
+
self.initialSpan = span(for: activeTouches, in: view)
|
|
28
|
+
self.scale = 1
|
|
29
|
+
if self.initialSpan > 0 {
|
|
30
|
+
self.onTrackingStarted?()
|
|
31
|
+
} else {
|
|
32
|
+
self.state = .failed
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent) {
|
|
38
|
+
super.touchesMoved(touches, with: event)
|
|
39
|
+
guard let view = self.view,
|
|
40
|
+
let activeTouches = activeTouches(with: event),
|
|
41
|
+
activeTouches.count == 3,
|
|
42
|
+
self.initialSpan > 0 else {
|
|
43
|
+
self.state = .failed
|
|
44
|
+
return
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
self.scale = span(for: activeTouches, in: view) / self.initialSpan
|
|
48
|
+
if abs(self.scale - 1) >= threeFingerPinchScaleDelta {
|
|
49
|
+
self.state = .recognized
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent) {
|
|
54
|
+
super.touchesEnded(touches, with: event)
|
|
55
|
+
if self.state == .possible {
|
|
56
|
+
self.state = .failed
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
override func touchesCancelled(_ touches: Set<UITouch>, with event: UIEvent) {
|
|
61
|
+
super.touchesCancelled(touches, with: event)
|
|
62
|
+
self.state = .cancelled
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
override func reset() {
|
|
66
|
+
super.reset()
|
|
67
|
+
self.initialSpan = 0
|
|
68
|
+
self.scale = 1
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
private func activeTouches(with event: UIEvent) -> [UITouch]? {
|
|
72
|
+
event.touches(for: self)?.filter { touch in
|
|
73
|
+
touch.phase != .ended && touch.phase != .cancelled
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private func span(for touches: [UITouch], in view: UIView) -> CGFloat {
|
|
78
|
+
guard !touches.isEmpty else {
|
|
79
|
+
return 0
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
let points = touches.map { $0.location(in: view) }
|
|
83
|
+
let center = points.reduce(CGPoint.zero) { result, point in
|
|
84
|
+
CGPoint(x: result.x + point.x, y: result.y + point.y)
|
|
85
|
+
}
|
|
86
|
+
let centerPoint = CGPoint(x: center.x / CGFloat(points.count), y: center.y / CGFloat(points.count))
|
|
87
|
+
let totalDistance = points.reduce(CGFloat(0)) { result, point in
|
|
88
|
+
result + hypot(point.x - centerPoint.x, point.y - centerPoint.y)
|
|
89
|
+
}
|
|
90
|
+
return totalDistance / CGFloat(points.count)
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
extension UIApplication {
|
|
95
|
+
public class func topViewController(_ base: UIViewController? = nil) -> UIViewController? {
|
|
96
|
+
let resolvedBase = base ?? keyWindowRootViewController
|
|
97
|
+
if let nav = resolvedBase as? UINavigationController {
|
|
98
|
+
return topViewController(nav.visibleViewController)
|
|
99
|
+
}
|
|
100
|
+
if let tab = resolvedBase as? UITabBarController, let selected = tab.selectedViewController {
|
|
101
|
+
return topViewController(selected)
|
|
102
|
+
}
|
|
103
|
+
if let presented = resolvedBase?.presentedViewController {
|
|
104
|
+
return topViewController(presented)
|
|
105
|
+
}
|
|
106
|
+
return resolvedBase
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
private static var keyWindowRootViewController: UIViewController? {
|
|
110
|
+
let scenes = shared.connectedScenes.compactMap { $0 as? UIWindowScene }
|
|
111
|
+
let foregroundScenes = scenes.filter { $0.activationState == .foregroundActive }
|
|
112
|
+
let candidates = foregroundScenes.isEmpty ? scenes : foregroundScenes
|
|
113
|
+
let window = candidates.flatMap(\.windows).first(where: \.isKeyWindow) ?? candidates.first?.windows.first
|
|
114
|
+
return window?.rootViewController
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
extension CapacitorUpdaterPlugin: UIGestureRecognizerDelegate {
|
|
119
|
+
func syncShakeMenuGestureRecognizer() {
|
|
120
|
+
DispatchQueue.main.async { [weak self] in
|
|
121
|
+
guard let self else { return }
|
|
122
|
+
|
|
123
|
+
let shouldInstall = self.shakeMenuGesture == Self.shakeMenuGestureThreeFingerPinch &&
|
|
124
|
+
(self.shakeMenuEnabled || self.shakeChannelSelectorEnabled)
|
|
125
|
+
|
|
126
|
+
guard shouldInstall, let targetView = self.bridge?.viewController?.view ?? self.bridge?.webView else {
|
|
127
|
+
self.removeShakeMenuGestureRecognizer()
|
|
128
|
+
return
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if self.shakeMenuPinchGestureRecognizer?.view === targetView {
|
|
132
|
+
return
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
self.removeShakeMenuGestureRecognizer()
|
|
136
|
+
|
|
137
|
+
let recognizer = ThreeFingerPinchGestureRecognizer(target: self, action: #selector(self.handleShakeMenuPinch(_:)))
|
|
138
|
+
recognizer.cancelsTouchesInView = false
|
|
139
|
+
recognizer.delaysTouchesBegan = false
|
|
140
|
+
recognizer.delaysTouchesEnded = false
|
|
141
|
+
recognizer.delegate = self
|
|
142
|
+
recognizer.onTrackingStarted = { [weak self] in
|
|
143
|
+
self?.logger.info("Three finger pinch tracking started")
|
|
144
|
+
}
|
|
145
|
+
targetView.addGestureRecognizer(recognizer)
|
|
146
|
+
self.shakeMenuPinchGestureRecognizer = recognizer
|
|
147
|
+
self.logger.info("Three finger pinch menu gesture initialized")
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
func removeShakeMenuGestureRecognizer() {
|
|
152
|
+
if let recognizer = self.shakeMenuPinchGestureRecognizer {
|
|
153
|
+
recognizer.view?.removeGestureRecognizer(recognizer)
|
|
154
|
+
self.shakeMenuPinchGestureRecognizer = nil
|
|
155
|
+
self.shakeMenuPinchGestureTriggered = false
|
|
156
|
+
self.logger.info("Three finger pinch menu gesture stopped")
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
@objc func handleShakeMenuPinch(_ recognizer: ThreeFingerPinchGestureRecognizer) {
|
|
161
|
+
guard recognizer.state == .recognized, !self.shakeMenuPinchGestureTriggered else {
|
|
162
|
+
return
|
|
163
|
+
}
|
|
164
|
+
guard abs(recognizer.scale - 1) >= threeFingerPinchScaleDelta else {
|
|
165
|
+
return
|
|
166
|
+
}
|
|
167
|
+
guard self.shakeMenuGesture == Self.shakeMenuGestureThreeFingerPinch, let bridge = self.bridge else {
|
|
168
|
+
return
|
|
169
|
+
}
|
|
170
|
+
guard let window = recognizer.view?.window ?? bridge.viewController?.view.window else {
|
|
171
|
+
return
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
self.shakeMenuPinchGestureTriggered = true
|
|
175
|
+
self.logger.info("Three finger pinch detected")
|
|
176
|
+
_ = window.showCapacitorUpdaterMenu(plugin: self, bridge: bridge, gestureName: "Three finger pinch")
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
public func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
|
|
180
|
+
if gestureRecognizer === self.shakeMenuPinchGestureRecognizer {
|
|
181
|
+
self.shakeMenuPinchGestureTriggered = false
|
|
182
|
+
}
|
|
183
|
+
return true
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
public func gestureRecognizer(
|
|
187
|
+
_: UIGestureRecognizer,
|
|
188
|
+
shouldRecognizeSimultaneouslyWith _: UIGestureRecognizer
|
|
189
|
+
) -> Bool {
|
|
190
|
+
true
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
extension UIWindow {
|
|
195
|
+
private var menuHostViewController: UIViewController? {
|
|
196
|
+
UIApplication.topViewController(rootViewController)
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
override open func motionEnded(_ motion: UIEvent.EventSubtype, with event: UIEvent?) {
|
|
200
|
+
if motion == .motionShake {
|
|
201
|
+
guard let bridgeViewController = rootViewController as? CAPBridgeViewController,
|
|
202
|
+
let bridge = bridgeViewController.bridge,
|
|
203
|
+
let plugin = bridge.plugin(withName: "CapacitorUpdater") as? CapacitorUpdaterPlugin else {
|
|
204
|
+
return
|
|
205
|
+
}
|
|
206
|
+
guard plugin.shakeMenuGesture == CapacitorUpdaterPlugin.shakeMenuGestureShake else {
|
|
207
|
+
return
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
_ = showCapacitorUpdaterMenu(plugin: plugin, bridge: bridge, gestureName: "Shake")
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
@discardableResult
|
|
215
|
+
fileprivate func showCapacitorUpdaterMenu(plugin: CapacitorUpdaterPlugin, bridge: CAPBridgeProtocol, gestureName: String) -> Bool {
|
|
216
|
+
let canShowPreviewMenu = plugin.shakeMenuEnabled && plugin.hasActivePreviewSession()
|
|
217
|
+
let canShowChannelSelector = plugin.shakeChannelSelectorEnabled
|
|
218
|
+
|
|
219
|
+
if !canShowPreviewMenu && !canShowChannelSelector {
|
|
220
|
+
if plugin.shakeMenuEnabled {
|
|
221
|
+
plugin.logger.info("\(gestureName) preview menu ignored because no preview session is active")
|
|
222
|
+
}
|
|
223
|
+
return false
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
let now = Date().timeIntervalSince1970
|
|
227
|
+
guard now - lastShakeMenuShownAt >= shakeMenuCooldownSeconds else {
|
|
228
|
+
plugin.logger.info("\(gestureName) menu ignored because cooldown is active")
|
|
229
|
+
return false
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
let didShow = canShowPreviewMenu
|
|
233
|
+
? showDefaultMenu(plugin: plugin, bridge: bridge)
|
|
234
|
+
: showChannelSelector(plugin: plugin, bridge: bridge)
|
|
235
|
+
|
|
236
|
+
if didShow {
|
|
237
|
+
lastShakeMenuShownAt = now
|
|
238
|
+
}
|
|
239
|
+
return didShow
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
@discardableResult
|
|
243
|
+
private func showDefaultMenu(plugin: CapacitorUpdaterPlugin, bridge: CAPBridgeProtocol) -> Bool {
|
|
244
|
+
// Prevent multiple alerts from showing
|
|
245
|
+
guard let topVC = self.menuHostViewController else {
|
|
246
|
+
return false
|
|
247
|
+
}
|
|
248
|
+
if topVC.isKind(of: UIAlertController.self) {
|
|
249
|
+
plugin.logger.info("UIAlertController is already presented")
|
|
250
|
+
return false
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
guard plugin.hasActivePreviewSession() else {
|
|
254
|
+
plugin.logger.info("Shake preview menu ignored because no preview session is active")
|
|
255
|
+
return false
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
let appName = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String ?? "App"
|
|
259
|
+
let title = "Preview \(appName) Menu"
|
|
260
|
+
let message = "Reload, switch, or leave the current preview."
|
|
261
|
+
let okButtonTitle = "Leave test app"
|
|
262
|
+
let reloadButtonTitle = "Reload preview"
|
|
263
|
+
let cancelButtonTitle = "Close menu"
|
|
264
|
+
|
|
265
|
+
let alertShake = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
|
266
|
+
|
|
267
|
+
alertShake.addAction(UIAlertAction(title: reloadButtonTitle, style: .default) { _ in
|
|
268
|
+
DispatchQueue.global(qos: .userInitiated).async {
|
|
269
|
+
if !plugin.reloadPreviewSessionFromShakeMenu() {
|
|
270
|
+
DispatchQueue.main.async {
|
|
271
|
+
self.showError(message: "Could not reload the test app.", plugin: plugin)
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
})
|
|
276
|
+
|
|
277
|
+
if !plugin.previewMenuPreviews().isEmpty {
|
|
278
|
+
alertShake.addAction(UIAlertAction(title: "Switch preview", style: .default) { _ in
|
|
279
|
+
let showSelector = {
|
|
280
|
+
self.showPreviewSelector(plugin: plugin)
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if let presenter = alertShake.presentingViewController {
|
|
284
|
+
presenter.dismiss(animated: true, completion: showSelector)
|
|
285
|
+
} else {
|
|
286
|
+
DispatchQueue.main.async(execute: showSelector)
|
|
287
|
+
}
|
|
288
|
+
})
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
if plugin.shakeChannelSelectorEnabled {
|
|
292
|
+
alertShake.addAction(UIAlertAction(title: "Switch channel", style: .default) { _ in
|
|
293
|
+
let showSelector = {
|
|
294
|
+
_ = self.showChannelSelector(plugin: plugin, bridge: bridge)
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
if let presenter = alertShake.presentingViewController {
|
|
298
|
+
presenter.dismiss(animated: true, completion: showSelector)
|
|
299
|
+
} else {
|
|
300
|
+
DispatchQueue.main.async(execute: showSelector)
|
|
301
|
+
}
|
|
302
|
+
})
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
alertShake.addAction(UIAlertAction(title: okButtonTitle, style: .default) { _ in
|
|
306
|
+
DispatchQueue.global(qos: .userInitiated).async {
|
|
307
|
+
if !plugin.leavePreviewSessionFromShakeMenu() {
|
|
308
|
+
DispatchQueue.main.async {
|
|
309
|
+
self.showError(message: "Could not leave the test app.", plugin: plugin)
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
})
|
|
314
|
+
|
|
315
|
+
alertShake.addAction(UIAlertAction(title: cancelButtonTitle, style: .default))
|
|
316
|
+
|
|
317
|
+
DispatchQueue.main.async {
|
|
318
|
+
topVC.present(alertShake, animated: true)
|
|
319
|
+
}
|
|
320
|
+
return true
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
private func showConfiguredDefaultMenu(plugin: CapacitorUpdaterPlugin, bridge: CAPBridgeProtocol) {
|
|
324
|
+
let appName = Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String ?? "App"
|
|
325
|
+
let title = "Preview \(appName) Menu"
|
|
326
|
+
let message = "What would you like to do?"
|
|
327
|
+
let okButtonTitle = "Go Home"
|
|
328
|
+
let reloadButtonTitle = "Reload app"
|
|
329
|
+
let cancelButtonTitle = "Close menu"
|
|
330
|
+
|
|
331
|
+
let updater = plugin.implementation
|
|
332
|
+
|
|
333
|
+
func resetBuiltin() {
|
|
334
|
+
updater.reset()
|
|
335
|
+
bridge.setServerBasePath("")
|
|
336
|
+
DispatchQueue.main.async {
|
|
337
|
+
if let viewController = (self.rootViewController as? CAPBridgeViewController) {
|
|
338
|
+
viewController.loadView()
|
|
339
|
+
viewController.viewDidLoad()
|
|
340
|
+
}
|
|
341
|
+
_ = updater.delete(id: updater.getCurrentBundleId())
|
|
342
|
+
plugin.logger.info("Reset to builtin version")
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
let bundleId = updater.getCurrentBundleId()
|
|
347
|
+
if let viewController = (self.rootViewController as? CAPBridgeViewController) {
|
|
348
|
+
plugin.logger.info("getServerBasePath: \(viewController.getServerBasePath())")
|
|
349
|
+
}
|
|
350
|
+
plugin.logger.info("bundleId: \(bundleId)")
|
|
351
|
+
|
|
352
|
+
let alertShake = UIAlertController(title: title, message: message, preferredStyle: .alert)
|
|
353
|
+
|
|
354
|
+
alertShake.addAction(UIAlertAction(title: okButtonTitle, style: .default) { _ in
|
|
355
|
+
guard let next = updater.getNextBundle() else {
|
|
356
|
+
resetBuiltin()
|
|
357
|
+
return
|
|
358
|
+
}
|
|
359
|
+
if !next.isBuiltin() {
|
|
360
|
+
plugin.logger.info("Resetting to: \(next.toString())")
|
|
361
|
+
// A pending bundle below the native version floor is rejected: never load it, fall back to builtin.
|
|
362
|
+
guard updater.set(bundle: next) else {
|
|
363
|
+
plugin.logger.warn("Cannot activate \(next.toString()), resetting to builtin")
|
|
364
|
+
resetBuiltin()
|
|
365
|
+
return
|
|
366
|
+
}
|
|
367
|
+
guard let destHot = try? updater.getBundleDirectory(id: next.getId()) else {
|
|
368
|
+
resetBuiltin()
|
|
369
|
+
return
|
|
370
|
+
}
|
|
371
|
+
plugin.logger.info("Reloading \(next.toString())")
|
|
372
|
+
bridge.setServerBasePath(destHot.path)
|
|
373
|
+
} else {
|
|
374
|
+
resetBuiltin()
|
|
375
|
+
}
|
|
376
|
+
plugin.logger.info("Reload app done")
|
|
377
|
+
})
|
|
378
|
+
|
|
379
|
+
alertShake.addAction(UIAlertAction(title: cancelButtonTitle, style: .default))
|
|
380
|
+
|
|
381
|
+
alertShake.addAction(UIAlertAction(title: reloadButtonTitle, style: .default) { _ in
|
|
382
|
+
DispatchQueue.main.async {
|
|
383
|
+
bridge.webView?.reload()
|
|
384
|
+
}
|
|
385
|
+
})
|
|
386
|
+
|
|
387
|
+
DispatchQueue.main.async {
|
|
388
|
+
if let topVC = self.menuHostViewController {
|
|
389
|
+
topVC.present(alertShake, animated: true)
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
private func previewLabel(_ preview: [String: Any]) -> String {
|
|
395
|
+
let bundle = preview["bundle"] as? [String: Any]
|
|
396
|
+
let name = preview["name"] as? String
|
|
397
|
+
let version = bundle?["version"] as? String
|
|
398
|
+
var label = [name, version, preview["id"] as? String]
|
|
399
|
+
.compactMap { value in
|
|
400
|
+
guard let value, !value.isEmpty else { return nil }
|
|
401
|
+
return value
|
|
402
|
+
}
|
|
403
|
+
.first ?? "Preview"
|
|
404
|
+
if preview["isActive"] as? Bool == true {
|
|
405
|
+
label += " (current)"
|
|
406
|
+
}
|
|
407
|
+
return label
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
private func showPreviewSelector(plugin: CapacitorUpdaterPlugin) {
|
|
411
|
+
guard let topVC = self.menuHostViewController else {
|
|
412
|
+
return
|
|
413
|
+
}
|
|
414
|
+
if topVC.isKind(of: UIAlertController.self) {
|
|
415
|
+
plugin.logger.info("UIAlertController is already presented")
|
|
416
|
+
return
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
let previews = plugin.previewMenuPreviews()
|
|
420
|
+
guard !previews.isEmpty else {
|
|
421
|
+
self.showError(message: "No saved previews available on this device.", plugin: plugin)
|
|
422
|
+
return
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
let alert = UIAlertController(title: "Select Preview", message: "Choose a local preview to open", preferredStyle: .actionSheet)
|
|
426
|
+
let previewsToShow = Array(previews.prefix(5))
|
|
427
|
+
for preview in previewsToShow {
|
|
428
|
+
let title = self.previewLabel(preview)
|
|
429
|
+
let id = preview["id"] as? String ?? ""
|
|
430
|
+
alert.addAction(UIAlertAction(title: title, style: .default) { [weak self] _ in
|
|
431
|
+
self?.selectPreview(id: id, plugin: plugin)
|
|
432
|
+
})
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
if previews.count > 5 {
|
|
436
|
+
alert.addAction(UIAlertAction(title: "More previews...", style: .default) { [weak self] _ in
|
|
437
|
+
self?.showSearchablePreviewPicker(previews: previews, plugin: plugin)
|
|
438
|
+
})
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
|
|
442
|
+
|
|
443
|
+
if let popoverController = alert.popoverPresentationController {
|
|
444
|
+
popoverController.sourceView = self
|
|
445
|
+
popoverController.sourceRect = CGRect(x: self.bounds.midX, y: self.bounds.midY, width: 0, height: 0)
|
|
446
|
+
popoverController.permittedArrowDirections = []
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
topVC.present(alert, animated: true)
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
private func showSearchablePreviewPicker(previews: [[String: Any]], plugin: CapacitorUpdaterPlugin) {
|
|
453
|
+
let alert = UIAlertController(title: "Search Previews", message: "Enter preview name or version", preferredStyle: .alert)
|
|
454
|
+
|
|
455
|
+
alert.addTextField { textField in
|
|
456
|
+
textField.placeholder = "Preview name..."
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
alert.addAction(UIAlertAction(title: "Search", style: .default) { [weak self, weak alert] _ in
|
|
460
|
+
guard let self else { return }
|
|
461
|
+
guard let searchText = alert?.textFields?.first?.text?.lowercased(), !searchText.isEmpty else {
|
|
462
|
+
self.showPreviewSelector(plugin: plugin)
|
|
463
|
+
return
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
let filtered = previews.filter { self.previewLabel($0).lowercased().contains(searchText) }
|
|
467
|
+
if filtered.isEmpty {
|
|
468
|
+
self.showError(message: "No previews found matching '\(searchText)'", plugin: plugin)
|
|
469
|
+
} else if filtered.count == 1, let id = filtered[0]["id"] as? String {
|
|
470
|
+
self.selectPreview(id: id, plugin: plugin)
|
|
471
|
+
} else {
|
|
472
|
+
self.presentPreviewPicker(previews: filtered, plugin: plugin)
|
|
473
|
+
}
|
|
474
|
+
})
|
|
475
|
+
|
|
476
|
+
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
|
|
477
|
+
|
|
478
|
+
DispatchQueue.main.async {
|
|
479
|
+
if let topVC = self.menuHostViewController {
|
|
480
|
+
topVC.present(alert, animated: true)
|
|
481
|
+
}
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
private func presentPreviewPicker(previews: [[String: Any]], plugin: CapacitorUpdaterPlugin) {
|
|
486
|
+
let alert = UIAlertController(title: "Select Preview", message: "Choose a local preview to open", preferredStyle: .actionSheet)
|
|
487
|
+
for preview in previews.prefix(5) {
|
|
488
|
+
let id = preview["id"] as? String ?? ""
|
|
489
|
+
alert.addAction(UIAlertAction(title: self.previewLabel(preview), style: .default) { [weak self] _ in
|
|
490
|
+
self?.selectPreview(id: id, plugin: plugin)
|
|
491
|
+
})
|
|
492
|
+
}
|
|
493
|
+
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
|
|
494
|
+
|
|
495
|
+
if let popoverController = alert.popoverPresentationController {
|
|
496
|
+
popoverController.sourceView = self
|
|
497
|
+
popoverController.sourceRect = CGRect(x: self.bounds.midX, y: self.bounds.midY, width: 0, height: 0)
|
|
498
|
+
popoverController.permittedArrowDirections = []
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
DispatchQueue.main.async {
|
|
502
|
+
if let topVC = self.menuHostViewController {
|
|
503
|
+
topVC.present(alert, animated: true)
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
private func selectPreview(id: String, plugin: CapacitorUpdaterPlugin) {
|
|
509
|
+
DispatchQueue.global(qos: .userInitiated).async {
|
|
510
|
+
if !plugin.setPreviewFromShakeMenu(id: id) {
|
|
511
|
+
DispatchQueue.main.async {
|
|
512
|
+
self.showError(message: "Could not switch preview.", plugin: plugin)
|
|
513
|
+
}
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
}
|
|
517
|
+
|
|
518
|
+
@discardableResult
|
|
519
|
+
private func showChannelSelector(plugin: CapacitorUpdaterPlugin, bridge: CAPBridgeProtocol) -> Bool {
|
|
520
|
+
// Prevent multiple alerts from showing
|
|
521
|
+
guard let topVC = self.menuHostViewController else {
|
|
522
|
+
return false
|
|
523
|
+
}
|
|
524
|
+
if topVC.isKind(of: UIAlertController.self) {
|
|
525
|
+
plugin.logger.info("UIAlertController is already presented")
|
|
526
|
+
return false
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
let updater = plugin.implementation
|
|
530
|
+
|
|
531
|
+
// Show loading indicator
|
|
532
|
+
let loadingAlert = UIAlertController(title: "Loading Channels...", message: nil, preferredStyle: .alert)
|
|
533
|
+
var didCancel = false
|
|
534
|
+
let loadingIndicator = UIActivityIndicatorView(style: .medium)
|
|
535
|
+
loadingIndicator.translatesAutoresizingMaskIntoConstraints = false
|
|
536
|
+
loadingIndicator.startAnimating()
|
|
537
|
+
loadingAlert.view.addSubview(loadingIndicator)
|
|
538
|
+
|
|
539
|
+
NSLayoutConstraint.activate([
|
|
540
|
+
loadingIndicator.centerXAnchor.constraint(equalTo: loadingAlert.view.centerXAnchor),
|
|
541
|
+
loadingIndicator.bottomAnchor.constraint(equalTo: loadingAlert.view.bottomAnchor, constant: -20)
|
|
542
|
+
])
|
|
543
|
+
|
|
544
|
+
// Add cancel button to loading alert
|
|
545
|
+
loadingAlert.addAction(UIAlertAction(title: "Cancel", style: .cancel) { _ in
|
|
546
|
+
didCancel = true
|
|
547
|
+
})
|
|
548
|
+
|
|
549
|
+
DispatchQueue.main.async {
|
|
550
|
+
topVC.present(loadingAlert, animated: true) {
|
|
551
|
+
// Fetch channels in background
|
|
552
|
+
DispatchQueue.global(qos: .userInitiated).async {
|
|
553
|
+
let result = updater.listChannels()
|
|
554
|
+
|
|
555
|
+
DispatchQueue.main.async {
|
|
556
|
+
loadingAlert.dismiss(animated: true) {
|
|
557
|
+
guard !didCancel else { return }
|
|
558
|
+
if !result.error.isEmpty {
|
|
559
|
+
self.showError(message: "Failed to load channels: \(result.error)", plugin: plugin)
|
|
560
|
+
} else if result.channels.isEmpty {
|
|
561
|
+
self.showError(message: "No channels available for self-assignment", plugin: plugin)
|
|
562
|
+
} else {
|
|
563
|
+
self.presentChannelPicker(channels: result.channels, plugin: plugin, bridge: bridge)
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
}
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
return true
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
private func presentChannelPicker(channels: [[String: Any]], plugin: CapacitorUpdaterPlugin, bridge: CAPBridgeProtocol) {
|
|
574
|
+
let alert = UIAlertController(title: "Select Channel", message: "Choose a channel to switch to", preferredStyle: .actionSheet)
|
|
575
|
+
|
|
576
|
+
// Get channel names
|
|
577
|
+
let channelNames = channels.compactMap { $0["name"] as? String }
|
|
578
|
+
|
|
579
|
+
// Show first 5 channels as actions
|
|
580
|
+
let channelsToShow = Array(channelNames.prefix(5))
|
|
581
|
+
|
|
582
|
+
for channelName in channelsToShow {
|
|
583
|
+
alert.addAction(UIAlertAction(title: channelName, style: .default) { [weak self] _ in
|
|
584
|
+
self?.selectChannel(name: channelName, plugin: plugin, bridge: bridge)
|
|
585
|
+
})
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
// If there are more channels, add a "More..." option
|
|
589
|
+
if channelNames.count > 5 {
|
|
590
|
+
alert.addAction(UIAlertAction(title: "More channels...", style: .default) { [weak self] _ in
|
|
591
|
+
self?.showSearchableChannelPicker(channels: channels, plugin: plugin, bridge: bridge)
|
|
592
|
+
})
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
|
|
596
|
+
|
|
597
|
+
// For iPad support
|
|
598
|
+
if let popoverController = alert.popoverPresentationController {
|
|
599
|
+
popoverController.sourceView = self
|
|
600
|
+
popoverController.sourceRect = CGRect(x: self.bounds.midX, y: self.bounds.midY, width: 0, height: 0)
|
|
601
|
+
popoverController.permittedArrowDirections = []
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
DispatchQueue.main.async {
|
|
605
|
+
if let topVC = self.menuHostViewController {
|
|
606
|
+
topVC.present(alert, animated: true)
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
private func showSearchableChannelPicker(channels: [[String: Any]], plugin: CapacitorUpdaterPlugin, bridge: CAPBridgeProtocol) {
|
|
612
|
+
let alert = UIAlertController(title: "Search Channels", message: "Enter channel name to search", preferredStyle: .alert)
|
|
613
|
+
|
|
614
|
+
alert.addTextField { textField in
|
|
615
|
+
textField.placeholder = "Channel name..."
|
|
616
|
+
}
|
|
617
|
+
|
|
618
|
+
let channelNames = channels.compactMap { $0["name"] as? String }
|
|
619
|
+
|
|
620
|
+
alert.addAction(UIAlertAction(title: "Search", style: .default) { [weak self, weak alert] _ in
|
|
621
|
+
guard let searchText = alert?.textFields?.first?.text?.lowercased(), !searchText.isEmpty else {
|
|
622
|
+
// If empty, show first 5
|
|
623
|
+
self?.presentChannelPicker(channels: channels, plugin: plugin, bridge: bridge)
|
|
624
|
+
return
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// Filter channels
|
|
628
|
+
let filtered = channelNames.filter { $0.lowercased().contains(searchText) }
|
|
629
|
+
|
|
630
|
+
if filtered.isEmpty {
|
|
631
|
+
self?.showError(message: "No channels found matching '\(searchText)'", plugin: plugin)
|
|
632
|
+
} else if filtered.count == 1 {
|
|
633
|
+
// Directly select if only one match
|
|
634
|
+
self?.selectChannel(name: filtered[0], plugin: plugin, bridge: bridge)
|
|
635
|
+
} else {
|
|
636
|
+
// Show filtered results
|
|
637
|
+
let filteredChannels = channels.filter { channel in
|
|
638
|
+
if let name = channel["name"] as? String {
|
|
639
|
+
return name.lowercased().contains(searchText)
|
|
640
|
+
}
|
|
641
|
+
return false
|
|
642
|
+
}
|
|
643
|
+
self?.presentChannelPicker(channels: filteredChannels, plugin: plugin, bridge: bridge)
|
|
644
|
+
}
|
|
645
|
+
})
|
|
646
|
+
|
|
647
|
+
alert.addAction(UIAlertAction(title: "Cancel", style: .cancel))
|
|
648
|
+
|
|
649
|
+
DispatchQueue.main.async {
|
|
650
|
+
if let topVC = self.menuHostViewController {
|
|
651
|
+
topVC.present(alert, animated: true)
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
private func selectChannel(name: String, plugin: CapacitorUpdaterPlugin, bridge: CAPBridgeProtocol) {
|
|
657
|
+
let updater = plugin.implementation
|
|
658
|
+
|
|
659
|
+
// Show progress indicator
|
|
660
|
+
let progressAlert = UIAlertController(title: "Switching to \(name)", message: "Setting channel...", preferredStyle: .alert)
|
|
661
|
+
let indicator = UIActivityIndicatorView(style: .medium)
|
|
662
|
+
indicator.translatesAutoresizingMaskIntoConstraints = false
|
|
663
|
+
indicator.startAnimating()
|
|
664
|
+
progressAlert.view.addSubview(indicator)
|
|
665
|
+
|
|
666
|
+
NSLayoutConstraint.activate([
|
|
667
|
+
indicator.centerXAnchor.constraint(equalTo: progressAlert.view.centerXAnchor),
|
|
668
|
+
indicator.bottomAnchor.constraint(equalTo: progressAlert.view.bottomAnchor, constant: -20)
|
|
669
|
+
])
|
|
670
|
+
|
|
671
|
+
DispatchQueue.main.async {
|
|
672
|
+
if let topVC = self.menuHostViewController {
|
|
673
|
+
topVC.present(progressAlert, animated: true) {
|
|
674
|
+
DispatchQueue.global(qos: .userInitiated).async {
|
|
675
|
+
// Set the channel - respect plugin's allowSetDefaultChannel config
|
|
676
|
+
let setResult = updater.setChannel(
|
|
677
|
+
channel: name,
|
|
678
|
+
defaultChannelKey: "CapacitorUpdater.defaultChannel",
|
|
679
|
+
defaultChannelFromSetChannelKey: CapacitorUpdaterPlugin.defaultChannelFromSetChannelStorageKey,
|
|
680
|
+
allowSetDefaultChannel: plugin.allowSetDefaultChannel,
|
|
681
|
+
configDefaultChannel: plugin.getConfig().getString("defaultChannel", "") ?? ""
|
|
682
|
+
)
|
|
683
|
+
|
|
684
|
+
if !setResult.error.isEmpty {
|
|
685
|
+
DispatchQueue.main.async {
|
|
686
|
+
progressAlert.dismiss(animated: true) {
|
|
687
|
+
self.showError(message: "Failed to set channel: \(setResult.error)", plugin: plugin)
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
return
|
|
691
|
+
}
|
|
692
|
+
guard plugin.persistDefaultChannelStateFromDefaults() else {
|
|
693
|
+
DispatchQueue.main.async {
|
|
694
|
+
progressAlert.dismiss(animated: true) {
|
|
695
|
+
self.showError(message: "Channel set to \(name), but local persistence failed.", plugin: plugin)
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
return
|
|
699
|
+
}
|
|
700
|
+
|
|
701
|
+
// Update progress message
|
|
702
|
+
DispatchQueue.main.async {
|
|
703
|
+
progressAlert.message = "Checking for updates..."
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
// Check for updates with the new channel
|
|
707
|
+
let pluginUpdateUrl = plugin.getUpdateUrl()
|
|
708
|
+
let updateUrlStr = pluginUpdateUrl.isEmpty ? CapacitorUpdaterPlugin.updateUrlDefault : pluginUpdateUrl
|
|
709
|
+
guard let updateUrl = URL(string: updateUrlStr) else {
|
|
710
|
+
DispatchQueue.main.async {
|
|
711
|
+
progressAlert.dismiss(animated: true) {
|
|
712
|
+
self.showError(
|
|
713
|
+
message: "Channel set to \(name). Invalid update URL, could not check for updates.",
|
|
714
|
+
plugin: plugin
|
|
715
|
+
)
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
return
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
let latest = updater.getLatest(url: updateUrl, channel: name)
|
|
722
|
+
let latestKind = latest.kind
|
|
723
|
+
|
|
724
|
+
let detail = [latest.message, latest.error, latestKind]
|
|
725
|
+
.compactMap { value in
|
|
726
|
+
guard let value, !value.isEmpty else { return nil }
|
|
727
|
+
return value
|
|
728
|
+
}
|
|
729
|
+
.first ?? "server did not provide a message"
|
|
730
|
+
|
|
731
|
+
// Handle update errors first (before "no new version" check)
|
|
732
|
+
if latestKind == "failed" || (latest.error?.isEmpty == false && latestKind != "up_to_date" && latestKind != "blocked") {
|
|
733
|
+
DispatchQueue.main.async {
|
|
734
|
+
progressAlert.dismiss(animated: true) {
|
|
735
|
+
self.showError(message: "Channel set to \(name). Update check failed: \(detail)", plugin: plugin)
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
return
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
if latestKind == "blocked" {
|
|
742
|
+
DispatchQueue.main.async {
|
|
743
|
+
progressAlert.dismiss(animated: true) {
|
|
744
|
+
self.showError(message: "Channel set to \(name). Update check blocked: \(detail)", plugin: plugin)
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
return
|
|
748
|
+
}
|
|
749
|
+
|
|
750
|
+
// Check if there's an actual update available. A manifest-only
|
|
751
|
+
// response legitimately has no URL (the files come from the
|
|
752
|
+
// manifest, not a zip), so only report "already on latest" when
|
|
753
|
+
// the URL is empty AND there is no manifest to download from.
|
|
754
|
+
let hasManifest = !(latest.manifest?.isEmpty ?? true)
|
|
755
|
+
if latestKind == "up_to_date" || (latest.url.isEmpty && !hasManifest) {
|
|
756
|
+
DispatchQueue.main.async {
|
|
757
|
+
progressAlert.dismiss(animated: true) {
|
|
758
|
+
self.showSuccess(message: "Channel set to \(name). Already on latest version.", plugin: plugin)
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
return
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// Update message
|
|
765
|
+
DispatchQueue.main.async {
|
|
766
|
+
progressAlert.message = "Downloading update \(latest.version)..."
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
// Download the update
|
|
770
|
+
do {
|
|
771
|
+
let bundle: BundleInfo
|
|
772
|
+
if let manifest = latest.manifest, !manifest.isEmpty {
|
|
773
|
+
bundle = try updater.downloadManifest(
|
|
774
|
+
manifest: manifest,
|
|
775
|
+
version: latest.version,
|
|
776
|
+
sessionKey: latest.sessionKey ?? ""
|
|
777
|
+
)
|
|
778
|
+
} else {
|
|
779
|
+
// Safe unwrap URL
|
|
780
|
+
guard let downloadUrl = URL(string: latest.url) else {
|
|
781
|
+
DispatchQueue.main.async {
|
|
782
|
+
progressAlert.dismiss(animated: true) {
|
|
783
|
+
self.showError(message: "Failed to download update: invalid update URL.", plugin: plugin)
|
|
784
|
+
}
|
|
785
|
+
}
|
|
786
|
+
return
|
|
787
|
+
}
|
|
788
|
+
// Same gate as download() and auto-update: the checksum is required and
|
|
789
|
+
// verified (decrypted when a public key is set) before extraction.
|
|
790
|
+
bundle = try updater.downloadVerified(
|
|
791
|
+
url: downloadUrl,
|
|
792
|
+
version: latest.version,
|
|
793
|
+
sessionKey: latest.sessionKey ?? "",
|
|
794
|
+
expectedChecksum: latest.checksum
|
|
795
|
+
)
|
|
796
|
+
}
|
|
797
|
+
|
|
798
|
+
// Set as next bundle
|
|
799
|
+
_ = updater.setNextBundle(next: bundle.getId())
|
|
800
|
+
|
|
801
|
+
DispatchQueue.main.async {
|
|
802
|
+
progressAlert.dismiss(animated: true) {
|
|
803
|
+
self.showSuccessWithReload(
|
|
804
|
+
message: "Update downloaded! Reload to apply version \(latest.version)?",
|
|
805
|
+
plugin: plugin,
|
|
806
|
+
bridge: bridge,
|
|
807
|
+
onReload: {
|
|
808
|
+
_ = updater.set(bundle: bundle)
|
|
809
|
+
_ = plugin.reloadCurrentBundle()
|
|
810
|
+
}
|
|
811
|
+
)
|
|
812
|
+
}
|
|
813
|
+
}
|
|
814
|
+
} catch {
|
|
815
|
+
DispatchQueue.main.async {
|
|
816
|
+
progressAlert.dismiss(animated: true) {
|
|
817
|
+
self.showError(message: "Failed to download update: \(error.localizedDescription)", plugin: plugin)
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
}
|
|
822
|
+
}
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
|
|
827
|
+
private func showError(message: String, plugin: CapacitorUpdaterPlugin) {
|
|
828
|
+
plugin.logger.error(message)
|
|
829
|
+
let alert = UIAlertController(title: "Error", message: message, preferredStyle: .alert)
|
|
830
|
+
alert.addAction(UIAlertAction(title: "OK", style: .default))
|
|
831
|
+
|
|
832
|
+
DispatchQueue.main.async {
|
|
833
|
+
if let topVC = self.menuHostViewController {
|
|
834
|
+
topVC.present(alert, animated: true)
|
|
835
|
+
}
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
private func showSuccess(message: String, plugin: CapacitorUpdaterPlugin) {
|
|
840
|
+
plugin.logger.info(message)
|
|
841
|
+
let alert = UIAlertController(title: "Success", message: message, preferredStyle: .alert)
|
|
842
|
+
alert.addAction(UIAlertAction(title: "OK", style: .default))
|
|
843
|
+
|
|
844
|
+
DispatchQueue.main.async {
|
|
845
|
+
if let topVC = self.menuHostViewController {
|
|
846
|
+
topVC.present(alert, animated: true)
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
private func showSuccessWithReload(
|
|
852
|
+
message: String,
|
|
853
|
+
plugin: CapacitorUpdaterPlugin,
|
|
854
|
+
bridge: CAPBridgeProtocol,
|
|
855
|
+
onReload: (() -> Void)? = nil
|
|
856
|
+
) {
|
|
857
|
+
plugin.logger.info(message)
|
|
858
|
+
let alert = UIAlertController(title: "Update Ready", message: message, preferredStyle: .alert)
|
|
859
|
+
alert.addAction(UIAlertAction(title: "Later", style: .cancel))
|
|
860
|
+
alert.addAction(UIAlertAction(title: "Reload Now", style: .default) { _ in
|
|
861
|
+
if let onReload = onReload {
|
|
862
|
+
onReload()
|
|
863
|
+
} else {
|
|
864
|
+
DispatchQueue.main.async {
|
|
865
|
+
bridge.webView?.reload()
|
|
866
|
+
}
|
|
867
|
+
}
|
|
868
|
+
})
|
|
869
|
+
|
|
870
|
+
DispatchQueue.main.async {
|
|
871
|
+
if let topVC = self.menuHostViewController {
|
|
872
|
+
topVC.present(alert, animated: true)
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
}
|