Skip to content

Commit

Permalink
feat: add updateAvailable event
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Apr 6, 2022
1 parent b733873 commit 75af960
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 6 deletions.
39 changes: 36 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ Do not password encrypt this file, or it will fail to unpack.
* [`cancelDelay()`](#canceldelay)
* [`addListener('download', ...)`](#addlistenerdownload)
* [`addListener('majorAvailable', ...)`](#addlistenermajoravailable)
* [`addListener('updateAvailable', ...)`](#addlistenerupdateavailable)
* [`addListener(string, ...)`](#addlistenerstring)
* [`removeAllListeners()`](#removealllisteners)
* [Interfaces](#interfaces)
Expand Down Expand Up @@ -337,6 +338,26 @@ Listen for Major update event in the App, let you know when major update is bloc
--------------------


### addListener('updateAvailable', ...)

```typescript
addListener(eventName: 'updateAvailable', listenerFunc: UpdateAvailableListener) => Promise<PluginListenerHandle> & PluginListenerHandle
```

Listen for update event in the App, let you know when update is ready to install at next app start

| Param | Type |
| ------------------ | --------------------------------------------------------------------------- |
| **`eventName`** | <code>'updateAvailable'</code> |
| **`listenerFunc`** | <code><a href="#updateavailablelistener">UpdateAvailableListener</a></code> |

**Returns:** <code>Promise&lt;<a href="#pluginlistenerhandle">PluginListenerHandle</a>&gt; & <a href="#pluginlistenerhandle">PluginListenerHandle</a></code>

**Since:** 2.3.0

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


### addListener(string, ...)

```typescript
Expand Down Expand Up @@ -381,9 +402,16 @@ removeAllListeners() => Promise<void>

#### MajorAvailableEvent

| Prop | Type | Description | Since |
| ------------- | ------------------- | ---------------------------------------------- | ----- |
| **`version`** | <code>string</code> | Current status of download, between 0 and 100. | 2.3.0 |
| Prop | Type | Description | Since |
| ------------- | ------------------- | ------------------------------------------- | ----- |
| **`version`** | <code>string</code> | Emit when a new major version is available. | 2.3.0 |


#### UpdateAvailableEvent

| Prop | Type | Description | Since |
| ------------- | ------------------- | ------------------------------------ | ----- |
| **`version`** | <code>string</code> | Emit when a new update is available. | 3.0.0 |


### Type Aliases
Expand All @@ -398,6 +426,11 @@ removeAllListeners() => Promise<void>

<code>(state: <a href="#majoravailableevent">MajorAvailableEvent</a>): void</code>


#### UpdateAvailableListener

<code>(state: <a href="#updateavailableevent">UpdateAvailableEvent</a>): void</code>

</docgen-api>

### Listen to download events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,14 @@ public void run() {
try {
String currentVersion = implementation.getVersionName();
String newVersion = (String) res.get("version");
JSObject ret = new JSObject();
ret.put("newVersion", newVersion);
String failingVersion = prefs.getString("failingVersion", "");
if (disableAutoUpdateUnderNative && new Version(newVersion).isHigherThan(finalCurrentVersionNative)) {
Log.i(TAG, "Cannot download revert, " + newVersion + " is lest than native version " + finalCurrentVersionNative);
}
else if (disableAutoUpdateToMajor && new Version(newVersion).getMajor() > new Version(currentVersion).getMajor()) {
Log.i(TAG, "Cannot download Major, " + newVersion + " is Breaking change from " + currentVersion);
JSObject ret = new JSObject();
ret.put("newVersion", newVersion);
notifyListeners("majorAvailable", ret);
}
else if (!newVersion.equals("") && !newVersion.equals(currentVersion) && !newVersion.equals(failingVersion)) {
Expand All @@ -266,6 +266,7 @@ public void run() {
editor.putString("nextVersion", dl);
editor.putString("nextVersionName", (String) res.get("version"));
editor.commit();
notifyListeners("updateAvailable", ret);
} catch (JSONException e) {
e.printStackTrace();
}
Expand Down
1 change: 1 addition & 0 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
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.description, forKey: "nextVersionName")
self.notifyListeners("updateAvailable", data: ["version": newVersion])
} else {
print("✨ Capacitor-updater: Download version \(newVersion) fail")
}
Expand Down
20 changes: 19 additions & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,24 @@ export interface DownloadEvent {
}
export interface MajorAvailableEvent {
/**
* Current status of download, between 0 and 100.
* Emit when a new major version is available.
*
* @since 2.3.0
*/
version: string;
}
export interface UpdateAvailableEvent {
/**
* Emit when a new update is available.
*
* @since 3.0.0
*/
version: string;
}

export type DownloadChangeListener = (state: DownloadEvent) => void;
export type MajorAvailableListener = (state: MajorAvailableEvent) => void;
export type UpdateAvailableListener = (state: UpdateAvailableEvent) => void;
export interface CapacitorUpdaterPlugin {
/**
* Download a new version from the provided URL, it should be a zip file, with files inside or with a unique folder inside with all your files
Expand Down Expand Up @@ -104,4 +113,13 @@ export interface CapacitorUpdaterPlugin {
eventName: 'majorAvailable',
listenerFunc: MajorAvailableListener,
): Promise<PluginListenerHandle> & PluginListenerHandle;
/**
* Listen for update event in the App, let you know when update is ready to install at next app start
*
* @since 2.3.0
*/
addListener(
eventName: 'updateAvailable',
listenerFunc: UpdateAvailableListener,
): Promise<PluginListenerHandle> & PluginListenerHandle;
}

0 comments on commit 75af960

Please sign in to comment.