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) {
initialView = <PasswordPrompt enableShowApp={this.enableShowApp} />
} else if (shouldShowApp) {
initialView = <App />
initialView = <App restartApp={() => this.checkDbPasswordSet()} />
}
return (
+2 -2
View File
@@ -53,7 +53,7 @@ class App extends Component {
}
render() {
const { date, navigation, goBack } = this.props
const { date, navigation, goBack, restartApp } = this.props
const { currentPage } = navigation
if (!currentPage) {
@@ -80,7 +80,7 @@ class App extends Component {
return (
<View style={styles.container}>
<Header {...headerProps} />
<Page {...pageProps} />
<Page {...pageProps} restartApp={restartApp} />
<Menu />
</View>
)
+5 -1
View File
@@ -34,7 +34,11 @@ export default class CreatePassword extends Component {
</Button>
)
} else {
return <EnterNewPassword />
return (
<EnterNewPassword
changeEncryptionAndRestart={this.props.changeEncryptionAndRestart}
/>
)
}
}
}
+2 -7
View File
@@ -4,13 +4,12 @@ import PropTypes from 'prop-types'
import Button from '../../common/button'
import ConfirmWithPassword from '../common/confirm-with-password'
import { changeEncryptionAndRestartApp } from '../../../db'
import labels from '../../../i18n/en/settings'
export default class DeletePassword extends Component {
static propTypes = {
onStartDelete: PropTypes.func,
onCancelDelete: PropTypes.func
onCancelDelete: PropTypes.func,
}
constructor() {
@@ -24,10 +23,6 @@ export default class DeletePassword extends Component {
this.props.onStartDelete()
}
startDeletePassword = async () => {
await changeEncryptionAndRestartApp()
}
cancelConfirmationWithPassword = () => {
this.setState({ enteringCurrentPassword: false })
this.props.onCancelDelete()
@@ -39,7 +34,7 @@ export default class DeletePassword extends Component {
if (enteringCurrentPassword) {
return (
<ConfirmWithPassword
onSuccess={this.startDeletePassword}
onSuccess={this.props.changeEncryptionAndRestart}
onCancel={this.cancelConfirmationWithPassword}
/>
)
@@ -6,14 +6,14 @@ import AppText from '../../common/app-text'
import AppTextInput from '../../common/app-text-input'
import Button from '../../common/button'
import { requestHash, changeEncryptionAndRestartApp } from '../../../db'
import { requestHash } from '../../../db'
import { Colors, Spacing } from '../../../styles'
import settings from '../../../i18n/en/settings'
const LISTENER_TYPE = 'create-or-change-pw'
export default class EnterNewPassword extends Component {
constructor() {
constructor(props) {
super()
this.state = {
password: '',
@@ -22,13 +22,16 @@ export default class EnterNewPassword extends Component {
}
nodejs.channel.addListener(
LISTENER_TYPE,
changeEncryptionAndRestartApp,
props.changeEncryptionAndRestart,
this
)
}
componentWillUnmount() {
nodejs.channel.removeListener(LISTENER_TYPE, changeEncryptionAndRestartApp)
nodejs.channel.removeListener(
LISTENER_TYPE,
this.props.changeEncryptionAndRestart
)
}
savePassword = () => {
+27 -2
View File
@@ -1,4 +1,9 @@
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 AppText from '../../common/app-text'
@@ -11,7 +16,7 @@ import DeletePassword from './delete'
import { hasEncryptionObservable } from '../../../local-storage'
import labels from '../../../i18n/en/settings'
export default class PasswordSetting extends Component {
class PasswordSetting extends Component {
constructor(props) {
super(props)
@@ -38,6 +43,12 @@ export default class PasswordSetting extends Component {
this.setState({ isDeletingPassword: false })
}
changeEncryptionAndRestart = async (hash) => {
await changeDbEncryption(hash)
await this.props.restartApp()
this.props.navigate('Home')
}
render() {
const { isPasswordSet, isChangingPassword, isDeletingPassword } = this.state
@@ -51,12 +62,17 @@ export default class PasswordSetting extends Component {
{isPasswordSet ? explainerEnabled : explainerDisabled}
</AppText>
{!isPasswordSet && <CreatePassword />}
{!isPasswordSet && (
<CreatePassword
changeEncryptionAndRestart={this.changeEncryptionAndRestart}
/>
)}
{isPasswordSet && !isDeletingPassword && (
<ChangePassword
onStartChange={this.onChangingPassword}
onCancelChange={this.onCancelChangingPassword}
changeEncryptionAndRestart={this.changeEncryptionAndRestart}
/>
)}
@@ -64,6 +80,7 @@ export default class PasswordSetting extends Component {
<DeletePassword
onStartDelete={this.onDeletingPassword}
onCancelDelete={this.onCancelDeletingPassword}
changeEncryptionAndRestart={this.changeEncryptionAndRestart}
/>
)}
</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) {
return <EnterNewPassword />
return (
<EnterNewPassword
changeEncryptionAndRestart={this.props.changeEncryptionAndRestart}
/>
)
}
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
if (hash) key = hashToInt8Array(hash)
const defaultPath = db.path
@@ -209,7 +209,6 @@ export async function changeEncryptionAndRestartApp(hash) {
db.close()
await fs.unlink(defaultPath)
await fs.moveFile(copyPath, defaultPath)
restartApp()
}
export function isDbEmpty() {