Skip to content

Commit

Permalink
feat: add getBuiltinVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Aug 16, 2023
1 parent 334553e commit 8f5feba
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,18 @@ public void notifyDownload(final String id, final int percent) {
}
}

@PluginMethod
public void getBuiltinVersion(final PluginCall call) {
try {
final JSObject ret = new JSObject();
ret.put("version", this.implementation.versionBuild);
call.resolve(ret);
} catch (final Exception e) {
Log.e(CapacitorUpdater.TAG, "Could not get version", e);
call.reject("Could not get version", e);
}
}

@PluginMethod
public void getDeviceId(final PluginCall call) {
try {
Expand Down Expand Up @@ -910,8 +922,14 @@ public void run() {
"Latest bundle already exists and download is NOT required. Update will occur next time app moves to background."
);
if(CapacitorUpdaterPlugin.this.directUpdate) {
CapacitorUpdaterPlugin.this.implementation.set(next)
this._reload()
CapacitorUpdaterPlugin.this.implementation.set(latest);
CapacitorUpdaterPlugin.this._reload();
final JSObject ret = new JSObject();
ret.put("bundle", latest.toJSON());
CapacitorUpdaterPlugin.this.notifyListeners(
"appReady",
ret
);
} else {
final JSObject ret = new JSObject();
ret.put("bundle", latest.toJSON());
Expand Down
4 changes: 2 additions & 2 deletions ios/Plugin/CapacitorUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ extension CustomError: LocalizedError {

public let TAG: String = "✨ Capacitor-updater:"
public let CAP_SERVER_PATH: String = "serverBasePath"
public var versionName: String = ""
public var versionBuild: String = ""
public var customId: String = ""
public var PLUGIN_VERSION: String = ""
public let timeout: Double = 20
Expand Down Expand Up @@ -395,7 +395,7 @@ extension CustomError: LocalizedError {
device_id: self.deviceID,
app_id: self.appId,
custom_id: self.customId,
version_build: self.versionName,
version_build: self.versionBuild,
version_code: self.versionCode,
version_os: self.versionOs,
version_name: self.getCurrentBundle().getVersionName(),
Expand Down
27 changes: 19 additions & 8 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
print("\(self.implementation.TAG) Cannot get version native \(currentVersionNative)")
}
print("\(self.implementation.TAG) version native \(self.currentVersionNative.description)")
implementation.versionName = getConfig().getString("version", Bundle.main.versionName)!
implementation.versionBuild = getConfig().getString("version", Bundle.main.versionName)!
autoDeleteFailed = getConfig().getBoolean("autoDeleteFailed", true)
autoDeletePrevious = getConfig().getBoolean("autoDeletePrevious", true)
directUpdate = getConfig().getBoolean("directUpdate", false)
Expand Down Expand Up @@ -104,6 +104,10 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
}
}

@objc func getBuiltinVersion(_ call: CAPPluginCall) {
call.resolve(["version": implementation.versionBuild])
}

@objc func getDeviceId(_ call: CAPPluginCall) {
call.resolve(["deviceId": implementation.deviceID])
}
Expand Down Expand Up @@ -527,8 +531,8 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
do {
print("\(self.implementation.TAG) New bundle: \(latestVersionName) found. Current is: \(current.getVersionName()). Update will occur next time app moves to background.")
let next = self.implementation.getBundleInfoByVersionName(version: latestVersionName)
if (next == nil || next.isDeleted()) {
if (next.isDeleted()) {
if (next == nil || ((next?.isDeleted()) != nil)) {
if ((next?.isDeleted()) != nil) {
print("\(self.implementation.TAG) Latest bundle already exists and will be deleted, download will overwrite it.")
let res = self.implementation.delete(id: next!.getId(), removeInfo: true)
if !res {
Expand All @@ -539,7 +543,14 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
}
let next = try self.implementation.download(url: downloadUrl, version: latestVersionName, sessionKey: sessionKey)
}
if next.isErrorStatus() {
guard let next = next else {
print("\(self.implementation.TAG) Error downloading file")
self.notifyListeners("downloadFailed", data: ["version": latestVersionName])
self.notifyListeners("noNeedUpdate", data: ["bundle": current.toJSON()])
self.endBackGroundTask()
return
}
if (next.isErrorStatus()) {
print("\(self.implementation.TAG) Latest version is in error state. Aborting update.")
self.notifyListeners("noNeedUpdate", data: ["bundle": current.toJSON()])
self.endBackGroundTask()
Expand All @@ -556,11 +567,11 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
throw ObjectSavableError.checksum
}
if (self.directUpdate) {
self.implementation.set(bundle: next!)
self.implementation.set(bundle: next)
self._reload()
} else {
self.notifyListeners("updateAvailable", data: ["bundle": next!.toJSON()])
_ = self.implementation.setNextBundle(next: next!.getId())
self.notifyListeners("updateAvailable", data: ["bundle": next.toJSON()])
_ = self.implementation.setNextBundle(next: next.getId())
}
} catch {
print("\(self.implementation.TAG) Error downloading file", error.localizedDescription)
Expand All @@ -573,7 +584,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
print("\(self.implementation.TAG) No need to update, \(current.getId()) is the latest bundle.")
self.notifyListeners("noNeedUpdate", data: ["bundle": current.toJSON()])
}
self.notifyListeners("appReady", data: ["bundle": latest!.toJSON()])
self.notifyListeners("appReady", data: ["bundle": current.toJSON()])
self.endBackGroundTask()
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,14 @@ export interface CapacitorUpdaterPlugin {
listenerFunc: AppReadyListener
): Promise<PluginListenerHandle> & PluginListenerHandle;

/**
* Get the native app version or the builtin version if set in config
*
* @returns {Promise<{ version: string }>} an Promise with version for this device
* @since 5.2.0
*/
getBuiltinVersion(): Promise<{ version: string }>;

/**
* Get unique ID used to identify device (sent to auto update server)
*
Expand Down
4 changes: 4 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export class CapacitorUpdaterWeb
console.warn("Cannot get ID in web");
return { deviceId: "default" };
}
async getBuiltinVersion(): Promise<{ version: string }> {
console.warn("Cannot get version in web");
return { version: "default" };
}
async getPluginVersion(): Promise<{ version: string }> {
console.warn("Cannot get plugin version in web");
return { version: "default" };
Expand Down

0 comments on commit 8f5feba

Please sign in to comment.