|
import type { ReactElement } from "react"; |
|
import { useState } from "react"; |
|
import { Button } from "react-native-paper"; |
|
import { useAsync } from "../../hooks/useAsync"; |
|
import { APP_VERSION } from "../../../../v"; |
|
import { supabase } from "../../db/supabase"; |
|
import type { Database } from "../../db/supabase.types"; |
|
import RNFetchBlob from "rn-fetch-blob"; |
|
import ApkInstallerModule from "../../../../apkinstaller"; |
|
|
|
type Version = { |
|
changelog: string; |
|
version_code: string; |
|
version_uuid: string; |
|
} |
|
|
|
export const UpdateBanner = (): ReactElement => { |
|
const [updateAvailable, setUpdateAvailable] = useState<Version>({ |
|
changelog: "", |
|
version_code: "", |
|
version_uuid: "" |
|
}); |
|
|
|
const [showModal, setShowModal] = useState(false); |
|
const [downloadProgress, setDownloadProgress] = useState(0.14); |
|
const [downloading, setDownloading] = useState(false); |
|
const [downloaded, setDownloaded] = useState(false); |
|
|
|
setInterval(() => { |
|
if (downloading) setDownloadProgress((prev) => prev + 0.01); |
|
}, 1000); |
|
|
|
useAsync(async() => { |
|
const { data, error } = await supabase.rpc("latest_version", { given_version_code: APP_VERSION }); |
|
if (error) throw error; |
|
if (!data) return; |
|
|
|
if (data.length === 0) return; |
|
setUpdateAvailable(data[0]); |
|
}, []); |
|
|
|
const downloadApk = (): void => { |
|
const { data } = supabase.storage.from("versions").getPublicUrl(`${updateAvailable.version_code}.apk`); |
|
if (!data) return; |
|
|
|
RNFetchBlob.config({ |
|
fileCache: true, |
|
path: `${RNFetchBlob.fs.dirs.DownloadDir}/mitwill-${updateAvailable.version_code}.apk`, |
|
overwrite: true |
|
}) |
|
.fetch("GET", data.publicUrl) |
|
.progress((received, total) => { |
|
setDownloadProgress(received / total); |
|
}) |
|
.then((res) => { |
|
console.log("The file saved to ", res.path()); |
|
setDownloading(false); |
|
setDownloaded(true); |
|
ApkInstallerModule.installApk(`${res.path()}`); |
|
}) |
|
.catch((error) => console.error(error)); |
|
}; |
|
|
|
return ( |
|
<Button onPress={() => { |
|
setDownloading(true); |
|
void downloadApk(); |
|
}}> |
|
Download |
|
</Button> |
|
); |
|
}; |