diff --git a/components/home.js b/components/home.js index 59d5340..025a77b 100644 --- a/components/home.js +++ b/components/home.js @@ -6,10 +6,9 @@ import { ScrollView } from 'react-native' import { LocalDate, ChronoUnit } from 'js-joda' -import nodejs from 'nodejs-mobile-react-native' import styles from '../styles/index' import cycleModule from '../lib/cycle' -import { requestHash, getOrCreateCycleDay, getBleedingDaysSortedByDate, fillWithMucusDummyData, fillWithCervixDummyData, changeEncryptionAndRestartApp } from '../db' +import { getOrCreateCycleDay, getBleedingDaysSortedByDate, fillWithMucusDummyData, fillWithCervixDummyData } from '../db' import {bleedingPrediction as labels} from './labels' export default class Home extends Component { @@ -35,22 +34,10 @@ export default class Home extends Component { })(this) getBleedingDaysSortedByDate().addListener(this.setStateWithCurrentText) - - this.startEncryption = async (msg) => { - msg = JSON.parse(msg) - changeEncryptionAndRestartApp(msg.message) - } - - nodejs.channel.addListener( - 'message', - this.startEncryption, - this - ) } componentWillUnmount() { getBleedingDaysSortedByDate().removeListener(this.setStateWithCurrentText) - nodejs.channel.removeListener('message', this.startEncryption) } passTodayToDayView() { @@ -84,14 +71,6 @@ export default class Home extends Component { title="fill with example data for cervix&temp"> - - - ) diff --git a/components/labels.js b/components/labels.js index d4b5876..15e922a 100644 --- a/components/labels.js +++ b/components/labels.js @@ -55,10 +55,12 @@ export const settings = { }, passwordSettings: { title: 'App password', - explainerDisabled: "Encrypt the app's database with a password. You have to enter the password every time the app is started.", + explainerDisabled: "Encrypt the app's database with a password. You need to enter the password every time the app is started.", explainerEnabled: "Password protection and database encryption is currently enabled", + setPassword: 'Set password', deletePassword: "Delete password", enterCurrent: "Please enter your current password", + enterNew: "Please enter a new password", backupReminderTitle: 'Have you made a backup of your data?', backupReminder: 'When you make changes to your password, we delete your old data and store it in a new version. To be safe, please backup your data using the export function before making changes to your password. Making any changes to your password setting will also restart the app immediately.', } diff --git a/components/settings/password-setting.js b/components/settings/password-setting.js index 7fc212f..be1b974 100644 --- a/components/settings/password-setting.js +++ b/components/settings/password-setting.js @@ -18,7 +18,7 @@ export default class PasswordSetting extends Component { constructor(props) { super(props) this.state = { - enabled: hasEncryptionObservable.value, + encryptionEnabled: hasEncryptionObservable.value, currentPassword: null, enteringCurrentPassword: false } @@ -38,8 +38,20 @@ export default class PasswordSetting extends Component { passHashToDb = async (msg) => { msg = JSON.parse(msg) if (msg.type != 'sha512') return + if (this.state.encryptionEnabled) { + await this.removeEncryption(msg.message) + } else if (!this.state.encryptionEnabled) { + await changeEncryptionAndRestartApp(msg.message) + } + } + + addEncryption = async hash => { + changeEncryptionAndRestartApp(hash) + } + + removeEncryption = async hash => { try { - await openDb({ hash: msg.message, persistConnection: false }) + await openDb({ hash, persistConnection: false }) } catch (err) { console.log(err) Alert.alert( @@ -69,7 +81,7 @@ export default class PasswordSetting extends Component { {labels.passwordSettings.title} - {this.state.enabled ? + {this.state.encryptionEnabled ? {labels.passwordSettings.explainerEnabled} : {labels.passwordSettings.explainerDisabled} @@ -90,30 +102,71 @@ export default class PasswordSetting extends Component { /> } - { - if (!this.state.enteringCurrentPassword) { - Alert.alert( - labels.passwordSettings.backupReminderTitle, - labels.passwordSettings.backupReminder, - [{ - text: shared.cancel, - style: 'cancel' - }, { - text: shared.ok, - onPress: () => this.setState({enteringCurrentPassword: true}) - }] - ) - } else { - requestHash(this.state.currentPassword) - } - }} - style={styles.settingsButton}> - - {labels.passwordSettings.deletePassword} - - + {this.state.encryptionEnabled && + { + if (!this.state.enteringCurrentPassword) { + showBackUpReminder(() => { + this.setState({ enteringCurrentPassword: true }) + }) + } else { + requestHash(this.state.currentPassword) + } + }} + style={styles.settingsButton}> + + {labels.passwordSettings.deletePassword} + + + } + + {this.state.enteringNewPassword && + + { + this.setState({ + newPassword: val + }) + }} + value={this.state.newPassword} + placeholder={labels.passwordSettings.enterNew} + secureTextEntry={true} + /> + + } + {!this.state.encryptionEnabled && + { + if (!this.state.enteringNewPassword) { + showBackUpReminder(() => { + this.setState({ enteringNewPassword: true }) + }) + } else { + requestHash(this.state.newPassword) + } + }} + style={styles.settingsButton}> + + {labels.passwordSettings.setPassword} + + + } ) } +} + +function showBackUpReminder(okHandler) { + Alert.alert( + labels.passwordSettings.backupReminderTitle, + labels.passwordSettings.backupReminder, + [{ + text: shared.cancel, + style: 'cancel' + }, { + text: shared.ok, + onPress: okHandler + }] + ) } \ No newline at end of file diff --git a/db/index.js b/db/index.js index 4ef5a4e..4173c1d 100644 --- a/db/index.js +++ b/db/index.js @@ -157,7 +157,7 @@ export function tryToImportWithoutDelete(cycleDays) { export function requestHash(pw) { nodejs.channel.send(JSON.stringify({ type: 'request-SHA512', - message: pw || 'mypassword' + message: pw })) }