Skip to content

Commit

Permalink
feat: add DownloadComplete event and remove updateAvailable
Browse files Browse the repository at this point in the history
  • Loading branch information
riderx committed Jun 30, 2022
1 parent f763cb5 commit 1cbc934
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 46 deletions.
54 changes: 27 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ Capacitor Updator works by unzipping a compiled app bundle to the native device
* [`reload()`](#reload)
* [`setDelay(...)`](#setdelay)
* [`addListener('download', ...)`](#addlistenerdownload)
* [`addListener('downloadComplete', ...)`](#addlistenerdownloadcomplete)
* [`addListener('majorAvailable', ...)`](#addlistenermajoravailable)
* [`addListener('updateAvailable', ...)`](#addlistenerupdateavailable)
* [`addListener('updateFailed', ...)`](#addlistenerupdatefailed)
* [`getId()`](#getid)
* [`getPluginVersion()`](#getpluginversion)
Expand Down Expand Up @@ -308,38 +308,38 @@ Listen for download event in the App, let you know when the download is started,
--------------------


### addListener('majorAvailable', ...)
### addListener('downloadComplete', ...)

```typescript
addListener(eventName: 'majorAvailable', listenerFunc: MajorAvailableListener) => Promise<PluginListenerHandle> & PluginListenerHandle
addListener(eventName: 'downloadComplete', listenerFunc: DownloadCompleteListener) => Promise<PluginListenerHandle> & PluginListenerHandle
```

Listen for Major update event in the App, let you know when major update is blocked by setting disableAutoUpdateBreaking
Listen for download event in the App, let you know when the download is started, loading and finished

| Param | Type |
| ------------------ | ------------------------------------------------------------------------- |
| **`eventName`** | <code>'majorAvailable'</code> |
| **`listenerFunc`** | <code><a href="#majoravailablelistener">MajorAvailableListener</a></code> |
| Param | Type |
| ------------------ | ----------------------------------------------------------------------------- |
| **`eventName`** | <code>'downloadComplete'</code> |
| **`listenerFunc`** | <code><a href="#downloadcompletelistener">DownloadCompleteListener</a></code> |

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

**Since:** 2.3.0
**Since:** 4.0.0

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


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

```typescript
addListener(eventName: 'updateAvailable', listenerFunc: UpdateAvailableListener) => Promise<PluginListenerHandle> & PluginListenerHandle
addListener(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
Listen for Major update event in the App, let you know when major update is blocked by setting disableAutoUpdateBreaking

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

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

Expand Down Expand Up @@ -460,20 +460,20 @@ removeAllListeners() => Promise<void>
| **`bundle`** | <code><a href="#bundleinfo">BundleInfo</a></code> | | |


#### DownloadCompleteEvent

| Prop | Type | Description | Since |
| ------------ | ------------------------------------------------- | ------------------------------------ | ----- |
| **`bundle`** | <code><a href="#bundleinfo">BundleInfo</a></code> | Emit when a new update is available. | 4.0.0 |


#### MajorAvailableEvent

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


#### UpdateAvailableEvent

| Prop | Type | Description | Since |
| ------------ | ------------------------------------------------- | ------------------------------------ | ----- |
| **`bundle`** | <code><a href="#bundleinfo">BundleInfo</a></code> | Emit when a new update is available. | 4.0.0 |


#### UpdateFailedEvent

| Prop | Type | Description | Since |
Expand All @@ -494,14 +494,14 @@ removeAllListeners() => Promise<void>
<code>(state: <a href="#downloadevent">DownloadEvent</a>): void</code>


#### MajorAvailableListener
#### DownloadCompleteListener

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


#### UpdateAvailableListener
#### MajorAvailableListener

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


#### UpdateFailedListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,12 @@ public void notifyDownload(final String id, final int percent) {
try {
final JSObject ret = new JSObject();
ret.put("percent", percent);
ret.put("bundle", this.implementation.getBundleInfo(id).toJSON());
var bundle = this.implementation.getBundleInfo(id).toJSON();
ret.put("bundle", bundle);
this.notifyListeners("download", ret);
if (percent == 100) {
this.notifyListeners("downloadComplete", bundle);

This comment has been minimized.

Copy link
@lincolnthree

lincolnthree Jun 30, 2022

Contributor

Good idea.

}
} catch (final Exception e) {
Log.e(CapacitorUpdater.TAG, "Could not notify listeners", e);
}
Expand Down Expand Up @@ -454,10 +458,6 @@ public void run() {
final BundleInfo next = CapacitorUpdaterPlugin.this.implementation.download(url, latestVersionName);

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

final JSObject updateAvailable = new JSObject();
updateAvailable.put("bundle", next.toJSON());
CapacitorUpdaterPlugin.this.notifyListeners("updateAvailable", updateAvailable);
} catch (final Exception e) {
Log.e(CapacitorUpdater.TAG, "error downloading file", e);
}
Expand Down
7 changes: 3 additions & 4 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
@objc func notifyDownload(id: String, percent: Int) {
let bundle = self.implementation.getBundleInfo(id: id)
self.notifyListeners("download", data: ["percent": percent, "bundle": bundle.toJSON()])
if (percent == 100) {
self.notifyListeners("downloadComplete", data: ["bundle": bundle.toJSON()])
}
}

@objc func getId(_ call: CAPPluginCall) {
Expand Down Expand Up @@ -319,10 +322,6 @@ public class CapacitorUpdaterPlugin: CAPPlugin {
let next = try self.implementation.download(url: downloadUrl, version: latestVersionName!)

let _ = self.implementation.setNextVersion(next: next.getId())

self.notifyListeners("updateAvailable", data: [
"bundle": next.getVersionName()
])
} catch {
print("\(self.implementation.TAG) Error downloading file", error.localizedDescription)
}
Expand Down
20 changes: 10 additions & 10 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export interface MajorAvailableEvent {
*/
version: string;
}
export interface UpdateAvailableEvent {
export interface DownloadCompleteEvent {
/**
* Emit when a new update is available.
*
Expand Down Expand Up @@ -126,8 +126,8 @@ export interface BundleInfo {
export type BundleStatus = 'success' | 'error' | 'pending' | 'downloading';

export type DownloadChangeListener = (state: DownloadEvent) => void;
export type DownloadCompleteListener = (state: DownloadCompleteEvent) => void;
export type MajorAvailableListener = (state: MajorAvailableEvent) => void;
export type UpdateAvailableListener = (state: UpdateAvailableEvent) => void;
export type UpdateFailedListener = (state: UpdateFailedEvent) => void;


Expand Down Expand Up @@ -232,23 +232,23 @@ export interface CapacitorUpdaterPlugin {
): Promise<PluginListenerHandle> & PluginListenerHandle;

/**
* Listen for Major update event in the App, let you know when major update is blocked by setting disableAutoUpdateBreaking
* Listen for download event in the App, let you know when the download is started, loading and finished
*
* @since 2.3.0
* @since 4.0.0
*/
addListener(
eventName: 'majorAvailable',
listenerFunc: MajorAvailableListener,
eventName: 'downloadComplete',
listenerFunc: DownloadCompleteListener,
): Promise<PluginListenerHandle> & PluginListenerHandle;

/**
* Listen for update event in the App, let you know when update is ready to install at next app start
* Listen for Major update event in the App, let you know when major update is blocked by setting disableAutoUpdateBreaking
*
* @since 2.3.0
*/
addListener(
eventName: 'updateAvailable',
listenerFunc: UpdateAvailableListener,
eventName: 'majorAvailable',
listenerFunc: MajorAvailableListener,
): Promise<PluginListenerHandle> & PluginListenerHandle;

/**
Expand Down

0 comments on commit 1cbc934

Please sign in to comment.