Skip to content

Commit

Permalink
feat: add current method
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Nov 25, 2021
1 parent 78ae091 commit 64f3a24
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 12 deletions.
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,22 @@ npx cap sync
import { SplashScreen } from '@capacitor/splash-screen'
import { App } from '@capacitor/app'
// Do the update when user leave app
let version = ""
App.addListener('appStateChange', async(state) => {
if (!state.isActive) {
SplashScreen.show()
const version = await CapacitorUpdater.download({
if (state.isActive) {
// Do the download during user active app time to prevent failed download
version = await CapacitorUpdater.download({
url: 'https://github.com/Forgr-ee/Mimesis/releases/download/0.0.1/dist.zip',
})
await CapacitorUpdater.set(version)
SplashScreen.hide() // in case the set fail, otherwise the new app will have to hide it
}
if (!state.isActive && version !== "") {
// Do the switch when user leave app
SplashScreen.show()
try {
await CapacitorUpdater.set(version)
} catch () {
SplashScreen.hide() // in case the set fail, otherwise the new app will have to hide it
}
}
})
Expand All @@ -52,6 +59,7 @@ npx cap sync
* [`delete(...)`](#delete)
* [`list()`](#list)
* [`reset()`](#reset)
* [`current()`](#current)

</docgen-index>

Expand Down Expand Up @@ -128,6 +136,19 @@ set the original version (the one sent to Apple store / Google play store ) as c

--------------------


### current()

```typescript
current() => Promise<{ current: string; }>
```

get the curent version, if none are set it return 'default'

**Returns:** <code>Promise&lt;{ current: string; }&gt;</code>

--------------------

</docgen-api>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,13 @@ public void reset(PluginCall call) {
implementation.reset();
call.resolve();
}

@PluginMethod
public void current(PluginCall call) {
String pathHot = implementation.getLastPathHot();
JSObject ret = new JSObject();
String current = pathHot.length() >= 10 ? pathHot.substring(pathHot.length() - 10)) : 'default';
ret.put("current", current);
call.resolve(ret);
}
}
18 changes: 13 additions & 5 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,18 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
if let vc = bridge.viewController as? CAPBridgeViewController {
let pathHot = implementation.getLastPathHot()
let pathPersist = implementation.getLastPathPersist()
if (pathHot != "") {
if (pathHot != "" && pathPersist != "") {
vc.setServerBasePath(path: pathHot)
}
if (pathPersist != "") {
let defaults = UserDefaults.standard
defaults.set(String(pathPersist.suffix(10)), forKey: "serverBasePath")
return call.resolve()
} else {
return call.reject("cannot set " + version)
}
return call.resolve()
}
call.reject("Update failed, viewController missing")
} else {
call.reject("Update failed, version don't exist")
call.reject("Update failed, version " + version + " don't exist")
}
}

Expand All @@ -65,6 +65,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
"versions": res
])
}

@objc func reset(_ call: CAPPluginCall) {
guard let bridge = self.bridge else { return call.reject("bridge missing") }

Expand All @@ -78,4 +79,11 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
call.reject("Update failed, viewController missing")
}

@objc func current(_ call: CAPPluginCall) {
let pathHot = implementation.getLastPathHot()
let current = pathHot.length >= 10 ? pathHot.suffix(10) : "default"
call.resolve([
"current": current
])
}
}
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.0.11",
"version": "1.0.12",
"license": "AGPL-3.0-only",
"description": "Download app update from url",
"main": "dist/plugin.cjs.js",
Expand Down
5 changes: 5 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,9 @@ export interface CapacitorUpdaterPlugin {
* @returns {Promise<void>} an empty Promise
*/
reset(): Promise<void>;
/**
* get the curent version, if none are set it return 'default'
* @returns {Promise<{ current: string }>} an Promise with the current version name
*/
current(): Promise<{ current: string }>;
}
4 changes: 4 additions & 0 deletions src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ export class CapacitorUpdaterWeb
async reset(): Promise<void> {
console.log('Cannot reset version in web');
}
async current(): Promise<{ current: string }> {
console.log('Cannot get current version in web');
return { current: 'default'};
}
}

0 comments on commit 64f3a24

Please sign in to comment.