Add edit bleeding and persist it.
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import realm from 'realm'
|
||||
import moment from 'moment'
|
||||
import { v4 as uuid } from 'uuid'
|
||||
|
||||
let db
|
||||
let cycleDaysSortedbyTempValueView = []
|
||||
let cycleDaysSortedbyDate = []
|
||||
|
||||
const TemperatureSchema = {
|
||||
name: 'Temperature',
|
||||
@@ -12,6 +14,14 @@ const TemperatureSchema = {
|
||||
}
|
||||
}
|
||||
|
||||
const BleedingSchema = {
|
||||
name: 'Bleeding',
|
||||
properties: {
|
||||
value: 'int',
|
||||
exclude: 'bool'
|
||||
}
|
||||
}
|
||||
|
||||
const CycleDaySchema = {
|
||||
name: 'CycleDay',
|
||||
primaryKey: 'key',
|
||||
@@ -21,6 +31,10 @@ const CycleDaySchema = {
|
||||
temperature: {
|
||||
type: 'Temperature',
|
||||
optional: true
|
||||
},
|
||||
bleeding: {
|
||||
type: 'Bleeding',
|
||||
optional: true
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,7 +43,8 @@ async function openDatabase() {
|
||||
db = await realm.open({
|
||||
schema: [
|
||||
CycleDaySchema,
|
||||
TemperatureSchema
|
||||
TemperatureSchema,
|
||||
BleedingSchema
|
||||
],
|
||||
// we only want this in dev mode
|
||||
deleteRealmIfMigrationNeeded: true
|
||||
@@ -37,9 +52,10 @@ async function openDatabase() {
|
||||
// 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)
|
||||
cycleDaysSortedbyDate = db.objects('CycleDay').sorted('date', true)
|
||||
}
|
||||
|
||||
async function saveTemperature(date, temperature) {
|
||||
function saveTemperature(date, temperature) {
|
||||
db.write(() => {
|
||||
const doc = {
|
||||
key: uuid(),
|
||||
@@ -50,8 +66,29 @@ async function saveTemperature(date, temperature) {
|
||||
})
|
||||
}
|
||||
|
||||
function saveBleeding(cycleDay, bleeding) {
|
||||
db.write(() => {
|
||||
cycleDay.bleeding = bleeding
|
||||
})
|
||||
}
|
||||
|
||||
function getOrCreateCycleDay(date) {
|
||||
let result = Array.from(cycleDaysSortedbyDate.filtered('date = $0', date))[0]
|
||||
if (!result) {
|
||||
db.write(() => {
|
||||
result = db.create('CycleDay', {
|
||||
key: uuid(),
|
||||
date
|
||||
})
|
||||
})
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
export {
|
||||
cycleDaysSortedbyTempValueView,
|
||||
openDatabase,
|
||||
saveTemperature
|
||||
saveTemperature,
|
||||
saveBleeding,
|
||||
getOrCreateCycleDay
|
||||
}
|
||||
Reference in New Issue
Block a user