mirror of
https://github.com/wger-project/flutter.git
synced 2026-02-18 00:17:48 +01:00
Properly update the rir value when copying from old logs
This commit is contained in:
@@ -17,27 +17,21 @@
|
||||
*/
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/workouts/slot_entry.dart';
|
||||
|
||||
/// Input widget for Reps In Reserve
|
||||
class RiRInputWidget extends StatefulWidget {
|
||||
final _logger = Logger('RiRInputWidget');
|
||||
|
||||
final num? _initialValue;
|
||||
final ValueChanged<String> onChanged;
|
||||
late String dropdownValue;
|
||||
late double _currentSetSliderValue;
|
||||
|
||||
static const SLIDER_START = -0.5;
|
||||
|
||||
RiRInputWidget(this._initialValue, {required this.onChanged}) {
|
||||
dropdownValue = _initialValue != null ? _initialValue.toString() : SlotEntry.DEFAULT_RIR;
|
||||
|
||||
// Read string RiR into a double
|
||||
if (_initialValue != null) {
|
||||
_currentSetSliderValue = _initialValue.toDouble();
|
||||
} else {
|
||||
_currentSetSliderValue = SLIDER_START;
|
||||
}
|
||||
_logger.finer('Initializing with initial value: $_initialValue');
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -45,6 +39,28 @@ class RiRInputWidget extends StatefulWidget {
|
||||
}
|
||||
|
||||
class _RiRInputWidgetState extends State<RiRInputWidget> {
|
||||
late double _currentSetSliderValue;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
_currentSetSliderValue = widget._initialValue?.toDouble() ?? RiRInputWidget.SLIDER_START;
|
||||
widget._logger.finer('initState - starting slider value: ${widget._initialValue}');
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(covariant RiRInputWidget oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
|
||||
final newValue = widget._initialValue?.toDouble() ?? RiRInputWidget.SLIDER_START;
|
||||
if (widget._initialValue != oldWidget._initialValue) {
|
||||
widget._logger.finer('didUpdateWidget - new initial value: ${widget._initialValue}');
|
||||
setState(() {
|
||||
_currentSetSliderValue = newValue;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns the string used in the slider
|
||||
String getSliderLabel(double value) {
|
||||
if (value < 0) {
|
||||
@@ -77,15 +93,15 @@ class _RiRInputWidgetState extends State<RiRInputWidget> {
|
||||
Text(AppLocalizations.of(context).rir),
|
||||
Expanded(
|
||||
child: Slider(
|
||||
value: widget._currentSetSliderValue,
|
||||
value: _currentSetSliderValue,
|
||||
min: RiRInputWidget.SLIDER_START,
|
||||
max: (SlotEntry.POSSIBLE_RIR_VALUES.length - 2) / 2,
|
||||
divisions: SlotEntry.POSSIBLE_RIR_VALUES.length - 1,
|
||||
label: getSliderLabel(widget._currentSetSliderValue),
|
||||
label: getSliderLabel(_currentSetSliderValue),
|
||||
onChanged: (double value) {
|
||||
widget.onChanged(mapDoubleToAllowedRir(value));
|
||||
setState(() {
|
||||
widget._currentSetSliderValue = value;
|
||||
_currentSetSliderValue = value;
|
||||
});
|
||||
},
|
||||
),
|
||||
|
||||
@@ -491,12 +491,14 @@ class LogsPastLogsWidget extends StatelessWidget {
|
||||
}
|
||||
|
||||
class LogFormWidget extends ConsumerStatefulWidget {
|
||||
final _logger = Logger('LogFormWidget');
|
||||
|
||||
final PageController controller;
|
||||
final SetConfigData configData;
|
||||
final Log log;
|
||||
final FocusNode focusNode;
|
||||
|
||||
const LogFormWidget({
|
||||
LogFormWidget({
|
||||
super.key,
|
||||
required this.controller,
|
||||
required this.configData,
|
||||
@@ -512,6 +514,7 @@ class _LogFormWidgetState extends ConsumerState<LogFormWidget> {
|
||||
final _form = GlobalKey<FormState>();
|
||||
var _detailed = false;
|
||||
bool _isSaving = false;
|
||||
late Log _log;
|
||||
|
||||
late final TextEditingController _repetitionsController;
|
||||
late final TextEditingController _weightController;
|
||||
@@ -520,6 +523,7 @@ class _LogFormWidgetState extends ConsumerState<LogFormWidget> {
|
||||
void initState() {
|
||||
super.initState();
|
||||
|
||||
_log = widget.log;
|
||||
_repetitionsController = TextEditingController();
|
||||
_weightController = TextEditingController();
|
||||
|
||||
@@ -552,7 +556,13 @@ class _LogFormWidgetState extends ConsumerState<LogFormWidget> {
|
||||
_repetitionsController.text = pastLog.repetitions != null
|
||||
? numberFormat.format(pastLog.repetitions)
|
||||
: '';
|
||||
widget._logger.finer('Setting log repetitions to ${_repetitionsController.text}');
|
||||
|
||||
_weightController.text = pastLog.weight != null ? numberFormat.format(pastLog.weight) : '';
|
||||
widget._logger.finer('Setting log weight to ${_weightController.text}');
|
||||
|
||||
_log.rir = pastLog.rir;
|
||||
widget._logger.finer('Setting log rir to ${_log.rir}');
|
||||
});
|
||||
}
|
||||
|
||||
@@ -578,7 +588,7 @@ class _LogFormWidgetState extends ConsumerState<LogFormWidget> {
|
||||
controller: _repetitionsController,
|
||||
configData: widget.configData,
|
||||
focusNode: widget.focusNode,
|
||||
log: widget.log,
|
||||
log: _log,
|
||||
setStateCallback: (fn) {
|
||||
setState(fn);
|
||||
},
|
||||
@@ -590,7 +600,7 @@ class _LogFormWidgetState extends ConsumerState<LogFormWidget> {
|
||||
controller: _weightController,
|
||||
configData: widget.configData,
|
||||
focusNode: widget.focusNode,
|
||||
log: widget.log,
|
||||
log: _log,
|
||||
setStateCallback: (fn) {
|
||||
setState(fn);
|
||||
},
|
||||
@@ -607,7 +617,7 @@ class _LogFormWidgetState extends ConsumerState<LogFormWidget> {
|
||||
controller: _repetitionsController,
|
||||
configData: widget.configData,
|
||||
focusNode: widget.focusNode,
|
||||
log: widget.log,
|
||||
log: _log,
|
||||
setStateCallback: (fn) {
|
||||
setState(fn);
|
||||
},
|
||||
@@ -616,7 +626,7 @@ class _LogFormWidgetState extends ConsumerState<LogFormWidget> {
|
||||
const SizedBox(width: 8),
|
||||
Flexible(
|
||||
child: RepetitionUnitInputWidget(
|
||||
widget.log.repetitionsUnitId,
|
||||
_log.repetitionsUnitId,
|
||||
onChanged: (v) => {},
|
||||
),
|
||||
),
|
||||
@@ -632,7 +642,7 @@ class _LogFormWidgetState extends ConsumerState<LogFormWidget> {
|
||||
controller: _weightController,
|
||||
configData: widget.configData,
|
||||
focusNode: widget.focusNode,
|
||||
log: widget.log,
|
||||
log: _log,
|
||||
setStateCallback: (fn) {
|
||||
setState(fn);
|
||||
},
|
||||
@@ -640,19 +650,19 @@ class _LogFormWidgetState extends ConsumerState<LogFormWidget> {
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
Flexible(
|
||||
child: WeightUnitInputWidget(widget.log.weightUnitId, onChanged: (v) => {}),
|
||||
child: WeightUnitInputWidget(_log.weightUnitId, onChanged: (v) => {}),
|
||||
),
|
||||
const SizedBox(width: 8),
|
||||
],
|
||||
),
|
||||
if (_detailed)
|
||||
RiRInputWidget(
|
||||
widget.log.rir,
|
||||
_log.rir,
|
||||
onChanged: (value) {
|
||||
if (value == '') {
|
||||
widget.log.rir = null;
|
||||
_log.rir = null;
|
||||
} else {
|
||||
widget.log.rir = num.parse(value);
|
||||
_log.rir = num.parse(value);
|
||||
}
|
||||
},
|
||||
),
|
||||
@@ -684,7 +694,7 @@ class _LogFormWidgetState extends ConsumerState<LogFormWidget> {
|
||||
await provider.Provider.of<RoutinesProvider>(
|
||||
context,
|
||||
listen: false,
|
||||
).addLog(widget.log);
|
||||
).addLog(_log);
|
||||
final page = gymState.getSlotEntryPageByIndex()!;
|
||||
gymProvider.markSlotPageAsDone(page.uuid, isDone: true);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user