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 {
}
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
......
......@@ -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<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage()
new MainReactPackage(),
new RealmReactPackage()
);
}
......
rootProject.name = 'drip'
include ':realm'
project(':realm').projectDir = new File(rootProject.projectDir, '../node_modules/realm/android')
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 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 {
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: []
currentValue: '',
rerenderToggle: false
}
db.find({ key: { $exists: true } }, (err, persistedTemperatures) => {
if (err) throw err
this.setState({
temperatures: [...persistedTemperatures, ...this.state.temperatures]
})
})
}
render() {
......@@ -33,26 +28,31 @@ export default class Temp extends Component {
onChangeText={(val) => {
this.setState({currentValue: val})
}}
keyboardType='numeric'
value = {this.state.currentValue}
/>
<Button
onPress={() => {
const newTemp = {
value: this.state.currentValue,
key: Date.now().toString()
}
this.setState({
temperatures: [newTemp, ...this.state.temperatures]
})
db.insert(newTemp, (err) => {
if (err) console.log(err)
})
console.log(Number(this.state.currentValue))
saveTemperature(
new Date(),
{
value: Number(this.state.currentValue),
exclude: false
}
)
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"
/>
<FlatList
data = {this.state.temperatures}
extraData = {this.state}
renderItem={({item}) => <Text>{item.value}</Text>}
data = { cycleDaysSortedbyTempValueView }
renderItem={({item}) => <Text>{item.temperature.value}</Text>}
extraData = { this.state }
/>
</View>
)
......
This diff is collapsed.
......@@ -17,8 +17,9 @@
"moment": "^2.22.1",
"react": "16.3.1",
"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": {
"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