Fix case for no previous bleeding day. Use new getCycleDay API
This commit is contained in:
+2
-2
@@ -11,9 +11,9 @@ import { saveBleeding } from './db'
|
||||
import { formatDateForViewHeader } from './format'
|
||||
import { bleeding as labels } from './labels'
|
||||
import cycleDayModule from './get-cycle-day-number'
|
||||
import { getCycleDaysSortedByDateView } from './db'
|
||||
import { bleedingDaysSortedByDate } from './db'
|
||||
|
||||
const getCycleDay = cycleDayModule(getCycleDaysSortedByDateView())
|
||||
const getCycleDay = cycleDayModule(bleedingDaysSortedByDate)
|
||||
|
||||
export default class Bleeding extends Component {
|
||||
constructor(props) {
|
||||
|
||||
+2
-2
@@ -8,9 +8,9 @@ import styles from './styles'
|
||||
import { formatDateForViewHeader } from './format'
|
||||
import { bleeding as labels} from './labels'
|
||||
import cycleDayModule from './get-cycle-day-number'
|
||||
import { getCycleDaysSortedByDateView } from './db'
|
||||
import { bleedingDaysSortedByDate } from './db'
|
||||
|
||||
const getCycleDay = cycleDayModule(getCycleDaysSortedByDateView())
|
||||
const getCycleDay = cycleDayModule(bleedingDaysSortedByDate)
|
||||
|
||||
export default class DayView extends Component {
|
||||
constructor(props) {
|
||||
|
||||
+5
-11
@@ -4,15 +4,10 @@ const LocalDate = joda.LocalDate
|
||||
|
||||
export default function config(bleedingDaysSortedByDateView, opts) {
|
||||
opts = opts || {
|
||||
// at the very minimum, a cycle can be a bleeding day
|
||||
// followed by a non-bleeding day, thus a length of 2
|
||||
maxBreakInBleeding: 1
|
||||
}
|
||||
|
||||
return function getCycleDayNumber(targetDateString) {
|
||||
// sort the cycle days in descending order so we travel into
|
||||
// the past as we iterate over the array
|
||||
// also, to retrieve the number, we only need the cycle days before the target day
|
||||
const targetDate = LocalDate.parse(targetDateString)
|
||||
const withWrappedDates = bleedingDaysSortedByDateView
|
||||
.filter(day => !day.bleeding.exclude)
|
||||
@@ -20,16 +15,15 @@ export default function config(bleedingDaysSortedByDateView, opts) {
|
||||
day.wrappedDate = LocalDate.parse(day.date)
|
||||
return day
|
||||
})
|
||||
// TODO write test for what if there is no first day before?? aka no firstbleedingdaybeforeindex
|
||||
|
||||
const firstBleedingDayBeforeTargetDayIndex = withWrappedDates.findIndex(day => day.wrappedDate.isBefore(targetDate))
|
||||
const cycleDays = withWrappedDates.slice(firstBleedingDayBeforeTargetDayIndex)
|
||||
if (firstBleedingDayBeforeTargetDayIndex < 0) return null
|
||||
const previousBleedingDays = withWrappedDates.slice(firstBleedingDayBeforeTargetDayIndex)
|
||||
|
||||
const lastPeriodStart = cycleDays.find((day, i) => {
|
||||
return thereIsNoPreviousBleedingDayWithinTheThreshold(day, cycleDays.slice(i + 1), opts.maxBreakInBleeding)
|
||||
const lastPeriodStart = previousBleedingDays.find((day, i) => {
|
||||
return thereIsNoPreviousBleedingDayWithinTheThreshold(day, previousBleedingDays.slice(i + 1), opts.maxBreakInBleeding)
|
||||
})
|
||||
|
||||
if (!lastPeriodStart) return null
|
||||
|
||||
const diffInDays = lastPeriodStart.wrappedDate.until(targetDate, joda.ChronoUnit.DAYS)
|
||||
|
||||
// cycle starts at day 1
|
||||
|
||||
@@ -6,22 +6,27 @@ import {
|
||||
} from 'react-native'
|
||||
import styles from './styles'
|
||||
import cycleDayModule from './get-cycle-day-number'
|
||||
import { getCycleDaysSortedByDateView, deleteAll } from './db'
|
||||
import { bleedingDaysSortedByDate, deleteAll } from './db'
|
||||
import { LocalDate } from 'js-joda'
|
||||
|
||||
const cycleDaysSortedByDateView = getCycleDaysSortedByDateView()
|
||||
const getCycleDayNumber = cycleDayModule(cycleDaysSortedByDateView)
|
||||
const now = new Date()
|
||||
const cycleDayNumber = getCycleDayNumber(LocalDate.of(now.getFullYear(), now.getMonth() + 1, now.getDate()))
|
||||
const welcomeTextWithCycleDay = `Welcome! Today is day ${cycleDayNumber} of your current cycle`
|
||||
const welcomeText = `Welcome! We don't have enough information to know what your current cycle day is`
|
||||
const getCycleDayNumber = cycleDayModule(bleedingDaysSortedByDate)
|
||||
|
||||
export default class Home extends Component {
|
||||
constructor(props) {
|
||||
super(props)
|
||||
const now = new Date()
|
||||
this.todayDateString = LocalDate.of(now.getFullYear(), now.getMonth() + 1, now.getDate()).toString()
|
||||
const cycleDayNumber = getCycleDayNumber(this.todayDateString)
|
||||
|
||||
this.state = {
|
||||
welcomeText: cycleDayNumber ? welcomeTextWithCycleDay : welcomeText
|
||||
welcomeText: determineWelcomeText(cycleDayNumber)
|
||||
}
|
||||
|
||||
bleedingDaysSortedByDate.addListener(setStateWithCurrentWelcomeText.bind(this))
|
||||
}
|
||||
|
||||
componentWillUnmount() {
|
||||
bleedingDaysSortedByDate.removeListener(setStateWithCurrentWelcomeText)
|
||||
}
|
||||
|
||||
render() {
|
||||
@@ -45,3 +50,13 @@ export default class Home extends Component {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
function determineWelcomeText(cycleDayNumber) {
|
||||
const welcomeTextWithCycleDay = `Welcome! Today is day ${cycleDayNumber} of your current cycle`
|
||||
const welcomeText = `Welcome! We don't have enough information to know what your current cycle day is`
|
||||
return cycleDayNumber ? welcomeTextWithCycleDay : welcomeText
|
||||
}
|
||||
|
||||
function setStateWithCurrentWelcomeText() {
|
||||
this.setState({ welcomeText: determineWelcomeText(getCycleDayNumber(this.todayDateString)) })
|
||||
}
|
||||
Reference in New Issue
Block a user