Merge branch 'refactoring-settings-view' into 'master'

Refactoring settings view

Closes #275

See merge request bloodyhealth/drip!147
This commit is contained in:
Julia Friesel
2019-01-10 08:06:32 +00:00
24 changed files with 211 additions and 288 deletions
+4 -4
View File
@@ -2,7 +2,7 @@ import React, { Component } from 'react'
import { ScrollView } from 'react-native'
import Hyperlink from 'react-native-hyperlink'
import AppText from '../app-text'
import SettingsSegment from './settings-segment'
import SettingsSegment from './shared/settings-segment'
import styles from '../../styles/index'
import labels, { links } from '../../i18n/en/settings'
import replace from '../helpers/replace-url-with-text'
@@ -13,21 +13,21 @@ export default class AboutSection extends Component {
<ScrollView>
<SettingsSegment title={labels.aboutSection.title}>
<Hyperlink linkStyle={styles.link} linkText={replace}>
<AppText>{`${labels.aboutSection.text} `}</AppText>
<AppText>{labels.aboutSection.text}</AppText>
</Hyperlink>
</SettingsSegment>
<SettingsSegment title={labels.philosophy.title}>
<AppText>{labels.philosophy.text}</AppText>
</SettingsSegment>
<SettingsSegment title={labels.credits.title}>
<AppText>{`${labels.credits.note}`}</AppText>
<AppText>{labels.credits.note}</AppText>
</SettingsSegment>
<SettingsSegment title={labels.website.title}>
<Hyperlink linkStyle={styles.link}>
<AppText>{links.website.url}</AppText>
</Hyperlink>
</SettingsSegment>
<SettingsSegment title={labels.version.title} last={true}>
<SettingsSegment title={labels.version.title} last>
<AppText>{require('../../package.json').version}</AppText>
</SettingsSegment>
</ScrollView>
@@ -4,9 +4,9 @@ import { Alert, ToastAndroid } from 'react-native'
import { clearDb, isDbEmpty } from '../../../db'
import { hasEncryptionObservable } from '../../../local-storage'
import SettingsButton from '../settings-button'
import ConfirmWithPassword from './confirm-with-password'
import alertError from '../alert-error'
import SettingsButton from '../shared/settings-button'
import ConfirmWithPassword from '../shared/confirm-with-password'
import alertError from '../shared/alert-error'
import settings from '../../../i18n/en/settings'
import { shared as sharedLabels } from '../../../i18n/en/labels'
@@ -2,7 +2,7 @@ import Share from 'react-native-share'
import { getCycleDaysSortedByDate } from '../../../db'
import getDataAsCsvDataUri from '../../../lib/import-export/export-to-csv'
import alertError from '../alert-error'
import alertError from '../shared/alert-error'
import settings from '../../../i18n/en/settings'
import { EXPORT_FILE_NAME } from './constants'
import RNFS from 'react-native-fs'
@@ -4,7 +4,7 @@ import rnfs from 'react-native-fs'
import importCsv from '../../../lib/import-export/import-from-csv'
import { shared as sharedLabels } from '../../../i18n/en/labels'
import labels from '../../../i18n/en/settings'
import alertError from '../alert-error'
import alertError from '../shared/alert-error'
export default function openImportDialogAndImport() {
Alert.alert(
+3 -3
View File
@@ -1,8 +1,8 @@
import React from 'react'
import { ScrollView } from 'react-native'
import AppText from '../../app-text'
import SettingsSegment from '../settings-segment'
import SettingsButton from '../settings-button'
import SettingsSegment from '../shared/settings-segment'
import SettingsButton from '../shared/settings-button'
import openImportDialogAndImport from './import-dialog'
import openShareDialogAndExport from './export-dialog'
import DeleteData from './delete-data'
@@ -25,7 +25,7 @@ const DataManagement = () => {
</SettingsSegment>
<SettingsSegment
title={labels.deleteSegment.title}
last={true}
last
>
<AppText>{labels.deleteSegment.explainer}</AppText>
<DeleteData />
+9 -9
View File
@@ -6,6 +6,7 @@ import Hyperlink from 'react-native-hyperlink'
import styles, { iconStyles } from '../../../styles'
import labels from '../../../i18n/en/settings'
import AppText from '../../app-text'
import SettingsSegment from '../shared/settings-segment'
import TempSlider from './temp-slider'
import UseCervixSetting from './use-cervix'
import Icon from 'react-native-vector-icons/Entypo'
@@ -20,15 +21,14 @@ export default class Settings extends Component {
render() {
return (
<ScrollView>
<UseCervixSetting/>
<View style={styles.settingsSegment}>
<AppText style={styles.settingsSegmentTitle}>
{labels.tempScale.segmentTitle}
</AppText>
<SettingsSegment title={labels.useCervix.title}>
<UseCervixSetting/>
</SettingsSegment>
<SettingsSegment title={labels.tempScale.segmentTitle}>
<AppText>{labels.tempScale.segmentExplainer}</AppText>
<TempSlider/>
</View>
<View style={[styles.settingsSegment, styles.settingsSegmentLast]}>
</SettingsSegment>
<SettingsSegment style={styles.settingsSegmentLast} >
<View style={{flexDirection: 'row', alignItems: 'center'}}>
<Icon name="info-with-circle" style={iconStyles.infoInHeading}/>
<AppText style={styles.settingsSegmentTitle}>{`${labels.preOvu.title} `}</AppText>
@@ -36,8 +36,8 @@ export default class Settings extends Component {
<Hyperlink linkStyle={styles.link} linkText={replaceUrlWithText}>
<AppText>{labels.preOvu.note}</AppText>
</Hyperlink>
</View>
</SettingsSegment>
</ScrollView>
)
}
}
}
@@ -9,7 +9,7 @@ import {
import { secondaryColor } from '../../../styles/index'
import labels from '../../../i18n/en/settings'
import config from '../../../config'
import alertError from '../alert-error'
import alertError from '../shared/alert-error'
export default class TempSlider extends Component {
constructor(props) {
+15 -24
View File
@@ -1,7 +1,6 @@
import React, { Component } from 'react'
import {
View,
TouchableOpacity,
Switch
} from 'react-native'
import AppText from '../../app-text'
@@ -9,7 +8,6 @@ import {
useCervixObservable,
saveUseCervix
} from '../../../local-storage'
import styles from '../../../styles/index'
import labels from '../../../i18n/en/settings'
export default class UseCervixSetting extends Component {
@@ -20,29 +18,22 @@ export default class UseCervixSetting extends Component {
render() {
return (
<TouchableOpacity
style={styles.settingsSegment}
>
<AppText style={styles.settingsSegmentTitle}>
{labels.useCervix.title}
</AppText>
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<View style={{ flex: 1 }}>
{this.state.useCervix ?
<AppText>{labels.useCervix.cervixModeOn}</AppText>
:
<AppText>{labels.useCervix.cervixModeOff}</AppText>
}
</View>
<Switch
value={this.state.useCervix}
onValueChange={bool => {
this.setState({ useCervix: bool })
saveUseCervix(bool)
}}
/>
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<View style={{ flex: 1 }}>
{this.state.useCervix ?
<AppText>{labels.useCervix.cervixModeOn}</AppText>
:
<AppText>{labels.useCervix.cervixModeOff}</AppText>
}
</View>
</TouchableOpacity>
<Switch
value={this.state.useCervix}
onValueChange={bool => {
this.setState({ useCervix: bool })
saveUseCervix(bool)
}}
/>
</View>
)
}
}
@@ -1,23 +0,0 @@
import { Alert } from 'react-native'
import { openDb } from '../../../db'
import { shared } from '../../../i18n/en/labels'
export default async function checkPassword({hash, onCancel, onTryAgain }) {
try {
await openDb(hash)
return true
} catch (err) {
Alert.alert(
shared.incorrectPassword,
shared.incorrectPasswordMessage,
[{
text: shared.cancel,
onPress: onCancel
}, {
text: shared.tryAgain,
onPress: onTryAgain
}]
)
return false
}
}
+1 -1
View File
@@ -2,7 +2,7 @@ import React, { Component } from 'react'
import { View } from 'react-native'
import settings from '../../../i18n/en/settings'
import EnterNewPassword from './enter-new-password'
import SettingsButton from '../settings-button'
import SettingsButton from '../shared/settings-button'
import showBackUpReminder from './show-backup-reminder'
export default class CreatePassword extends Component {
+27 -60
View File
@@ -1,80 +1,47 @@
import React, { Component } from 'react'
import {
View,
TouchableOpacity
} from 'react-native'
import nodejs from 'nodejs-mobile-react-native'
import AppText from '../../app-text'
import styles from '../../../styles'
import labels from '../../../i18n/en/settings'
import { requestHash, changeEncryptionAndRestartApp } from '../../../db'
import PasswordField from './password-field'
import showBackUpReminder from './show-backup-reminder'
import checkCurrentPassword from './check-current-password'
import { changeEncryptionAndRestartApp } from '../../../db'
import ConfirmWithPassword from '../shared/confirm-with-password'
import SettingsButton from '../shared/settings-button'
export default class DeletePassword extends Component {
constructor() {
super()
this.state = {
enteringCurrentPassword: false,
currentPassword: null
enteringCurrentPassword: false
}
nodejs.channel.addListener(
'pre-delete-pw-check',
this.removeEncryption,
this
)
}
componentWillUnmount() {
nodejs.channel.removeListener('pre-delete-pw-check', this.removeEncryption)
startConfirmWithPassword = () => {
this.setState({ enteringCurrentPassword: true })
this.props.onStartDeletingPassword()
}
removeEncryption = async hash => {
const passwordIsCorrect = await checkCurrentPassword({
hash,
onTryAgain: () => this.setState({currentPassword: null}),
onCancel: () => this.setState({
enteringCurrentPassword: false,
currentPassword: null
})
})
startDeletePassword = async () => {
await changeEncryptionAndRestartApp()
}
if (passwordIsCorrect) await changeEncryptionAndRestartApp()
cancelConfirmationWithPassword = () => {
this.setState({ enteringCurrentPassword: false })
}
render() {
const { enteringCurrentPassword } = this.state
if (enteringCurrentPassword) {
return (
<ConfirmWithPassword
onSuccess={this.startDeletePassword}
onCancel={this.cancelConfirmationWithPassword}
/>
)
}
return (
<View>
{this.state.enteringCurrentPassword &&
<PasswordField
onChangeText={val => this.setState({ currentPassword: val })}
value={this.state.currentPassword}
placeholder={labels.passwordSettings.enterCurrent}
/>
}
<TouchableOpacity
onPress={() => {
if (!this.state.enteringCurrentPassword) {
showBackUpReminder(() => {
this.setState({ enteringCurrentPassword: true })
}, true)
} else {
requestHash('pre-delete-pw-check', this.state.currentPassword)
}
}}
disabled={
this.state.enteringCurrentPassword &&
!this.state.currentPassword
}
style={styles.settingsButton}
>
<AppText style={styles.settingsButtonText}>
{labels.passwordSettings.deletePassword}
</AppText>
</TouchableOpacity>
</View>
<SettingsButton onPress={this.startConfirmWithPassword} >
{labels.passwordSettings.deletePassword}
</SettingsButton>
)
}
}
@@ -4,8 +4,8 @@ import nodejs from 'nodejs-mobile-react-native'
import { requestHash, changeEncryptionAndRestartApp } from '../../../db'
import AppText from '../../app-text'
import PasswordField from './password-field'
import SettingsButton from '../settings-button'
import PasswordField from '../shared/password-field'
import SettingsButton from '../shared/settings-button'
import styles from '../../../styles'
import settings from '../../../i18n/en/settings'
+41 -20
View File
@@ -1,9 +1,9 @@
import React, { Component } from 'react'
import { View, ScrollView } from 'react-native'
import { ScrollView } from 'react-native'
import CreatePassword from './create'
import ChangePassword from './update'
import DeletePassword from './delete'
import SettingsSegment from '../settings-segment'
import SettingsSegment from '../shared/settings-segment'
import AppText from '../../app-text'
import {
hasEncryptionObservable
@@ -14,33 +14,54 @@ export default class PasswordSetting extends Component {
constructor(props) {
super(props)
this.state = {
showUpdateAndDelete: hasEncryptionObservable.value,
showCreate: !hasEncryptionObservable.value
isPasswordSet: hasEncryptionObservable.value,
isChangingPassword: false,
isDeletingPassword: false
}
}
onChangingPassword = () => {
this.setState({ isChangingPassword: true })
}
onDeletingPassword = () => {
this.setState({ isDeletingPassword: true })
}
render() {
const {
isPasswordSet,
isChangingPassword,
isDeletingPassword,
} = this.state
const {
title,
explainerEnabled,
explainerDisabled
} = labels.passwordSettings
return (
<ScrollView>
<SettingsSegment title={labels.passwordSettings.title}>
<SettingsSegment title={title}>
<AppText>
{ isPasswordSet ? explainerEnabled : explainerDisabled }
</AppText>
{this.state.showUpdateAndDelete ?
<AppText>{labels.passwordSettings.explainerEnabled}</AppText>
:
<AppText>{labels.passwordSettings.explainerDisabled}</AppText>
}
{ !isPasswordSet && <CreatePassword/> }
{this.state.showUpdateAndDelete &&
<View>
<ChangePassword/>
<DeletePassword/>
</View>
}
{this.state.showCreate &&
<CreatePassword/>
}
{ (isPasswordSet && !isDeletingPassword) && (
<ChangePassword
onStartChangingPassword = {this.onChangingPassword}
/>
)}
{ (isPasswordSet && !isChangingPassword) && (
<DeletePassword
onStartDeletingPassword = {this.onDeletingPassword}
/>
)}
</SettingsSegment>
</ScrollView>
)
+25 -61
View File
@@ -1,14 +1,9 @@
import React, { Component } from 'react'
import { View } from 'react-native'
import nodejs from 'nodejs-mobile-react-native'
import { shared as sharedLabels } from '../../../i18n/en/labels'
import settings from '../../../i18n/en/settings'
import { requestHash } from '../../../db'
import EnterNewPassword from './enter-new-password'
import PasswordField from './password-field'
import SettingsButton from '../settings-button'
import SettingsButton from '../shared/settings-button'
import showBackUpReminder from './show-backup-reminder'
import checkCurrentPassword from './check-current-password'
import ConfirmWithPassword from '../shared/confirm-with-password'
export default class ChangePassword extends Component {
@@ -19,49 +14,29 @@ export default class ChangePassword extends Component {
enteringCurrentPassword: false,
enteringNewPassword: 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) {
this.setState({
currentPassword: null,
enteringNewPassword: true,
enteringCurrentPassword: false
})
}
}
startChangingPassword = () => {
showBackUpReminder(() => {
this.setState({ enteringCurrentPassword: true })
})
this.props.onStartChangingPassword()
}
handleCurrentPasswordInput = (currentPassword) => {
this.setState({ currentPassword })
startEnteringNewPassword = () => {
this.setState({
currentPassword: null,
enteringNewPassword: true,
enteringCurrentPassword: false
})
}
checkCurrentPassword = () => {
requestHash('pre-change-pw-check', this.state.currentPassword)
cancelConfirmationWithPassword = () => {
this.setState({
currentPassword: null,
enteringNewPassword: false,
enteringCurrentPassword: false
})
}
render() {
@@ -76,19 +51,10 @@ export default class ChangePassword extends Component {
if (enteringCurrentPassword) {
return (
<View>
<PasswordField
placeholder={labels.enterCurrent}
value={currentPassword}
onChangeText={this.handleCurrentPasswordInput}
/>
<SettingsButton
onPress={this.checkCurrentPassword}
disabled={!currentPassword}
>
{sharedLabels.unlock}
</SettingsButton>
</View>
<ConfirmWithPassword
onSuccess={this.startEnteringNewPassword}
onCancel={this.cancelConfirmationWithPassword}
/>
)
}
@@ -97,14 +63,12 @@ export default class ChangePassword extends Component {
}
return (
<View>
<SettingsButton
onPress={this.startChangingPassword}
disabled={currentPassword}
>
{labels.changePassword}
</SettingsButton>
</View>
<SettingsButton
onPress={this.startChangingPassword}
disabled={currentPassword}
>
{labels.changePassword}
</SettingsButton>
)
}
}
+9 -2
View File
@@ -2,9 +2,12 @@ import React, { Component } from 'react'
import {
ScrollView,
} from 'react-native'
import SettingsSegment from '../shared/settings-segment'
import TempReminderPicker from './temp-reminder-picker'
import PeriodReminderPicker from './period-reminder'
import labels from '../../../i18n/en/settings'
export default class Settings extends Component {
constructor(props) {
super(props)
@@ -14,8 +17,12 @@ export default class Settings extends Component {
render() {
return (
<ScrollView>
<TempReminderPicker/>
<PeriodReminderPicker/>
<SettingsSegment title={labels.tempReminder.title}>
<TempReminderPicker/>
</SettingsSegment>
<SettingsSegment title={labels.periodReminder.title}>
<PeriodReminderPicker/>
</SettingsSegment>
</ScrollView>
)
}
@@ -8,7 +8,6 @@ import {
periodReminderObservable,
savePeriodReminder
} from '../../../local-storage'
import styles from '../../../styles/index'
import labels from '../../../i18n/en/settings'
export default class PeriodReminderPicker extends Component {
@@ -19,22 +18,17 @@ export default class PeriodReminderPicker extends Component {
render() {
return (
<View style={styles.settingsSegment}>
<AppText style={styles.settingsSegmentTitle}>
{labels.periodReminder.title}
</AppText>
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<View style={{ flex: 1 }}>
<AppText>{labels.periodReminder.reminderText}</AppText>
</View>
<Switch
value={this.state.enabled}
onValueChange={switchOn => {
this.setState({ enabled: switchOn })
savePeriodReminder({enabled: switchOn})
}}
/>
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<View style={{ flex: 1 }}>
<AppText>{labels.periodReminder.reminderText}</AppText>
</View>
<Switch
value={this.state.enabled}
onValueChange={switchOn => {
this.setState({ enabled: switchOn })
savePeriodReminder({enabled: switchOn})
}}
/>
</View>
)
}
@@ -10,64 +10,63 @@ import {
tempReminderObservable,
saveTempReminder
} from '../../../local-storage'
import styles from '../../../styles/index'
import labels from '../../../i18n/en/settings'
import padWithZeros from '../../helpers/pad-time-with-zeros'
export default class TempReminderPicker extends Component {
constructor(props) {
super(props)
this.state = Object.assign({}, tempReminderObservable.value)
const { time, enabled } = tempReminderObservable.value
this.state = {
time,
enabled,
isTimePickerVisible: false
}
}
render() {
return (
<TouchableOpacity
style={styles.settingsSegment}
style={{ flexDirection: 'row', alignItems: 'center' }}
onPress={() => this.setState({ isTimePickerVisible: true })}
>
<AppText style={styles.settingsSegmentTitle}>
{labels.tempReminder.title}
</AppText>
<View style={{ flexDirection: 'row', alignItems: 'center' }}>
<View style={{ flex: 1 }}>
{this.state.time && this.state.enabled ?
<AppText>{labels.tempReminder.timeSet(this.state.time)}</AppText>
:
<AppText>{labels.tempReminder.noTimeSet}</AppText>
}
</View>
<Switch
value={this.state.enabled}
onValueChange={switchOn => {
this.setState({ enabled: switchOn })
if (switchOn && !this.state.time) {
this.setState({ isTimePickerVisible: true })
}
if (!switchOn) saveTempReminder({ enabled: false })
}}
/>
<DateTimePicker
mode="time"
isVisible={this.state.isTimePickerVisible}
onConfirm={jsDate => {
const time = padWithZeros(jsDate)
this.setState({
time,
isTimePickerVisible: false,
enabled: true
})
saveTempReminder({
time,
enabled: true
})
}}
onCancel={() => {
this.setState({ isTimePickerVisible: false })
if (!this.state.time) this.setState({enabled: false})
}}
/>
<View style={{ flex: 1 }}>
{this.state.time && this.state.enabled ?
<AppText>{labels.tempReminder.timeSet(this.state.time)}</AppText>
:
<AppText>{labels.tempReminder.noTimeSet}</AppText>
}
</View>
<Switch
value={this.state.enabled}
onValueChange={switchOn => {
this.setState({ enabled: switchOn })
if (switchOn && !this.state.time) {
this.setState({ isTimePickerVisible: true })
}
if (!switchOn) saveTempReminder({ enabled: false })
}}
/>
<DateTimePicker
mode="time"
isVisible={this.state.isTimePickerVisible}
onConfirm={jsDate => {
const time = padWithZeros(jsDate)
this.setState({
time,
isTimePickerVisible: false,
enabled: true
})
saveTempReminder({
time,
enabled: true
})
}}
onCancel={() => {
this.setState({ isTimePickerVisible: false })
if (!this.state.time) this.setState({enabled: false})
}}
/>
</TouchableOpacity>
)
}
@@ -1,5 +1,5 @@
import { Alert } from 'react-native'
import { shared as sharedLabels } from '../../i18n/en/labels'
import { shared as sharedLabels } from '../../../i18n/en/labels'
export default function alertError(msg) {
Alert.alert(sharedLabels.errorTitle, msg)
@@ -4,8 +4,8 @@ import { View, Alert } from 'react-native'
import nodejs from 'nodejs-mobile-react-native'
import { requestHash, openDb } from '../../../db'
import PasswordField from '../password/password-field'
import SettingsButton from '../settings-button'
import PasswordField from './password-field'
import SettingsButton from '../shared/settings-button'
import settings from '../../../i18n/en/settings'
import { shared } from '../../../i18n/en/labels'
@@ -79,7 +79,7 @@ export default class ConfirmWithPassword extends Component {
onPress={this.initPasswordCheck}
disabled={!password}
>
{settings.deleteSegment.title}
{shared.confirmToProceed}
</SettingsButton>
</View>
)
@@ -2,8 +2,8 @@ import React from 'react'
import PropTypes from 'prop-types'
import { TouchableOpacity } from 'react-native'
import AppText from '../app-text'
import styles from '../../styles'
import AppText from '../../app-text'
import styles from '../../../styles'
const SettingsButton = ({ children, ...props }) => {
return (
@@ -2,22 +2,25 @@ import React from 'react'
import PropTypes from 'prop-types'
import { View } from 'react-native'
import AppText from '../app-text'
import styles from '../../styles'
import AppText from '../../app-text'
import styles from '../../../styles'
const SettingsSegment = ({ children, ...props }) => {
const style = [styles.settingsSegment, props.style]
if (props.last) style.push(styles.settingsSegmentLast)
return (
<View style={style}>
<AppText style={styles.settingsSegmentTitle}>{props.title}</AppText>
<View style={[styles.settingsSegment, props.style]}>
{
props.title
&& <AppText style={styles.settingsSegmentTitle}>{props.title}</AppText>
}
{children}
</View>
)
}
SettingsSegment.propTypes = {
title: PropTypes.string.isRequired
title: PropTypes.string
}
export default SettingsSegment
+1 -1
View File
@@ -11,7 +11,7 @@ export const shared = {
incorrectPasswordMessage: 'That password is incorrect.',
tryAgain: 'Try again',
ok: 'OK',
unlock: 'Unlock',
confirmToProceed: 'Confirm to proceed',
date: 'Date',
cycleDayWithLinebreak: 'Cycle\nday',
loading: 'Loading ...',
+1 -1
View File
@@ -2,7 +2,7 @@ import {tempReminderObservable, periodReminderObservable} from '../local-storage
import Notification from 'react-native-push-notification'
import { LocalDate } from 'js-joda'
import Moment from 'moment'
import { settings as labels } from '../i18n/en/settings'
import labels from '../i18n/en/settings'
import { getBleedingDaysSortedByDate } from '../db'
import cycleModule from './cycle'
import nothingChanged from '../db/db-unchanged'