From ed474fd5cbc42436048613ccc3e1dac09cd21fcd Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Sat, 4 Aug 2018 17:18:44 +0200 Subject: [PATCH] Import file contents and move csv function to db module --- android/app/build.gradle | 2 + .../main/java/com/drip/MainApplication.java | 4 ++ android/settings.gradle | 4 ++ components/settings.js | 2 + db/index.js | 52 ++++++++++++++----- ios/drip.xcodeproj/project.pbxproj | 38 ++++++++++++++ package-lock.json | 24 +++++++++ package.json | 2 + 8 files changed, 114 insertions(+), 14 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index 16ed21e..3fd611e 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -137,6 +137,8 @@ android { } dependencies { + compile project(':react-native-fs') + compile project(':react-native-document-picker') compile project(':react-native-share') compile project(':realm') compile project(':react-native-svg') diff --git a/android/app/src/main/java/com/drip/MainApplication.java b/android/app/src/main/java/com/drip/MainApplication.java index 532b206..b0f0e16 100644 --- a/android/app/src/main/java/com/drip/MainApplication.java +++ b/android/app/src/main/java/com/drip/MainApplication.java @@ -3,6 +3,8 @@ package com.drip; import android.app.Application; import com.facebook.react.ReactApplication; +import com.rnfs.RNFSPackage; +import com.reactnativedocumentpicker.ReactNativeDocumentPicker; import cl.json.RNSharePackage; import cl.json.ShareApplication; import io.realm.react.RealmReactPackage; @@ -27,6 +29,8 @@ public class MainApplication extends Application implements ReactApplication, Sh protected List getPackages() { return Arrays.asList( new MainReactPackage(), + new RNFSPackage(), + new ReactNativeDocumentPicker(), new RNSharePackage(), new RealmReactPackage(), new SvgPackage() diff --git a/android/settings.gradle b/android/settings.gradle index 0c603cd..7412acd 100644 --- a/android/settings.gradle +++ b/android/settings.gradle @@ -1,4 +1,8 @@ rootProject.name = 'drip' +include ':react-native-fs' +project(':react-native-fs').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-fs/android') +include ':react-native-document-picker' +project(':react-native-document-picker').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-document-picker/android') include ':react-native-share' project(':react-native-share').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-share/android') include ':realm' diff --git a/components/settings.js b/components/settings.js index 3abc167..0c9fa8a 100644 --- a/components/settings.js +++ b/components/settings.js @@ -8,6 +8,8 @@ import { import Share from 'react-native-share' import getDataAsCsvDataUri from '../lib/export-to-csv' +import { DocumentPicker, DocumentPickerUtil } from 'react-native-document-picker' +import rnfs from 'react-native-fs' import styles from '../styles/index' import { settings as labels } from './labels' diff --git a/db/index.js b/db/index.js index d3d49e8..2e9ffd3 100644 --- a/db/index.js +++ b/db/index.js @@ -1,5 +1,8 @@ import Realm from 'realm' import { LocalDate } from 'js-joda' +import { Base64 } from 'js-base64' +import objectPath from 'object-path' + import { cycleWithTempAndNoMucusShift, cycleWithFhm, @@ -175,21 +178,42 @@ function getPreviousTemperature(cycleDay) { return winner.temperature.value } -function getColumnNamesForCsv() { - return getPrefixedKeys('CycleDay') +function getCycleDaysAsCsvDataUri() { + if (!cycleDaysSortedByDate.length) return null + const csv = transformToCsv(cycleDaysSortedByDate) + const encoded = Base64.encodeURI(csv) + return `data:text/csv;base64,${encoded}` - 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 + 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 + }, []) } - acc.push(...getPrefixedKeys(childSchemaName, prefixedKey)) - return acc - }, []) + } } } @@ -203,5 +227,5 @@ export { deleteAll, getPreviousTemperature, getCycleDay, - getColumnNamesForCsv + getCycleDaysAsCsvDataUri } diff --git a/ios/drip.xcodeproj/project.pbxproj b/ios/drip.xcodeproj/project.pbxproj index 109fe9f..80a206a 100644 --- a/ios/drip.xcodeproj/project.pbxproj +++ b/ios/drip.xcodeproj/project.pbxproj @@ -43,6 +43,8 @@ 62F2A4645AC84CDC9506FF27 /* libc++.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 9AEBF0735214455AAEDF56D5 /* libc++.tbd */; }; D91133DCE120440893E2FD2E /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = CD8C8B91E0A747B3883A0D56 /* libz.tbd */; }; 26DC04B498C64CE5AAA0C4F8 /* libRNShare.a in Frameworks */ = {isa = PBXBuildFile; fileRef = A8B59389C2FC4F19BD30ABC3 /* libRNShare.a */; }; + 29DF0CCC1AEA4C92BCA0BCCD /* libRNDocumentPicker.a in Frameworks */ = {isa = PBXBuildFile; fileRef = D211D71BE5A8436A978770A9 /* libRNDocumentPicker.a */; }; + 17AD822C42A44BADA96BD860 /* libRNFS.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 84CCEBD3B2C44758853BC941 /* libRNFS.a */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -357,6 +359,10 @@ CD8C8B91E0A747B3883A0D56 /* libz.tbd */ = {isa = PBXFileReference; name = "libz.tbd"; path = "usr/lib/libz.tbd"; sourceTree = SDKROOT; fileEncoding = undefined; lastKnownFileType = sourcecode.text-based-dylib-definition; explicitFileType = undefined; includeInIndex = 0; }; 4E6AB77B55F2491487B6124E /* RNShare.xcodeproj */ = {isa = PBXFileReference; name = "RNShare.xcodeproj"; path = "../node_modules/react-native-share/ios/RNShare.xcodeproj"; sourceTree = ""; fileEncoding = undefined; lastKnownFileType = wrapper.pb-project; explicitFileType = undefined; includeInIndex = 0; }; A8B59389C2FC4F19BD30ABC3 /* libRNShare.a */ = {isa = PBXFileReference; name = "libRNShare.a"; path = "libRNShare.a"; sourceTree = ""; fileEncoding = undefined; lastKnownFileType = archive.ar; explicitFileType = undefined; includeInIndex = 0; }; + 1F05FE29622E4F21AF70C2B7 /* RNDocumentPicker.xcodeproj */ = {isa = PBXFileReference; name = "RNDocumentPicker.xcodeproj"; path = "../node_modules/react-native-document-picker/ios/RNDocumentPicker.xcodeproj"; sourceTree = ""; fileEncoding = undefined; lastKnownFileType = wrapper.pb-project; explicitFileType = undefined; includeInIndex = 0; }; + D211D71BE5A8436A978770A9 /* libRNDocumentPicker.a */ = {isa = PBXFileReference; name = "libRNDocumentPicker.a"; path = "libRNDocumentPicker.a"; sourceTree = ""; fileEncoding = undefined; lastKnownFileType = archive.ar; explicitFileType = undefined; includeInIndex = 0; }; + 49089E09BFCF4F3DB209B6E9 /* RNFS.xcodeproj */ = {isa = PBXFileReference; name = "RNFS.xcodeproj"; path = "../node_modules/react-native-fs/RNFS.xcodeproj"; sourceTree = ""; fileEncoding = undefined; lastKnownFileType = wrapper.pb-project; explicitFileType = undefined; includeInIndex = 0; }; + 84CCEBD3B2C44758853BC941 /* libRNFS.a */ = {isa = PBXFileReference; name = "libRNFS.a"; path = "libRNFS.a"; sourceTree = ""; fileEncoding = undefined; lastKnownFileType = archive.ar; explicitFileType = undefined; includeInIndex = 0; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -390,6 +396,8 @@ 62F2A4645AC84CDC9506FF27 /* libc++.tbd in Frameworks */, D91133DCE120440893E2FD2E /* libz.tbd in Frameworks */, 26DC04B498C64CE5AAA0C4F8 /* libRNShare.a in Frameworks */, + 29DF0CCC1AEA4C92BCA0BCCD /* libRNDocumentPicker.a in Frameworks */, + 17AD822C42A44BADA96BD860 /* libRNFS.a in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -582,6 +590,8 @@ 8316A5AD64274E6FBA6C9FFE /* RNSVG.xcodeproj */, 7F6C9FA9B66B453CA602B334 /* RealmReact.xcodeproj */, 4E6AB77B55F2491487B6124E /* RNShare.xcodeproj */, + 1F05FE29622E4F21AF70C2B7 /* RNDocumentPicker.xcodeproj */, + 49089E09BFCF4F3DB209B6E9 /* RNFS.xcodeproj */, ); name = Libraries; sourceTree = ""; @@ -1209,12 +1219,16 @@ LIBRARY_SEARCH_PATHS = ( "$(inherited)", "\"$(SRCROOT)/$(TARGET_NAME)\"", + "\"$(SRCROOT)/$(TARGET_NAME)\"", + "\"$(SRCROOT)/$(TARGET_NAME)\"", ); HEADER_SEARCH_PATHS = ( "$(inherited)", "$(SRCROOT)/../node_modules/react-native-svg/ios/**", "$(SRCROOT)/../node_modules/realm/src/**", "$(SRCROOT)/../node_modules/react-native-share/ios", + "$(SRCROOT)/../node_modules/react-native-document-picker/ios/RNDocumentPicker", + "$(SRCROOT)/../node_modules/react-native-fs/**", ); }; name = Debug; @@ -1236,12 +1250,16 @@ LIBRARY_SEARCH_PATHS = ( "$(inherited)", "\"$(SRCROOT)/$(TARGET_NAME)\"", + "\"$(SRCROOT)/$(TARGET_NAME)\"", + "\"$(SRCROOT)/$(TARGET_NAME)\"", ); HEADER_SEARCH_PATHS = ( "$(inherited)", "$(SRCROOT)/../node_modules/react-native-svg/ios/**", "$(SRCROOT)/../node_modules/realm/src/**", "$(SRCROOT)/../node_modules/react-native-share/ios", + "$(SRCROOT)/../node_modules/react-native-document-picker/ios/RNDocumentPicker", + "$(SRCROOT)/../node_modules/react-native-fs/**", ); }; name = Release; @@ -1266,6 +1284,8 @@ "$(SRCROOT)/../node_modules/react-native-svg/ios/**", "$(SRCROOT)/../node_modules/realm/src/**", "$(SRCROOT)/../node_modules/react-native-share/ios", + "$(SRCROOT)/../node_modules/react-native-document-picker/ios/RNDocumentPicker", + "$(SRCROOT)/../node_modules/react-native-fs/**", ); }; name = Debug; @@ -1289,6 +1309,8 @@ "$(SRCROOT)/../node_modules/react-native-svg/ios/**", "$(SRCROOT)/../node_modules/realm/src/**", "$(SRCROOT)/../node_modules/react-native-share/ios", + "$(SRCROOT)/../node_modules/react-native-document-picker/ios/RNDocumentPicker", + "$(SRCROOT)/../node_modules/react-native-fs/**", ); }; name = Release; @@ -1319,12 +1341,16 @@ LIBRARY_SEARCH_PATHS = ( "$(inherited)", "\"$(SRCROOT)/$(TARGET_NAME)\"", + "\"$(SRCROOT)/$(TARGET_NAME)\"", + "\"$(SRCROOT)/$(TARGET_NAME)\"", ); HEADER_SEARCH_PATHS = ( "$(inherited)", "$(SRCROOT)/../node_modules/react-native-svg/ios/**", "$(SRCROOT)/../node_modules/realm/src/**", "$(SRCROOT)/../node_modules/react-native-share/ios", + "$(SRCROOT)/../node_modules/react-native-document-picker/ios/RNDocumentPicker", + "$(SRCROOT)/../node_modules/react-native-fs/**", ); }; name = Debug; @@ -1355,12 +1381,16 @@ LIBRARY_SEARCH_PATHS = ( "$(inherited)", "\"$(SRCROOT)/$(TARGET_NAME)\"", + "\"$(SRCROOT)/$(TARGET_NAME)\"", + "\"$(SRCROOT)/$(TARGET_NAME)\"", ); HEADER_SEARCH_PATHS = ( "$(inherited)", "$(SRCROOT)/../node_modules/react-native-svg/ios/**", "$(SRCROOT)/../node_modules/realm/src/**", "$(SRCROOT)/../node_modules/react-native-share/ios", + "$(SRCROOT)/../node_modules/react-native-document-picker/ios/RNDocumentPicker", + "$(SRCROOT)/../node_modules/react-native-fs/**", ); }; name = Release; @@ -1390,12 +1420,16 @@ LIBRARY_SEARCH_PATHS = ( "$(inherited)", "\"$(SRCROOT)/$(TARGET_NAME)\"", + "\"$(SRCROOT)/$(TARGET_NAME)\"", + "\"$(SRCROOT)/$(TARGET_NAME)\"", ); HEADER_SEARCH_PATHS = ( "$(inherited)", "$(SRCROOT)/../node_modules/react-native-svg/ios/**", "$(SRCROOT)/../node_modules/realm/src/**", "$(SRCROOT)/../node_modules/react-native-share/ios", + "$(SRCROOT)/../node_modules/react-native-document-picker/ios/RNDocumentPicker", + "$(SRCROOT)/../node_modules/react-native-fs/**", ); }; name = Debug; @@ -1425,12 +1459,16 @@ LIBRARY_SEARCH_PATHS = ( "$(inherited)", "\"$(SRCROOT)/$(TARGET_NAME)\"", + "\"$(SRCROOT)/$(TARGET_NAME)\"", + "\"$(SRCROOT)/$(TARGET_NAME)\"", ); HEADER_SEARCH_PATHS = ( "$(inherited)", "$(SRCROOT)/../node_modules/react-native-svg/ios/**", "$(SRCROOT)/../node_modules/realm/src/**", "$(SRCROOT)/../node_modules/react-native-share/ios", + "$(SRCROOT)/../node_modules/react-native-document-picker/ios/RNDocumentPicker", + "$(SRCROOT)/../node_modules/react-native-fs/**", ); }; name = Release; diff --git a/package-lock.json b/package-lock.json index 69a218b..9abb77d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2015,6 +2015,11 @@ } } }, + "base-64": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/base-64/-/base-64-0.1.0.tgz", + "integrity": "sha1-eAqZyE59YAJgNhURxId2E78k9rs=" + }, "base64-js": { "version": "1.3.0", "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.3.0.tgz", @@ -6369,6 +6374,11 @@ "resolved": "https://registry.npmjs.org/react-native-dismiss-keyboard/-/react-native-dismiss-keyboard-1.0.0.tgz", "integrity": "sha1-MohiQrPyMX4SHzrrmwpYXiuHm0k=" }, + "react-native-document-picker": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/react-native-document-picker/-/react-native-document-picker-2.1.0.tgz", + "integrity": "sha512-BFCBXwz8xuLvHLVFVeQM+RhaY8yZ38PEWt9WSbq5VIoZ/VssP6uu51XxOfdwaMALOrAHIojK0SiYnd155upZAg==" + }, "react-native-drawer-layout": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/react-native-drawer-layout/-/react-native-drawer-layout-1.3.2.tgz", @@ -6385,6 +6395,15 @@ "react-native-drawer-layout": "1.3.2" } }, + "react-native-fs": { + "version": "2.10.14", + "resolved": "https://registry.npmjs.org/react-native-fs/-/react-native-fs-2.10.14.tgz", + "integrity": "sha512-4bCzkg4dE/xUyXkMVz0AiyqLKAgTZPlZl/nEzRiSr2q6VnWDgO229MSgHLHhUtD2cqZkV0Z83WEbGpvXxWOAHA==", + "requires": { + "base-64": "^0.1.0", + "utf8": "^2.1.1" + } + }, "react-native-modal": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/react-native-modal/-/react-native-modal-3.1.0.tgz", @@ -8334,6 +8353,11 @@ "resolved": "https://registry.npmjs.org/use/-/use-3.1.1.tgz", "integrity": "sha512-cwESVXlO3url9YWlFW/TA9cshCEhtu7IKJ/p5soJ/gGpj7vbvFrAY/eIioQ6Dw23KjZhYgiIo8HOs1nQ2vr/oQ==" }, + "utf8": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/utf8/-/utf8-2.1.2.tgz", + "integrity": "sha1-H6DZJw6b6FDZsFAn9jUZv0ZFfZY=" + }, "util": { "version": "0.10.3", "resolved": "https://registry.npmjs.org/util/-/util-0.10.3.tgz", diff --git a/package.json b/package.json index 217d5ff..e6b0de6 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,8 @@ "react": "16.4.1", "react-native": "^0.56.0", "react-native-calendars": "^1.19.3", + "react-native-document-picker": "^2.1.0", + "react-native-fs": "^2.10.14", "react-native-modal-datetime-picker-nevo": "^4.11.0", "react-native-share": "^1.1.0", "react-native-simple-radio-button": "^2.7.1",