Skip to content

Commit

Permalink
Polish the crud workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
rolandgeider committed Nov 16, 2024
1 parent a4f334e commit f7461ab
Show file tree
Hide file tree
Showing 13 changed files with 299 additions and 2,070 deletions.
6 changes: 3 additions & 3 deletions lib/helpers/consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ const TESTSERVER_PASSWORD = 'flutteruser';
const MANIFEST_KEY_CHECK_UPDATE = 'wger.check_min_app_version';

/// Default weight unit is "kg"
const DEFAULT_WEIGHT_UNIT = 1;
const WEIGHT_UNIT_KG_ID = 1;

/// Default impression for a workout session (neutral)
const DEFAULT_IMPRESSION = 2;

// Weight and repetition units for the workout logs
const REP_UNIT_REPETITIONS = 1;
const REP_UNIT_TILL_FAILURE = 2;
const REP_UNIT_REPETITIONS_ID = 1;
const REP_UNIT_TILL_FAILURE_ID = 2;

const WEIGHT_UNIT_KG = 1;
const WEIGHT_UNIT_LB = 2;
Expand Down
2 changes: 1 addition & 1 deletion lib/helpers/misc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ String repText(
// 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 (repetitionUnitObj.id != REP_UNIT_REPETITIONS || weight == 0 || weight == null) {
if (repetitionUnitObj.id != REP_UNIT_REPETITIONS_ID || weight == 0 || weight == null) {
out.add(repetitionUnitObj.name);
}
}
Expand Down
2 changes: 2 additions & 0 deletions lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -706,6 +706,8 @@
"images": "Images",
"language": "Language",
"addExercise": "Add exercise",
"addSuperset": "Add superset",
"setHasNoExercises": "This set has no exercises yet!",
"contributeExercise": "Contribute an exercise",
"translation": "Translation",
"translateExercise": "Translate this exercise now",
Expand Down
57 changes: 41 additions & 16 deletions lib/models/workouts/slot_entry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/

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/base_config.dart';
Expand Down Expand Up @@ -68,11 +69,11 @@ class SlotEntry {
@JsonKey(required: true, name: 'repetition_rounding', fromJson: stringToNum)
late num repetitionRounding;

@JsonKey(required: true, name: 'reps_configs')
late List<BaseConfig> repsConfigs;
@JsonKey(required: false, name: 'reps_configs', includeToJson: false, defaultValue: [])
late List<BaseConfig> repsConfigs = [];

@JsonKey(required: true, name: 'max_reps_configs')
late List<BaseConfig> maxRepsConfigs;
@JsonKey(required: false, name: 'max_reps_configs', includeToJson: false, defaultValue: [])
late List<BaseConfig> maxRepsConfigs = [];

@JsonKey(required: true, name: 'weight_unit')
late int weightUnitId;
Expand All @@ -83,23 +84,23 @@ class SlotEntry {
@JsonKey(required: true, name: 'weight_rounding', fromJson: stringToNum)
late num weightRounding;

@JsonKey(required: true, name: 'weight_configs')
late List<BaseConfig> weightConfigs;
@JsonKey(required: false, name: 'weight_configs', includeToJson: false, defaultValue: [])
late List<BaseConfig> weightConfigs = [];

@JsonKey(required: true, name: 'max_weight_configs')
late List<BaseConfig> maxWeightConfigs;
@JsonKey(required: false, name: 'max_weight_configs', includeToJson: false, defaultValue: [])
late List<BaseConfig> maxWeightConfigs = [];

@JsonKey(required: true, name: 'set_nr_configs')
late List<BaseConfig> nrOfSetsConfigs;
@JsonKey(required: false, name: 'set_nr_configs', includeToJson: false, defaultValue: [])
late List<BaseConfig> nrOfSetsConfigs = [];

@JsonKey(required: true, name: 'rir_configs')
late List<BaseConfig> rirConfigs;
@JsonKey(required: false, name: 'rir_configs', includeToJson: false, defaultValue: [])
late List<BaseConfig> rirConfigs = [];

@JsonKey(required: true, name: 'rest_configs')
late List<BaseConfig> restTimeConfigs;
@JsonKey(required: false, name: 'rest_configs', includeToJson: false, defaultValue: [])
late List<BaseConfig> restTimeConfigs = [];

@JsonKey(required: true, name: 'max_rest_configs')
late List<BaseConfig> maxRestTimeConfigs;
@JsonKey(required: false, name: 'max_rest_configs', includeToJson: false, defaultValue: [])
late List<BaseConfig> maxRestTimeConfigs = [];

@JsonKey(required: true)
late Object? config;
Expand All @@ -119,6 +120,30 @@ class SlotEntry {

SlotEntry.empty();

SlotEntry.withData({
required this.slotId,
String? comment,
int? order,
String? type,
required Exercise exercise,
int? weightUnitId,
num? weightRounding,
int? repetitionUnitId,
num? repetitionRounding,
}) {
this.order = order ?? 1;
this.comment = comment ?? '';
config = null;
this.type = type ?? 'normal';
exerciseObj = exercise;
exerciseId = exercise.id!;
this.weightUnitId = weightUnitId ?? WEIGHT_UNIT_KG_ID;
this.weightRounding = weightRounding ?? 2.5;

this.repetitionUnitId = repetitionUnitId ?? REP_UNIT_REPETITIONS_ID;
this.repetitionRounding = repetitionRounding ?? 1;
}

get rir {
return 'DELETE ME! RIR';
}
Expand Down
72 changes: 32 additions & 40 deletions lib/models/workouts/slot_entry.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 27 additions & 4 deletions lib/providers/routines.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class RoutinesProvider with ChangeNotifier {

/// Return the default weight unit (kg)
WeightUnit get defaultWeightUnit {
return _weightUnits.firstWhere((element) => element.id == DEFAULT_WEIGHT_UNIT);
return _weightUnits.firstWhere((element) => element.id == WEIGHT_UNIT_KG_ID);
}

List<RepetitionUnit> get repetitionUnits {
Expand All @@ -95,7 +95,7 @@ class RoutinesProvider with ChangeNotifier {

/// Return the default weight unit (reps)
RepetitionUnit get defaultRepetitionUnit {
return _repetitionUnit.firstWhere((element) => element.id == REP_UNIT_REPETITIONS);
return _repetitionUnit.firstWhere((element) => element.id == REP_UNIT_REPETITIONS_ID);
}

List<Routine> getPlans() {
Expand Down Expand Up @@ -477,7 +477,7 @@ class RoutinesProvider with ChangeNotifier {
}

Future<void> deleteSlot(int slotId) async {
final data = await baseProvider.deleteRequest(_slotsUrlPath, slotId);
await baseProvider.deleteRequest(_slotsUrlPath, slotId);

for (final routine in _routines) {
for (final day in routine.days) {
Expand Down Expand Up @@ -508,8 +508,31 @@ class RoutinesProvider with ChangeNotifier {
notifyListeners();
}

Future<SlotEntry> addSlotEntry(SlotEntry entry) async {
final data = await baseProvider.post(
entry.toJson(),
baseProvider.makeUrl(_slotEntriesUrlPath),
);
final newEntry = SlotEntry.fromJson(data);
newEntry.exerciseObj = (await _exercises.fetchAndSetExercise(newEntry.exerciseId))!;

for (final routine in _routines) {
for (final day in routine.days) {
for (final slot in day.slots) {
if (slot.id == entry.slotId) {
slot.entries.add(newEntry);
break;
}
}
}
}

notifyListeners();
return newEntry;
}

Future<void> deleteSlotEntry(int id) async {
await baseProvider.deleteRequest(_slotsUrlPath, id);
await baseProvider.deleteRequest(_slotEntriesUrlPath, id);
for (final routine in _routines) {
for (final day in routine.days) {
for (final slot in day.slots) {
Expand Down
Loading

0 comments on commit f7461ab

Please sign in to comment.