Skip to content

Commit

Permalink
feat: implement presentNextTime for Android (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
duguyihou authored Feb 26, 2024
1 parent 3d22026 commit d2bf0c4
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 17 deletions.
51 changes: 51 additions & 0 deletions android/src/main/java/com/neoversion/NeoPreference.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.neoversion

import android.content.Context
import android.content.SharedPreferences

class NeoPreference(private val context: Context) {
private val sharedPreferences: SharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE)
fun save(key: String, value: String) {
val editor = sharedPreferences.edit()
editor.putString(key, value)
editor.apply()
}
fun save(key: String, value: Int) {
val editor = sharedPreferences.edit()
editor.putInt(key, value)
editor.apply()
}
fun save(key : String, status: Boolean){
val editor = sharedPreferences.edit()
editor.putBoolean(key, status)
editor.apply()
}

fun getValueString(key: String): String? {
return sharedPreferences.getString(key, null)
}

fun getValueInt(key: String): Int {
return sharedPreferences.getInt(key, 0)
}

fun getValueBoolean(key: String, defaultValue: Boolean) : Boolean{
return sharedPreferences.getBoolean(key, defaultValue)
}

fun clearSharedPreferences(){
val editor = sharedPreferences.edit()
editor.clear()
editor.apply()
}

fun removeValue(key: String){
val editor = sharedPreferences.edit()
editor.remove(key)
editor.apply()
}

companion object {
const val PREF_NAME = "neo-version-preference"
}
}
19 changes: 12 additions & 7 deletions android/src/main/java/com/neoversion/NeoVersionModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,21 @@ class NeoVersionModule(reactContext: ReactApplicationContext) :
}

private val appUpdateManager = AppUpdateManagerFactory.create(reactContext)
private val neoPreference = NeoPreference(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 storedStalenessDays = neoPreference.getValueInt("storedStalenessDays")
val updateAvailability = appUpdateInfo.updateAvailability()
val isUpdateAvailable = updateAvailability == UpdateAvailability.UPDATE_AVAILABLE
map.putBoolean("isUpdateAvailable", isUpdateAvailable)
appUpdateInfo.clientVersionStalenessDays()?.let {
map.putInt("stalenessDays", it)
}
promise.resolve(map)
val stalenessDays = appUpdateInfo.clientVersionStalenessDays() ?: 0
val isUpdateAvailable = updateAvailability == UpdateAvailability.UPDATE_AVAILABLE && stalenessDays > storedStalenessDays
promise.resolve(isUpdateAvailable)
}
}

Expand All @@ -42,6 +41,7 @@ class NeoVersionModule(reactContext: ReactApplicationContext) :
appUpdateInfoTask.addOnFailureListener { err: Exception ->
promise.reject("ERROR", err.toString())
}

appUpdateInfoTask.addOnSuccessListener { appUpdateInfo ->
try {
currentActivity?.let {
Expand All @@ -59,6 +59,11 @@ class NeoVersionModule(reactContext: ReactApplicationContext) :
}
}

@ReactMethod
fun presentNextTime(day: Int) {
neoPreference.save("storedStalenessDays", day)
}

companion object {
const val NAME = "NeoVersion"
const val IN_APP_UPDATE_REQUEST_CODE = 42139
Expand Down
11 changes: 5 additions & 6 deletions src/Android/neoVersion.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import { NativeModules } from 'react-native';

const neoVersion = NativeModules.NeoVersion;

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

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

Expand All @@ -16,4 +11,8 @@ export function startUpdate(): Promise<void> {
return neoVersion.startUpdate(0);
}

export function presentNextTime(day: number) {
return neoVersion.presentNextTime(day);
}

export default neoVersion;
15 changes: 11 additions & 4 deletions src/Android/useNeoVersionCheck.android.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { useEffect } from 'react';
import { getVersionInfo, startUpdate } from './neoVersion.android';
import {
getVersionInfo,
presentNextTime,
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) {
const isUpdateAvailable = await getVersionInfo();
if (isUpdateAvailable) {
Alert.alert(
configuration?.title ?? 'Update Available',
configuration?.message ??
Expand All @@ -22,12 +26,15 @@ export const useNeoVersionCheck = (configuration?: Partial<Configuration>) => {
},
{
text: 'Next time',
onPress: () => {
presentNextTime(configuration?.frequency ?? 7);
},
style: 'default',
},
]
);
}
};
performVersionCheck();
}, [configuration?.message, configuration?.title]);
}, [configuration?.frequency, configuration?.message, configuration?.title]);
};

0 comments on commit d2bf0c4

Please sign in to comment.