Skip to content

Commit 8aceea9

Browse files
committed
Fix temperature unit init
1 parent 9d27987 commit 8aceea9

5 files changed

+64
-53
lines changed

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.7.10
1+
1.7.11

lib/widgets/home_page.dart

+43-41
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ class HomePage extends StatefulWidget {
2222
}
2323

2424
class _HomePageState extends State<HomePage> {
25+
TemperatureUnit _temperatureUnit = form.defaultTemperatureUnit;
2526
double _inoculation = 0;
2627
double _bulkRise = 0;
2728
double _flour = 0;
2829
double _water = 0;
2930
double _levain = 0;
3031
double _salt = 0;
31-
TemperatureUnit? _temperatureUnit;
3232

3333
final totalWeightController = TextEditingController();
3434
final hydrationController = TextEditingController();
@@ -45,8 +45,8 @@ class _HomePageState extends State<HomePage> {
4545
void _updateFermentationValues() {
4646
final temperature = _parseValue(temperatureController);
4747

48-
_inoculation = getInoculationValue(temperature, _temperatureUnit!);
49-
_bulkRise = getBulkRiseValue(temperature, _temperatureUnit!);
48+
_inoculation = getInoculationValue(temperature, _temperatureUnit);
49+
_bulkRise = getBulkRiseValue(temperature, _temperatureUnit);
5050
}
5151

5252
void _updateFermentationState() {
@@ -79,7 +79,7 @@ class _HomePageState extends State<HomePage> {
7979
.firstWhere((element) => element.value.unit == selection)
8080
.key;
8181
temperatureController.text = convertTemperatureUnit(
82-
_parseValue(temperatureController), _temperatureUnit!)
82+
_parseValue(temperatureController), _temperatureUnit)
8383
.toStringAsFixed(1)
8484
.replaceFirst('.0', '');
8585
_storeTemperatureUnit();
@@ -117,14 +117,13 @@ class _HomePageState extends State<HomePage> {
117117
saltController.text =
118118
await getInitialOrDefaultValue(saltLevelKey, form.defaultSaltLevel);
119119

120-
_updateFermentationValues();
121-
_updateIngredientsValues();
120+
_updateFermentationState();
121+
_updateIngredientsState();
122122
}
123123

124124
@override
125125
initState() {
126126
_loadInitialValues();
127-
128127
super.initState();
129128
}
130129

@@ -203,37 +202,40 @@ class _HomePageState extends State<HomePage> {
203202
fontSize:
204203
Theme.of(context).textTheme.bodyLarge!.fontSize,
205204
color: Colors.grey.shade800)),
206-
if (_temperatureUnit != null)
207-
UnitChoice(
208-
a11yLabel: locale.a11yTemperatureUnitChoiceLabel,
209-
unitList: temperatureUnitMap.values
210-
.map((element) => UnitSingleChoiceDescriptor(
211-
value: element.unit,
212-
tooltip: element.description))
213-
.toList(),
214-
initialUnitValue:
215-
temperatureUnitMap[_temperatureUnit]!.unit,
216-
onSelectionChanged: _updateTemperatureUnit),
205+
UnitChoice(
206+
a11yLabel: locale.a11yTemperatureUnitChoiceLabel,
207+
unitList: temperatureUnitMap.values
208+
.map((element) => UnitSingleChoiceDescriptor(
209+
value: element.unit,
210+
tooltip: element.description))
211+
.toList(),
212+
getInitialUnitValue: () async {
213+
return await getInitialOrDefaultValue(
214+
temperatureUnitKey,
215+
form.defaultTemperatureUnitValue);
216+
},
217+
onSelectionChanged: (selection) {
218+
_updateTemperatureUnit(selection);
219+
_storeTemperatureValue();
220+
}),
221+
],
222+
),
223+
Row(
224+
children: <TextFieldWithAffixes>[
225+
TextFieldWithAffixes(
226+
paddingTop: 10,
227+
controller: temperatureController,
228+
prefixText: locale.inputPrefixTemperature,
229+
suffixText: temperatureUnitMap[_temperatureUnit]!.unit,
230+
tooltip: locale.inputTooltipTemperature,
231+
maxValue: form.maxValueTemperatureMap[_temperatureUnit]!,
232+
onChangedCallbacks: [
233+
_updateFermentationState,
234+
_storeTemperatureValue
235+
],
236+
),
217237
],
218238
),
219-
if (_temperatureUnit != null)
220-
Row(
221-
children: <TextFieldWithAffixes>[
222-
TextFieldWithAffixes(
223-
paddingTop: 10,
224-
controller: temperatureController,
225-
prefixText: locale.inputPrefixTemperature,
226-
suffixText: temperatureUnitMap[_temperatureUnit]!.unit,
227-
tooltip: locale.inputTooltipTemperature,
228-
maxValue:
229-
form.maxValueTemperatureMap[_temperatureUnit]!,
230-
onChangedCallbacks: [
231-
_updateFermentationState,
232-
_storeTemperatureValue
233-
],
234-
),
235-
],
236-
),
237239
Row(
238240
children: <TextFieldWithAffixes>[
239241
TextFieldWithAffixes(
@@ -252,11 +254,11 @@ class _HomePageState extends State<HomePage> {
252254
Row(
253255
children: <TextFieldWithAffixes>[
254256
TextFieldWithAffixes(
255-
controller: hydrationController,
256-
prefixText: locale.inputPrefixHydration,
257-
suffixText: locale.unitPercent,
258-
tooltip: locale.inputTooltipHydration,
259-
maxValue: form.maxValueHydration,
257+
controller: hydrationController,
258+
prefixText: locale.inputPrefixHydration,
259+
suffixText: locale.unitPercent,
260+
tooltip: locale.inputTooltipHydration,
261+
maxValue: form.maxValueHydration,
260262
onChangedCallbacks: [
261263
_updateIngredientsState,
262264
_storeHydrationValue

lib/widgets/unit_choice_segmented_button.dart

+18-9
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,32 @@ class UnitChoice<T extends Enum> extends StatefulWidget {
1212
super.key,
1313
required this.a11yLabel,
1414
required this.unitList,
15-
required this.initialUnitValue,
15+
required this.getInitialUnitValue,
1616
required this.onSelectionChanged,
1717
});
1818

1919
final String a11yLabel;
2020
final List<UnitSingleChoiceDescriptor> unitList;
21-
final String initialUnitValue;
22-
final Function(String) onSelectionChanged;
21+
final Future<String> Function() getInitialUnitValue;
22+
final void Function(String) onSelectionChanged;
2323

2424
@override
2525
State<UnitChoice> createState() => _UnitChoiceState<T>();
2626
}
2727

2828
class _UnitChoiceState<T extends Enum> extends State<UnitChoice> {
29-
String? unit;
29+
String? _unit;
30+
31+
Future<void> _loadInitialValue() async {
32+
final String initial = await widget.getInitialUnitValue();
33+
setState(() {
34+
_unit = initial;
35+
});
36+
}
3037

3138
@override
3239
void initState() {
33-
unit = widget.initialUnitValue;
40+
_loadInitialValue();
3441
super.initState();
3542
}
3643

@@ -40,7 +47,7 @@ class _UnitChoiceState<T extends Enum> extends State<UnitChoice> {
4047
focusable: true,
4148
inMutuallyExclusiveGroup: true,
4249
label: widget.a11yLabel,
43-
value: unit,
50+
value: _unit,
4451
child: SegmentedButton(
4552
multiSelectionEnabled: false,
4653
showSelectedIcon: true,
@@ -56,11 +63,13 @@ class _UnitChoiceState<T extends Enum> extends State<UnitChoice> {
5663
style: Theme.of(context).textTheme.bodyLarge),
5764
tooltip: widget.unitList.last.tooltip),
5865
],
59-
selected: {unit},
66+
selected: {_unit},
6067
onSelectionChanged: (Set newSelection) {
6168
setState(() {
62-
unit = newSelection.first;
63-
widget.onSelectionChanged(unit!);
69+
if (_unit != null) {
70+
widget.onSelectionChanged(newSelection.first);
71+
}
72+
_unit = newSelection.first;
6473
});
6574
},
6675
));

web/download-release-asset.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<head>
44
<title>Download Sourdoc release asset</title>
55
<script>
6-
const version = '1.7.10';
6+
const version = '1.7.11';
77
const build = new URLSearchParams(window.location.search).get('build');
88
window.location.href = 'https://github.com/duddu/sourdoc/releases/download/v' + version + '/' + build + '-sourdoc-v' + version + '.tgz';
99
</script>

web/index.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<script async src="https://www.googletagmanager.com/gtag/js?id=G-ZS2J7KXZ0Q"></script>
2828
<script defer src="gtm-gtag.js"></script>
2929
<script>
30-
const serviceWorkerVersion = '1.7.10';
30+
const serviceWorkerVersion = '1.7.11';
3131
</script>
3232
<script defer src="flutter.js"></script>
3333
</head>

0 commit comments

Comments
 (0)