Introduce realm and persist temperatures

This commit is contained in:
Julia Friesel
2018-05-31 21:38:39 +02:00
parent 718573d31b
commit 167f24b601
8 changed files with 1191 additions and 225 deletions
+1
View File
@@ -137,6 +137,7 @@ android {
} }
dependencies { dependencies {
compile project(':realm')
compile fileTree(dir: "libs", include: ["*.jar"]) compile fileTree(dir: "libs", include: ["*.jar"])
compile "com.android.support:appcompat-v7:23.0.1" compile "com.android.support:appcompat-v7:23.0.1"
compile "com.facebook.react:react-native:+" // From node_modules compile "com.facebook.react:react-native:+" // From node_modules
@@ -3,6 +3,7 @@ package com.drip;
import android.app.Application; import android.app.Application;
import com.facebook.react.ReactApplication; import com.facebook.react.ReactApplication;
import io.realm.react.RealmReactPackage;
import com.facebook.react.ReactNativeHost; import com.facebook.react.ReactNativeHost;
import com.facebook.react.ReactPackage; import com.facebook.react.ReactPackage;
import com.facebook.react.shell.MainReactPackage; import com.facebook.react.shell.MainReactPackage;
@@ -22,7 +23,8 @@ public class MainApplication extends Application implements ReactApplication {
@Override @Override
protected List<ReactPackage> getPackages() { protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList( return Arrays.<ReactPackage>asList(
new MainReactPackage() new MainReactPackage(),
new RealmReactPackage()
); );
} }
+2
View File
@@ -1,3 +1,5 @@
rootProject.name = 'drip' rootProject.name = 'drip'
include ':realm'
project(':realm').projectDir = new File(rootProject.projectDir, '../node_modules/realm/android')
include ':app' include ':app'
+57
View File
@@ -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
}
+6 -1
View File
@@ -1,4 +1,9 @@
import { AppRegistry } from 'react-native' import { AppRegistry } from 'react-native'
import Home from './app' import Home from './app'
import { openDatabase } from './db'
AppRegistry.registerComponent('home', () => Home) // TODO error handling
openDatabase()
.then(() => {
AppRegistry.registerComponent('home', () => Home)
})
+18 -26
View File
@@ -4,25 +4,17 @@ import {
Text, Text,
Button, Button,
TextInput, TextInput,
FlatList FlatList,
Keyboard
} from 'react-native' } 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 { export default class Temp extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
this.state = { this.state = { currentValue: '' }
temperatures: []
}
db.find({ key: { $exists: true } }, (err, persistedTemperatures) => {
if (err) throw err
this.setState({
temperatures: [...persistedTemperatures, ...this.state.temperatures]
})
})
} }
render() { render() {
@@ -33,26 +25,26 @@ export default class Temp extends Component {
onChangeText={(val) => { onChangeText={(val) => {
this.setState({currentValue: val}) this.setState({currentValue: val})
}} }}
keyboardType='numeric'
value = {this.state.currentValue}
/> />
<Button <Button
onPress={() => { onPress={() => {
const newTemp = { saveTemperature(
value: this.state.currentValue, new Date(),
key: Date.now().toString() {
} value: Number(this.state.currentValue),
this.setState({ exclude: false
temperatures: [newTemp, ...this.state.temperatures] }
}) )
db.insert(newTemp, (err) => { this.setState({currentValue: ''})
if (err) console.log(err) Keyboard.dismiss()
})
}} }}
title="Save" title="Save"
/> />
<FlatList <FlatList
data = {this.state.temperatures} data = { cycleDaysSortedbyTempValueView }
extraData = {this.state} renderItem={({item}) => <Text>{item.temperature.value}</Text>}
renderItem={({item}) => <Text>{item.value}</Text>}
/> />
</View> </View>
) )
+1101 -195
View File
File diff suppressed because it is too large Load Diff
+3 -2
View File
@@ -17,8 +17,9 @@
"moment": "^2.22.1", "moment": "^2.22.1",
"react": "16.3.1", "react": "16.3.1",
"react-native": "0.55.4", "react-native": "0.55.4",
"react-native-local-mongodb": "^2.1.0", "react-navigation": "^2.0.4",
"react-navigation": "^2.0.4" "realm": "^2.7.1",
"uuid": "^3.2.1"
}, },
"devDependencies": { "devDependencies": {
"babel-preset-react-native": "4.0.0", "babel-preset-react-native": "4.0.0",