Reset app state without closing/restarting it

This commit is contained in:
Sofiya Tepikin
2022-08-10 15:40:48 +02:00
parent 0ec23219bf
commit 08b0dc7734
8 changed files with 51 additions and 21 deletions
+1 -1
View File
@@ -83,7 +83,7 @@ export default class AppWrapper extends Component {
} else if (shouldShowPasswordPrompt) { } else if (shouldShowPasswordPrompt) {
initialView = <PasswordPrompt enableShowApp={this.enableShowApp} /> initialView = <PasswordPrompt enableShowApp={this.enableShowApp} />
} else if (shouldShowApp) { } else if (shouldShowApp) {
initialView = <App /> initialView = <App restartApp={() => this.checkDbPasswordSet()} />
} }
return ( return (
+2 -2
View File
@@ -53,7 +53,7 @@ class App extends Component {
} }
render() { render() {
const { date, navigation, goBack } = this.props const { date, navigation, goBack, restartApp } = this.props
const { currentPage } = navigation const { currentPage } = navigation
if (!currentPage) { if (!currentPage) {
@@ -80,7 +80,7 @@ class App extends Component {
return ( return (
<View style={styles.container}> <View style={styles.container}>
<Header {...headerProps} /> <Header {...headerProps} />
<Page {...pageProps} /> <Page {...pageProps} restartApp={restartApp} />
<Menu /> <Menu />
</View> </View>
) )
+5 -1
View File
@@ -34,7 +34,11 @@ export default class CreatePassword extends Component {
</Button> </Button>
) )
} else { } else {
return <EnterNewPassword /> return (
<EnterNewPassword
changeEncryptionAndRestart={this.props.changeEncryptionAndRestart}
/>
)
} }
} }
} }
+3 -8
View File
@@ -4,13 +4,12 @@ import PropTypes from 'prop-types'
import Button from '../../common/button' import Button from '../../common/button'
import ConfirmWithPassword from '../common/confirm-with-password' import ConfirmWithPassword from '../common/confirm-with-password'
import { changeEncryptionAndRestartApp } from '../../../db'
import labels from '../../../i18n/en/settings' import labels from '../../../i18n/en/settings'
export default class DeletePassword extends Component { export default class DeletePassword extends Component {
static propTypes = { static propTypes = {
onStartDelete: PropTypes.func, onStartDelete: PropTypes.func,
onCancelDelete: PropTypes.func onCancelDelete: PropTypes.func,
} }
constructor() { constructor() {
@@ -24,10 +23,6 @@ export default class DeletePassword extends Component {
this.props.onStartDelete() this.props.onStartDelete()
} }
startDeletePassword = async () => {
await changeEncryptionAndRestartApp()
}
cancelConfirmationWithPassword = () => { cancelConfirmationWithPassword = () => {
this.setState({ enteringCurrentPassword: false }) this.setState({ enteringCurrentPassword: false })
this.props.onCancelDelete() this.props.onCancelDelete()
@@ -39,7 +34,7 @@ export default class DeletePassword extends Component {
if (enteringCurrentPassword) { if (enteringCurrentPassword) {
return ( return (
<ConfirmWithPassword <ConfirmWithPassword
onSuccess={this.startDeletePassword} onSuccess={this.props.changeEncryptionAndRestart}
onCancel={this.cancelConfirmationWithPassword} onCancel={this.cancelConfirmationWithPassword}
/> />
) )
@@ -51,4 +46,4 @@ export default class DeletePassword extends Component {
</Button> </Button>
) )
} }
} }
@@ -6,14 +6,14 @@ import AppText from '../../common/app-text'
import AppTextInput from '../../common/app-text-input' import AppTextInput from '../../common/app-text-input'
import Button from '../../common/button' import Button from '../../common/button'
import { requestHash, changeEncryptionAndRestartApp } from '../../../db' import { requestHash } from '../../../db'
import { Colors, Spacing } from '../../../styles' import { Colors, Spacing } from '../../../styles'
import settings from '../../../i18n/en/settings' import settings from '../../../i18n/en/settings'
const LISTENER_TYPE = 'create-or-change-pw' const LISTENER_TYPE = 'create-or-change-pw'
export default class EnterNewPassword extends Component { export default class EnterNewPassword extends Component {
constructor() { constructor(props) {
super() super()
this.state = { this.state = {
password: '', password: '',
@@ -22,13 +22,16 @@ export default class EnterNewPassword extends Component {
} }
nodejs.channel.addListener( nodejs.channel.addListener(
LISTENER_TYPE, LISTENER_TYPE,
changeEncryptionAndRestartApp, props.changeEncryptionAndRestart,
this this
) )
} }
componentWillUnmount() { componentWillUnmount() {
nodejs.channel.removeListener(LISTENER_TYPE, changeEncryptionAndRestartApp) nodejs.channel.removeListener(
LISTENER_TYPE,
this.props.changeEncryptionAndRestart
)
} }
savePassword = () => { savePassword = () => {
+27 -2
View File
@@ -1,4 +1,9 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import { connect } from 'react-redux'
import { navigate } from '../../../slices/navigation'
import { changeDbEncryption } from '../../../db'
import AppPage from '../../common/app-page' import AppPage from '../../common/app-page'
import AppText from '../../common/app-text' import AppText from '../../common/app-text'
@@ -11,7 +16,7 @@ import DeletePassword from './delete'
import { hasEncryptionObservable } from '../../../local-storage' import { hasEncryptionObservable } from '../../../local-storage'
import labels from '../../../i18n/en/settings' import labels from '../../../i18n/en/settings'
export default class PasswordSetting extends Component { class PasswordSetting extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
@@ -38,6 +43,12 @@ export default class PasswordSetting extends Component {
this.setState({ isDeletingPassword: false }) this.setState({ isDeletingPassword: false })
} }
changeEncryptionAndRestart = async (hash) => {
await changeDbEncryption(hash)
await this.props.restartApp()
this.props.navigate('Home')
}
render() { render() {
const { isPasswordSet, isChangingPassword, isDeletingPassword } = this.state const { isPasswordSet, isChangingPassword, isDeletingPassword } = this.state
@@ -51,12 +62,17 @@ export default class PasswordSetting extends Component {
{isPasswordSet ? explainerEnabled : explainerDisabled} {isPasswordSet ? explainerEnabled : explainerDisabled}
</AppText> </AppText>
{!isPasswordSet && <CreatePassword />} {!isPasswordSet && (
<CreatePassword
changeEncryptionAndRestart={this.changeEncryptionAndRestart}
/>
)}
{isPasswordSet && !isDeletingPassword && ( {isPasswordSet && !isDeletingPassword && (
<ChangePassword <ChangePassword
onStartChange={this.onChangingPassword} onStartChange={this.onChangingPassword}
onCancelChange={this.onCancelChangingPassword} onCancelChange={this.onCancelChangingPassword}
changeEncryptionAndRestart={this.changeEncryptionAndRestart}
/> />
)} )}
@@ -64,6 +80,7 @@ export default class PasswordSetting extends Component {
<DeletePassword <DeletePassword
onStartDelete={this.onDeletingPassword} onStartDelete={this.onDeletingPassword}
onCancelDelete={this.onCancelDeletingPassword} onCancelDelete={this.onCancelDeletingPassword}
changeEncryptionAndRestart={this.changeEncryptionAndRestart}
/> />
)} )}
</Segment> </Segment>
@@ -71,3 +88,11 @@ export default class PasswordSetting extends Component {
) )
} }
} }
const mapDispatchToProps = (dispatch) => {
return {
navigate: (page) => dispatch(navigate(page)),
}
}
export default connect(null, mapDispatchToProps)(PasswordSetting)
+5 -1
View File
@@ -70,7 +70,11 @@ export default class ChangePassword extends Component {
} }
if (enteringNewPassword) { if (enteringNewPassword) {
return <EnterNewPassword /> return (
<EnterNewPassword
changeEncryptionAndRestart={this.props.changeEncryptionAndRestart}
/>
)
} }
return ( return (
+1 -2
View File
@@ -195,7 +195,7 @@ export function requestHash(type, pw) {
) )
} }
export async function changeEncryptionAndRestartApp(hash) { export async function changeDbEncryption(hash) {
let key let key
if (hash) key = hashToInt8Array(hash) if (hash) key = hashToInt8Array(hash)
const defaultPath = db.path const defaultPath = db.path
@@ -209,7 +209,6 @@ export async function changeEncryptionAndRestartApp(hash) {
db.close() db.close()
await fs.unlink(defaultPath) await fs.unlink(defaultPath)
await fs.moveFile(copyPath, defaultPath) await fs.moveFile(copyPath, defaultPath)
restartApp()
} }
export function isDbEmpty() { export function isDbEmpty() {