Merge branch 'reopening-after-back-fix' into 'master'

Fixes reopenning after back button

Closes #388

See merge request bloodyhealth/drip!231
This commit is contained in:
Julia Friesel
2019-06-19 07:54:47 +00:00
4 changed files with 100 additions and 66 deletions
+63 -21
View File
@@ -1,44 +1,86 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import { View } from 'react-native'
import nodejs from 'nodejs-mobile-react-native' import nodejs from 'nodejs-mobile-react-native'
import { getLicenseFlag, saveEncryptionFlag } from '../local-storage'
import { openDb } from '../db'
import App from './app' import App from './app'
import PasswordPrompt from './password-prompt' import PasswordPrompt from './password-prompt'
import License from './license' import License from './license'
import { getLicenseFlag } from '../local-storage' import AppLoadingView from './app-loading'
export default class AppWrapper extends Component { export default class AppWrapper extends Component {
constructor() { constructor() {
super() super()
this.state = { this.state = {
retrievingLicenseSetting: true isCheckingLicenseAgreement: true,
shouldShowLicenseAgreement: false,
shouldShowPasswordPrompt: false,
shouldShowApp: false,
} }
nodejs.start('main.js') nodejs.start('main.js')
this.checkLicenseAgreement() this.checkLicenseAgreement()
this.checkDbPasswordSet()
} }
async checkLicenseAgreement() { async checkLicenseAgreement() {
const agreed = await getLicenseFlag() const isLicenseFlagSet = await getLicenseFlag()
this.setState({retrievingLicenseSetting: false}) if (!isLicenseFlagSet) {
if (!agreed) this.setState({showLicense: true}) 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() { render() {
const whiteScreen = <View style={{ flex: 1 }}></View> const {
const licenseScreen = <License setLicense={() => { isCheckingLicenseAgreement,
this.setState({showLicense: false}) shouldShowLicenseAgreement,
}}/> shouldShowPasswordPrompt,
const passwordPrompt = <PasswordPrompt showApp={() => { shouldShowApp,
this.setState({showApp: true}) } = this.state
}}/>
if (this.state.retrievingLicenseSetting) { if (isCheckingLicenseAgreement) {
return whiteScreen return <AppLoadingView />
} else if (this.state.showLicense) {
return licenseScreen
} else if (!this.state.showApp) {
return passwordPrompt
} else {
return <App/>
} }
if (shouldShowLicenseAgreement) {
return <License setLicense={this.disableShowLicenseAgreement}/>
}
if (shouldShowPasswordPrompt) {
return <PasswordPrompt enableShowApp={this.enableShowApp} />
}
return shouldShowApp && <App />
} }
} }
+5 -1
View File
@@ -12,6 +12,7 @@ import settingsViews from './settings'
import Stats from './stats' import Stats from './stats'
import {headerTitles, menuTitles} from '../i18n/en/labels' import {headerTitles, menuTitles} from '../i18n/en/labels'
import setupNotifications from '../lib/notifications' import setupNotifications from '../lib/notifications'
import { closeDb } from '../db'
// design wants everyhting lowercased, but we don't // design wants everyhting lowercased, but we don't
// have CSS pseudo properties // have CSS pseudo properties
@@ -55,7 +56,10 @@ export default class App extends Component {
handleBackButtonPress = () => { handleBackButtonPress = () => {
const { currentPage, currentProps } = this.state const { currentPage, currentProps } = this.state
if (currentPage === HOME_PAGE) return false if (currentPage === HOME_PAGE) {
closeDb()
return false
}
if (this.isSymptomView()) { if (this.isSymptomView()) {
this.navigate( this.navigate(
this.originForSymptomView, { date: currentProps.date } this.originForSymptomView, { date: currentProps.date }
+2 -18
View File
@@ -20,19 +20,6 @@ export default class PasswordPrompt extends Component {
this.passHashToDb, this.passHashToDb,
this 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 => { passHashToDb = async hash => {
@@ -48,7 +35,7 @@ export default class PasswordPrompt extends Component {
) )
return return
} }
this.props.showApp() this.props.enableShowApp()
} }
confirmDeletion = async () => { confirmDeletion = async () => {
@@ -72,7 +59,7 @@ export default class PasswordPrompt extends Component {
onPress: async () => { onPress: async () => {
await deleteDbAndOpenNew() await deleteDbAndOpenNew()
await saveEncryptionFlag(false) await saveEncryptionFlag(false)
this.props.showApp() this.props.enableShowApp()
} }
}] }]
) )
@@ -89,9 +76,7 @@ export default class PasswordPrompt extends Component {
return ( return (
<View flex={1}> <View flex={1}>
<Header title={menuTitles.PasswordPrompt.toLowerCase()} /> <Header title={menuTitles.PasswordPrompt.toLowerCase()} />
{this.state.showPasswordPrompt &&
<View style={styles.passwordPromptPage}> <View style={styles.passwordPromptPage}>
<TextInput <TextInput
onChangeText={val => this.setState({ password: val })} onChangeText={val => this.setState({ password: val })}
style={styles.passwordPromptField} style={styles.passwordPromptField}
@@ -117,7 +102,6 @@ export default class PasswordPrompt extends Component {
</AppText> </AppText>
</TouchableOpacity> </TouchableOpacity>
</View> </View>
}
</View> </View>
) )
} }
+4
View File
@@ -55,6 +55,10 @@ export async function openDb (hash) {
return true return true
} }
export function closeDb() {
db.close()
}
export function getBleedingDaysSortedByDate() { export function getBleedingDaysSortedByDate() {
return db.objects('CycleDay').filtered('bleeding != null').sorted('date', true) return db.objects('CycleDay').filtered('bleeding != null').sorted('date', true)
} }