Skip to content

Commit

Permalink
feat(app-update): add currentVersionName, availableVersionName, .…
Browse files Browse the repository at this point in the history
….. (#137)

* feat(app-update): add `currentVersionName`, `availableVersionName`, ...

* wip
  • Loading branch information
robingenz authored Feb 5, 2024
1 parent d091ddf commit be586c0
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/curly-zoos-end.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capawesome/capacitor-app-update': minor
---

feat: add `currentVersionName`, `availableVersionName`, ...
41 changes: 27 additions & 14 deletions packages/app-update/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,25 @@ A working example can be found here: [robingenz/capacitor-plugin-demo](https://g
## Usage

```typescript
import { Capacitor } from '@capacitor/core';
import { AppUpdate } from '@capawesome/capacitor-app-update';

const getCurrentAppVersion = async () => {
const result = await AppUpdate.getAppUpdateInfo();
return result.currentVersion;
if (Capacitor.getPlatform() === 'android') {
return result.currentVersionCode;
} else {
return result.currentVersionName;
}
};

const getAvailableAppVersion = async () => {
const result = await AppUpdate.getAppUpdateInfo();
return result.availableVersion;
if (Capacitor.getPlatform() === 'android') {
return result.availableVersionCode;
} else {
return result.availableVersionName;
}
};

const openAppStore = async () => {
Expand Down Expand Up @@ -203,18 +212,22 @@ Remove all listeners for this plugin.

#### AppUpdateInfo

| Prop | Type | Description |
| --------------------------------- | ----------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| **`currentVersion`** | <code>string</code> | Version code (Android) or CFBundleShortVersionString (iOS) of the currently installed app version. Only available on Android and iOS. |
| **`availableVersion`** | <code>string</code> | Version code (Android) or CFBundleShortVersionString (iOS) of the update. Only available on Android and iOS. |
| **`availableVersionReleaseDate`** | <code>string</code> | Release date of the update in ISO 8601 (UTC) format. Only available on iOS. |
| **`updateAvailability`** | <code><a href="#appupdateavailability">AppUpdateAvailability</a></code> | The app update availability. Only available on Android and iOS. |
| **`updatePriority`** | <code>number</code> | In-app update priority for this update, as defined by the developer in the Google Play Developer API. Only available on Android. |
| **`immediateUpdateAllowed`** | <code>boolean</code> | `true` if an immediate update is allowed, otherwise `false`. Only available on Android. |
| **`flexibleUpdateAllowed`** | <code>boolean</code> | `true` if a flexible update is allowed, otherwise `false`. Only available on Android. |
| **`clientVersionStalenessDays`** | <code>number</code> | Number of days since the Google Play Store app on the user's device has learnt about an available update if an update is available or in progress. Only available on Android. |
| **`installStatus`** | <code><a href="#flexibleupdateinstallstatus">FlexibleUpdateInstallStatus</a></code> | Flexible in-app update install status. Only available on Android. |
| **`minimumOsVersion`** | <code>string</code> | The minimum version of the operating system required for the app to run in iOS. Only available on iOS. |
| Prop | Type | Description | Since |
| --------------------------------- | ----------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----- |
| **`currentVersionName`** | <code>string</code> | The current version name of the app. On **Android**, this is the `versionName` from the `android/app/build.gradle` file. On **iOS**, this is the `CFBundleShortVersionString` from the `Info.plist` file. Only available on Android and iOS. | 5.1.0 |
| **`availableVersionName`** | <code>string</code> | The available version name of the update. On **iOS**, this is the `CFBundleShortVersionString` from the `Info.plist` file. Only available on iOS. | 5.1.0 |
| **`currentVersion`** | <code>string</code> | Version code (Android) or CFBundleShortVersionString (iOS) of the currently installed app version. Only available on Android and iOS. | |
| **`currentVersionCode`** | <code>string</code> | The current version code of the app. On **Android**, this is the `versionCode` from the `android/app/build.gradle` file. On **iOS**, this is the `CFBundleVersion` from the `Info.plist` file. Only available on Android and iOS. | 5.1.0 |
| **`availableVersion`** | <code>string</code> | Version code (Android) or CFBundleShortVersionString (iOS) of the update. Only available on Android and iOS. | |
| **`availableVersionCode`** | <code>string</code> | The available version code of the update. On **Android**, this is the `versionCode` from the `android/app/build.gradle` file. Only available on Android. | 5.1.0 |
| **`availableVersionReleaseDate`** | <code>string</code> | Release date of the update in ISO 8601 (UTC) format. Only available on iOS. | |
| **`updateAvailability`** | <code><a href="#appupdateavailability">AppUpdateAvailability</a></code> | The app update availability. Only available on Android and iOS. | |
| **`updatePriority`** | <code>number</code> | In-app update priority for this update, as defined by the developer in the Google Play Developer API. Only available on Android. | |
| **`immediateUpdateAllowed`** | <code>boolean</code> | `true` if an immediate update is allowed, otherwise `false`. Only available on Android. | |
| **`flexibleUpdateAllowed`** | <code>boolean</code> | `true` if a flexible update is allowed, otherwise `false`. Only available on Android. | |
| **`clientVersionStalenessDays`** | <code>number</code> | Number of days since the Google Play Store app on the user's device has learnt about an available update if an update is available or in progress. Only available on Android. | |
| **`installStatus`** | <code><a href="#flexibleupdateinstallstatus">FlexibleUpdateInstallStatus</a></code> | Flexible in-app update install status. Only available on Android. | |
| **`minimumOsVersion`** | <code>string</code> | The minimum version of the operating system required for the app to run in iOS. Only available on iOS. | |


#### GetAppUpdateInfoOptions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,11 @@ public void getAppUpdateInfo(PluginCall call) {
return;
}
JSObject ret = new JSObject();
ret.put("currentVersionName", pInfo.versionName);
ret.put("currentVersion", String.valueOf(pInfo.versionCode));
ret.put("currentVersionCode", String.valueOf(pInfo.versionCode));
ret.put("availableVersion", String.valueOf(appUpdateInfo.availableVersionCode()));
ret.put("availableVersionCode", String.valueOf(appUpdateInfo.availableVersionCode()));
ret.put("updateAvailability", appUpdateInfo.updateAvailability());
ret.put("updatePriority", appUpdateInfo.updatePriority());
ret.put("immediateUpdateAllowed", appUpdateInfo.isUpdateTypeAllowed(AppUpdateType.IMMEDIATE));
Expand Down
14 changes: 9 additions & 5 deletions packages/app-update/ios/Plugin/AppUpdatePlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ public class AppUpdatePlugin: CAPPlugin {
guard
let info = Bundle.main.infoDictionary,
let bundleId = info["CFBundleIdentifier"] as? String,
let currentVersion = info["CFBundleShortVersionString"] as? String,
let currentVersionCode = info["CFBundleVersion"] as? String,
let currentVersionName = info["CFBundleShortVersionString"] as? String,
var lookupUrl = URL(string: "https://itunes.apple.com/lookup?bundleId=\(bundleId)&date=\(date)")
else {
call.reject("Invalid bundle info provided")
Expand All @@ -30,21 +31,24 @@ public class AppUpdatePlugin: CAPPlugin {
guard
let json = try JSONSerialization.jsonObject(with: data, options: [.allowFragments]) as? [String: Any],
let result = (json["results"] as? [Any])?.first as? [String: Any],
let availableVersion = result["version"] as? String,
let availableVersionName = result["version"] as? String,
let availableVersionReleaseDate = result["currentVersionReleaseDate"] as? String,
let minimumOsVersion = result["minimumOsVersion"] as? String
else {
call.reject("Required app information could not be fetched")
return
}
var updateAvailability = AppUpdatePlugin.updateAvailabilityNotAvailable
let updateAvailable = self.compareVersions(currentVersion, availableVersion) == .orderedDescending
let updateAvailable = self.compareVersions(currentVersionName, availableVersionName) == .orderedDescending
if updateAvailable {
updateAvailability = AppUpdatePlugin.updateAvailabilityAvailable
}
call.resolve([
"currentVersion": currentVersion,
"availableVersion": availableVersion,
"currentVersionName": currentVersionName,
"availableVersionName": availableVersionName,
"currentVersion": currentVersionName,
"currentVersionCode": currentVersionCode,
"availableVersion": availableVersionName,
"availableVersionReleaseDate": availableVersionReleaseDate,
"updateAvailability": updateAvailability,
"minimumOsVersion": minimumOsVersion
Expand Down
50 changes: 50 additions & 0 deletions packages/app-update/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,68 @@ export interface GetAppUpdateInfoOptions {
}

export interface AppUpdateInfo {
/**
* The current version name of the app.
*
* On **Android**, this is the `versionName` from the `android/app/build.gradle` file.
* On **iOS**, this is the `CFBundleShortVersionString` from the `Info.plist` file.
*
* Only available on Android and iOS.
*
* @since 5.1.0
* @example "1.0.0"
*/
currentVersionName: string;
/**
* The available version name of the update.
*
* On **iOS**, this is the `CFBundleShortVersionString` from the `Info.plist` file.
*
* Only available on iOS.
*
* @since 5.1.0
* @example "1.1.0"
*/
availableVersionName?: string;
/**
* Version code (Android) or CFBundleShortVersionString (iOS) of the currently installed app version.
*
* Only available on Android and iOS.
*
* @deprecated On **Android**, use `currentVersionCode` instead. On **iOS**, use `currentVersionName` instead.
*/
currentVersion: string;
/**
* The current version code of the app.
*
* On **Android**, this is the `versionCode` from the `android/app/build.gradle` file.
* On **iOS**, this is the `CFBundleVersion` from the `Info.plist` file.
*
* Only available on Android and iOS.
*
* @since 5.1.0
* @example "1"
*/
currentVersionCode: string;
/**
* Version code (Android) or CFBundleShortVersionString (iOS) of the update.
*
* Only available on Android and iOS.
*
* @deprecated On **Android**, use `availableVersionCode` instead. On **iOS**, use `availableVersionName` instead.
*/
availableVersion: string;
/**
* The available version code of the update.
*
* On **Android**, this is the `versionCode` from the `android/app/build.gradle` file.
*
* Only available on Android.
*
* @since 5.1.0
* @example "2"
*/
availableVersionCode?: string;
/**
* Release date of the update in ISO 8601 (UTC) format.
*
Expand Down

0 comments on commit be586c0

Please sign in to comment.