mirror of
https://github.com/wger-project/flutter.git
synced 2026-12-03 10:33:54 +01:00
180 lines
6.1 KiB
Dart
180 lines
6.1 KiB
Dart
/*
|
|
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
|
* Copyright (c) 2020 - 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:clock/clock.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
import 'package:logging/logging.dart';
|
|
import 'package:wger/helpers/routines/validators.dart';
|
|
import 'package:wger/l10n/generated/app_localizations.dart';
|
|
import 'package:wger/models/workouts/session.dart';
|
|
import 'package:wger/providers/workout_session_notifier.dart';
|
|
import 'package:wger/widgets/core/datetime_input.dart';
|
|
import 'package:wger/widgets/core/form_submit_button.dart';
|
|
|
|
class SessionForm extends ConsumerStatefulWidget {
|
|
final _logger = Logger('SessionForm');
|
|
final WorkoutSession _session;
|
|
final Function()? _onSaved;
|
|
|
|
static const SLIDER_START = -0.5;
|
|
|
|
SessionForm(int routineId, {Function()? onSaved, WorkoutSession? session, int? dayId})
|
|
: _onSaved = onSaved,
|
|
_session =
|
|
session ??
|
|
WorkoutSession(
|
|
routineId: routineId,
|
|
dayId: dayId,
|
|
date: clock.now(),
|
|
timeStart: null,
|
|
timeEnd: null,
|
|
);
|
|
|
|
@override
|
|
_SessionFormState createState() => _SessionFormState();
|
|
}
|
|
|
|
class _SessionFormState extends ConsumerState<SessionForm> {
|
|
final _form = GlobalKey<FormState>();
|
|
|
|
final notesController = TextEditingController();
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
notesController.text = widget._session.notes ?? '';
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
notesController.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final sessionProvider = ref.read(workoutSessionProvider.notifier);
|
|
|
|
return Form(
|
|
key: _form,
|
|
child: Column(
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
children: [
|
|
ToggleButtons(
|
|
key: const ValueKey('impression-toggle-buttons'),
|
|
renderBorder: false,
|
|
onPressed: (int index) {
|
|
setState(() {
|
|
widget._session.impression = WorkoutImpression.values[index];
|
|
});
|
|
},
|
|
isSelected: WorkoutImpression.values
|
|
.map((e) => e == widget._session.impression)
|
|
.toList(),
|
|
children: const [
|
|
Icon(Icons.sentiment_very_dissatisfied),
|
|
Icon(Icons.sentiment_neutral),
|
|
Icon(Icons.sentiment_very_satisfied),
|
|
],
|
|
),
|
|
TextFormField(
|
|
decoration: InputDecoration(
|
|
labelText: AppLocalizations.of(context).notes,
|
|
),
|
|
maxLines: 3,
|
|
maxLength: WorkoutSession.maxNotesChars,
|
|
controller: notesController,
|
|
keyboardType: TextInputType.multiline,
|
|
onFieldSubmitted: (_) {},
|
|
onSaved: (newValue) {
|
|
widget._session.notes = newValue;
|
|
},
|
|
validator: (value) {
|
|
if (value != null && value.length > WorkoutSession.maxNotesChars) {
|
|
return AppLocalizations.of(
|
|
context,
|
|
).enterMaxCharacters(WorkoutSession.maxNotesChars.toString());
|
|
}
|
|
return null;
|
|
},
|
|
),
|
|
Row(
|
|
spacing: 10,
|
|
children: [
|
|
Flexible(
|
|
child: TimeInputWidget(
|
|
key: const ValueKey('time-start'),
|
|
value: widget._session.timeStart,
|
|
labelText: AppLocalizations.of(context).timeStart,
|
|
onCleared: () => widget._session.timeStart = null,
|
|
onChanged: (time) => widget._session.timeStart = time,
|
|
),
|
|
),
|
|
Flexible(
|
|
child: TimeInputWidget(
|
|
key: const ValueKey('time-end'),
|
|
value: widget._session.timeEnd,
|
|
labelText: AppLocalizations.of(context).timeEnd,
|
|
onCleared: () => widget._session.timeEnd = null,
|
|
onChanged: (time) => widget._session.timeEnd = time,
|
|
),
|
|
),
|
|
],
|
|
),
|
|
const SizedBox(height: 5),
|
|
FormSubmitButton(
|
|
key: const ValueKey('save-button'),
|
|
label: AppLocalizations.of(context).save,
|
|
onPressed: () async {
|
|
if (!_form.currentState!.validate()) {
|
|
return;
|
|
}
|
|
_form.currentState!.save();
|
|
|
|
final i18n = AppLocalizations.of(context);
|
|
final error = validateWorkoutSessionTimes(
|
|
timeStart: widget._session.timeStart,
|
|
timeEnd: widget._session.timeEnd,
|
|
i18n: i18n,
|
|
);
|
|
if (error != null) {
|
|
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text(error)));
|
|
return;
|
|
}
|
|
|
|
// A WgerHttpException is surfaced inline by FormSubmitButton.
|
|
if (widget._session.id == null) {
|
|
widget._logger.fine('Adding new session');
|
|
await sessionProvider.addEntry(widget._session);
|
|
} else {
|
|
widget._logger.fine('Editing existing session with id ${widget._session.id}');
|
|
await sessionProvider.updateEntry(widget._session);
|
|
}
|
|
|
|
if (context.mounted && widget._onSaved != null) {
|
|
widget._onSaved!();
|
|
}
|
|
},
|
|
),
|
|
],
|
|
),
|
|
);
|
|
}
|
|
}
|