mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-06 07:54:02 +01:00
# Conflicts: # integration_test/1_dashboard.dart # integration_test/3_gym_mode.dart # ios/Podfile.lock # lib/database/exercises/exercise_database.dart # lib/database/exercises/type_converters.dart # lib/models/exercises/exercise.dart # lib/models/exercises/exercise.g.dart # lib/models/exercises/exercise_api.dart # lib/models/exercises/exercise_api.freezed.dart # lib/models/exercises/exercise_api.g.dart # lib/models/exercises/translation.dart # lib/models/exercises/translation.g.dart # lib/models/exercises/variation.dart # lib/providers/exercises.dart # lib/widgets/add_exercise/steps/step_1_basics.dart # lib/widgets/add_exercise/steps/step_2_variations.dart # lib/widgets/exercises/exercises.dart # lib/widgets/measurements/forms.dart # lib/widgets/routines/forms/slot.dart # pubspec.lock # pubspec.yaml # test/core/settings_test.mocks.dart # test/exercises/contribute_exercise_image_test.mocks.dart # test/exercises/contribute_exercise_test.mocks.dart # test/exercises/exercise_provider_load_test.dart # test/exercises/exercise_provider_test.dart # test/exercises/exercises_detail_widget_test.dart # test/exercises/exercises_detail_widget_test.mocks.dart # test/exercises/model_exercise_test.dart # test/measurements/measurement_entries_screen_test.dart # test/routine/gym_mode/gym_mode_test.dart # test/routine/gym_mode/gym_mode_test.mocks.dart # test/routine/routines_provider_test.mocks.dart # test/weight/weight_screen_test.mocks.dart # test/widgets/routines/gym_mode/log_page_test.mocks.dart # test_data/measurements.dart
352 lines
12 KiB
Dart
352 lines
12 KiB
Dart
/*
|
|
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
|
* Copyright (c) 2026 wger Team
|
|
*
|
|
* wger Workout Manager is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:intl/intl.dart';
|
|
import 'package:wger/helpers/consts.dart';
|
|
import 'package:wger/l10n/generated/app_localizations.dart';
|
|
import 'package:wger/models/measurements/measurement_category.dart';
|
|
import 'package:wger/models/measurements/measurement_entry.dart';
|
|
import 'package:wger/providers/measurement_notifier.dart';
|
|
import 'package:wger/widgets/core/error.dart';
|
|
import 'package:wger/widgets/core/progress_indicator.dart';
|
|
|
|
class MeasurementCategoryForm extends ConsumerWidget {
|
|
final _form = GlobalKey<FormState>();
|
|
final nameController = TextEditingController();
|
|
final unitController = TextEditingController();
|
|
|
|
final Map<String, dynamic> categoryData = {
|
|
'id': null,
|
|
'name': '',
|
|
'unit': '',
|
|
};
|
|
|
|
MeasurementCategoryForm([MeasurementCategory? category]) {
|
|
if (category != null) {
|
|
categoryData['id'] = category.id;
|
|
categoryData['unit'] = category.unit;
|
|
categoryData['name'] = category.name;
|
|
}
|
|
|
|
unitController.text = categoryData['unit']!;
|
|
nameController.text = categoryData['name']!;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
return Form(
|
|
key: _form,
|
|
child: Column(
|
|
children: [
|
|
// Name
|
|
TextFormField(
|
|
decoration: InputDecoration(
|
|
labelText: AppLocalizations.of(context).name,
|
|
helperText: AppLocalizations.of(context).measurementCategoriesHelpText,
|
|
),
|
|
controller: nameController,
|
|
onSaved: (newValue) {
|
|
categoryData['name'] = newValue;
|
|
},
|
|
validator: (value) {
|
|
if (value!.isEmpty) {
|
|
return AppLocalizations.of(context).enterValue;
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
|
|
// Unit
|
|
TextFormField(
|
|
decoration: InputDecoration(
|
|
labelText: AppLocalizations.of(context).unit,
|
|
helperText: AppLocalizations.of(context).measurementEntriesHelpText,
|
|
),
|
|
controller: unitController,
|
|
onSaved: (newValue) {
|
|
categoryData['unit'] = newValue;
|
|
},
|
|
validator: (value) {
|
|
if (value!.isEmpty) {
|
|
return AppLocalizations.of(context).enterValue;
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
ElevatedButton(
|
|
child: Text(AppLocalizations.of(context).save),
|
|
onPressed: () async {
|
|
// Validate and save the current values to the weightEntry
|
|
final isValid = _form.currentState!.validate();
|
|
if (!isValid) {
|
|
return;
|
|
}
|
|
_form.currentState!.save();
|
|
|
|
final notifier = ref.read(measurementProvider.notifier);
|
|
final category = MeasurementCategory(
|
|
id: categoryData['id'],
|
|
name: categoryData['name'],
|
|
unit: categoryData['unit'],
|
|
);
|
|
|
|
// Save the entry on the server
|
|
categoryData['id'] == null
|
|
? await notifier.addCategory(category)
|
|
: notifier.updateCategory(category);
|
|
|
|
if (context.mounted) {
|
|
Navigator.of(context).pop();
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|
|
|
|
class MeasurementEntryForm extends ConsumerWidget {
|
|
final _form = GlobalKey<FormState>();
|
|
final String _categoryId;
|
|
final _valueController = TextEditingController();
|
|
final _dateController = TextEditingController(text: '');
|
|
final _timeController = TextEditingController(text: '');
|
|
final _notesController = TextEditingController();
|
|
|
|
late final Map<String, dynamic> _entryData;
|
|
|
|
MeasurementEntryForm(this._categoryId, [MeasurementEntry? entry]) {
|
|
_entryData = {
|
|
'id': null,
|
|
'date': DateTime.now(),
|
|
'value': '',
|
|
'notes': '',
|
|
};
|
|
|
|
if (entry != null) {
|
|
_entryData['id'] = entry.id;
|
|
_entryData['value'] = entry.value;
|
|
_entryData['date'] = entry.date;
|
|
_entryData['notes'] = entry.notes;
|
|
}
|
|
|
|
_valueController.text = '';
|
|
_notesController.text = _entryData['notes']!;
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context, WidgetRef ref) {
|
|
final dateFormat = DateFormat.yMd(Localizations.localeOf(context).languageCode);
|
|
final timeFormat = DateFormat.Hm(Localizations.localeOf(context).languageCode);
|
|
|
|
final notifier = ref.read(measurementProvider.notifier);
|
|
final Future<MeasurementCategory?> categoryFuture = notifier.getCategoryById(_categoryId);
|
|
|
|
if (_dateController.text.isEmpty) {
|
|
_dateController.text = dateFormat.format(_entryData['date']);
|
|
}
|
|
if (_timeController.text.isEmpty) {
|
|
_timeController.text = timeFormat.format(_entryData['date']);
|
|
}
|
|
|
|
final numberFormat = NumberFormat.decimalPattern(Localizations.localeOf(context).toString());
|
|
|
|
// If the value is not empty, format it
|
|
if (_valueController.text.isEmpty && _entryData['value'] != null && _entryData['value'] != '') {
|
|
_valueController.text = numberFormat.format(_entryData['value']);
|
|
}
|
|
|
|
return FutureBuilder(
|
|
future: categoryFuture,
|
|
builder: (context, snapshot) {
|
|
if (snapshot.connectionState == ConnectionState.waiting) {
|
|
return const BoxedProgressIndicator();
|
|
}
|
|
if (snapshot.hasError) {
|
|
return StreamErrorIndicator(snapshot.error.toString());
|
|
}
|
|
if (!snapshot.hasData || snapshot.data == null) {
|
|
return const Text('Category not found');
|
|
}
|
|
|
|
final category = snapshot.data!;
|
|
|
|
return Form(
|
|
key: _form,
|
|
child: Column(
|
|
children: [
|
|
// Date
|
|
TextFormField(
|
|
decoration: InputDecoration(
|
|
labelText: AppLocalizations.of(context).date,
|
|
suffixIcon: const Icon(
|
|
Icons.calendar_today,
|
|
key: Key('calendarIcon'),
|
|
),
|
|
),
|
|
readOnly: true,
|
|
controller: _dateController,
|
|
onTap: () async {
|
|
FocusScope.of(context).requestFocus(FocusNode());
|
|
|
|
final pickedDate = await showDatePicker(
|
|
context: context,
|
|
initialDate: _entryData['date'],
|
|
firstDate: DateTime(DateTime.now().year - 10),
|
|
lastDate: DateTime.now(),
|
|
);
|
|
|
|
if (pickedDate != null) {
|
|
_dateController.text = dateFormat.format(pickedDate);
|
|
}
|
|
},
|
|
onSaved: (newValue) {
|
|
final date = dateFormat.parse(newValue!);
|
|
_entryData['date'] = (_entryData['date'] as DateTime).copyWith(
|
|
year: date.year,
|
|
month: date.month,
|
|
day: date.day,
|
|
);
|
|
},
|
|
validator: (value) {
|
|
if (value!.isEmpty) {
|
|
return AppLocalizations.of(context).enterValue;
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
|
|
// Time
|
|
TextFormField(
|
|
decoration: InputDecoration(
|
|
labelText: AppLocalizations.of(context).time,
|
|
suffixIcon: const Icon(
|
|
Icons.access_time_outlined,
|
|
key: Key('clockIcon'),
|
|
),
|
|
),
|
|
readOnly: true,
|
|
controller: _timeController,
|
|
onTap: () async {
|
|
final pickedTime = await showTimePicker(
|
|
context: context,
|
|
initialTime: TimeOfDay.fromDateTime(_entryData['date']),
|
|
);
|
|
|
|
if (pickedTime != null) {
|
|
final now = DateTime.now();
|
|
final dt = DateTime(
|
|
now.year,
|
|
now.month,
|
|
now.day,
|
|
pickedTime.hour,
|
|
pickedTime.minute,
|
|
);
|
|
_timeController.text = timeFormat.format(dt);
|
|
}
|
|
},
|
|
onSaved: (newValue) {
|
|
final time = timeFormat.parse(newValue!);
|
|
_entryData['date'] = (_entryData['date'] as DateTime).copyWith(
|
|
hour: time.hour,
|
|
minute: time.minute,
|
|
second: time.second,
|
|
);
|
|
},
|
|
),
|
|
|
|
// Value
|
|
TextFormField(
|
|
decoration: InputDecoration(
|
|
labelText: AppLocalizations.of(context).value,
|
|
suffixIcon: Text(category.unit),
|
|
suffixIconConstraints: const BoxConstraints(minWidth: 0, minHeight: 0),
|
|
),
|
|
controller: _valueController,
|
|
keyboardType: textInputTypeDecimal,
|
|
validator: (value) {
|
|
if (value!.isEmpty) {
|
|
return AppLocalizations.of(context).enterValue;
|
|
}
|
|
try {
|
|
numberFormat.parse(value);
|
|
} catch (error) {
|
|
return AppLocalizations.of(context).enterValidNumber;
|
|
}
|
|
return null;
|
|
},
|
|
onSaved: (newValue) {
|
|
_entryData['value'] = numberFormat.parse(newValue!);
|
|
},
|
|
),
|
|
// Notes
|
|
TextFormField(
|
|
decoration: InputDecoration(labelText: AppLocalizations.of(context).notes),
|
|
controller: _notesController,
|
|
onSaved: (newValue) {
|
|
_entryData['notes'] = newValue;
|
|
},
|
|
validator: (value) {
|
|
const minLength = 0;
|
|
const maxLength = 100;
|
|
if (value!.isNotEmpty && (value.length < minLength || value.length > maxLength)) {
|
|
return AppLocalizations.of(context).enterCharacters(
|
|
minLength.toString(),
|
|
maxLength.toString(),
|
|
);
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
|
|
ElevatedButton(
|
|
child: Text(AppLocalizations.of(context).save),
|
|
onPressed: () async {
|
|
final isValid = _form.currentState!.validate();
|
|
if (!isValid) {
|
|
return;
|
|
}
|
|
_form.currentState!.save();
|
|
|
|
final entry = MeasurementEntry(
|
|
id: _entryData['id'],
|
|
categoryId: category.id,
|
|
date: _entryData['date'],
|
|
value: _entryData['value'],
|
|
notes: _entryData['notes'],
|
|
);
|
|
_entryData['id'] == null
|
|
? await notifier.addEntry(entry)
|
|
: await notifier.updateEntry(entry);
|
|
|
|
if (context.mounted) {
|
|
Navigator.of(context).pop();
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
}
|