Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get latest channel #507

Merged
merged 3 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ export default config;
* [`reload()`](#reload)
* [`setMultiDelay(...)`](#setmultidelay)
* [`cancelDelay()`](#canceldelay)
* [`getLatest()`](#getlatest)
* [`getLatest(...)`](#getlatest)
* [`setChannel(...)`](#setchannel)
* [`unsetChannel(...)`](#unsetchannel)
* [`getChannel()`](#getchannel)
Expand Down Expand Up @@ -568,14 +568,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 @@ -1034,6 +1038,13 @@ Get if auto update is available (not disabled by serverUrl).
| **`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 @@ -1048,10 +1048,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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you sure this will not cause duplicated when already set?

}
} 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 @@ -777,9 +777,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 @@ -866,6 +868,7 @@ public void run() {
try {
CapacitorUpdaterPlugin.this.implementation.getLatest(
CapacitorUpdaterPlugin.this.updateUrl,
null,
res -> {
if (res.has("error")) {
Log.e(
Expand Down Expand Up @@ -1158,6 +1161,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 @@ -405,8 +405,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 @@ -723,7 +724,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 @@ -897,7 +898,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 @@ -385,7 +385,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 @@ -729,6 +729,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
Loading