UNPKG

react-native-code-sync

Version:

React-native plugin for the code-sync functionality on live via OTA (Over-the-air)

189 lines (175 loc) 6.75 kB
import React from 'react' import { checkForOTAUpdate } from './code-sync/OtaApiCalls' import { useCheckVersion } from './code-sync/OtaUpdate' import { Utility } from './utils' import { OtaUpdateModal } from './code-sync/OtaUpdateModal' import OtaUpdateClass from './code-sync/OtaUpdateClass' import { setCodeSyncConfig } from './config' const useCheckCodeSync = () => { let timeout const { onCheckVersion, checkForUpgradedAppVersionAvailable } = useCheckVersion() const checkForCodeSync = ({ params, onUpdateFailed = () => { }, onStartUpdate = () => { }, onDownloadProgress = () => { }, onEndUpdate = () => { }, onUpdateNotAvailable = () => { } }) => { if (Utility.checkForDevMode()) return null return new Promise(async (resolve, reject) => { try { if (!params) { onUpdateFailed?.() reject('Please pass required params as \"project_key\" and \"version\"') console.error('Please pass required params as \"project_key\" and \"version\"') return } await checkForUpgradedAppVersionAvailable() const response = await checkForOTAUpdate(params) Utility.log("response====>@@@", response) if (response?.status == 200) { if (!response?.data?.length) resolve(null) const bundleInfo = response?.data?.[0] if (!bundleInfo?.v_id) resolve(null) onCheckVersion({ bundleUrl: bundleInfo?.v_link, curentbundleVersion: bundleInfo?.v_id, serverAppVersion: bundleInfo?.v_version, onUpdateComplete: () => { onDownloadProgress?.(100) onEndUpdate?.() }, onUpdateFailed: () => onUpdateFailed?.(), onStartUpdate: () => onStartUpdate?.(), downloadProgress: value => { if (timeout) clearTimeout(timeout) timeout = setTimeout(() => { timeout = null onDownloadProgress?.(value?.toFixed(2)) }, 0); }, onUpdateNotAvailable: onUpdateNotAvailable }) resolve(bundleInfo) } else resolve(null) } catch (error) { console.error(error) reject(error) } }) } return { checkForCodeSync } } class CheckCodeSyncClass { constructor() { this.timeout = null } checkForCodeSync = ({ params, onUpdateFailed = () => { }, onStartUpdate = () => { }, onDownloadProgress = () => { }, onEndUpdate = () => { }, onUpdateNotAvailable = () => { } }) => { if (Utility.checkForDevMode()) return null return new Promise(async (resolve, reject) => { try { if (!params) { onUpdateFailed?.() reject('Please pass required params as \"project_key\" and \"version\"') console.error('Please pass required params as \"project_key\" and \"version\"') return } const otaUpdateClass = new OtaUpdateClass(); await otaUpdateClass.checkForUpgradedAppVersionAvailable() const response = await checkForOTAUpdate(params) if (response?.status == 200) { if (!response?.data?.length) resolve(null) const bundleInfo = response?.data?.[0] if (!bundleInfo?.v_id) resolve(null) otaUpdateClass.onCheckVersion({ bundleUrl: bundleInfo?.v_link, curentbundleVersion: bundleInfo?.v_id, serverAppVersion: bundleInfo?.v_version, onUpdateComplete: () => { onDownloadProgress?.(100) onEndUpdate?.() }, onUpdateFailed: () => onUpdateFailed?.(), onStartUpdate: () => onStartUpdate?.(), downloadProgress: value => { if (this.timeout) clearTimeout(this.timeout) this.timeout = setTimeout(() => { this.timeout = null onDownloadProgress?.(value?.toFixed(2)) }, 0); }, onUpdateNotAvailable: onUpdateNotAvailable }) resolve(bundleInfo) } else resolve(null) } catch (error) { console.error(error) reject(error) } }) } } const CodeSyncModal = ({ codeSyncProgress, visible, otaUpdateError, onCancelPress, onRestartPress, appLogo, showCancelButton, themeColor, downloadCompleteMessage, downloadingInProgressMessage, downloadErrorMessage, cancelButtonLabel, restartButtonLabel, percentageTextStyle, messageStyle, mainContaineStyle, cancelButtonLabelStyle, restartButtonLabelStyle, customUI }) => <OtaUpdateModal progressPercentage={`${codeSyncProgress} %`} progressValue={codeSyncProgress} visible={visible} otaUpdateError={otaUpdateError} onCancelPress={onCancelPress} onRestartPress={onRestartPress} appLogo={appLogo} showCancelButton={showCancelButton} themeColor={themeColor} downloadCompleteMessage={downloadCompleteMessage} downloadingInProgressMessage={downloadingInProgressMessage} downloadErrorMessage={downloadErrorMessage} cancelButtonLabel={cancelButtonLabel} restartButtonLabel={restartButtonLabel} percentageTextStyle={percentageTextStyle} messageStyle={messageStyle} mainContaineStyle={mainContaineStyle} cancelButtonLabelStyle={cancelButtonLabelStyle} restartButtonLabelStyle={restartButtonLabelStyle} customUI={customUI} /> const restartApp = () => Utility.reloadApp() export { CodeSyncModal, CheckCodeSyncClass, useCheckCodeSync, restartApp, setCodeSyncConfig }