Add wrapper for password screen and logged-in app

This commit is contained in:
Julia Friesel
2018-09-14 09:19:31 +02:00
parent 467dc8d424
commit 6c49d8a36c
3 changed files with 67 additions and 45 deletions
+26
View File
@@ -0,0 +1,26 @@
import React, { Component } from 'react'
import { View } from 'react-native'
import nodejs from 'nodejs-mobile-react-native'
import App from './app'
import PasswordPrompt from './password-prompt'
export default class AppWrapper extends Component {
constructor() {
super()
this.state = {}
nodejs.start('main.js')
}
render() {
return (
<View style={{ flex: 1 }}>
{this.state.showApp ?
<App/>
:
<PasswordPrompt
onCorrectPassword={() => this.setState({showApp: true})}
/>
}
</View>
)
}
}
+14 -18
View File
@@ -5,25 +5,25 @@ import { AppText } from './app-text'
import { hasEncryptionObservable } from '../local-storage' import { hasEncryptionObservable } from '../local-storage'
import styles from '../styles' import styles from '../styles'
import { passwordPrompt, shared } from './labels' import { passwordPrompt, shared } from './labels'
import { openDbConnection, requestHash, deleteDbAndOpenNew, openDb } from '../db' import { requestHash, deleteDbAndOpenNew, openDb } from '../db'
import App from './app'
export default class PasswordPrompt extends Component { export default class PasswordPrompt extends Component {
constructor() { constructor(props) {
super() super(props)
this.state = { this.state = {
password: null password: null
} }
hasEncryptionObservable.once((hasEncryption) => { hasEncryptionObservable.once(async hasEncryption => {
hasEncryption = JSON.parse(hasEncryption)
if (hasEncryption) { if (hasEncryption) {
this.setState({showPasswordPrompt: true}) this.setState({showPasswordPrompt: true})
} else { } else {
openDbConnection('something-wrong') await openDb({persistConnection: true})
this.setState({showApp: true}) console.log(this.props)
this.props.onCorrectPassword()
} }
}) })
nodejs.start('main.js')
nodejs.channel.addListener( nodejs.channel.addListener(
'message', 'message',
this.passHashToDb, this.passHashToDb,
@@ -35,8 +35,8 @@ export default class PasswordPrompt extends Component {
msg = JSON.parse(msg) msg = JSON.parse(msg)
if (msg.type != 'sha512') return if (msg.type != 'sha512') return
try { try {
console.log('password prompt opening db')
await openDb({hash: msg.message, persistConnection: true }) await openDb({hash: msg.message, persistConnection: true })
this.setState({ showApp: true })
} catch (err) { } catch (err) {
Alert.alert( Alert.alert(
shared.incorrectPassword, shared.incorrectPassword,
@@ -46,7 +46,9 @@ export default class PasswordPrompt extends Component {
onPress: () => this.setState({password: null}) onPress: () => this.setState({password: null})
}] }]
) )
return
} }
this.setState({ showApp: true })
} }
componentWillUnmount() { componentWillUnmount() {
@@ -55,15 +57,11 @@ export default class PasswordPrompt extends Component {
render() { render() {
return ( return (
<View style={{ flex: 1 }}>
{this.state.showApp ?
<App password={this.state.password}/>
:
<View style={styles.passwordPrompt}> <View style={styles.passwordPrompt}>
{this.state.showPasswordPrompt && {this.state.showPasswordPrompt &&
<View> <View>
<TextInput <TextInput
onChangeText={val => this.setState({password: val})} onChangeText={val => this.setState({ password: val })}
style={styles.passwordField} style={styles.passwordField}
/> />
<TouchableOpacity <TouchableOpacity
@@ -73,14 +71,14 @@ export default class PasswordPrompt extends Component {
}} }}
> >
<AppText style={styles.settingsButtonText}> <AppText style={styles.settingsButtonText}>
{ passwordPrompt.title } {passwordPrompt.title}
</AppText> </AppText>
</TouchableOpacity> </TouchableOpacity>
<TouchableOpacity <TouchableOpacity
style={styles.settingsButton} style={styles.settingsButton}
onPress={async () => { onPress={async () => {
await deleteDbAndOpenNew() await deleteDbAndOpenNew()
this.setState({showApp: true}) this.setState({ showApp: true })
}} }}
> >
<AppText style={styles.settingsButtonText}> <AppText style={styles.settingsButtonText}>
@@ -90,8 +88,6 @@ export default class PasswordPrompt extends Component {
</View> </View>
} }
</View> </View>
}
</View>
) )
} }
} }
+2 -2
View File
@@ -1,4 +1,4 @@
import { AppRegistry } from 'react-native' import { AppRegistry } from 'react-native'
import PasswordPrompt from './components/password-prompt' import AppWrapper from './components/app-wrapper'
AppRegistry.registerComponent('home', () => PasswordPrompt) AppRegistry.registerComponent('home', () => AppWrapper)