mirror of
https://github.com/wger-project/flutter.git
synced 2026-02-18 23:42:00 +01:00
132 lines
3.2 KiB
Dart
132 lines
3.2 KiB
Dart
import 'package:json_annotation/json_annotation.dart';
|
||
import 'package:wger/helpers/consts.dart';
|
||
import 'package:wger/helpers/json.dart';
|
||
import 'package:wger/models/exercises/exercise.dart';
|
||
import 'package:wger/models/workouts/repetition_unit.dart';
|
||
import 'package:wger/models/workouts/weight_unit.dart';
|
||
|
||
part 'setting.g.dart';
|
||
|
||
@JsonSerializable()
|
||
class Setting {
|
||
static const POSSIBLE_RIR_VALUES = ['', '1', '1.5', '2', '2.5', '3', '3.5'];
|
||
static const DEFAULT_RIR = '';
|
||
|
||
@JsonKey(required: true)
|
||
int? id;
|
||
|
||
@JsonKey(required: true, name: 'set')
|
||
late int setId;
|
||
|
||
@JsonKey(required: true)
|
||
late int order;
|
||
|
||
@JsonKey(ignore: true)
|
||
late Exercise exerciseObj;
|
||
|
||
@JsonKey(required: true, name: 'exercise')
|
||
late int exerciseId;
|
||
|
||
@JsonKey(required: true, name: 'repetition_unit')
|
||
late int repetitionUnitId;
|
||
|
||
@JsonKey(ignore: true)
|
||
late RepetitionUnit repetitionUnitObj;
|
||
|
||
@JsonKey(required: true)
|
||
int? reps;
|
||
|
||
@JsonKey(required: true, fromJson: toNum, toJson: toString)
|
||
num? weight;
|
||
|
||
@JsonKey(required: true, name: 'weight_unit')
|
||
late int weightUnitId;
|
||
|
||
@JsonKey(ignore: true)
|
||
late WeightUnit weightUnitObj;
|
||
|
||
@JsonKey(required: true)
|
||
late String comment = '';
|
||
|
||
@JsonKey(required: true)
|
||
String? rir = '';
|
||
|
||
// Generated by the server
|
||
@JsonKey(ignore: true)
|
||
late String repsText = '';
|
||
|
||
Setting({
|
||
this.id,
|
||
required this.setId,
|
||
required this.order,
|
||
required this.exerciseId,
|
||
required this.repetitionUnitId,
|
||
required this.reps,
|
||
required this.weightUnitId,
|
||
required this.comment,
|
||
required this.rir,
|
||
});
|
||
|
||
Setting.empty();
|
||
|
||
// Boilerplate
|
||
factory Setting.fromJson(Map<String, dynamic> json) => _$SettingFromJson(json);
|
||
Map<String, dynamic> toJson() => _$SettingToJson(this);
|
||
|
||
void setExercise(Exercise exercise) {
|
||
exerciseObj = exercise;
|
||
exerciseId = exercise.id;
|
||
}
|
||
|
||
void setWeightUnit(WeightUnit weightUnit) {
|
||
weightUnitObj = weightUnit;
|
||
weightUnitId = weightUnit.id;
|
||
}
|
||
|
||
void setRepetitionUnit(RepetitionUnit repetitionUnit) {
|
||
repetitionUnitObj = repetitionUnit;
|
||
repetitionUnitId = repetitionUnit.id;
|
||
}
|
||
|
||
void setRir(String rir) {
|
||
if (POSSIBLE_RIR_VALUES.contains(rir)) {
|
||
this.rir = rir;
|
||
} else {
|
||
throw Exception('RiR value not allowed');
|
||
}
|
||
}
|
||
|
||
/// Returns the text representation for a single setting, used in the gym mode
|
||
String get singleSettingRepText {
|
||
// TODO: how to (easily?) translate strings like the units or 'RiR'?
|
||
|
||
List<String> out = [];
|
||
|
||
if (reps != null) {
|
||
out.add(reps.toString());
|
||
|
||
// The default repetition unit is 'reps', which we don't show unless there
|
||
// is no weight defined so that we don't just output something like "8" but
|
||
// rather "8 repetitions". If there is weight we want to output "8 x 50kg",
|
||
// since the repetitions are implied. If other units are used, we always
|
||
// print them
|
||
if (repetitionUnitId != DEFAULT_REPETITION_UNIT || weight == 0) {
|
||
out.add(repetitionUnitObj.name);
|
||
}
|
||
}
|
||
|
||
if (weight != null && weight != 0) {
|
||
out.add('×');
|
||
out.add(weight.toString());
|
||
out.add(weightUnitObj.name);
|
||
}
|
||
|
||
if (rir != null && rir != '') {
|
||
out.add('\n');
|
||
out.add('($rir RiR)');
|
||
}
|
||
|
||
return out.join(' ');
|
||
}
|
||
}
|