Skip to content

Commit

Permalink
Merge pull request #507 from Cap-go/get_latest_channel
Browse files Browse the repository at this point in the history
Get latest channel
  • Loading branch information
riderx authored Jan 3, 2025
2 parents 372666b + fb4e27c commit 5e1cf16
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 10 deletions.
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ export default config;
* [`reload()`](#reload)
* [`setMultiDelay(...)`](#setmultidelay)
* [`cancelDelay()`](#canceldelay)
* [`getLatest()`](#getlatest)
* [`getLatest(...)`](#getlatest)
* [`setChannel(...)`](#setchannel)
* [`unsetChannel(...)`](#unsetchannel)
* [`getChannel()`](#getchannel)
Expand Down Expand Up @@ -572,14 +572,18 @@ Cancels a {@link <a href="#delaycondition">DelayCondition</a>} to process an upd
--------------------


### getLatest()
### getLatest(...)

```typescript
getLatest() => Promise<LatestVersion>
getLatest(options?: GetLatestOptions | undefined) => Promise<LatestVersion>
```

Get Latest bundle available from update Url

| Param | Type |
| ------------- | ------------------------------------------------------------- |
| **`options`** | <code><a href="#getlatestoptions">GetLatestOptions</a></code> |

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

**Since:** 4.0.0
Expand Down Expand Up @@ -1054,6 +1058,13 @@ Returns null if no next bundle is set.
| **`download_url`** | <code>string \| null</code> |


#### GetLatestOptions

| Prop | Type | Description | Default | Since |
| ------------- | ------------------- | ----------------------------------------------------------------------------------------------- | ---------------------- | ----- |
| **`channel`** | <code>string</code> | The channel to get the latest version for The channel must allow 'self_assign' for this to work | <code>undefined</code> | 6.8.0 |


#### ChannelRes

| Prop | Type | Description | Since |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1058,10 +1058,17 @@ public void onResponse(
);
}

public void getLatest(final String updateUrl, final Callback callback) {
public void getLatest(
final String updateUrl,
final String channel,
final Callback callback
) {
JSONObject json;
try {
json = this.createInfoObject();
if (channel != null && json != null) {
json.put("defaultChannel", channel);
}
} catch (JSONException e) {
Log.e(TAG, "Error getLatest JSONException", e);
final JSObject retError = new JSObject();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -860,9 +860,11 @@ public void list(final PluginCall call) {

@PluginMethod
public void getLatest(final PluginCall call) {
final String channel = call.getString("channel");
startNewThread(() ->
CapacitorUpdaterPlugin.this.implementation.getLatest(
CapacitorUpdaterPlugin.this.updateUrl,
channel,
res -> {
if (res.has("error")) {
call.reject(res.getString("error"));
Expand Down Expand Up @@ -965,6 +967,7 @@ public void run() {
try {
CapacitorUpdaterPlugin.this.implementation.getLatest(
CapacitorUpdaterPlugin.this.updateUrl,
null,
res -> {
if (res.has("error")) {
Log.e(
Expand Down Expand Up @@ -1257,6 +1260,7 @@ private Thread backgroundDownload() {
);
CapacitorUpdaterPlugin.this.implementation.getLatest(
CapacitorUpdaterPlugin.this.updateUrl,
null,
res -> {
final BundleInfo current =
CapacitorUpdaterPlugin.this.implementation.getCurrentBundle();
Expand Down
7 changes: 5 additions & 2 deletions ios/Plugin/CapacitorUpdater.swift
Original file line number Diff line number Diff line change
Expand Up @@ -579,10 +579,13 @@ extension CustomError: LocalizedError {
)
}

public func getLatest(url: URL) -> AppVersion {
public func getLatest(url: URL, channel: String?) -> AppVersion {
let semaphore: DispatchSemaphore = DispatchSemaphore(value: 0)
let latest: AppVersion = AppVersion()
let parameters: InfoObject = self.createInfoObject()
var parameters: InfoObject = self.createInfoObject()
if let channel = channel {
parameters.defaultChannel = channel
}
print("\(self.TAG) Auto-update parameters: \(parameters)")
let request = AF.request(url, method: .post, parameters: parameters, encoder: JSONParameterEncoder.default, requestModifier: { $0.timeoutInterval = self.timeout })

Expand Down
7 changes: 4 additions & 3 deletions ios/Plugin/CapacitorUpdaterPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -430,8 +430,9 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
}

@objc func getLatest(_ call: CAPPluginCall) {
let channel = call.getString("channel")
DispatchQueue.global(qos: .background).async {
let res = self.implementation.getLatest(url: URL(string: self.updateUrl)!)
let res = self.implementation.getLatest(url: URL(string: self.updateUrl)!, channel: channel)
if res.error != nil {
call.reject( res.error!)
} else if res.message != nil {
Expand Down Expand Up @@ -748,7 +749,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
self.endBackGroundTask()
}
print("\(self.implementation.TAG) Check for update via \(self.updateUrl)")
let res = self.implementation.getLatest(url: url)
let res = self.implementation.getLatest(url: url, channel: nil)
let current = self.implementation.getCurrentBundle()

if (res.message) != nil {
Expand Down Expand Up @@ -922,7 +923,7 @@ public class CapacitorUpdaterPlugin: CAPPlugin, CAPBridgedPlugin {
}
let timer = Timer.scheduledTimer(withTimeInterval: TimeInterval(periodCheckDelay), repeats: true) { _ in
DispatchQueue.global(qos: .background).async {
let res = self.implementation.getLatest(url: url)
let res = self.implementation.getLatest(url: url, channel: nil)
let current = self.implementation.getCurrentBundle()

if res.version != current.getVersionName() {
Expand Down
12 changes: 11 additions & 1 deletion src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ export interface CapacitorUpdaterPlugin {
* @throws {Error}
* @since 4.0.0
*/
getLatest(): Promise<LatestVersion>;
getLatest(options?: GetLatestOptions): Promise<LatestVersion>;

/**
* Sets the channel for this device. The channel has to allow for self assignment for this to work.
Expand Down Expand Up @@ -748,6 +748,16 @@ export interface DelayCondition {
value?: string;
}

export interface GetLatestOptions {
/**
* The channel to get the latest version for
* The channel must allow 'self_assign' for this to work
* @since 6.8.0
* @default undefined
*/
channel?: string;
}

export interface AppReadyResult {
bundle: BundleInfo;
}
Expand Down

0 comments on commit 5e1cf16

Please sign in to comment.