diff --git a/components/app-wrapper.js b/components/app-wrapper.js index 616ad0a..47cbd2b 100644 --- a/components/app-wrapper.js +++ b/components/app-wrapper.js @@ -1,5 +1,4 @@ -import React, { Component } from 'react' -import { StyleSheet, View } from 'react-native' +import React, { useState, useEffect } from 'react' import { Provider } from 'react-redux' import nodejs from 'nodejs-mobile-react-native' @@ -14,91 +13,45 @@ import PasswordPrompt from './password-prompt' import store from '../store' -export default class AppWrapper extends Component { - constructor() { - super() - this.state = { - isCheckingLicenseAgreement: true, - shouldShowLicenseAgreement: false, - shouldShowPasswordPrompt: false, - shouldShowApp: false, - } - nodejs.start('main.js') - this.checkLicenseAgreement() - this.checkDbPasswordSet() - } +export default function AppWrapper() { + const [isLoading, setIsLoading] = useState(true) + const [isLicenseAccepted, setIsLicenseAccepted] = useState(false) + const [isDbEncrypted, setIsDbEncrypted] = useState(false) - async checkLicenseAgreement() { + const checkIsLicenseAccepted = async () => { const isLicenseFlagSet = await getLicenseFlag() - if (!isLicenseFlagSet) { - this.enableShowLicenseAgreement() - } else { - this.setState({ isCheckingLicenseAgreement: false }) - } + setIsLicenseAccepted(isLicenseFlagSet) + setIsLoading(false) } - async checkDbPasswordSet() { - const canConnectToDb = await openDb() - if (canConnectToDb) { - this.enableShowApp() - await saveEncryptionFlag(false) - return false - } - this.setState({ shouldShowPasswordPrompt: true }) - await saveEncryptionFlag(true) + const checkIsDbEncrypted = async () => { + const isEncrypted = !(await openDb()) + if (isEncrypted) setIsDbEncrypted(true) + await saveEncryptionFlag(isEncrypted) } - enableShowLicenseAgreement = () => { - this.setState({ - shouldShowLicenseAgreement: true, - isCheckingLicenseAgreement: false, - }) + useEffect(() => { + nodejs.start('main.js') + checkIsLicenseAccepted() + checkIsDbEncrypted() + }, []) + + if (isLoading) { + return } - disableShowLicenseAgreement = () => { - this.setState({ shouldShowLicenseAgreement: false }) + if (!isLicenseAccepted) { + return setIsLicenseAccepted(true)} /> } - enableShowApp = () => { - this.setState({ - shouldShowApp: true, - shouldShowPasswordPrompt: false, - }) - } - - render() { - const { - isCheckingLicenseAgreement, - shouldShowLicenseAgreement, - shouldShowPasswordPrompt, - shouldShowApp, - } = this.state - - let initialView = null - - if (isCheckingLicenseAgreement) { - initialView = - } else if (shouldShowLicenseAgreement) { - initialView = - } else if (shouldShowPasswordPrompt) { - initialView = - } else if (shouldShowApp) { - initialView = this.checkDbPasswordSet()} /> - } - - return ( - - - - {initialView} - - - ) - } + return ( + + + {isDbEncrypted ? ( + setIsDbEncrypted(false)} /> + ) : ( + checkIsDbEncrypted()} /> + )} + + ) } - -const styles = StyleSheet.create({ - container: { - flex: 1, - }, -})