diff --git a/lstMobile/src/lib/lastestVersion.ts b/lstMobile/src/lib/lastestVersion.ts new file mode 100644 index 0000000..6b6429c --- /dev/null +++ b/lstMobile/src/lib/lastestVersion.ts @@ -0,0 +1,50 @@ +import ReactNativeBlobUtil from "react-native-blob-util"; + +export async function downloadLatestApk(serverIp: string, port: string) { + const url = `http://${serverIp}:${port}/lst/api/mobile/apk/latest`; + + const apkPath = `${ReactNativeBlobUtil.fs.dirs.DownloadDir}/lst-mobile.apk`; + + // delete old apk if it exists + const exists = await ReactNativeBlobUtil.fs.exists(apkPath); + + if (exists) { + const stat = await ReactNativeBlobUtil.fs.stat(apkPath); + + // last modified time + const lastModified = Number(stat.lastModified); + + // current time + const now = Date.now(); + + // 5 minutes in ms + const fiveMinutes = 5 * 60 * 1000; + + // skip download if file is fresh + if (now - lastModified < fiveMinutes) { + console.log("APK already downloaded recently, skipping."); + + return { + skipped: true, + path: apkPath, + }; + } + + // delete old apk before redownload + await ReactNativeBlobUtil.fs.unlink(apkPath); + } + + const res = await ReactNativeBlobUtil.config({ + addAndroidDownloads: { + useDownloadManager: true, + notification: true, + title: "LST Mobile Update", + description: "Downloading update for StageNow install", + mime: "application/vnd.android.package-archive", + path: apkPath, + mediaScannable: true, + }, + }).fetch("GET", url); + + return res.path(); +} diff --git a/lstMobile/src/lib/versionValidation.ts b/lstMobile/src/lib/versionValidation.ts index 7e2ae57..6990c4d 100644 --- a/lstMobile/src/lib/versionValidation.ts +++ b/lstMobile/src/lib/versionValidation.ts @@ -1,6 +1,8 @@ import axios from "axios"; +import Constants from "expo-constants"; import { useAppStore } from "../hooks/useAppStore"; import { useServerStore } from "../hooks/useServerCheck"; +import { downloadLatestApk } from "./lastestVersion"; export type ServerVersionInfo = { packageName: string; @@ -60,13 +62,11 @@ export const versionCheck = async () => { setServerVersion(res.data); } - // const build = Constants.expoConfig?.android?.versionCode ?? 1; + const build = Constants.expoConfig?.android?.versionCode ?? 1; - // if (build < res.data.minSupportedVersionCode) { - // setStartupRoute("/updateScreen"); - // setReady(true); - // return; - // } + if (build < res.data.versionCode) { + await downloadLatestApk(serverIp, port); + } } catch (error) { console.log("Version check error:", error); }