Skip to content
Snippets Groups Projects
Commit ecc4acf3 authored by Julia Friesel's avatar Julia Friesel
Browse files

Merge branch '6-hook-up-realm' into 'master'

Resolve "hook up realm"

Closes #6

See merge request bloodyhealth/drip!4
parents 718573d3 b0d7cd49
No related branches found
No related tags found
No related merge requests found
...@@ -137,6 +137,7 @@ android { ...@@ -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; ...@@ -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 { ...@@ -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()
); );
} }
......
rootProject.name = 'drip' rootProject.name = 'drip'
include ':realm'
project(':realm').projectDir = new File(rootProject.projectDir, '../node_modules/realm/android')
include ':app' include ':app'
db.js 0 → 100644
import realm from 'realm'
import { v4 as uuid } from 'uuid'
let db
let cycleDaysSortedbyTempValueView = []
const TemperatureSchema = {
name: 'Temperature',
properties: {
value: 'double',
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
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)
})
\ No newline at end of file
...@@ -4,25 +4,20 @@ import { ...@@ -4,25 +4,20 @@ 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 = {
temperatures: [] currentValue: '',
rerenderToggle: false
} }
db.find({ key: { $exists: true } }, (err, persistedTemperatures) => {
if (err) throw err
this.setState({
temperatures: [...persistedTemperatures, ...this.state.temperatures]
})
})
} }
render() { render() {
...@@ -33,26 +28,31 @@ export default class Temp extends Component { ...@@ -33,26 +28,31 @@ 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 = { console.log(Number(this.state.currentValue))
value: this.state.currentValue, saveTemperature(
key: Date.now().toString() new Date(),
} {
this.setState({ value: Number(this.state.currentValue),
temperatures: [newTemp, ...this.state.temperatures] exclude: false
}) }
db.insert(newTemp, (err) => { )
if (err) console.log(err) this.setState({currentValue: ''})
}) // FlatList only reacts to primitive value changes,
// this boolean toggle makes sure the list updates
this.setState({ reRender: !this.state.rerenderToggle})
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>} extraData = { this.state }
/> />
</View> </View>
) )
......
This diff is collapsed.
...@@ -17,8 +17,9 @@ ...@@ -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",
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment