Skip to content

Commit

Permalink
fix: save sync
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Jul 10, 2022
1 parent 95a8979 commit c1d088e
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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!");
}
Expand Down
47 changes: 26 additions & 21 deletions ios/Plugin/CapacitorUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -398,8 +401,7 @@ extension CustomError: LocalizedError {
public func reset(isInternal: Bool) {
self.setCurrentBundle(bundle: "")
self.setFallbackVersion(fallback: Optional<BundleInfo>.none)
let _ = self.setNextVersion(next: Optional<String>.none)
UserDefaults.standard.synchronize()
let _ = self.setNext(next: Optional<String>.none)
if(!isInternal) {
sendStats(action: "reset", versionName: self.getCurrentBundle().getVersionName())
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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? {
Expand All @@ -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
}
}
46 changes: 24 additions & 22 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -250,51 +250,53 @@ 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()
}

private func _checkCancelDelay(killed: Bool) -> Void {
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()
}
}

Expand Down Expand Up @@ -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
}
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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<String>.none)
let _ = self.implementation.setNext(next: Optional<String>.none)
} else {
print("\(self.implementation.TAG) Updated to bundle: \(next!) Failed!")
}
Expand Down

0 comments on commit c1d088e

Please sign in to comment.