Skip to content

Commit

Permalink
fix: async get version issue
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Feb 10, 2022
1 parent 9713835 commit 0155e64
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ public void load() {
if (this.autoUpdateUrl == null || this.autoUpdateUrl.equals("")) return;
Application application = (Application) this.getContext().getApplicationContext();
application.registerActivityLifecycleCallbacks(this);
new Thread(new Runnable(){
@Override
public void run() {
onActivityStarted(getActivity());
}
}).start();
onActivityStarted(getActivity());
}

@PluginMethod
Expand Down Expand Up @@ -159,34 +154,38 @@ public void notifyAppReady(PluginCall call) {
public void onActivityStarted(@NonNull Activity activity) {
Log.i(TAG, "Check for update in the server");
if (autoUpdateUrl == null || autoUpdateUrl.equals("")) return;
implementation.getLatest(autoUpdateUrl, (res) -> {
try {
String currentVersion = implementation.getVersionName();
String newVersion = (String) res.get("version");
String failingVersion = prefs.getString("failingVersion", "");
Log.i(TAG, "currentVersion " + currentVersion + ", newVersion " + newVersion + ", failingVersion " + failingVersion + ".");
if (!newVersion.equals(currentVersion) && !newVersion.equals(failingVersion)) {
new Thread(new Runnable(){
@Override
public void run() {
// Do network action in this function
try {
String dl = implementation.download((String) res.get("url"));
Log.i(TAG, "New version: " + newVersion + " found. Current is " + (currentVersion == "" ? "builtin" : currentVersion) + ", next backgrounding will trigger update.");
editor.putString("nextVersion", dl);
editor.putString("nextVersionName", (String) res.get("version"));
editor.commit();
} catch (JSONException e) {
e.printStackTrace();
}
new Thread(new Runnable(){
@Override
public void run() {
implementation.getLatest(autoUpdateUrl, (res) -> {
try {
String currentVersion = implementation.getVersionName();
String newVersion = (String) res.get("version");
String failingVersion = prefs.getString("failingVersion", "");
Log.i(TAG, "currentVersion " + currentVersion + ", newVersion " + newVersion + ", failingVersion " + failingVersion + ".");
if (!newVersion.equals(currentVersion) && !newVersion.equals(failingVersion)) {
new Thread(new Runnable(){
@Override
public void run() {
// Do network action in this function
try {
String dl = implementation.download((String) res.get("url"));
Log.i(TAG, "New version: " + newVersion + " found. Current is " + (currentVersion == "" ? "builtin" : currentVersion) + ", next backgrounding will trigger update.");
editor.putString("nextVersion", dl);
editor.putString("nextVersionName", (String) res.get("version"));
editor.commit();
} catch (JSONException e) {
e.printStackTrace();
}
}
}).start();
}
}).start();
}
} catch (JSONException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
});
}
});

}).start();
}

@Override
Expand Down
44 changes: 22 additions & 22 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
let nc = NotificationCenter.default
nc.addObserver(self, selector: #selector(appMovedToBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
nc.addObserver(self, selector: #selector(appMovedToForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
DispatchQueue.main.async {
self.appMovedToForeground() // check for update on startup
}
self.appMovedToForeground()
}
}

Expand Down Expand Up @@ -83,25 +81,27 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
}

@objc func appMovedToForeground() {
print("✨ Capacitor-updater: Check for update in the server")
let url = URL(string: autoUpdateUrl)!
let res = implementation.getLatest(url: url)
if (res == nil) {
return
}
guard let downloadUrl = URL(string: res?.url ?? "") else {
return
}
let currentVersion = implementation.getVersionName()
let failingVersion = UserDefaults.standard.string(forKey: "failingVersion") ?? ""
let newVersion = res?.version ?? ""
if (newVersion != "" && newVersion != currentVersion && newVersion != failingVersion) {
let dl = implementation.download(url: downloadUrl)
print("✨ Capacitor-updater: New version: " + newVersion + " found. Current is " + (currentVersion == "" ? "builtin" : currentVersion) + ", next backgrounding will trigger update.")
UserDefaults.standard.set(dl, forKey: "nextVersion")
UserDefaults.standard.set(newVersion, forKey: "nextVersionName")
} else {
print("✨ Capacitor-updater: No need to update, " + currentVersion + " is the latest")
DispatchQueue.main.async {
print("✨ Capacitor-updater: Check for update in the server")
let url = URL(string: autoUpdateUrl)!
let res = implementation.getLatest(url: url)
if (res == nil) {
return
}
guard let downloadUrl = URL(string: res?.url ?? "") else {
return
}
let currentVersion = implementation.getVersionName()
let failingVersion = UserDefaults.standard.string(forKey: "failingVersion") ?? ""
let newVersion = res?.version ?? ""
if (newVersion != "" && newVersion != currentVersion && newVersion != failingVersion) {
let dl = implementation.download(url: downloadUrl)
print("✨ Capacitor-updater: New version: " + newVersion + " found. Current is " + (currentVersion == "" ? "builtin" : currentVersion) + ", next backgrounding will trigger update.")
UserDefaults.standard.set(dl, forKey: "nextVersion")
UserDefaults.standard.set(newVersion, forKey: "nextVersionName")
} else {
print("✨ Capacitor-updater: No need to update, " + currentVersion + " is the latest")
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "capacitor-updater",
"version": "1.1.4",
"version": "1.1.5",
"license": "AGPL-3.0-only",
"description": "Download app update from url",
"main": "dist/plugin.cjs.js",
Expand Down

0 comments on commit 0155e64

Please sign in to comment.