Skip to content

Commit

Permalink
fix: appMovedToForeground
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Jun 23, 2022
1 parent 712deea commit 9f054ed
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -454,17 +454,13 @@ public void run() {

CapacitorUpdaterPlugin.this.implementation.setNextVersion(next.getVersion());

this.notifyUpdateAvailable(next.getVersion());
final JSObject updateAvailable = new JSObject();
updateAvailable.put("version", next.getVersion());
CapacitorUpdaterPlugin.this.notifyListeners("updateAvailable", updateAvailable);
} catch (final Exception e) {
Log.e(CapacitorUpdater.TAG, "error downloading file", e);
}
}

private void notifyUpdateAvailable(final String version) {
final JSObject updateAvailable = new JSObject();
updateAvailable.put("version", version);
CapacitorUpdaterPlugin.this.notifyListeners("updateAvailable", updateAvailable);
}
}).start();
} else {
Log.i(CapacitorUpdater.TAG, "No need to update, " + current + " is the latest version.");
Expand Down
47 changes: 47 additions & 0 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,53 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
}

@objc func appMovedToForeground() {
if (self._isAutoUpdateEnabled()) {
DispatchQueue.global(qos: .background).async {
print("\(self.implementation.TAG) Check for update via \(self.autoUpdateUrl)")
let url = URL(string: self.autoUpdateUrl)!
let res = self.implementation.getLatest(url: url)
if (res == nil) {
return
}
guard let downloadUrl = URL(string: res?.url ?? "") else {
print("\(self.implementation.TAG) Error \(res?.message ?? "Unknow error")")
if (res?.major == true) {
self.notifyListeners("majorAvailable", data: ["version": res?.version ?? "0.0.0"])
}
return
}
let current = self.implementation.getCurrentBundle()
let latestVersionName = res?.version
if (latestVersionName != nil && latestVersionName != "" && current.getName() != latestVersionName) {
let latest = self.implementation.getVersionInfoByName(version: latestVersionName!)
if (latest != nil) {
if(latest!.isErrorStatus()) {
print("\(self.implementation.TAG) Latest version already exists, and is in error state. Aborting update.")
return;
}
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!.getVersion());
return;
}
}

do {
print("\(self.implementation.TAG) New version: \(latestVersionName!) found. Current is: \(current.getName()). Update will occur next time app moves to background.")
let next = try self.implementation.download(url: downloadUrl, versionName: latestVersionName!)

let _ = self.implementation.setNextVersion(next: next.getVersion());

self.notifyListeners("updateAvailable", data: [
"version": next.getVersion()
])
} catch {
print("\(self.implementation.TAG) Error downloading file", error.localizedDescription)
}
}
}

self.checkAppReady()
DispatchQueue.global(qos: .background).async {
print("\(self.implementation.TAG) Check for update in the server")
let url = URL(string: self.autoUpdateUrl)!
Expand Down

0 comments on commit 9f054ed

Please sign in to comment.