diff --git a/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java b/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java index f153d6590..06f9fb24f 100644 --- a/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java +++ b/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdater.java @@ -307,7 +307,7 @@ public void rollback(final BundleInfo bundle) { public void reset(final boolean internal) { this.setCurrentBundle(new File("public")); this.setFallbackVersion(null); - this.setNextVersion(null); + this.setNext(null); if(!internal) { this.sendStats("reset", this.getCurrentBundle().getVersionName()); } @@ -511,7 +511,7 @@ public BundleInfo getNextVersion() { } } - public boolean setNextVersion(final String next) { + public boolean setNext(final String next) { if (next == null) { this.editor.remove(NEXT_VERSION); } else { diff --git a/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java b/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java index 4fb854b2a..ee21bb0f9 100644 --- a/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java +++ b/android/src/main/java/ee/forgr/capacitor_updater/CapacitorUpdaterPlugin.java @@ -236,7 +236,7 @@ public void next(final PluginCall call) { try { Log.i(CapacitorUpdater.TAG, "Setting next active id " + id); - if (!this.implementation.setNextVersion(id)) { + if (!this.implementation.setNext(id)) { Log.e(CapacitorUpdater.TAG, "Set next id failed. Bundle " + id + " does not exist."); call.reject("Set next id failed. Bundle " + id + " does not exist."); } else { @@ -522,7 +522,7 @@ public void run() { } if(latest.isDownloaded()){ Log.e(CapacitorUpdater.TAG, "Latest bundle already exists and download is NOT required. Update will occur next time app moves to background."); - CapacitorUpdaterPlugin.this.implementation.setNextVersion(latest.getId()); + CapacitorUpdaterPlugin.this.implementation.setNext(latest.getId()); return; } } @@ -537,7 +537,7 @@ public void run() { final String url = (String) res.get("url"); final BundleInfo next = CapacitorUpdaterPlugin.this.implementation.download(url, latestVersionName); - CapacitorUpdaterPlugin.this.implementation.setNextVersion(next.getId()); + CapacitorUpdaterPlugin.this.implementation.setNext(next.getId()); } catch (final Exception e) { Log.e(CapacitorUpdater.TAG, "error downloading file", e); } @@ -582,7 +582,7 @@ public void onActivityStopped(@NonNull final Activity activity) { Log.d(CapacitorUpdater.TAG, "Next bundle is: " + next.getVersionName()); if (this.implementation.set(next) && this._reload()) { Log.i(CapacitorUpdater.TAG, "Updated to bundle: " + next.getVersionName()); - this.implementation.setNextVersion(null); + this.implementation.setNext(null); } else { Log.e(CapacitorUpdater.TAG, "Update to bundle: " + next.getVersionName() + " Failed!"); } diff --git a/ios/Plugin/CapacitorUpdater.swift b/ios/Plugin/CapacitorUpdater.swift index acd8f0d16..4f3b3a67d 100644 --- a/ios/Plugin/CapacitorUpdater.swift +++ b/ios/Plugin/CapacitorUpdater.swift @@ -259,8 +259,8 @@ extension CustomError: LocalizedError { private func setCurrentBundle(bundle: String) { UserDefaults.standard.set(bundle, forKey: self.CAP_SERVER_PATH) - print("\(self.TAG) Current bundle set to: \(bundle)") UserDefaults.standard.synchronize() + print("\(self.TAG) Current bundle set to: \(bundle == "" ? BundleInfo.ID_BUILTIN : bundle)") } public func download(url: URL, version: String) throws -> BundleInfo { @@ -372,9 +372,12 @@ extension CustomError: LocalizedError { public func set(id: String) -> Bool { let newBundle: BundleInfo = self.getBundleInfo(id: id) + if(newBundle.isBuiltin()) { + self.reset() + return true + } if (bundleExists(id: id)) { - let url: URL = self.getBundleDirectory(id: id) - self.setCurrentBundle(bundle: String(url.path.suffix(10))) + self.setCurrentBundle(bundle: id) self.setBundleStatus(id: id, status: BundleStatus.PENDING) sendStats(action: "set", versionName: newBundle.getVersionName()) return true @@ -398,8 +401,7 @@ extension CustomError: LocalizedError { public func reset(isInternal: Bool) { self.setCurrentBundle(bundle: "") self.setFallbackVersion(fallback: Optional.none) - let _ = self.setNextVersion(next: Optional.none) - UserDefaults.standard.synchronize() + let _ = self.setNext(next: Optional.none) if(!isInternal) { sendStats(action: "reset", versionName: self.getCurrentBundle().getVersionName()) } @@ -441,7 +443,7 @@ extension CustomError: LocalizedError { } do { let result: BundleInfo = try UserDefaults.standard.getObj(forKey: "\(id)\(self.INFO_SUFFIX)", castTo: BundleInfo.self) - print("\(self.TAG) Returning info bundle [\(id)]", result.toString()) +// print("\(self.TAG) Returning info bundle [\(id)]", result.toString()) return result } catch { print("\(self.TAG) Failed to parse info for bundle [\(id)]", error.localizedDescription) @@ -495,25 +497,22 @@ extension CustomError: LocalizedError { self.saveBundleInfo(id: id, bundle: info.setStatus(status: status.localizedString)) } - private func getCurrentBundleVersion() -> String { - if(self.isUsingBuiltin()) { - return BundleInfo.ID_BUILTIN - } else { - let path: String = self.getCurrentBundleId() - return path.lastPathComponent - } - } - public func getCurrentBundle() -> BundleInfo { return self.getBundleInfo(id: self.getCurrentBundleId()); } public func getCurrentBundleId() -> String { - return UserDefaults.standard.string(forKey: self.CAP_SERVER_PATH) ?? self.DEFAULT_FOLDER + guard let bundleID = UserDefaults.standard.string(forKey: self.CAP_SERVER_PATH) else { + return BundleInfo.ID_BUILTIN + } + if (bundleID == "") { + return BundleInfo.ID_BUILTIN + } + return bundleID } public func isUsingBuiltin() -> Bool { - return self.getCurrentBundleId() == self.DEFAULT_FOLDER + return (UserDefaults.standard.string(forKey: self.CAP_SERVER_PATH) ?? "") == self.DEFAULT_FOLDER } public func getFallbackVersion() -> BundleInfo { @@ -523,6 +522,7 @@ extension CustomError: LocalizedError { private func setFallbackVersion(fallback: BundleInfo?) { UserDefaults.standard.set(fallback == nil ? BundleInfo.ID_BUILTIN : fallback!.getId(), forKey: self.FALLBACK_VERSION) + UserDefaults.standard.synchronize() } public func getNextVersion() -> BundleInfo? { @@ -534,19 +534,24 @@ extension CustomError: LocalizedError { } } - public func setNextVersion(next: String?) -> Bool { + public func setNext(next: String?) -> Bool { guard let nextId = next else { UserDefaults.standard.removeObject(forKey: self.NEXT_VERSION) UserDefaults.standard.synchronize() - return + return false } - let bundle: URL = self.getBundleDirectory(id: next) + let newBundle: BundleInfo = self.getBundleInfo(id: nextId) + if(newBundle.isBuiltin()) { + self.reset() + return true + } + let bundle: URL = self.getBundleDirectory(id: nextId) if (!bundle.exist) { return false } UserDefaults.standard.set(next, forKey: self.NEXT_VERSION) - self.setBundleStatus(id: next!, status: BundleStatus.PENDING) UserDefaults.standard.synchronize() + self.setBundleStatus(id: next!, status: BundleStatus.PENDING) return true } } diff --git a/ios/Plugin/CapacitorUpdaterPlugin.swift b/ios/Plugin/CapacitorUpdaterPlugin.swift index 37857d011..3f321c4d0 100644 --- a/ios/Plugin/CapacitorUpdaterPlugin.swift +++ b/ios/Plugin/CapacitorUpdaterPlugin.swift @@ -140,7 +140,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin { return } print("\(self.implementation.TAG) Setting next active id \(id)") - if (!self.implementation.setNextVersion(next: id)) { + if (!self.implementation.setNext(next: id)) { print("\(self.implementation.TAG) Set next version failed. id \(id) does not exist.") call.reject("Set next version failed. id \(id) does not exist.") } else { @@ -250,19 +250,19 @@ public class CapacitorUpdaterPlugin: CAPPlugin { UserDefaults.standard.set(kind, forKey: DELAY_UPDATE) UserDefaults.standard.set(val, forKey: DELAY_UPDATE_VAL) UserDefaults.standard.synchronize() - print("\(self.implementation.TAG) Delay update saved.") + print("\(self.implementation.TAG) Delay update saved.", kind, val) call.resolve() } - private func _cancelDelay() -> Void { - print("\(self.implementation.TAG) delay Canceled") + private func _cancelDelay(source: String) -> Void { + print("\(self.implementation.TAG) delay Canceled from \(source)") UserDefaults.standard.removeObject(forKey: DELAY_UPDATE) UserDefaults.standard.removeObject(forKey: DELAY_UPDATE_VAL) UserDefaults.standard.synchronize() } @objc func cancelDelay(_ call: CAPPluginCall) { - self._cancelDelay() + self._cancelDelay(source: "JS") call.resolve() } @@ -270,31 +270,33 @@ public class CapacitorUpdaterPlugin: CAPPlugin { let delayUpdate = UserDefaults.standard.string(forKey: DELAY_UPDATE) if (delayUpdate != nil) { if (delayUpdate == "background" && !killed) { - self._cancelDelay() + self._cancelDelay(source: "background check") } else if (delayUpdate == "kill" && killed) { - self._cancelDelay() + self._cancelDelay(source: "kill check") } - let delayVal = UserDefaults.standard.string(forKey: DELAY_UPDATE_VAL) - if (delayVal == nil) { - self._cancelDelay() - } else if (delayUpdate == "date") { + guard let delayVal = UserDefaults.standard.string(forKey: DELAY_UPDATE_VAL) else { + self._cancelDelay(source: "delayVal absent") + return + } + if (delayUpdate == "date") { let dateFormatter = ISO8601DateFormatter() - let date = dateFormatter.date(from: delayVal!)! - let toDay = Date() - if (toDay < date) { - self._cancelDelay() + guard let ExpireDate = dateFormatter.date(from: delayVal) else { + self._cancelDelay(source: "date parsing issue") + return + } + if (ExpireDate < Date()) { + self._cancelDelay(source: "date expired") } } else if (delayUpdate == "nativeVersion") { do { - let versionLimit = try Version(delayVal!) + let versionLimit = try Version(delayVal) if (self.currentVersionNative >= versionLimit) { - self._cancelDelay() + self._cancelDelay(source: "nativeVersion above limit") } } catch { - self._cancelDelay() + self._cancelDelay(source: "nativeVersion cannot parse") } } - UserDefaults.standard.synchronize() } } @@ -370,7 +372,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin { } if(latest!.isDownloaded()){ print("\(self.implementation.TAG) Latest version already exists and download is NOT required. Update will occur next time app moves to background.") - let _ = self.implementation.setNextVersion(next: latest!.getId()) + let _ = self.implementation.setNext(next: latest!.getId()) return } } @@ -379,7 +381,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin { print("\(self.implementation.TAG) New bundle: \(latestVersionName!) found. Current is: \(current.getVersionName()). Update will occur next time app moves to background.") let next = try self.implementation.download(url: downloadUrl, version: latestVersionName!) - let _ = self.implementation.setNextVersion(next: next.getId()) + let _ = self.implementation.setNext(next: next.getId()) } catch { print("\(self.implementation.TAG) Error downloading file", error.localizedDescription) } @@ -412,7 +414,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin { print("\(self.implementation.TAG) Next bundle is: \(next!.toString())") if (self.implementation.set(bundle: next!) && self._reload()) { print("\(self.implementation.TAG) Updated to bundle: \(next!)") - let _ = self.implementation.setNextVersion(next: Optional.none) + let _ = self.implementation.setNext(next: Optional.none) } else { print("\(self.implementation.TAG) Updated to bundle: \(next!) Failed!") }