Clean up file structure
This commit is contained in:
@@ -6,5 +6,6 @@ export const settings = {
|
|||||||
},
|
},
|
||||||
exportTitle: 'My Drip data export',
|
exportTitle: 'My Drip data export',
|
||||||
exportSubject: 'My Drip data export',
|
exportSubject: 'My Drip data export',
|
||||||
buttonLabel: 'Export data'
|
exportLabel: 'Export data',
|
||||||
|
importLabel: 'Import data'
|
||||||
}
|
}
|
||||||
+19
-17
@@ -7,12 +7,12 @@ import {
|
|||||||
} from 'react-native'
|
} from 'react-native'
|
||||||
|
|
||||||
import Share from 'react-native-share'
|
import Share from 'react-native-share'
|
||||||
import getDataAsCsvDataUri from '../lib/export-to-csv'
|
|
||||||
import { DocumentPicker, DocumentPickerUtil } from 'react-native-document-picker'
|
import { DocumentPicker, DocumentPickerUtil } from 'react-native-document-picker'
|
||||||
import rnfs from 'react-native-fs'
|
import rnfs from 'react-native-fs'
|
||||||
import styles from '../styles/index'
|
import styles from '../styles/index'
|
||||||
import { settings as labels } from './labels'
|
import { settings as labels } from './labels'
|
||||||
import { importCsv } from '../db'
|
import getDataAsCsvDataUri from '../lib/import-export/export-to-csv'
|
||||||
|
import importCsv from '../lib/import-export/import-from-csv'
|
||||||
|
|
||||||
export default class Settings extends Component {
|
export default class Settings extends Component {
|
||||||
render() {
|
render() {
|
||||||
@@ -21,7 +21,23 @@ export default class Settings extends Component {
|
|||||||
<View style={styles.homeButtons}>
|
<View style={styles.homeButtons}>
|
||||||
<View style={styles.homeButton}>
|
<View style={styles.homeButton}>
|
||||||
<Button
|
<Button
|
||||||
onPress={async () => {
|
onPress={ openShareDialogAndExport }
|
||||||
|
title={labels.exportLabel}>
|
||||||
|
</Button>
|
||||||
|
</View>
|
||||||
|
<View style={styles.homeButton}>
|
||||||
|
<Button
|
||||||
|
onPress={ getFileContentAndImport }
|
||||||
|
title={labels.importLabel}>
|
||||||
|
</Button>
|
||||||
|
</View>
|
||||||
|
</View>
|
||||||
|
</ScrollView>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function openShareDialogAndExport() {
|
||||||
let data
|
let data
|
||||||
try {
|
try {
|
||||||
data = getDataAsCsvDataUri()
|
data = getDataAsCsvDataUri()
|
||||||
@@ -45,20 +61,6 @@ export default class Settings extends Component {
|
|||||||
console.error(err)
|
console.error(err)
|
||||||
return Alert.alert(labels.errors.problemSharing)
|
return Alert.alert(labels.errors.problemSharing)
|
||||||
}
|
}
|
||||||
}}
|
|
||||||
title={labels.buttonLabel}>
|
|
||||||
</Button>
|
|
||||||
</View>
|
|
||||||
<View style={styles.homeButton}>
|
|
||||||
<Button
|
|
||||||
onPress={ getFileContentAndImport }
|
|
||||||
title="Import data">
|
|
||||||
</Button>
|
|
||||||
</View>
|
|
||||||
</View>
|
|
||||||
</ScrollView>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getFileContentAndImport() {
|
async function getFileContentAndImport() {
|
||||||
|
|||||||
+2
-153
@@ -1,9 +1,5 @@
|
|||||||
import Realm from 'realm'
|
import Realm from 'realm'
|
||||||
import { LocalDate } from 'js-joda'
|
import { LocalDate } from 'js-joda'
|
||||||
import { Base64 } from 'js-base64'
|
|
||||||
import objectPath from 'object-path'
|
|
||||||
import csvParser from 'csvtojson'
|
|
||||||
import isObject from 'isobject'
|
|
||||||
|
|
||||||
import {
|
import {
|
||||||
cycleWithTempAndNoMucusShift,
|
cycleWithTempAndNoMucusShift,
|
||||||
@@ -180,154 +176,9 @@ function getPreviousTemperature(cycleDay) {
|
|||||||
return winner.temperature.value
|
return winner.temperature.value
|
||||||
}
|
}
|
||||||
|
|
||||||
function getCycleDaysAsCsvDataUri() {
|
|
||||||
if (!cycleDaysSortedByDate.length) return null
|
|
||||||
const csv = transformToCsv(cycleDaysSortedByDate)
|
|
||||||
const encoded = Base64.encodeURI(csv)
|
|
||||||
return `data:text/csv;base64,${encoded}`
|
|
||||||
|
|
||||||
function transformToCsv() {
|
|
||||||
const columnNames = getColumnNamesForCsv()
|
|
||||||
const rows = cycleDaysSortedByDate
|
|
||||||
.map(day => {
|
|
||||||
return columnNames.map(column => {
|
|
||||||
return objectPath.get(day, column, '')
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.map(row => row.join(','))
|
|
||||||
|
|
||||||
rows.unshift(columnNames.join(','))
|
|
||||||
return rows.join('\n')
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getColumnNamesForCsv() {
|
|
||||||
return getPrefixedKeys('CycleDay')
|
|
||||||
|
|
||||||
function getPrefixedKeys(schemaName, prefix) {
|
|
||||||
const schema = db.schema.find(x => x.name === schemaName).properties
|
|
||||||
return Object.keys(schema).reduce((acc, key) => {
|
|
||||||
const prefixedKey = prefix ? [prefix, key].join('.') : key
|
|
||||||
const childSchemaName = schema[key].objectType
|
|
||||||
if (!childSchemaName) {
|
|
||||||
acc.push(prefixedKey)
|
|
||||||
return acc
|
|
||||||
}
|
|
||||||
acc.push(...getPrefixedKeys(childSchemaName, prefixedKey))
|
|
||||||
return acc
|
|
||||||
}, [])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getDbType(modelProperties, path) {
|
|
||||||
if (path.length === 1) return modelProperties[path[0]].type
|
|
||||||
const modelName = modelProperties[path[0]].objectType
|
|
||||||
const model = db.schema.find(x => x.name === modelName)
|
|
||||||
return getDbType(model.properties, path.slice(1))
|
|
||||||
}
|
|
||||||
|
|
||||||
async function importCsv(csv, deleteFirst) {
|
|
||||||
const cycleDayProperties = db.schema.find(x => x.name === 'CycleDay').properties
|
|
||||||
const parseFuncs = {
|
|
||||||
bool: val => {
|
|
||||||
if (val.toLowerCase() === 'true') return true
|
|
||||||
if (val.toLowerCase() === 'false') return false
|
|
||||||
return val
|
|
||||||
},
|
|
||||||
int: parseNumberIfPossible,
|
|
||||||
float: parseNumberIfPossible,
|
|
||||||
double: parseNumberIfPossible,
|
|
||||||
string: val => val
|
|
||||||
}
|
|
||||||
|
|
||||||
function parseNumberIfPossible(val) {
|
|
||||||
// Number and parseFloat catch different cases of weirdness,
|
|
||||||
// so we test them both
|
|
||||||
if (isNaN(Number(val)) || isNaN(parseFloat(val))) return val
|
|
||||||
return Number(val)
|
|
||||||
}
|
|
||||||
|
|
||||||
const config = {
|
|
||||||
ignoreEmpty: true,
|
|
||||||
colParser: getColumnNamesForCsv().reduce((acc, colName) => {
|
|
||||||
const path = colName.split('.')
|
|
||||||
const dbType = getDbType(cycleDayProperties, path)
|
|
||||||
acc[colName] = item => {
|
|
||||||
if (item === '') return null
|
|
||||||
return parseFuncs[dbType](item)
|
|
||||||
}
|
|
||||||
return acc
|
|
||||||
}, {})
|
|
||||||
}
|
|
||||||
|
|
||||||
let cycleDays
|
|
||||||
try {
|
|
||||||
cycleDays = await csvParser(config)
|
|
||||||
.fromString(csv)
|
|
||||||
.on('header', validateHeaders)
|
|
||||||
} catch(err) {
|
|
||||||
// TODO
|
|
||||||
console.log(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//remove symptoms where all fields are null
|
|
||||||
putNullForEmptySymptoms(cycleDays)
|
|
||||||
|
|
||||||
if (deleteFirst) {
|
|
||||||
db.write(() => {
|
|
||||||
db.delete(db.objects('CycleDay'))
|
|
||||||
cycleDays.forEach(tryToCreateCycleDay)
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
db.write(() => {
|
|
||||||
cycleDays.forEach((day, i) => {
|
|
||||||
const existing = getCycleDay(day.date)
|
|
||||||
if (existing) {
|
|
||||||
db.delete(existing)
|
|
||||||
}
|
|
||||||
tryToCreateCycleDay(day, i)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function tryToCreateCycleDay(day, i) {
|
|
||||||
try {
|
|
||||||
db.create('CycleDay', day)
|
|
||||||
} catch (err) {
|
|
||||||
const msg = `Error for line ${i + 1}(${day.date}): ${err.message}`
|
|
||||||
throw new Error(msg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function validateHeaders(headers) {
|
|
||||||
const expectedHeaders = getColumnNamesForCsv()
|
|
||||||
if (!headers.every(header => {
|
|
||||||
return expectedHeaders.indexOf(header) > -1
|
|
||||||
})) {
|
|
||||||
const msg = `Expected CSV column titles to be ${expectedHeaders.join()}`
|
|
||||||
throw new Error(msg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function putNullForEmptySymptoms(data) {
|
|
||||||
data.forEach(replaceWithNullIfAllPropertiesAreNull)
|
|
||||||
|
|
||||||
function replaceWithNullIfAllPropertiesAreNull(obj) {
|
|
||||||
Object.keys(obj).forEach((key) => {
|
|
||||||
if (!isObject(obj[key])) return
|
|
||||||
if (Object.values(obj[key]).every(val => val === null)) {
|
|
||||||
obj[key] = null
|
|
||||||
return
|
|
||||||
}
|
|
||||||
replaceWithNullIfAllPropertiesAreNull(obj[key])
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
db,
|
||||||
saveSymptom,
|
saveSymptom,
|
||||||
getOrCreateCycleDay,
|
getOrCreateCycleDay,
|
||||||
bleedingDaysSortedByDate,
|
bleedingDaysSortedByDate,
|
||||||
@@ -336,7 +187,5 @@ export {
|
|||||||
fillWithDummyData,
|
fillWithDummyData,
|
||||||
deleteAll,
|
deleteAll,
|
||||||
getPreviousTemperature,
|
getPreviousTemperature,
|
||||||
getCycleDay,
|
getCycleDay
|
||||||
getCycleDaysAsCsvDataUri,
|
|
||||||
importCsv
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import objectPath from 'object-path'
|
import objectPath from 'object-path'
|
||||||
import { Base64 } from 'js-base64'
|
import { Base64 } from 'js-base64'
|
||||||
|
import { cycleDaysSortedByDate } from '../../db'
|
||||||
import { getColumnNamesForCsv, cycleDaysSortedByDate } from '../db'
|
import getColumnNamesForCsv from './get-csv-column-names'
|
||||||
|
|
||||||
export default function makeDataURI() {
|
export default function makeDataURI() {
|
||||||
if (!cycleDaysSortedByDate.length) return null
|
if (!cycleDaysSortedByDate.length) return null
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
import { db } from '../../db'
|
||||||
|
|
||||||
|
export default function getColumnNamesForCsv() {
|
||||||
|
return getPrefixedKeys('CycleDay')
|
||||||
|
|
||||||
|
function getPrefixedKeys(schemaName, prefix) {
|
||||||
|
const schema = db.schema.find(x => x.name === schemaName).properties
|
||||||
|
return Object.keys(schema).reduce((acc, key) => {
|
||||||
|
const prefixedKey = prefix ? [prefix, key].join('.') : key
|
||||||
|
const childSchemaName = schema[key].objectType
|
||||||
|
if (!childSchemaName) {
|
||||||
|
acc.push(prefixedKey)
|
||||||
|
return acc
|
||||||
|
}
|
||||||
|
acc.push(...getPrefixedKeys(childSchemaName, prefixedKey))
|
||||||
|
return acc
|
||||||
|
}, [])
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,110 @@
|
|||||||
|
import csvParser from 'csvtojson'
|
||||||
|
import isObject from 'isobject'
|
||||||
|
import { db, getCycleDay } from '../../db'
|
||||||
|
import getColumnNamesForCsv from './get-csv-column-names'
|
||||||
|
|
||||||
|
export default async function importCsv(csv, deleteFirst) {
|
||||||
|
const parseFuncs = {
|
||||||
|
bool: val => {
|
||||||
|
if (val.toLowerCase() === 'true') return true
|
||||||
|
if (val.toLowerCase() === 'false') return false
|
||||||
|
return val
|
||||||
|
},
|
||||||
|
int: parseNumberIfPossible,
|
||||||
|
float: parseNumberIfPossible,
|
||||||
|
double: parseNumberIfPossible,
|
||||||
|
string: val => val
|
||||||
|
}
|
||||||
|
|
||||||
|
function parseNumberIfPossible(val) {
|
||||||
|
// Number and parseFloat catch different cases of weirdness,
|
||||||
|
// so we test them both
|
||||||
|
if (isNaN(Number(val)) || isNaN(parseFloat(val))) return val
|
||||||
|
return Number(val)
|
||||||
|
}
|
||||||
|
|
||||||
|
const cycleDayDbSchema = db.schema.find(x => x.name === 'CycleDay').properties
|
||||||
|
const config = {
|
||||||
|
ignoreEmpty: true,
|
||||||
|
colParser: getColumnNamesForCsv().reduce((acc, colName) => {
|
||||||
|
const path = colName.split('.')
|
||||||
|
const dbType = getDbType(cycleDayDbSchema, path)
|
||||||
|
acc[colName] = item => {
|
||||||
|
if (item === '') return null
|
||||||
|
return parseFuncs[dbType](item)
|
||||||
|
}
|
||||||
|
return acc
|
||||||
|
}, {})
|
||||||
|
}
|
||||||
|
|
||||||
|
let cycleDays
|
||||||
|
try {
|
||||||
|
cycleDays = await csvParser(config)
|
||||||
|
.fromString(csv)
|
||||||
|
.on('header', validateHeaders)
|
||||||
|
} catch(err) {
|
||||||
|
// TODO
|
||||||
|
console.log(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
//remove symptoms where all fields are null
|
||||||
|
putNullForEmptySymptoms(cycleDays)
|
||||||
|
|
||||||
|
if (deleteFirst) {
|
||||||
|
db.write(() => {
|
||||||
|
db.delete(db.objects('CycleDay'))
|
||||||
|
cycleDays.forEach(tryToCreateCycleDay)
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
db.write(() => {
|
||||||
|
cycleDays.forEach((day, i) => {
|
||||||
|
const existing = getCycleDay(day.date)
|
||||||
|
if (existing) {
|
||||||
|
db.delete(existing)
|
||||||
|
}
|
||||||
|
tryToCreateCycleDay(day, i)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function tryToCreateCycleDay(day, i) {
|
||||||
|
try {
|
||||||
|
db.create('CycleDay', day)
|
||||||
|
} catch (err) {
|
||||||
|
const msg = `Error for line ${i + 1}(${day.date}): ${err.message}`
|
||||||
|
throw new Error(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function validateHeaders(headers) {
|
||||||
|
const expectedHeaders = getColumnNamesForCsv()
|
||||||
|
if (!headers.every(header => {
|
||||||
|
return expectedHeaders.indexOf(header) > -1
|
||||||
|
})) {
|
||||||
|
const msg = `Expected CSV column titles to be ${expectedHeaders.join()}`
|
||||||
|
throw new Error(msg)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function putNullForEmptySymptoms(data) {
|
||||||
|
data.forEach(replaceWithNullIfAllPropertiesAreNull)
|
||||||
|
|
||||||
|
function replaceWithNullIfAllPropertiesAreNull(obj) {
|
||||||
|
Object.keys(obj).forEach((key) => {
|
||||||
|
if (!isObject(obj[key])) return
|
||||||
|
if (Object.values(obj[key]).every(val => val === null)) {
|
||||||
|
obj[key] = null
|
||||||
|
return
|
||||||
|
}
|
||||||
|
replaceWithNullIfAllPropertiesAreNull(obj[key])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getDbType(modelProperties, path) {
|
||||||
|
if (path.length === 1) return modelProperties[path[0]].type
|
||||||
|
const modelName = modelProperties[path[0]].objectType
|
||||||
|
const model = db.schema.find(x => x.name === modelName)
|
||||||
|
return getDbType(model.properties, path.slice(1))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user