From 167f24b6011dd6b6e5c66b732deed0a69b4061a8 Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Thu, 31 May 2018 21:38:39 +0200 Subject: [PATCH 1/3] Introduce realm and persist temperatures --- android/app/build.gradle | 1 + .../main/java/com/drip/MainApplication.java | 4 +- android/settings.gradle | 2 + db.js | 57 + index.js | 7 +- list.js | 44 +- package-lock.json | 1296 ++++++++++++++--- package.json | 5 +- 8 files changed, 1191 insertions(+), 225 deletions(-) create mode 100644 db.js diff --git a/android/app/build.gradle b/android/app/build.gradle index 291cc74..5623cac 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -137,6 +137,7 @@ android { } dependencies { + compile project(':realm') compile fileTree(dir: "libs", include: ["*.jar"]) compile "com.android.support:appcompat-v7:23.0.1" compile "com.facebook.react:react-native:+" // From node_modules diff --git a/android/app/src/main/java/com/drip/MainApplication.java b/android/app/src/main/java/com/drip/MainApplication.java index be0858a..00ce2c0 100644 --- a/android/app/src/main/java/com/drip/MainApplication.java +++ b/android/app/src/main/java/com/drip/MainApplication.java @@ -3,6 +3,7 @@ package com.drip; import android.app.Application; import com.facebook.react.ReactApplication; +import io.realm.react.RealmReactPackage; import com.facebook.react.ReactNativeHost; import com.facebook.react.ReactPackage; import com.facebook.react.shell.MainReactPackage; @@ -22,7 +23,8 @@ public class MainApplication extends Application implements ReactApplication { @Override protected List getPackages() { return Arrays.asList( - new MainReactPackage() + new MainReactPackage(), + new RealmReactPackage() ); } diff --git a/android/settings.gradle b/android/settings.gradle index 3f3ea06..6bba8cd 100644 --- a/android/settings.gradle +++ b/android/settings.gradle @@ -1,3 +1,5 @@ rootProject.name = 'drip' +include ':realm' +project(':realm').projectDir = new File(rootProject.projectDir, '../node_modules/realm/android') include ':app' diff --git a/db.js b/db.js new file mode 100644 index 0000000..87cd04f --- /dev/null +++ b/db.js @@ -0,0 +1,57 @@ +import realm from 'realm' +import { v4 as uuid } from 'uuid' + +let db +let cycleDaysSortedbyTempValueView = [] + +const TemperatureSchema = { + name: 'Temperature', + properties: { + value: 'int', + exclude: 'bool' + } +} + +const CycleDaySchema = { + name: 'CycleDay', + primaryKey: 'key', + properties: { + key: 'string', + date: 'date', + temperature: { + type: 'Temperature', + optional: true + } + } +} + +async function openDatabase() { + db = await realm.open({ + schema: [ + CycleDaySchema, + TemperatureSchema + ], + // we only want this in dev mode + deleteRealmIfMigrationNeeded: true + }) + // just for testing purposes, the highest temperature will be topmost + // because I was too layz to make a scroll view + cycleDaysSortedbyTempValueView = db.objects('CycleDay').sorted('temperature.value', true) +} + +async function saveTemperature(date, temperature) { + db.write(() => { + const doc = { + key: uuid(), + date, + temperature + } + db.create('CycleDay', doc) + }) +} + +export { + cycleDaysSortedbyTempValueView, + openDatabase, + saveTemperature +} \ No newline at end of file diff --git a/index.js b/index.js index ff3019e..f1543b7 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,9 @@ import { AppRegistry } from 'react-native' import Home from './app' +import { openDatabase } from './db' -AppRegistry.registerComponent('home', () => Home) +// TODO error handling +openDatabase() + .then(() => { + AppRegistry.registerComponent('home', () => Home) + }) \ No newline at end of file diff --git a/list.js b/list.js index 9e53528..156b82c 100644 --- a/list.js +++ b/list.js @@ -4,25 +4,17 @@ import { Text, Button, TextInput, - FlatList + FlatList, + Keyboard } from 'react-native' -import * as styles from './styles' -import Datastore from 'react-native-local-mongodb' -const db = new Datastore({ filename: 'asyncStorageKey', autoload: true }) +import * as styles from './styles' +import { cycleDaysSortedbyTempValueView, saveTemperature } from './db' export default class Temp extends Component { constructor(props) { super(props) - this.state = { - temperatures: [] - } - db.find({ key: { $exists: true } }, (err, persistedTemperatures) => { - if (err) throw err - this.setState({ - temperatures: [...persistedTemperatures, ...this.state.temperatures] - }) - }) + this.state = { currentValue: '' } } render() { @@ -33,26 +25,26 @@ export default class Temp extends Component { onChangeText={(val) => { this.setState({currentValue: val}) }} + keyboardType='numeric' + value = {this.state.currentValue} />