wt1.1.2e NumberPicker
This commit is contained in:
@@ -0,0 +1,91 @@
|
||||
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;
|
||||
final List<double> repeats = List();
|
||||
|
||||
double initialRM;
|
||||
double unitQuantity;
|
||||
double quantity;
|
||||
double origQuantity;
|
||||
|
||||
double firstQuantity; // quantity of the first test
|
||||
double firstUnitQuantity; // unit quantity of the first test
|
||||
|
||||
@override
|
||||
ExerciseControlBloc({this.exerciseRepository, this.readonly, this.percentToCalculate}) : super(ExerciseControlInitial()) {
|
||||
firstUnitQuantity = exerciseRepository.exercise.unitQuantity;
|
||||
firstQuantity = exerciseRepository.exercise.quantity;
|
||||
repeats.add(firstUnitQuantity);
|
||||
repeats.add(firstQuantity);
|
||||
|
||||
initialRM = this.calculate1RM(percent75: false);
|
||||
unitQuantity = this.calculate1RM(percent75: true).roundToDouble();
|
||||
quantity = percentToCalculate == 0.75 ? 12 : 30;
|
||||
origQuantity = quantity;
|
||||
|
||||
exerciseRepository.setUnitQuantity(unitQuantity);
|
||||
|
||||
}
|
||||
|
||||
@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++;
|
||||
//print("step " + step.toString() + " quantity " + quantity.toString() + " origQ: " + origQuantity.toString());
|
||||
repeats.add(quantity);
|
||||
quantity = origQuantity;
|
||||
await exerciseRepository.addExercise();
|
||||
|
||||
|
||||
exerciseRepository.setQuantity(quantity);
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
part of 'exercise_control_bloc.dart';
|
||||
|
||||
@immutable
|
||||
abstract class ExerciseControlEvent extends Equatable {
|
||||
const ExerciseControlEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class ExerciseControlLoad extends ExerciseControlEvent {
|
||||
const ExerciseControlLoad();
|
||||
}
|
||||
|
||||
class ExerciseControlQuantityChange extends ExerciseControlEvent {
|
||||
final double quantity;
|
||||
final int step;
|
||||
const ExerciseControlQuantityChange({this.quantity, this.step});
|
||||
}
|
||||
|
||||
class ExerciseControlSubmit extends ExerciseControlEvent {
|
||||
final int step;
|
||||
const ExerciseControlSubmit({this.step});
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
part of 'exercise_control_bloc.dart';
|
||||
|
||||
@immutable
|
||||
abstract class ExerciseControlState extends Equatable {
|
||||
const ExerciseControlState();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class ExerciseControlInitial extends ExerciseControlState {
|
||||
const ExerciseControlInitial();
|
||||
}
|
||||
|
||||
class ExerciseControlLoading extends ExerciseControlState {
|
||||
const ExerciseControlLoading();
|
||||
}
|
||||
|
||||
class ExerciseControlReady extends ExerciseControlState {
|
||||
const ExerciseControlReady();
|
||||
}
|
||||
|
||||
class ExerciseControlError extends ExerciseControlState {
|
||||
final String message;
|
||||
const ExerciseControlError({this.message});
|
||||
|
||||
|
||||
@override
|
||||
List<Object> get props => [message];
|
||||
}
|
||||
Reference in New Issue
Block a user