mirror of
https://github.com/wger-project/flutter.git
synced 2026-11-30 09:42:50 +01:00
Make the "impression" in workout session an enum
This is cleaner and simplifies a handful of places
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* This file is part of wger Workout Manager <https://github.com/wger-project>.
|
||||
* Copyright (c) 2020, 2020- wger Team
|
||||
* 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
|
||||
@@ -17,17 +17,16 @@
|
||||
*/
|
||||
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:wger/models/workouts/session.dart';
|
||||
|
||||
class IntToStringConverter extends TypeConverter<int, String> {
|
||||
const IntToStringConverter();
|
||||
/// Maps a [WorkoutImpression] to and from the Django wire format
|
||||
/// (`'1'`, `'2'`, `'3'`).
|
||||
class WorkoutImpressionConverter extends TypeConverter<WorkoutImpression, String> {
|
||||
const WorkoutImpressionConverter();
|
||||
|
||||
@override
|
||||
int fromSql(String fromDb) {
|
||||
return int.parse(fromDb);
|
||||
}
|
||||
WorkoutImpression fromSql(String fromDb) => WorkoutImpression.fromWire(fromDb);
|
||||
|
||||
@override
|
||||
String toSql(int value) {
|
||||
return value.toString();
|
||||
}
|
||||
String toSql(WorkoutImpression value) => value.wireValue;
|
||||
}
|
||||
@@ -21,8 +21,8 @@ import 'package:drift_sqlite_async/drift_sqlite_async.dart';
|
||||
import 'package:flutter/material.dart' show TimeOfDay;
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:powersync/powersync.dart' as ps;
|
||||
import 'package:wger/database/converters/int_to_string_converter.dart';
|
||||
import 'package:wger/database/converters/time_of_day_converter.dart';
|
||||
import 'package:wger/database/converters/workout_impression_converter.dart';
|
||||
import 'package:wger/models/body_weight/weight_entry.dart';
|
||||
import 'package:wger/models/core/language.dart';
|
||||
import 'package:wger/models/core/license.dart';
|
||||
|
||||
@@ -14675,7 +14675,7 @@ typedef $$WorkoutSessionTableTableCreateCompanionBuilder =
|
||||
Value<int?> dayId,
|
||||
required DateTime date,
|
||||
Value<String?> notes,
|
||||
required int impression,
|
||||
required WorkoutImpression impression,
|
||||
Value<TimeOfDay?> timeStart,
|
||||
Value<TimeOfDay?> timeEnd,
|
||||
Value<int> rowid,
|
||||
@@ -14687,7 +14687,7 @@ typedef $$WorkoutSessionTableTableUpdateCompanionBuilder =
|
||||
Value<int?> dayId,
|
||||
Value<DateTime> date,
|
||||
Value<String?> notes,
|
||||
Value<int> impression,
|
||||
Value<WorkoutImpression> impression,
|
||||
Value<TimeOfDay?> timeStart,
|
||||
Value<TimeOfDay?> timeEnd,
|
||||
Value<int> rowid,
|
||||
@@ -14727,10 +14727,11 @@ class $$WorkoutSessionTableTableFilterComposer
|
||||
builder: (column) => ColumnFilters(column),
|
||||
);
|
||||
|
||||
ColumnWithTypeConverterFilters<int, int, String> get impression => $composableBuilder(
|
||||
column: $table.impression,
|
||||
builder: (column) => ColumnWithTypeConverterFilters(column),
|
||||
);
|
||||
ColumnWithTypeConverterFilters<WorkoutImpression, WorkoutImpression, String> get impression =>
|
||||
$composableBuilder(
|
||||
column: $table.impression,
|
||||
builder: (column) => ColumnWithTypeConverterFilters(column),
|
||||
);
|
||||
|
||||
ColumnWithTypeConverterFilters<TimeOfDay?, TimeOfDay, String> get timeStart => $composableBuilder(
|
||||
column: $table.timeStart,
|
||||
@@ -14817,7 +14818,7 @@ class $$WorkoutSessionTableTableAnnotationComposer
|
||||
GeneratedColumn<String> get notes =>
|
||||
$composableBuilder(column: $table.notes, builder: (column) => column);
|
||||
|
||||
GeneratedColumnWithTypeConverter<int, String> get impression => $composableBuilder(
|
||||
GeneratedColumnWithTypeConverter<WorkoutImpression, String> get impression => $composableBuilder(
|
||||
column: $table.impression,
|
||||
builder: (column) => column,
|
||||
);
|
||||
@@ -14871,7 +14872,7 @@ class $$WorkoutSessionTableTableTableManager
|
||||
Value<int?> dayId = const Value.absent(),
|
||||
Value<DateTime> date = const Value.absent(),
|
||||
Value<String?> notes = const Value.absent(),
|
||||
Value<int> impression = const Value.absent(),
|
||||
Value<WorkoutImpression> impression = const Value.absent(),
|
||||
Value<TimeOfDay?> timeStart = const Value.absent(),
|
||||
Value<TimeOfDay?> timeEnd = const Value.absent(),
|
||||
Value<int> rowid = const Value.absent(),
|
||||
@@ -14893,7 +14894,7 @@ class $$WorkoutSessionTableTableTableManager
|
||||
Value<int?> dayId = const Value.absent(),
|
||||
required DateTime date,
|
||||
Value<String?> notes = const Value.absent(),
|
||||
required int impression,
|
||||
required WorkoutImpression impression,
|
||||
Value<TimeOfDay?> timeStart = const Value.absent(),
|
||||
Value<TimeOfDay?> timeEnd = const Value.absent(),
|
||||
Value<int> rowid = const Value.absent(),
|
||||
|
||||
@@ -18,8 +18,8 @@
|
||||
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:powersync/powersync.dart' as ps;
|
||||
import 'package:wger/database/converters/int_to_string_converter.dart';
|
||||
import 'package:wger/database/converters/time_of_day_converter.dart';
|
||||
import 'package:wger/database/converters/workout_impression_converter.dart';
|
||||
import 'package:wger/models/workouts/log.dart';
|
||||
import 'package:wger/models/workouts/repetition_unit.dart';
|
||||
import 'package:wger/models/workouts/routine.dart';
|
||||
@@ -77,7 +77,7 @@ class WorkoutLogTable extends Table {
|
||||
|
||||
RealColumn get weight => real().nullable()();
|
||||
RealColumn get weightTarget => real().nullable().named('weight_target')();
|
||||
IntColumn get weightUnitId => integer().nullable().named('weight_unit_id').nullable()();
|
||||
IntColumn get weightUnitId => integer().nullable().named('weight_unit_id')();
|
||||
|
||||
DateTimeColumn get date => dateTime()();
|
||||
}
|
||||
@@ -120,7 +120,7 @@ class WorkoutSessionTable extends Table {
|
||||
IntColumn get dayId => integer().named('day_id').nullable()();
|
||||
DateTimeColumn get date => dateTime()();
|
||||
TextColumn get notes => text().nullable()();
|
||||
TextColumn get impression => text().map(const IntToStringConverter())();
|
||||
TextColumn get impression => text().map(const WorkoutImpressionConverter())();
|
||||
TextColumn get timeStart =>
|
||||
text().named('time_start').nullable().map(const TimeOfDayConverter())();
|
||||
TextColumn get timeEnd => text().named('time_end').nullable().map(const TimeOfDayConverter())();
|
||||
|
||||
@@ -38,9 +38,6 @@ const TESTSERVER_PASSWORD = 'flutteruser';
|
||||
/// auto-login and mirrors what the server does with MIN_APP_VERSION.
|
||||
const MIN_SERVER_VERSION = '2.5';
|
||||
|
||||
/// Default impression for a workout session (neutral)
|
||||
const DEFAULT_IMPRESSION = 2;
|
||||
|
||||
// Weight and repetition units for the workout logs
|
||||
const REP_UNIT_REPETITIONS_ID = 1;
|
||||
const REP_UNIT_TILL_FAILURE_ID = 2;
|
||||
|
||||
@@ -19,24 +19,50 @@
|
||||
import 'package:clock/clock.dart';
|
||||
import 'package:drift/drift.dart' as drift;
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:powersync/powersync.dart' show uuid;
|
||||
import 'package:wger/database/powersync/database.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
import 'package:wger/models/exercises/exercise.dart';
|
||||
import 'package:wger/models/workouts/log.dart';
|
||||
|
||||
const IMPRESSION_MAP = {1: 'bad', 2: 'neutral', 3: 'good'};
|
||||
/// User's general impression of a workout session.
|
||||
///
|
||||
/// The wire values mirror Django's `WorkoutSession.IMPRESSION` choices
|
||||
/// (`CharField` with `'1'`, `'2'`, `'3'`), so the same string round-trips
|
||||
/// through PowerSync without any extra mapping on the connector.
|
||||
enum WorkoutImpression {
|
||||
bad('1'),
|
||||
neutral('2'),
|
||||
good('3')
|
||||
;
|
||||
|
||||
final String wireValue;
|
||||
const WorkoutImpression(this.wireValue);
|
||||
|
||||
/// Looks up an enum case by its Django wire value.
|
||||
static WorkoutImpression fromWire(String value) =>
|
||||
WorkoutImpression.values.firstWhere((e) => e.wireValue == value);
|
||||
}
|
||||
|
||||
extension WorkoutImpressionL10n on WorkoutImpression {
|
||||
/// Localized human-readable label (e.g. "Good", "Neutral", "Bad").
|
||||
String localized(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context);
|
||||
return switch (this) {
|
||||
WorkoutImpression.bad => l10n.impressionBad,
|
||||
WorkoutImpression.neutral => l10n.impressionNeutral,
|
||||
WorkoutImpression.good => l10n.impressionGood,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class WorkoutSession {
|
||||
final _logger = Logger('WorkoutSession');
|
||||
|
||||
String id;
|
||||
/// `null` only for instances built in-memory before the first persist;
|
||||
/// Drift fills it in via the table's `clientDefault` UUID.
|
||||
String? id;
|
||||
late int? routineId;
|
||||
int? dayId;
|
||||
DateTime date;
|
||||
late int impression;
|
||||
WorkoutImpression impression;
|
||||
late String? notes;
|
||||
late TimeOfDay? timeStart;
|
||||
late TimeOfDay? timeEnd;
|
||||
@@ -44,21 +70,20 @@ class WorkoutSession {
|
||||
List<Log> logs = [];
|
||||
|
||||
WorkoutSession({
|
||||
String? id,
|
||||
this.id,
|
||||
this.dayId,
|
||||
required this.routineId,
|
||||
this.impression = DEFAULT_IMPRESSION,
|
||||
this.impression = WorkoutImpression.neutral,
|
||||
this.notes = '',
|
||||
this.timeStart,
|
||||
this.timeEnd,
|
||||
this.logs = const [],
|
||||
DateTime? date,
|
||||
}) : id = id ?? uuid.v7(),
|
||||
date = date ?? clock.now();
|
||||
}) : date = date ?? clock.now();
|
||||
|
||||
WorkoutSessionTableCompanion toCompanion({bool includeId = false}) {
|
||||
WorkoutSessionTableCompanion toCompanion() {
|
||||
return WorkoutSessionTableCompanion(
|
||||
id: drift.Value(id),
|
||||
id: id != null ? drift.Value(id!) : const drift.Value.absent(),
|
||||
routineId: routineId != null ? drift.Value(routineId) : const drift.Value.absent(),
|
||||
dayId: dayId != null ? drift.Value(dayId) : const drift.Value.absent(),
|
||||
// Server-side `date` is a `DateField` (no time, no TZ). We send here the
|
||||
@@ -122,17 +147,4 @@ class WorkoutSession {
|
||||
}
|
||||
return exerciseSet.toList();
|
||||
}
|
||||
|
||||
String impressionAsString(BuildContext context) {
|
||||
if (impression == 1) {
|
||||
return AppLocalizations.of(context).impressionBad;
|
||||
} else if (impression == 2) {
|
||||
return AppLocalizations.of(context).impressionNeutral;
|
||||
} else if (impression == 3) {
|
||||
return AppLocalizations.of(context).impressionGood;
|
||||
}
|
||||
|
||||
_logger.warning('Unknown impression value: $impression');
|
||||
return AppLocalizations.of(context).impressionGood;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ class _DashboardCalendarWidgetState extends riverpod.ConsumerState<DashboardCale
|
||||
events[date]!.add(
|
||||
Event(
|
||||
EventType.session,
|
||||
'${i18n.impression}: ${session.impressionAsString(context)} $time',
|
||||
'${i18n.impression}: ${session.impression.localized(context)} $time',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -21,7 +21,6 @@ import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:wger/core/exceptions/http_exception.dart';
|
||||
import 'package:wger/helpers/consts.dart';
|
||||
import 'package:wger/helpers/errors.dart';
|
||||
import 'package:wger/helpers/json.dart';
|
||||
import 'package:wger/l10n/generated/app_localizations.dart';
|
||||
@@ -32,19 +31,16 @@ class SessionForm extends ConsumerStatefulWidget {
|
||||
final _logger = Logger('SessionForm');
|
||||
final WorkoutSession _session;
|
||||
final Function()? _onSaved;
|
||||
final bool _isNew;
|
||||
|
||||
static const SLIDER_START = -0.5;
|
||||
|
||||
SessionForm(int routineId, {Function()? onSaved, WorkoutSession? session, int? dayId})
|
||||
: _onSaved = onSaved,
|
||||
_isNew = session == null,
|
||||
_session =
|
||||
session ??
|
||||
WorkoutSession(
|
||||
routineId: routineId,
|
||||
dayId: dayId,
|
||||
impression: DEFAULT_IMPRESSION,
|
||||
date: clock.now(),
|
||||
timeEnd: TimeOfDay.fromDateTime(clock.now()),
|
||||
timeStart: null,
|
||||
@@ -58,14 +54,10 @@ class _SessionFormState extends ConsumerState<SessionForm> {
|
||||
Widget errorMessage = const SizedBox.shrink();
|
||||
final _form = GlobalKey<FormState>();
|
||||
|
||||
final impressionController = TextEditingController();
|
||||
final notesController = TextEditingController();
|
||||
final timeStartController = TextEditingController();
|
||||
final timeEndController = TextEditingController();
|
||||
|
||||
/// Selected impression: bad, neutral, good
|
||||
var selectedImpression = [false, false, false];
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
@@ -77,12 +69,10 @@ class _SessionFormState extends ConsumerState<SessionForm> {
|
||||
? ''
|
||||
: timeToString(widget._session.timeEnd)!;
|
||||
notesController.text = widget._session.notes ?? '';
|
||||
selectedImpression[widget._session.impression - 1] = true;
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
impressionController.dispose();
|
||||
notesController.dispose();
|
||||
timeStartController.dispose();
|
||||
timeEndController.dispose();
|
||||
@@ -104,18 +94,12 @@ class _SessionFormState extends ConsumerState<SessionForm> {
|
||||
renderBorder: false,
|
||||
onPressed: (int index) {
|
||||
setState(() {
|
||||
for (int buttonIndex = 0; buttonIndex < selectedImpression.length; buttonIndex++) {
|
||||
widget._session.impression = index + 1;
|
||||
|
||||
if (buttonIndex == index) {
|
||||
selectedImpression[buttonIndex] = true;
|
||||
} else {
|
||||
selectedImpression[buttonIndex] = false;
|
||||
}
|
||||
}
|
||||
widget._session.impression = WorkoutImpression.values[index];
|
||||
});
|
||||
},
|
||||
isSelected: selectedImpression,
|
||||
isSelected: WorkoutImpression.values
|
||||
.map((e) => e == widget._session.impression)
|
||||
.toList(),
|
||||
children: const [
|
||||
Icon(Icons.sentiment_very_dissatisfied),
|
||||
Icon(Icons.sentiment_neutral),
|
||||
@@ -252,7 +236,7 @@ class _SessionFormState extends ConsumerState<SessionForm> {
|
||||
|
||||
// Save the entry on the server
|
||||
try {
|
||||
if (widget._isNew) {
|
||||
if (widget._session.id == null) {
|
||||
widget._logger.fine('Adding new session');
|
||||
await sessionProvider.addEntry(widget._session);
|
||||
} else {
|
||||
|
||||
@@ -66,7 +66,7 @@ class _SessionInfoState extends State<SessionInfo> {
|
||||
children: [
|
||||
SessionRow(
|
||||
label: i18n.impression,
|
||||
value: widget._session.impressionAsString(context),
|
||||
value: widget._session.impression.localized(context),
|
||||
),
|
||||
SessionRow(
|
||||
label: i18n.duration,
|
||||
|
||||
@@ -50,10 +50,10 @@ void main() {
|
||||
test('injects the row id and copies all fields', () {
|
||||
final out = connector.genericTransform(
|
||||
'manager_workoutsession',
|
||||
{'notes': 'leg day', 'impression': 1},
|
||||
{'notes': 'leg day', 'impression': '1'},
|
||||
'42',
|
||||
);
|
||||
expect(out, {'id': '42', 'notes': 'leg day', 'impression': 1});
|
||||
expect(out, {'id': '42', 'notes': 'leg day', 'impression': '1'});
|
||||
});
|
||||
|
||||
test('strips the `_id` suffix from foreign-key column names', () {
|
||||
|
||||
@@ -54,7 +54,7 @@ void main() {
|
||||
id: '1',
|
||||
routineId: 1,
|
||||
notes: 'Existing notes',
|
||||
impression: 1,
|
||||
impression: WorkoutImpression.bad,
|
||||
date: DateTime.now(),
|
||||
timeStart: const TimeOfDay(hour: 10, minute: 0),
|
||||
timeEnd: const TimeOfDay(hour: 11, minute: 0),
|
||||
@@ -93,7 +93,7 @@ void main() {
|
||||
id: '1',
|
||||
routineId: 1,
|
||||
notes: 'Old notes',
|
||||
impression: 2,
|
||||
impression: WorkoutImpression.neutral,
|
||||
date: DateTime.now(),
|
||||
);
|
||||
|
||||
|
||||
@@ -156,7 +156,7 @@ void main() {
|
||||
as WorkoutSession;
|
||||
|
||||
expect(captured.id, '1');
|
||||
expect(captured.impression, 3);
|
||||
expect(captured.impression, WorkoutImpression.good);
|
||||
expect(captured.notes, equals('This is a note'));
|
||||
expect(captured.timeStart, equals(const TimeOfDay(hour: 10, minute: 0)));
|
||||
expect(captured.timeEnd, equals(const TimeOfDay(hour: 12, minute: 34)));
|
||||
|
||||
@@ -93,7 +93,7 @@ Routine getTestRoutine({List<Exercise>? exercises}) {
|
||||
id: '1',
|
||||
routineId: 1,
|
||||
date: DateTime(2021, 5, 1),
|
||||
impression: 3,
|
||||
impression: WorkoutImpression.good,
|
||||
notes: 'This is a note',
|
||||
timeStart: const TimeOfDay(hour: 10, minute: 0),
|
||||
timeEnd: const TimeOfDay(hour: 12, minute: 34),
|
||||
@@ -104,7 +104,7 @@ Routine getTestRoutine({List<Exercise>? exercises}) {
|
||||
id: '2',
|
||||
routineId: 1,
|
||||
date: DateTime(2021, 5, 2),
|
||||
impression: 1,
|
||||
impression: WorkoutImpression.bad,
|
||||
notes: 'This is a note',
|
||||
timeStart: const TimeOfDay(hour: 6, minute: 12),
|
||||
timeEnd: const TimeOfDay(hour: 8, minute: 1),
|
||||
|
||||
Reference in New Issue
Block a user