-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat/long term quick adaptation screen
- Loading branch information
1 parent
fa6b6eb
commit 9a36148
Showing
5 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import React from 'react' | ||
import {Appearance, ScrollView, StatusBar, View} from 'react-native' | ||
import {SafeAreaView} from 'react-native-safe-area-context' | ||
import {screenStyles} from './styles' | ||
import {Colors} from 'react-native/Libraries/NewAppScreen' | ||
import {MealGlycemiaMeasure, QuickInsulin} from '../core/QuickInsulin' | ||
import {Text} from 'react-native-paper' | ||
import {DbNumericTextInput} from '../components/DbNumericTextInputComponent' | ||
|
||
interface QuickInsulinAdaptationState { | ||
glycemiasAfter: number[] | ||
adaptation: number | undefined | ||
} | ||
|
||
export class QuickInsulinAdaptationScreen extends React.Component< | ||
{}, | ||
QuickInsulinAdaptationState | ||
> { | ||
constructor(props: {}) { | ||
super(props) | ||
this.state = { | ||
glycemiasAfter: new Array(3), | ||
adaptation: undefined, | ||
} | ||
} | ||
|
||
manageComputation = () => { | ||
const containsInvalidElement = (numbers: number[]): boolean => { | ||
for (let n of numbers) { | ||
if (n === undefined || isNaN(n)) { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
if (containsInvalidElement(this.state.glycemiasAfter)) { | ||
this.setState({ | ||
adaptation: undefined, | ||
}) | ||
return | ||
} | ||
|
||
let quickInsulin = new QuickInsulin() | ||
|
||
let objective = quickInsulin.findObjectiveCriterion() | ||
if ( | ||
objective === undefined || | ||
objective.min === undefined || | ||
objective.max === undefined | ||
) { | ||
throw new Error('Check your configuration. Objective not found') | ||
} | ||
let lObjective: {min: number; max: number} = objective | ||
let measures = this.state.glycemiasAfter.map( | ||
g => new MealGlycemiaMeasure(g, lObjective), | ||
) | ||
let adaptation = quickInsulin.computeLongtermAdaptation(measures) | ||
this.setState({ | ||
adaptation: adaptation, | ||
}) | ||
} | ||
|
||
render(): React.ReactNode { | ||
const manageGlycemiaAfter = (i: number, glycemiaStr: string) => { | ||
let newAfter = Array.from(this.state.glycemiasAfter) | ||
newAfter[i] = parseFloat(glycemiaStr) | ||
|
||
this.setState( | ||
{ | ||
glycemiasAfter: newAfter, | ||
}, | ||
this.manageComputation, | ||
) | ||
} | ||
|
||
const glycemiaIntervalInputs = [] | ||
for (let i = 0; i < 3; i++) { | ||
glycemiaIntervalInputs.push( | ||
<View key={i} style={screenStyles.intervalContainer}> | ||
<Text style={screenStyles.intervalLabel}> | ||
Glycemia measure {i + 1}:{' '} | ||
</Text> | ||
<DbNumericTextInput | ||
testID={'glycemiaAfterInput' + (i + 1)} | ||
placeholder={'glycemia after ' + (i + 1)} | ||
onChangeText={newtText => { | ||
manageGlycemiaAfter(i, newtText) | ||
}} | ||
/> | ||
</View>, | ||
) | ||
} | ||
|
||
const isDarkMode = Appearance.getColorScheme() === 'dark' | ||
|
||
const backgroundStyle = { | ||
backgroundColor: isDarkMode ? Colors.darker : Colors.lighter, | ||
} | ||
|
||
return ( | ||
<SafeAreaView style={screenStyles.screenBackground}> | ||
<StatusBar | ||
barStyle={isDarkMode ? 'light-content' : 'dark-content'} | ||
backgroundColor={backgroundStyle.backgroundColor} | ||
/> | ||
<ScrollView | ||
contentInsetAdjustmentBehavior="automatic" | ||
style={backgroundStyle}> | ||
<View>{glycemiaIntervalInputs}</View> | ||
<View> | ||
{this.state.adaptation !== undefined && ( | ||
<Text testID='adaptationResult' style={screenStyles.intervalLabel}> | ||
Adaptation: {this.state.adaptation} | ||
</Text> | ||
)} | ||
</View> | ||
</ScrollView> | ||
</SafeAreaView> | ||
) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/screens/__tests__/QuickInsulinAdaptationScreen.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import 'react-native' | ||
import React from 'react' | ||
|
||
// Note: import explicitly to use the types shiped with jest. | ||
import {it, expect} from '@jest/globals' | ||
|
||
// Note: test renderer must be required after react-native. | ||
//import renderer from 'react-test-renderer'; | ||
import {fireEvent, render, screen} from '@testing-library/react-native' | ||
import {BasalAdaptationScreen} from '../BasalAdaptationScreen' | ||
import { QuickInsulinAdaptationScreen } from '../QuickInsulinAdaptationScreen' | ||
|
||
it('renders correctly', () => { | ||
let SampleScreen = () => <QuickInsulinAdaptationScreen /> | ||
|
||
render(<SampleScreen />) | ||
|
||
for (let i = 1; i < 4; i++) { | ||
expect(screen.getByTestId('glycemiaAfterInput' + i)).toBeDefined() | ||
} | ||
expect(() => screen.getByTestId('adaptationResult')).toThrowError() | ||
}) | ||
|
||
it('display result when all glycemia levels are entered', () => { | ||
let SampleScreen = () => <QuickInsulinAdaptationScreen /> | ||
|
||
render(<SampleScreen />) | ||
|
||
for (let i = 1; i < 4; i++) { | ||
let currentBeforeInput = screen.getByTestId('glycemiaAfterInput' + i) | ||
fireEvent.changeText(currentBeforeInput, '1.2') | ||
} | ||
|
||
expect(screen.getByTestId('adaptationResult').props.children).toBeDefined() | ||
expect(screen.getByTestId('adaptationResult').props.children).toEqual(["Adaptation: ", 0]) | ||
}) |