Introduce realm and persist temperatures
This commit is contained in:
@@ -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()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
rootProject.name = 'drip'
|
||||
include ':realm'
|
||||
project(':realm').projectDir = new File(rootProject.projectDir, '../node_modules/realm/android')
|
||||
|
||||
include ':app'
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
})
|
||||
@@ -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}
|
||||
/>
|
||||
<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)
|
||||
})
|
||||
saveTemperature(
|
||||
new Date(),
|
||||
{
|
||||
value: Number(this.state.currentValue),
|
||||
exclude: false
|
||||
}
|
||||
)
|
||||
this.setState({currentValue: ''})
|
||||
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>}
|
||||
/>
|
||||
</View>
|
||||
)
|
||||
|
||||
Generated
+1101
-195
File diff suppressed because it is too large
Load Diff
+3
-2
@@ -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",
|
||||
|
||||
Reference in New Issue
Block a user