Newer
Older
import { ScrollView, View, TouchableOpacity, TouchableHighlight, Dimensions } from 'react-native'
import { LocalDate, ChronoUnit } from 'js-joda'
import Svg, { G } from 'react-native-svg'
import { secondaryColor, cycleDayColor, periodColor } from '../styles'
import { home as labels, bleedingPrediction as predictLabels, shared } from './labels'
import CycleDayIcon from '../assets/home-circle'
import cycleModule from '../lib/cycle'
import { getOrCreateCycleDay, getCycleDaysSortedByDate } from '../db'
import { getFertilityStatusForDay } from '../lib/sympto-adapter'
import styles from '../styles'
import AppText, { AppTextLight } from './app-text'
import nothingChanged from '../db/db-unchanged'
export default class Home extends Component {
this.getCycleDayNumber = cycleModule().getCycleDayNumber
this.getBleedingPrediction = cycleModule().getPredictedMenses
this.todayDateString = LocalDate.now().toString()
const prediction = this.getBleedingPrediction()
const fertilityStatus = getFertilityStatusForDay(this.todayDateString)
cycleDayNumber: this.getCycleDayNumber(this.todayDateString),
predictionText: determinePredictionText(prediction),
bleedingPredictionRange: getBleedingPredictionRange(prediction),
...fertilityStatus
this.cycleDays = getCycleDaysSortedByDate()
this.cycleDays.addListener(this.updateState)
updateState = (_, changes) => {
if (nothingChanged(changes)) return
const prediction = this.getBleedingPrediction()
const fertilityStatus = getFertilityStatusForDay(this.todayDateString)
cycleDayNumber: this.getCycleDayNumber(this.todayDateString),
predictionText: determinePredictionText(prediction),
bleedingPredictionRange: getBleedingPredictionRange(prediction),
...fertilityStatus
}
componentWillUnmount() {
const todayDateString = LocalDate.now().toString()
const cycleDay = getOrCreateCycleDay(todayDateString)
const navigate = this.props.navigate
const cycleDayMoreText = this.state.cycleDayNumber ?
labels.cycleDayKnown(this.state.cycleDayNumber)
:
labels.cycleDayNotEnoughInfo
const {height, width} = Dimensions.get('window')
<View flex={1}>
<ScrollView>
<View
style={styles.homeView}
>
<TouchableOpacity
onPress={() => this.passTodayTo('CycleDay')}
style={styles.homeIconElement}
>
<View position='absolute'>
<Svg
width={80}
height={80}
viewBox='340 345 170 170'
>
<G fill="none" stroke="#1E0B7A" strokeWidth="2">
<CycleDayIcon/>
</G>
</Svg>
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
</View>
<View style={[styles.homeIconTextWrapper, styles.wrapperCycle]}>
<AppTextLight style={styles.iconText}>
{this.state.cycleDayNumber || labels.unknown}
</AppTextLight>
</View>
{ this.state.showMore &&
<AppText style={styles.paragraph}>{cycleDayMoreText}</AppText>
}
<View style={[
styles.homeButton,
{ backgroundColor: cycleDayColor }
]}>
<AppText style={styles.homeButtonText}>
{labels.editToday}
</AppText>
</View>
</TouchableOpacity>
<TouchableOpacity
onPress={() => this.passTodayTo('BleedingEditView')}
style={styles.homeIconElement}
>
<View position='absolute'>
<Drop/>
</View>
<View style={[styles.homeIconTextWrapper, styles.wrapperDrop]}>
<AppTextLight style={styles.iconText}>
{this.state.bleedingPredictionRange}
</AppTextLight>
</View>
{this.state.showMore &&
<AppText style={styles.paragraph}>
{this.state.predictionText}
</AppText>
}
<View style={[
styles.homeButton,
{ backgroundColor: periodColor }
]}>
<AppText style={styles.homeButtonText}>
{labels.trackPeriod}
</AppText>
</View>
</TouchableOpacity>
<TouchableOpacity
onPress={() => this.props.navigate('Chart')}
style={styles.homeIconElement}
>
<View style={styles.homeCircle}>
<AppTextLight style={styles.iconText}>
{this.state.phase ?
this.state.phase.toString()
:
labels.unknown
}
</AppTextLight>
</View>
{this.state.phase &&
<AppTextLight>
{`${labels.phase(this.state.phase)} (${this.state.status})`}
</AppTextLight>
}
{this.state.showMore &&
<AppText styles={styles.paragraph}>
{this.state.statusText}
</AppText>
}
<View style={[
styles.homeButton,
{ backgroundColor: secondaryColor }
]}>
<AppText style={styles.homeButtonText}>
{labels.checkFertility}
</AppText>
</View>
</TouchableOpacity>
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
</ScrollView>
{!this.state.showMore &&
<TouchableHighlight
onPress={() => this.setState({showMore: true})}
style={[styles.showMore, {
top: height / 2 - styles.header.height - 30,
left: width - 40
}]}
>
<View style={{alignItems: 'center'}}>
<AppTextLight>{shared.more}</AppTextLight>
<Icon name='chevron-thin-down' />
</View>
</TouchableHighlight>
}
{this.state.showMore &&
<TouchableHighlight
onPress={() => this.setState({showMore: false})}
style={[styles.showLess, {
top: height / 2 - styles.header.height - 30,
left: 10
}]}
>
<View style={{alignItems: 'center'}}>
<AppTextLight>{shared.less}</AppTextLight>
<Icon name='chevron-thin-down' />
</View>
</TouchableHighlight>
}
</View>
function determinePredictionText(bleedingPrediction) {
if (!bleedingPrediction.length) return predictLabels.noPrediction
const todayDate = LocalDate.now()
const bleedingStart = LocalDate.parse(bleedingPrediction[0][0])
const bleedingEnd = LocalDate.parse(
bleedingPrediction[0][ bleedingPrediction[0].length - 1 ]
)
if (todayDate.isBefore(bleedingStart)) {
todayDate.until(bleedingStart, ChronoUnit.DAYS),
todayDate.until(bleedingEnd, ChronoUnit.DAYS)
)
}
if (todayDate.isAfter(bleedingEnd)) {
return predictLabels.predictionInPast(
bleedingStart.toString(), bleedingEnd.toString()
)
}
const daysToEnd = todayDate.until(bleedingEnd, ChronoUnit.DAYS)
if (daysToEnd === 0) {
} else if (daysToEnd === 1) {
return predictLabels.predictionStartedXDaysLeft(daysToEnd)
function getBleedingPredictionRange(prediction) {
if (!prediction.length) return labels.unknown
const todayDate = LocalDate.now()
const bleedingStart = LocalDate.parse(prediction[0][0])
const bleedingEnd = LocalDate.parse(prediction[0][ prediction[0].length - 1 ])
if (todayDate.isBefore(bleedingStart)) {
return `${todayDate.until(bleedingStart, ChronoUnit.DAYS)}-${todayDate.until(bleedingEnd, ChronoUnit.DAYS)}`
}
if (todayDate.isAfter(bleedingEnd)) {
return labels.unknown
}
return '0'
}