Skip to content

Commit

Permalink
feat/no more button on punctual insulin screen
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-perpignane committed Oct 11, 2023
1 parent 6724711 commit 6461420
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 147 deletions.
55 changes: 0 additions & 55 deletions src/components/SectionComponent.tsx

This file was deleted.

6 changes: 5 additions & 1 deletion src/core/Acetone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ export class Acetone {

public computeAdaptation(acetoneLevel: number): number {
if (!this.acetoneAdaptationByLevel.has(acetoneLevel)) {
throw new Error('unknown acetone level: {acetoneLevel}')
let validLevels = [...this.acetoneAdaptationByLevel.keys()].join(', ')

throw new Error(
`Invalid acetone level: ${acetoneLevel}. Valid levels are: ${validLevels}`,
)
}

return this.acetoneAdaptationByLevel.get(acetoneLevel)
Expand Down
19 changes: 16 additions & 3 deletions src/core/QuickInsulin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ import conditions from './quick_insulin_conditions.json'
export class PuntualAdaptationResult {
private _glycemiaAdaptation: number
private _acetoneAdaptation: number
private _checkAcetone: boolean

constructor(glycemiaAdapation: number, acetoneAdaptation: number) {
constructor(
glycemiaAdapation: number,
acetoneAdaptation: number,
checkAcetone: boolean,
) {
this._glycemiaAdaptation = glycemiaAdapation
this._acetoneAdaptation = acetoneAdaptation
this._checkAcetone = checkAcetone
}

public get glycemiaAdaptation() {
Expand All @@ -21,6 +27,10 @@ export class PuntualAdaptationResult {
public get totalAdaptation() {
return this.acetoneAdaptation + this.glycemiaAdaptation
}

public get checkAcetone() {
return this._checkAcetone
}
}

export class QuickInsulin {
Expand All @@ -37,11 +47,11 @@ export class QuickInsulin {

if (!glycemiaCondition) {
throw new Error(
'No adaptation found. Check quick insulin conditions config',
'No adaptation found. Check quick insulin conditions configuration',
)
}

if (glycemiaCondition.checkAcetone && acetoneLevel == null) {
if (glycemiaCondition.checkAcetone && acetoneLevel === undefined) {
throw new AcetoneNeededError('Please provide acetone level')
}

Expand All @@ -55,6 +65,9 @@ export class QuickInsulin {
return new PuntualAdaptationResult(
glycemiaCondition.adaptation,
acetoneAdaptation,
glycemiaCondition.checkAcetone === undefined
? false
: glycemiaCondition.checkAcetone,
)
}
}
Expand Down
137 changes: 68 additions & 69 deletions src/screens/PunctualGlycemiaScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
QuickInsulin,
} from '../core/QuickInsulin'
import {DbNumericTextInput} from '../components/DbNumericTextInputComponent'
import {DbButton} from '../components/DbButtonComponent'
import {screenStyles} from './styles'

interface PunctualGlycemiaState {
Expand Down Expand Up @@ -51,82 +50,105 @@ export class PunctualGlycemiaScreen extends React.Component<
`
}

setGlycemiaLevel = (glycemiaLevel: number | undefined): void => {
private manageInvalidGlycemia = () => {
this.setVisibleAcetone(false)
this.setAcetoneLevel(undefined)
this.setErrorMessage('Invalid glycemia level')
this.setPunctualAdaptationResult(null)
}

private setGlycemiaLevel = (glycemiaLevel: number | undefined): void => {
console.log('setting glycemia level')
this.setState({
glycemiaLevel: glycemiaLevel,
})
}

setAcetoneLevel = (acetoneLevel: number | undefined): void => {
private setAcetoneLevel = (acetoneLevel: number | undefined): void => {
this.setState({
acetoneLevel: acetoneLevel,
})
}

manageNumberInput = (
strValue: string,
setValueFunction: (value: number | undefined) => void,
): void => {
if (!strValue) {
setValueFunction(undefined)
private computeAdaptation = (
glycemiaLevel: number,
acetoneLevel: number | undefined,
) => {
let quickInsulin = new QuickInsulin()
try {
let lPunctualAdaptationResult = quickInsulin.computePunctualAdaptation(
glycemiaLevel,
acetoneLevel,
)
this.setPunctualAdaptationResult(lPunctualAdaptationResult)
this.setVisibleAcetone(lPunctualAdaptationResult.checkAcetone)
this.setErrorMessage('')
} catch (e: any) {
if (e instanceof AcetoneNeededError) {
try {
Vibration.vibrate()
} catch (ve) {}
this.setVisibleAcetone(true)
this.setErrorMessage('Please provide acetone level')
} else {
this.setErrorMessage(e.toString())
}
this.setPunctualAdaptationResult(null)
}
}

private manageGlycemiaLevelInput = (newText: string) => {
let glycemiaLevel = this.manageNumberInput(newText)

if (glycemiaLevel === undefined) {
this.manageInvalidGlycemia()
return
}

this.setGlycemiaLevel(glycemiaLevel)

this.computeAdaptation(glycemiaLevel, this.state.acetoneLevel)
}

private manageAcetoneLevelInput = (newText: string) => {
let acetoneLevel = this.manageNumberInput(newText)
this.setAcetoneLevel(acetoneLevel)
if (this.state.glycemiaLevel) {
this.computeAdaptation(this.state.glycemiaLevel, acetoneLevel)
}
}

private manageNumberInput = (strValue: string): number | undefined => {
if (!strValue) {
return undefined
}
let numberValue: number = Number(strValue)
if (isNaN(numberValue) || !isFinite(numberValue)) {
this.setErrorMessage('Invalid number')
return undefined
} else {
setValueFunction(numberValue)
this.manageValidation()
this.setErrorMessage('')
return numberValue
}
}

setPunctualAdaptationResult = (
private setPunctualAdaptationResult = (
punctualAdaptationResult: PuntualAdaptationResult | null,
): void => {
this.setState({
punctualAdaptationResult: punctualAdaptationResult,
})
}

setVisibleAcetone = (visible: boolean) => {
private setVisibleAcetone = (visible: boolean) => {
this.setState({visibleAcetone: visible})
}

setErrorMessage = (errorMessage: string) => {
private setErrorMessage = (errorMessage: string) => {
this.setState({
errorMessage: errorMessage,
})
}

manageValidation = (): void => {
if (this.state.glycemiaLevel === undefined) {
this.setPunctualAdaptationResult(null)
return
}

let quickInsulin = new QuickInsulin()
try {
let lPunctualAdaptationResult = quickInsulin.computePunctualAdaptation(
this.state.glycemiaLevel,
this.state.acetoneLevel,
)
this.setPunctualAdaptationResult(lPunctualAdaptationResult)
} catch (e) {
if (e instanceof AcetoneNeededError) {
try {
Vibration.vibrate()
} catch (ve) {}

this.setVisibleAcetone(true)
} else {
this.setAcetoneLevel(undefined)
this.setVisibleAcetone(false)
this.setPunctualAdaptationResult(null)
}
}
}

render(): JSX.Element {
const isDarkMode = Appearance.getColorScheme() === 'dark'

Expand All @@ -147,16 +169,8 @@ export class PunctualGlycemiaScreen extends React.Component<
<Text>Glycemia level:</Text>
<DbNumericTextInput
id="glycemia"
keyboardType="numeric"
onChangeText={newText =>
this.manageNumberInput(newText, this.setGlycemiaLevel)
}
onChangeText={newText => this.manageGlycemiaLevelInput(newText)}
testID="glycemiaInput"
defaultValue={
this.state.glycemiaLevel == null
? ''
: this.state.glycemiaLevel.toString()
}
/>
</View>

Expand All @@ -166,31 +180,16 @@ export class PunctualGlycemiaScreen extends React.Component<
<DbNumericTextInput
id="acetone"
placeholder="Enter acetone level"
onChangeText={newText =>
this.manageNumberInput(newText, this.setAcetoneLevel)
}
onChangeText={newText => this.manageAcetoneLevelInput(newText)}
testID="acetoneInput"
defaultValue={
this.state.acetoneLevel == null
? ''
: this.state.acetoneLevel.toString()
}
/>
</View>
)}

<View>
<DbButton
title="Validate"
testID="validateButton"
onPress={this.manageValidation}
/>
</View>

{this.state.punctualAdaptationResult && (
<View>
<Text testID="adaptationText">
{this.state.punctualAdaptationResult?.totalAdaptation?.toString()}
{this.state.punctualAdaptationResult.totalAdaptation.toString()}
</Text>
</View>
)}
Expand Down
Loading

0 comments on commit 6461420

Please sign in to comment.