Skip to content

Commit

Permalink
5 update type for android (#9)
Browse files Browse the repository at this point in the history
* build: install ktor

* build: install app-update-ktx

* feat: implement getVersionInfo and startUpdate

* revert: ktor

* refactor: folder structure
  • Loading branch information
duguyihou authored Feb 25, 2024
1 parent 148adbe commit c362b8c
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 20 deletions.
5 changes: 4 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
buildscript {
// Buildscript is evaluated before everything else so we can't use getExtOrDefault
def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["NeoVersion_kotlinVersion"]

ext {
app_update_ktx = "2.1.0"
}
repositories {
google()
mavenCentral()
Expand Down Expand Up @@ -90,5 +92,6 @@ dependencies {
//noinspection GradleDynamicVersion
implementation "com.facebook.react:react-native:+"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation("com.google.android.play:app-update-ktx:$app_update_ktx")
}

51 changes: 46 additions & 5 deletions android/src/main/java/com/neoversion/NeoVersionModule.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package com.neoversion

import android.content.IntentSender.SendIntentException
import com.facebook.react.bridge.Arguments
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.facebook.react.bridge.Promise
import com.google.android.play.core.appupdate.AppUpdateManagerFactory
import com.google.android.play.core.install.model.UpdateAvailability

class NeoVersionModule(reactContext: ReactApplicationContext) :
ReactContextBaseJavaModule(reactContext) {
Expand All @@ -12,14 +16,51 @@ class NeoVersionModule(reactContext: ReactApplicationContext) :
return NAME
}

// Example method
// See https://reactnative.dev/docs/native-modules-android
private val appUpdateManager = AppUpdateManagerFactory.create(reactContext)

@ReactMethod
fun getVersionInfo(promise: Promise) {
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
appUpdateInfoTask.addOnFailureListener { err ->
promise.reject("ERROR", err.toString())
}
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
val map = Arguments.createMap()
val updateAvailability = appUpdateInfo.updateAvailability()
val isUpdateAvailable = updateAvailability == UpdateAvailability.UPDATE_AVAILABLE
map.putBoolean("isUpdateAvailable", isUpdateAvailable)
appUpdateInfo.clientVersionStalenessDays()?.let {
map.putInt("stalenessDays", it)
}
promise.resolve(map)
}
}

@ReactMethod
fun multiply(a: Double, b: Double, promise: Promise) {
promise.resolve(a * b)
fun startUpdate(updateType: Int = 0, promise: Promise) {
val appUpdateInfoTask = appUpdateManager.appUpdateInfo
appUpdateInfoTask.addOnFailureListener { err: Exception ->
promise.reject("ERROR", err.toString())
}
appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
try {
currentActivity?.let {
appUpdateManager.startUpdateFlowForResult(
appUpdateInfo,
updateType,
it,
IN_APP_UPDATE_REQUEST_CODE
)
}
promise.resolve("DONE")
} catch (err: SendIntentException) {
promise.reject("ERROR", err.toString())
}
}
}

companion object {
const val NAME = "NeoVersion"
const val IN_APP_UPDATE_REQUEST_CODE = 42139
}
}
1 change: 1 addition & 0 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ android {
namespace "com.neoversionexample"
defaultConfig {
applicationId "com.neoversionexample"
// applicationId "com.facebook.katana"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 1
Expand Down
3 changes: 2 additions & 1 deletion example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import * as React from 'react';

import { StyleSheet, View, Text } from 'react-native';
import { useNeoVersionCheck } from '../../src';
import { useNeoVersionCheck } from '../../src/index.android';

export default function App() {
useNeoVersionCheck();

return (
<View style={styles.container}>
<Text>React Native Neo Version</Text>
Expand Down
18 changes: 18 additions & 0 deletions src/Android/neoVersion.android.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { NativeModules } from 'react-native';

const neoVersion = NativeModules.NeoVersion;

type VersionInfo = {
isUpdateAvailable: boolean;
stalenessDays: number;
};

export function getVersionInfo(): Promise<Partial<VersionInfo>> {
return neoVersion.getVersionInfo();
}

export function startUpdate(): Promise<void> {
return neoVersion.startUpdate();
}

export default neoVersion;
33 changes: 33 additions & 0 deletions src/Android/useNeoVersionCheck.android.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useEffect } from 'react';
import { getVersionInfo, startUpdate } from './neoVersion.android';
import { Alert } from 'react-native';
import type { Configuration } from '../types';

export const useNeoVersionCheck = (configuration?: Partial<Configuration>) => {
useEffect(() => {
const performVersionCheck = async () => {
const info = await getVersionInfo();
if (info.isUpdateAvailable) {
Alert.alert(
configuration?.title ?? 'Update Available',
configuration?.message ??
'Please update the app to have the best experience',
[
{
text: 'Update',
onPress: () => {
startUpdate();
},
style: 'default',
},
{
text: 'Next time',
style: 'default',
},
]
);
}
};
performVersionCheck();
}, [configuration?.message, configuration?.title]);
};
6 changes: 5 additions & 1 deletion src/alertButton.ts → src/iOS/alertButton.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import type { AlertButton } from 'react-native';
import { launchAppStore, presentNextTime, skipThisVersion } from './neoVersion';
import {
launchAppStore,
presentNextTime,
skipThisVersion,
} from './neoVersion.ios';

export const updateButton = (): AlertButton => {
return {
Expand Down
2 changes: 1 addition & 1 deletion src/neoVersion.ts → src/iOS/neoVersion.ios.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NativeModules } from 'react-native';
import type { UpdateType } from './types';
import type { UpdateType } from '../types';

const neoVersion = NativeModules.NeoVersion;

Expand Down
2 changes: 1 addition & 1 deletion src/rules.ts → src/iOS/rules.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AlertButton } from 'react-native';
import { nextTimeButton, skipButton, updateButton } from './alertButton';
import { type AlertType, type UpdateType, type Rules } from './types';
import { type AlertType, type UpdateType, type Rules } from '../types';

const criticalRules: Rules = {
alertType: 'force',
Expand Down
11 changes: 2 additions & 9 deletions src/useNeoVersionCheck.ts → src/iOS/useNeoVersionCheck.ios.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import { useEffect } from 'react';
import { Alert } from 'react-native';
import type { AlertType } from './types';
import type { Configuration } from '../types';
import { generateAlertButtons, parse, shouldPresentAlert } from './rules';
import { computeDaysSincePresentation, getVersionInfo } from './neoVersion';

type Configuration = {
title: string;
message: string;
alertType: AlertType;
frequency: number;
};
import { computeDaysSincePresentation, getVersionInfo } from './neoVersion.ios';

export const useNeoVersionCheck = (configuration?: Partial<Configuration>) => {
useEffect(() => {
Expand Down
1 change: 1 addition & 0 deletions src/index.android.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useNeoVersionCheck } from './Android/useNeoVersionCheck.android';
1 change: 1 addition & 0 deletions src/index.ios.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { useNeoVersionCheck } from './iOS/useNeoVersionCheck.ios';
1 change: 0 additions & 1 deletion src/index.tsx

This file was deleted.

7 changes: 7 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@ export type Rules = {
alertType: AlertType;
frequency: FrequencyVal | number;
};

export type Configuration = {
title: string;
message: string;
alertType: AlertType;
frequency: number;
};

0 comments on commit c362b8c

Please sign in to comment.