feat(mobile): added auto download of latest

this will predownload the latest if its there, this will speed up the update as the user will only
need to scan a single command and it will install restart app
This commit is contained in:
2026-05-27 20:50:07 -05:00
parent 3a0c729b9a
commit 6d0fb8aee4
2 changed files with 56 additions and 6 deletions

View File

@@ -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();
}

View File

@@ -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);
}