92 lines
2.9 KiB
Dart
92 lines
2.9 KiB
Dart
import 'dart:async';
|
|
import 'package:aitrainer_app/repository/exercise_repository.dart';
|
|
import 'package:bloc/bloc.dart';
|
|
import 'package:equatable/equatable.dart';
|
|
import 'package:meta/meta.dart';
|
|
|
|
part 'exercise_control_event.dart';
|
|
|
|
part 'exercise_control_state.dart';
|
|
|
|
class ExerciseControlBloc extends Bloc<ExerciseControlEvent, ExerciseControlState> {
|
|
final ExerciseRepository exerciseRepository;
|
|
final bool readonly;
|
|
final double percentToCalculate;
|
|
int step = 1;
|
|
|
|
double initialRM;
|
|
double unitQuantity;
|
|
double quantity;
|
|
double origQuantity;
|
|
|
|
double firstQuantity; // quantity of the first test
|
|
double firstUnitQuantity; // unit quantity of the first test
|
|
|
|
double scrollOffset = 0;
|
|
|
|
@override
|
|
ExerciseControlBloc({this.exerciseRepository, this.readonly, this.percentToCalculate}) : super(ExerciseControlInitial()) {
|
|
initialRM = this.calculate1RM(percent75: false);
|
|
unitQuantity = this.calculate1RM(percent75: true).roundToDouble();
|
|
quantity = percentToCalculate == 0.75 ? 12 : 30;
|
|
origQuantity = quantity;
|
|
|
|
exerciseRepository.setUnitQuantity(unitQuantity);
|
|
exerciseRepository.setQuantity(quantity);
|
|
}
|
|
|
|
@override
|
|
Stream<ExerciseControlState> mapEventToState(ExerciseControlEvent event) async* {
|
|
try {
|
|
if (event is ExerciseControlLoad) {
|
|
yield ExerciseControlLoading();
|
|
step = 1;
|
|
yield ExerciseControlReady();
|
|
} else if (event is ExerciseControlQuantityChange) {
|
|
//yield ExerciseControlLoading();
|
|
if (event.step == step) {
|
|
exerciseRepository.setQuantity(event.quantity);
|
|
quantity = event.quantity;
|
|
}
|
|
//yield ExerciseControlReady();
|
|
} else if (event is ExerciseControlSubmit) {
|
|
yield ExerciseControlLoading();
|
|
if (event.step == step) {
|
|
step++;
|
|
scrollOffset = step * 400.0;
|
|
|
|
quantity = origQuantity;
|
|
if (exerciseRepository.exercise.quantity == null) {
|
|
exerciseRepository.setQuantity(12);
|
|
}
|
|
exerciseRepository.end = DateTime.now();
|
|
await exerciseRepository.addExercise();
|
|
|
|
exerciseRepository.setQuantity(quantity);
|
|
exerciseRepository.exercise.exerciseId = null;
|
|
}
|
|
yield ExerciseControlReady();
|
|
}
|
|
} on Exception catch (e) {
|
|
yield ExerciseControlError(message: e.toString());
|
|
}
|
|
}
|
|
|
|
double calculate1RM({bool percent75}) {
|
|
if (exerciseRepository.exercise == null) {
|
|
exerciseRepository.getLastExercise();
|
|
}
|
|
double weight = exerciseRepository.exercise.unitQuantity;
|
|
double repeat = exerciseRepository.exercise.quantity;
|
|
if (weight == 0 || repeat == 0) {
|
|
return 0;
|
|
}
|
|
|
|
double rmWendler = weight * repeat * 0.0333 + weight;
|
|
double rmOconner = weight * (1 + repeat / 40);
|
|
double average = (rmWendler + rmOconner) / 2;
|
|
|
|
return percent75 ? average * this.percentToCalculate : average;
|
|
}
|
|
}
|