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:
50
lstMobile/src/lib/lastestVersion.ts
Normal file
50
lstMobile/src/lib/lastestVersion.ts
Normal 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();
|
||||||
|
}
|
||||||
@@ -1,6 +1,8 @@
|
|||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import Constants from "expo-constants";
|
||||||
import { useAppStore } from "../hooks/useAppStore";
|
import { useAppStore } from "../hooks/useAppStore";
|
||||||
import { useServerStore } from "../hooks/useServerCheck";
|
import { useServerStore } from "../hooks/useServerCheck";
|
||||||
|
import { downloadLatestApk } from "./lastestVersion";
|
||||||
|
|
||||||
export type ServerVersionInfo = {
|
export type ServerVersionInfo = {
|
||||||
packageName: string;
|
packageName: string;
|
||||||
@@ -60,13 +62,11 @@ export const versionCheck = async () => {
|
|||||||
setServerVersion(res.data);
|
setServerVersion(res.data);
|
||||||
}
|
}
|
||||||
|
|
||||||
// const build = Constants.expoConfig?.android?.versionCode ?? 1;
|
const build = Constants.expoConfig?.android?.versionCode ?? 1;
|
||||||
|
|
||||||
// if (build < res.data.minSupportedVersionCode) {
|
if (build < res.data.versionCode) {
|
||||||
// setStartupRoute("/updateScreen");
|
await downloadLatestApk(serverIp, port);
|
||||||
// setReady(true);
|
}
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log("Version check error:", error);
|
console.log("Version check error:", error);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user