From 6bb6cc114435781426819a72ed7e49b636942841 Mon Sep 17 00:00:00 2001 From: Julia Friesel Date: Fri, 8 Jun 2018 20:55:03 +0200 Subject: [PATCH] Save date as local date string and make it the primary key --- bleeding.js | 1 - calendar.js | 7 ++----- db.js | 16 +++++----------- 3 files changed, 7 insertions(+), 17 deletions(-) diff --git a/bleeding.js b/bleeding.js index fd38fd9..6501246 100644 --- a/bleeding.js +++ b/bleeding.js @@ -11,7 +11,6 @@ import { saveBleeding } from './db' import { formatDateForViewHeader } from './format' import { bleeding as labels } from './labels' import getCycleDay from './get-cycle-day' -import { bleedingDaysSortedByDate } from './db' export default class Bleeding extends Component { constructor(props) { diff --git a/calendar.js b/calendar.js index 2cb6f94..38a8223 100644 --- a/calendar.js +++ b/calendar.js @@ -16,9 +16,7 @@ export default class DatePickView extends Component { } passDateToDayView(result) { - // TODO this also has date as a string, perhaps useful for LocalDateFormat - const date = new Date(result.year, result.month - 1, result.day) - const cycleDay = getOrCreateCycleDay(date) + const cycleDay = getOrCreateCycleDay(result.dateString) const navigate = this.props.navigation.navigate navigate('dayView', { cycleDay }) } @@ -39,8 +37,7 @@ export default class DatePickView extends Component { function getBleedingDaysInCalFormat(bleedingDaysSortedByDate) { const shadesOfRed = ['#ffbaba', '#ff7b7b', '#ff5252', '#ff0000'] return bleedingDaysSortedByDate.reduce((acc, day) => { - const dateString = day.date.toISOString().slice(0, 10) - acc[dateString] = { startingDay: true, endingDay: true, color: shadesOfRed[day.bleeding.value] } + acc[day.date] = { startingDay: true, endingDay: true, color: shadesOfRed[day.bleeding.value] } return acc }, {}) } \ No newline at end of file diff --git a/db.js b/db.js index b1a6406..dd2648e 100644 --- a/db.js +++ b/db.js @@ -1,9 +1,7 @@ import realm from 'realm' -import { v4 as uuid } from 'uuid' let db let cycleDaysSortedbyTempValueView = [] -let cycleDaysSortedbyDate = [] let bleedingDaysSortedByDate = [] const TemperatureSchema = { @@ -24,10 +22,9 @@ const BleedingSchema = { const CycleDaySchema = { name: 'CycleDay', - primaryKey: 'key', + primaryKey: 'date', properties: { - key: 'string', - date: 'date', + date: 'string', temperature: { type: 'Temperature', optional: true @@ -52,14 +49,12 @@ 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').filtered('temperature != null').sorted('temperature.value', true) - cycleDaysSortedbyDate = db.objects('CycleDay').sorted('date', true) bleedingDaysSortedByDate = db.objects('CycleDay').filtered('bleeding != null').sorted('date', true) } function saveTemperature(date, temperature) { db.write(() => { const doc = { - key: uuid(), date, temperature } @@ -73,13 +68,12 @@ function saveBleeding(cycleDay, bleeding) { }) } -function getOrCreateCycleDay(date) { - let result = Array.from(cycleDaysSortedbyDate.filtered('date = $0', date))[0] +function getOrCreateCycleDay(localDate) { + let result = db.objectForPrimaryKey('CycleDay', localDate) if (!result) { db.write(() => { result = db.create('CycleDay', { - key: uuid(), - date + date: localDate }) }) }