adds delete app data functionality
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
import { Alert } from 'react-native'
|
||||
import { shared as sharedLabels } from '../../../i18n/en/labels'
|
||||
import labels from '../../../i18n/en/settings'
|
||||
|
||||
export default function showDeleteDialog(okHandler) {
|
||||
|
||||
const { question, message, confirmation } = labels.deleteSegment
|
||||
|
||||
Alert.alert(
|
||||
question,
|
||||
message,
|
||||
[{
|
||||
text: confirmation,
|
||||
onPress: okHandler
|
||||
}, {
|
||||
text: sharedLabels.cancel, style: 'cancel', onPress: () => { }
|
||||
}]
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
import React, { Component } from 'react'
|
||||
import { View } from 'react-native'
|
||||
import nodejs from 'nodejs-mobile-react-native'
|
||||
import RNFS from 'react-native-fs'
|
||||
|
||||
import settings from '../../../i18n/en/settings'
|
||||
import { requestHash, clearDb } from '../../../db'
|
||||
import { hasEncryptionObservable } from '../../../local-storage'
|
||||
import PasswordField from '../password/password-field'
|
||||
import SettingsButton from '../settings-button'
|
||||
import showDeleteDialog from './delete-data-dialog'
|
||||
import checkCurrentPassword from '../password/check-current-password'
|
||||
import alertError from '../alert-error'
|
||||
|
||||
|
||||
export default class DeleteData extends Component {
|
||||
constructor() {
|
||||
super()
|
||||
this.state = {
|
||||
isPasswordSet: hasEncryptionObservable.value,
|
||||
currentPassword: null,
|
||||
enteringCurrentPassword: false
|
||||
}
|
||||
|
||||
nodejs.channel.addListener(
|
||||
'pre-change-pw-check',
|
||||
this.openNewPasswordField,
|
||||
this
|
||||
)
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
nodejs.channel.removeListener('pre-change-pw-check', this.openNewPasswordField)
|
||||
}
|
||||
|
||||
openNewPasswordField = async hash => {
|
||||
const passwordCorrect = await checkCurrentPassword({
|
||||
hash,
|
||||
onTryAgain: () => this.setState({ currentPassword: null }),
|
||||
onCancel: () => this.setState({
|
||||
enteringCurrentPassword: false,
|
||||
currentPassword: null
|
||||
})
|
||||
})
|
||||
|
||||
if (passwordCorrect) {
|
||||
await this.deleteAppData()
|
||||
}
|
||||
}
|
||||
|
||||
startDataDeletion = () => {
|
||||
showDeleteDialog(() => {
|
||||
if (this.state.isPasswordSet) {
|
||||
this.setState({ enteringCurrentPassword: true })
|
||||
} else {
|
||||
this.deleteAppData()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
deleteExportedFile = async () => {
|
||||
const path = RNFS.DocumentDirectoryPath + '/data.csv'
|
||||
const isFileExist = await RNFS.exists(path)
|
||||
if (isFileExist) {
|
||||
await RNFS.unlink(path)
|
||||
}
|
||||
}
|
||||
|
||||
deleteAppData = async () => {
|
||||
const { errors } = settings.deleteSegment
|
||||
try {
|
||||
await clearDb()
|
||||
await this.deleteExportedFile()
|
||||
this.props.onDeleteData()
|
||||
} catch (err) {
|
||||
return alertError(errors.couldNotDeleteFile)
|
||||
}
|
||||
}
|
||||
|
||||
handleCurrentPasswordInput = (currentPassword) => {
|
||||
this.setState({ currentPassword })
|
||||
}
|
||||
|
||||
checkCurrentPassword = () => {
|
||||
requestHash('pre-change-pw-check', this.state.currentPassword)
|
||||
}
|
||||
|
||||
render() {
|
||||
|
||||
const {
|
||||
enteringCurrentPassword,
|
||||
currentPassword
|
||||
} = this.state
|
||||
|
||||
const labels = settings.passwordSettings
|
||||
|
||||
if (enteringCurrentPassword) {
|
||||
return (
|
||||
<View>
|
||||
<PasswordField
|
||||
placeholder={labels.enterCurrent}
|
||||
value={currentPassword}
|
||||
onChangeText={this.handleCurrentPasswordInput}
|
||||
/>
|
||||
<SettingsButton
|
||||
onPress={this.checkCurrentPassword}
|
||||
disabled={!currentPassword}
|
||||
>
|
||||
{settings.deleteSegment.title}
|
||||
</SettingsButton>
|
||||
</View>
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<View>
|
||||
<SettingsButton
|
||||
onPress={this.startDataDeletion}
|
||||
disabled={currentPassword}
|
||||
>
|
||||
{settings.deleteSegment.title}
|
||||
</SettingsButton>
|
||||
</View>
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -1,35 +1,51 @@
|
||||
import React from 'react'
|
||||
import React, { Component } from 'react'
|
||||
import { ScrollView } from 'react-native'
|
||||
import labels from '../../../i18n/en/settings'
|
||||
import AppText from '../../app-text'
|
||||
import SettingsSegment from '../settings-segment'
|
||||
import SettingsButton from '../settings-button'
|
||||
import openImportDialogAndImport from './import-dialog'
|
||||
import openShareDialogAndExport from './export-dialog'
|
||||
import { isDbEmpty } from '../../../db'
|
||||
import DeleteData from './delete-data'
|
||||
import labels from '../../../i18n/en/settings'
|
||||
|
||||
const Settings = () => {
|
||||
return (
|
||||
<ScrollView>
|
||||
<SettingsSegment title={labels.export.button}>
|
||||
<AppText>{labels.export.segmentExplainer}</AppText>
|
||||
<SettingsButton onPress={openShareDialogAndExport}>
|
||||
{labels.export.button}
|
||||
</SettingsButton>
|
||||
</SettingsSegment>
|
||||
<SettingsSegment title={labels.import.button}>
|
||||
<AppText>{labels.import.segmentExplainer}</AppText>
|
||||
<SettingsButton onPress={openImportDialogAndImport}>
|
||||
{labels.import.button}
|
||||
</SettingsButton>
|
||||
</SettingsSegment>
|
||||
<SettingsSegment title={labels.deleteSegment.title}>
|
||||
<AppText>{labels.deleteSegment.explainer}</AppText>
|
||||
<SettingsButton onPress={openImportDialogAndImport}>
|
||||
{labels.deleteSegment.title}
|
||||
</SettingsButton>
|
||||
</SettingsSegment>
|
||||
</ScrollView>
|
||||
)
|
||||
class DataManagement extends Component {
|
||||
constructor() {
|
||||
super()
|
||||
this.state = {
|
||||
isDataEmpty: isDbEmpty()
|
||||
}
|
||||
}
|
||||
onDeleteData = () => {
|
||||
this.setState({
|
||||
isDataEmpty: true
|
||||
})
|
||||
}
|
||||
render() {
|
||||
return (
|
||||
<ScrollView>
|
||||
<SettingsSegment title={labels.export.button}>
|
||||
<AppText>{labels.export.segmentExplainer}</AppText>
|
||||
<SettingsButton onPress={openShareDialogAndExport}>
|
||||
{labels.export.button}
|
||||
</SettingsButton>
|
||||
</SettingsSegment>
|
||||
<SettingsSegment title={labels.import.button}>
|
||||
<AppText>{labels.import.segmentExplainer}</AppText>
|
||||
<SettingsButton onPress={openImportDialogAndImport}>
|
||||
{labels.import.button}
|
||||
</SettingsButton>
|
||||
</SettingsSegment>
|
||||
{!isDbEmpty() && (
|
||||
<SettingsSegment title={labels.deleteSegment.title}>
|
||||
<AppText>{labels.deleteSegment.explainer}</AppText>
|
||||
<DeleteData onDeleteData={this.onDeleteData} />
|
||||
</SettingsSegment>
|
||||
)}
|
||||
</ScrollView>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default Settings
|
||||
|
||||
export default DataManagement
|
||||
|
||||
@@ -8,7 +8,6 @@ import AppText from '../../app-text'
|
||||
import {
|
||||
hasEncryptionObservable
|
||||
} from '../../../local-storage'
|
||||
import styles from '../../../styles/index'
|
||||
import labels from '../../../i18n/en/settings'
|
||||
|
||||
export default class PasswordSetting extends Component {
|
||||
|
||||
+10
@@ -223,12 +223,22 @@ export async function changeEncryptionAndRestartApp(hash) {
|
||||
restart.Restart()
|
||||
}
|
||||
|
||||
export function isDbEmpty () {
|
||||
return db.empty
|
||||
}
|
||||
|
||||
export async function deleteDbAndOpenNew() {
|
||||
const exists = await fs.exists(Realm.defaultPath)
|
||||
if (exists) await fs.unlink(Realm.defaultPath)
|
||||
await openDb()
|
||||
}
|
||||
|
||||
export async function clearDb() {
|
||||
await db.write(async () => {
|
||||
await db.deleteAll()
|
||||
})
|
||||
}
|
||||
|
||||
function hashToInt8Array(hash) {
|
||||
const key = new Uint8Array(64)
|
||||
for (let i = 0; i < key.length; i++) {
|
||||
|
||||
+8
-1
@@ -37,7 +37,14 @@ export default {
|
||||
},
|
||||
deleteSegment: {
|
||||
title: 'Delete app data',
|
||||
explainer: 'Delete all app data from this phone'
|
||||
explainer: 'Delete all app data from this phone',
|
||||
question: 'Do you want to delete data from this phone?',
|
||||
message: 'Please note that deletion of the data is permanent and irreversible. We recomend to export existing data before deletion.',
|
||||
confirmation: 'Yes, I want to delete data permanently',
|
||||
errors: {
|
||||
couldNotDeleteFile: 'Could not delete file',
|
||||
postFix: 'No data was deleted or changed'
|
||||
}
|
||||
},
|
||||
tempScale: {
|
||||
segmentTitle: 'Temperature scale',
|
||||
|
||||
Reference in New Issue
Block a user