Skip to content
Snippets Groups Projects
Commit 6065470b authored by Julia Friesel's avatar Julia Friesel
Browse files

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

Fixes reopenning after back button

Closes #388

See merge request bloodyhealth/drip!231
parents a152b497 e4209e28
No related branches found
No related tags found
No related merge requests found
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 = <View style={{ flex: 1 }}></View>
const licenseScreen = <License setLicense={() => {
this.setState({showLicense: false})
}}/>
const passwordPrompt = <PasswordPrompt showApp={() => {
this.setState({showApp: true})
}}/>
if (this.state.retrievingLicenseSetting) {
return whiteScreen
} else if (this.state.showLicense) {
return licenseScreen
} else if (!this.state.showApp) {
return passwordPrompt
} else {
return <App/>
const {
isCheckingLicenseAgreement,
shouldShowLicenseAgreement,
shouldShowPasswordPrompt,
shouldShowApp,
} = this.state
if (isCheckingLicenseAgreement) {
return <AppLoadingView />
}
if (shouldShowLicenseAgreement) {
return <License setLicense={this.disableShowLicenseAgreement}/>
}
if (shouldShowPasswordPrompt) {
return <PasswordPrompt enableShowApp={this.enableShowApp} />
}
return shouldShowApp && <App />
}
}
\ No newline at end of file
......@@ -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 }
......
......@@ -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 (
<View flex={1}>
<Header title={menuTitles.PasswordPrompt.toLowerCase()} />
{this.state.showPasswordPrompt &&
<View style={styles.passwordPromptPage}>
<TextInput
onChangeText={val => this.setState({ password: val })}
style={styles.passwordPromptField}
secureTextEntry={true}
placeholder={labels.enterPassword}
/>
<TouchableOpacity
style={styles.passwordPromptButton}
onPress={() => {
requestHash('check-pw', this.state.password)
}}
disabled={!this.state.password}
>
<AppText style={styles.passwordPromptButtonText}>
{labels.title}
</AppText>
</TouchableOpacity>
<TouchableOpacity
onPress={this.confirmDeletion}
>
<AppText style={styles.passwordPromptForgotPasswordText}>
{labels.forgotPassword}
</AppText>
</TouchableOpacity>
</View>
}
<View style={styles.passwordPromptPage}>
<TextInput
onChangeText={val => this.setState({ password: val })}
style={styles.passwordPromptField}
secureTextEntry={true}
placeholder={labels.enterPassword}
/>
<TouchableOpacity
style={styles.passwordPromptButton}
onPress={() => {
requestHash('check-pw', this.state.password)
}}
disabled={!this.state.password}
>
<AppText style={styles.passwordPromptButtonText}>
{labels.title}
</AppText>
</TouchableOpacity>
<TouchableOpacity
onPress={this.confirmDeletion}
>
<AppText style={styles.passwordPromptForgotPasswordText}>
{labels.forgotPassword}
</AppText>
</TouchableOpacity>
</View>
</View>
)
}
......
......@@ -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)
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment