From fa20e84bfec933804c66f80df04937bd01aaed8d Mon Sep 17 00:00:00 2001 From: Sofiya Tepikin Date: Thu, 30 May 2019 23:27:53 +0200 Subject: [PATCH 1/2] Fixes reopenning after back button --- components/app.js | 6 +++++- db/index.js | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/components/app.js b/components/app.js index 34f6f11..3fd7bf4 100644 --- a/components/app.js +++ b/components/app.js @@ -12,6 +12,7 @@ import settingsViews from './settings' import Stats from './stats' import {headerTitles, menuTitles} from '../i18n/en/labels' import setupNotifications from '../lib/notifications' +import { closeDb } from '../db' // design wants everyhting lowercased, but we don't // have CSS pseudo properties @@ -55,7 +56,10 @@ export default class App extends Component { handleBackButtonPress = () => { const { currentPage, currentProps } = this.state - if (currentPage === HOME_PAGE) return false + if (currentPage === HOME_PAGE) { + closeDb() + return false + } if (this.isSymptomView()) { this.navigate( this.originForSymptomView, { date: currentProps.date } diff --git a/db/index.js b/db/index.js index 5fcbab0..b8ea002 100644 --- a/db/index.js +++ b/db/index.js @@ -55,6 +55,10 @@ export async function openDb (hash) { return true } +export function closeDb() { + db.close() +} + export function getBleedingDaysSortedByDate() { return db.objects('CycleDay').filtered('bleeding != null').sorted('date', true) } From e4209e28b7bda936e23e246b96558fa69ba93599 Mon Sep 17 00:00:00 2001 From: Sofiya Tepikin Date: Tue, 18 Jun 2019 00:10:54 +0200 Subject: [PATCH 2/2] Refactors App wrapper component --- components/app-wrapper.js | 84 ++++++++++++++++++++++++++--------- components/password-prompt.js | 72 ++++++++++++------------------ 2 files changed, 91 insertions(+), 65 deletions(-) diff --git a/components/app-wrapper.js b/components/app-wrapper.js index 4e8665b..31419b9 100644 --- a/components/app-wrapper.js +++ b/components/app-wrapper.js @@ -1,44 +1,86 @@ import React, { Component } from 'react' -import { View } from 'react-native' import nodejs from 'nodejs-mobile-react-native' + +import { getLicenseFlag, saveEncryptionFlag } from '../local-storage' +import { openDb } from '../db' + import App from './app' import PasswordPrompt from './password-prompt' import License from './license' -import { getLicenseFlag } from '../local-storage' +import AppLoadingView from './app-loading' export default class AppWrapper extends Component { constructor() { super() this.state = { - retrievingLicenseSetting: true + isCheckingLicenseAgreement: true, + shouldShowLicenseAgreement: false, + shouldShowPasswordPrompt: false, + shouldShowApp: false, } nodejs.start('main.js') this.checkLicenseAgreement() + this.checkDbPasswordSet() } async checkLicenseAgreement() { - const agreed = await getLicenseFlag() - this.setState({retrievingLicenseSetting: false}) - if (!agreed) this.setState({showLicense: true}) + const isLicenseFlagSet = await getLicenseFlag() + if (!isLicenseFlagSet) { + this.enableShowLicenseAgreement() + } else { + this.setState({ isCheckingLicenseAgreement: false }) + } + } + + async checkDbPasswordSet() { + const canConnectToDb = await openDb() + if (canConnectToDb) { + this.enableShowApp() + await saveEncryptionFlag(false) + return false + } + this.setState({ shouldShowPasswordPrompt: true }) + await saveEncryptionFlag(true) + } + + enableShowLicenseAgreement = () => { + this.setState({ + shouldShowLicenseAgreement: true, + isCheckingLicenseAgreement: false + }) + } + + disableShowLicenseAgreement = () => { + this.setState({ shouldShowLicenseAgreement: false }) + } + + enableShowApp = () => { + this.setState({ + shouldShowApp: true, + shouldShowPasswordPrompt: false + }) } render() { - const whiteScreen = - const licenseScreen = { - this.setState({showLicense: false}) - }}/> - const passwordPrompt = { - this.setState({showApp: true}) - }}/> + const { + isCheckingLicenseAgreement, + shouldShowLicenseAgreement, + shouldShowPasswordPrompt, + shouldShowApp, + } = this.state - if (this.state.retrievingLicenseSetting) { - return whiteScreen - } else if (this.state.showLicense) { - return licenseScreen - } else if (!this.state.showApp) { - return passwordPrompt - } else { - return + if (isCheckingLicenseAgreement) { + return } + + if (shouldShowLicenseAgreement) { + return + } + + if (shouldShowPasswordPrompt) { + return + } + + return shouldShowApp && } } \ No newline at end of file diff --git a/components/password-prompt.js b/components/password-prompt.js index 908bc68..356a699 100644 --- a/components/password-prompt.js +++ b/components/password-prompt.js @@ -20,19 +20,6 @@ export default class PasswordPrompt extends Component { this.passHashToDb, this ) - - this.tryToOpenDb() - } - - async tryToOpenDb() { - const connected = await openDb() - if (!connected) { - this.setState({ showPasswordPrompt: true }) - await saveEncryptionFlag(true) - return - } - await saveEncryptionFlag(false) - this.props.showApp() } passHashToDb = async hash => { @@ -48,7 +35,7 @@ export default class PasswordPrompt extends Component { ) return } - this.props.showApp() + this.props.enableShowApp() } confirmDeletion = async () => { @@ -72,7 +59,7 @@ export default class PasswordPrompt extends Component { onPress: async () => { await deleteDbAndOpenNew() await saveEncryptionFlag(false) - this.props.showApp() + this.props.enableShowApp() } }] ) @@ -89,35 +76,32 @@ export default class PasswordPrompt extends Component { return (
- {this.state.showPasswordPrompt && - - - this.setState({ password: val })} - style={styles.passwordPromptField} - secureTextEntry={true} - placeholder={labels.enterPassword} - /> - { - requestHash('check-pw', this.state.password) - }} - disabled={!this.state.password} - > - - {labels.title} - - - - - {labels.forgotPassword} - - - - } + + this.setState({ password: val })} + style={styles.passwordPromptField} + secureTextEntry={true} + placeholder={labels.enterPassword} + /> + { + requestHash('check-pw', this.state.password) + }} + disabled={!this.state.password} + > + + {labels.title} + + + + + {labels.forgotPassword} + + + ) }