WT 1.26
This commit is contained in:
@@ -1,28 +1,142 @@
|
||||
import 'package:intl/intl.dart';
|
||||
import 'dart:async';
|
||||
import 'dart:collection';
|
||||
|
||||
import 'package:aitrainer_app/model/cache.dart';
|
||||
import 'package:aitrainer_app/util/app_language.dart';
|
||||
import 'package:aitrainer_app/model/exercise.dart';
|
||||
import 'package:aitrainer_app/model/workout_menu_tree.dart';
|
||||
import 'package:aitrainer_app/repository/exercise_repository.dart';
|
||||
import 'package:aitrainer_app/repository/workout_tree_repository.dart';
|
||||
import 'package:aitrainer_app/service/logging.dart';
|
||||
import 'package:aitrainer_app/util/calculate.dart';
|
||||
import 'package:aitrainer_app/util/app_language.dart';
|
||||
import 'package:aitrainer_app/util/common.dart';
|
||||
import 'package:aitrainer_app/util/enums.dart';
|
||||
import 'package:aitrainer_app/util/group_data.dart';
|
||||
import 'package:aitrainer_app/util/diagram_data.dart';
|
||||
import 'package:aitrainer_app/util/track.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:bloc/bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:fl_chart/fl_chart.dart';
|
||||
|
||||
part 'development_by_muscle_event.dart';
|
||||
|
||||
part 'development_by_muscle_state.dart';
|
||||
|
||||
enum DiagramType { sumMass, oneRepMax, percent }
|
||||
enum DiagramDateType { daily, weekly, monthly, yearly }
|
||||
|
||||
class GroupDate extends GroupData with Common {
|
||||
final List<Exercise> inputList;
|
||||
final List<DiagramData> outputList;
|
||||
|
||||
String? _origDatePart;
|
||||
late int _origExerciseTypeId;
|
||||
late Exercise _origExercise;
|
||||
|
||||
late double _sumQuantity;
|
||||
late double _maxQuantity;
|
||||
late int _countExercises;
|
||||
|
||||
late DiagramType diagramType;
|
||||
late DiagramDateType dateRate;
|
||||
|
||||
GroupDate({required this.inputList, required this.outputList});
|
||||
|
||||
double getQuantityByDate(Exercise exercise) {
|
||||
double sum = 0;
|
||||
if (this.diagramType == DiagramType.sumMass) {
|
||||
if (exercise.unitQuantity != null) {
|
||||
sum = exercise.quantity! * exercise.unitQuantity!;
|
||||
} else {
|
||||
sum = exercise.quantity!;
|
||||
}
|
||||
} else if (this.diagramType == DiagramType.oneRepMax || this.diagramType == DiagramType.percent) {
|
||||
if (exercise.unitQuantity != null) {
|
||||
sum = calculate1RM(exercise.quantity!, exercise.unitQuantity!);
|
||||
} else {
|
||||
sum = exercise.quantity!;
|
||||
}
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
@override
|
||||
void addTempData(Exercise exercise) {
|
||||
double newQuantity = getQuantityByDate(exercise);
|
||||
_sumQuantity = _sumQuantity + newQuantity;
|
||||
if (_maxQuantity < newQuantity) {
|
||||
_maxQuantity = newQuantity;
|
||||
}
|
||||
_countExercises = _countExercises + 1;
|
||||
_origDatePart = getDatePart(exercise.dateAdd!, dateRate);
|
||||
_origExerciseTypeId = exercise.exerciseTypeId!;
|
||||
_origExercise = exercise;
|
||||
}
|
||||
|
||||
@override
|
||||
bool checkNewType(Exercise exercise) {
|
||||
String exerciseDatePart = getDatePart(exercise.dateAdd!, dateRate);
|
||||
return _origDatePart == null || _origDatePart != exerciseDatePart || _origExerciseTypeId != exercise.exerciseTypeId;
|
||||
}
|
||||
|
||||
String getDatePart(DateTime date, DiagramDateType dateRate) {
|
||||
String datePart = DateFormat('MM.dd', AppLanguage().appLocal.toString()).format(date);
|
||||
if (dateRate == DiagramDateType.weekly) {
|
||||
datePart = weekNumber(date).toString();
|
||||
} else if (dateRate == DiagramDateType.monthly) {
|
||||
datePart = DateFormat('MMM', AppLanguage().appLocal.toString()).format(date);
|
||||
} else if (dateRate == DiagramDateType.yearly) {
|
||||
datePart = DateFormat('y', AppLanguage().appLocal.toString()).format(date);
|
||||
} else if (dateRate == DiagramDateType.daily) {
|
||||
datePart = DateFormat('MM.dd', AppLanguage().appLocal.toString()).format(date);
|
||||
}
|
||||
return datePart;
|
||||
}
|
||||
|
||||
@override
|
||||
void iteration() {
|
||||
this.resetTemp();
|
||||
Exercise? tempExercise;
|
||||
inputList.forEach((element) {
|
||||
tempExercise = element;
|
||||
if (this.checkNewType(element)) {
|
||||
if (_origDatePart == null) {
|
||||
this.addTempData(element);
|
||||
} else {
|
||||
this.temp2Output(_origExercise);
|
||||
this.resetTemp();
|
||||
this.addTempData(element);
|
||||
}
|
||||
} else {
|
||||
this.addTempData(element);
|
||||
}
|
||||
});
|
||||
if (tempExercise != null) {
|
||||
this.temp2Output(tempExercise!);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void temp2Output(Exercise exercise) {
|
||||
if (exercise.unitQuantity == null) {
|
||||
return;
|
||||
}
|
||||
Exercise newExercise = exercise.copy();
|
||||
newExercise.datePart = _origDatePart;
|
||||
if (this.diagramType == DiagramType.oneRepMax || this.diagramType == DiagramType.percent) {
|
||||
newExercise.calculated = _maxQuantity;
|
||||
} else {
|
||||
newExercise.calculated = _sumQuantity / _countExercises;
|
||||
}
|
||||
DiagramData data = DiagramData(newExercise.datePart!, newExercise.calculated);
|
||||
outputList.add(data);
|
||||
}
|
||||
|
||||
@override
|
||||
void resetTemp() {
|
||||
_countExercises = 0;
|
||||
_sumQuantity = 0;
|
||||
_maxQuantity = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
class DiagramType {
|
||||
static String sumMass = "sumMass";
|
||||
static String oneRepMax = "oneRepMax";
|
||||
@@ -33,7 +147,7 @@ class DiagramType {
|
||||
=========== GROUPDATE CLASS
|
||||
*/
|
||||
|
||||
class GroupDate extends GroupData with Calculate, Common {
|
||||
class GroupDate extends GroupData with Common {
|
||||
final List<Exercise> inputList;
|
||||
final List<Exercise> outputList;
|
||||
|
||||
@@ -136,7 +250,7 @@ class GroupDate extends GroupData with Calculate, Common {
|
||||
/*
|
||||
=========== CHART DATA CLASS
|
||||
*/
|
||||
class GroupChart extends GroupData with Calculate {
|
||||
class GroupChart extends GroupData with Common {
|
||||
final List<dynamic> inputList;
|
||||
LinkedHashMap<int, ChartDataExtended> outputList = LinkedHashMap();
|
||||
|
||||
@@ -186,9 +300,8 @@ class GroupChart extends GroupData with Calculate {
|
||||
_minData = diagramValue;
|
||||
}
|
||||
|
||||
BarChartGroupData data = BarChartGroupData(x: exercise.dateAdd!.millisecondsSinceEpoch, barRods: [
|
||||
BarChartRodData(y: diagramValue, width: 12, colors: [Colors.lightBlue, Colors.lightBlueAccent])
|
||||
]);
|
||||
BarChartGroupData data = BarChartGroupData(
|
||||
x: exercise.dateAdd!.millisecondsSinceEpoch, barRods: [BarChartRodData(toY: diagramValue, width: 12, color: Colors.lightBlue)]);
|
||||
_chartData.add(data);
|
||||
_origExerciseTypeId = exercise.exerciseTypeId!;
|
||||
}
|
||||
@@ -262,7 +375,7 @@ class ChartDataExtended {
|
||||
element.barRods.forEach((rods) {
|
||||
var barChartData = {
|
||||
'x': element.x,
|
||||
'y': rods.y,
|
||||
'y': rods.toY,
|
||||
};
|
||||
listBarChartData.add(barChartData);
|
||||
});
|
||||
@@ -274,20 +387,20 @@ class ChartDataExtended {
|
||||
};
|
||||
return chartData;
|
||||
}
|
||||
}
|
||||
} */
|
||||
|
||||
class DevelopmentByMuscleBloc extends Bloc<DevelopmentByMuscleEvent, DevelopmentByMuscleState> with Calculate, Logging {
|
||||
class DevelopmentByMuscleBloc extends Bloc<DevelopmentByMuscleEvent, DevelopmentByMuscleState> with Common, Logging {
|
||||
final WorkoutTreeRepository workoutTreeRepository;
|
||||
|
||||
final ExerciseRepository exerciseRepository = ExerciseRepository();
|
||||
LinkedHashMap<int, ChartDataExtended> listChartData = LinkedHashMap();
|
||||
late List<BarChartGroupData> chartData;
|
||||
String diagramType = DiagramType.sumMass;
|
||||
String dateRate = DateRate.daily;
|
||||
double basePercent = 0;
|
||||
final List<DiagramData> diagramData = [];
|
||||
int actualExerciseType = 0;
|
||||
DiagramType diagramType = DiagramType.sumMass;
|
||||
DiagramDateType diagramDateType = DiagramDateType.monthly;
|
||||
|
||||
@override
|
||||
DevelopmentByMuscleBloc({required this.workoutTreeRepository}) : super(DevelopmentByMuscleStateInitial());
|
||||
DevelopmentByMuscleBloc({required this.workoutTreeRepository}) : super(DevelopmentByMuscleStateInitial()) {
|
||||
on<DevelopmentByMuscleLoad>(_onLoad);
|
||||
}
|
||||
|
||||
Future<void> getData() async {
|
||||
workoutTreeRepository.sortedTree.clear();
|
||||
@@ -299,93 +412,13 @@ class DevelopmentByMuscleBloc extends Bloc<DevelopmentByMuscleEvent, Development
|
||||
workoutTree.selected = false;
|
||||
});
|
||||
});
|
||||
|
||||
this.getChartData();
|
||||
}
|
||||
|
||||
void getChartData() {
|
||||
List<Exercise>? exercises = exerciseRepository.getExerciseList();
|
||||
|
||||
//print("-- Start calculate --- ");
|
||||
exercises = this.groupByDate(exercises);
|
||||
|
||||
exercises = sort(exercises, true);
|
||||
/* exercises.forEach((exercise) {
|
||||
print ("Chart exercise " + exercise.toJsonDatePart().toString());
|
||||
});*/
|
||||
|
||||
listChartData = LinkedHashMap();
|
||||
GroupChart groupChart = GroupChart(inputList: exercises, outputList: listChartData);
|
||||
groupChart.diagramType = this.diagramType;
|
||||
groupChart.iteration();
|
||||
listChartData = groupChart.outputList;
|
||||
|
||||
listChartData.forEach((key, value) {
|
||||
//trace("typeid " + key.toString() + " chardata " + value.toJson().toString());
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
List<Exercise> groupByDate(List<Exercise>? exercises) {
|
||||
List<Exercise> groupedExercises = [];
|
||||
if (exercises != null) {
|
||||
exercises = sort(exercises, false);
|
||||
exercises.forEach((exercise) {
|
||||
//trace("Date exercise " + exercise.toJsonDatePart().toString());
|
||||
});
|
||||
|
||||
GroupDate groupDate = GroupDate(inputList: exercises, outputList: groupedExercises);
|
||||
groupDate.dateRate = this.dateRate;
|
||||
groupDate.diagramType = this.diagramType;
|
||||
groupDate.iteration();
|
||||
groupedExercises = groupDate.outputList;
|
||||
}
|
||||
|
||||
/* groupedExercises.forEach((element) {
|
||||
print("Grouped " + element.toJsonDatePart().toString());
|
||||
});*/
|
||||
|
||||
return groupedExercises;
|
||||
}
|
||||
|
||||
List<Exercise> sort(List<Exercise> exercises, bool asc) {
|
||||
exercises.sort((a, b) {
|
||||
var aDateId = a.exerciseTypeId.toString() + "_" + a.datePart.toString();
|
||||
var bDateId = b.exerciseTypeId.toString() + "_" + b.datePart.toString();
|
||||
|
||||
return asc ? aDateId.compareTo(bDateId) : bDateId.compareTo(aDateId);
|
||||
});
|
||||
return exercises;
|
||||
}
|
||||
|
||||
String getDateFormat(DateTime datetime) {
|
||||
return DateFormat('yMd', AppLanguage().appLocal.toString()).format(datetime);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<DevelopmentByMuscleState> mapEventToState(DevelopmentByMuscleEvent event) async* {
|
||||
try {
|
||||
if (event is DevelopmentByMuscleLoad) {
|
||||
yield DevelopmentByMuscleLoadingState();
|
||||
Track().track(TrackingEvent.my_muscle_development);
|
||||
Cache().setActivityDonePrefs(ActivityDone.isMuscleDevelopmentSeen);
|
||||
await getData();
|
||||
yield DevelopmentByMuscleReadyState();
|
||||
} else if (event is DevelopmentByMuscleDiagramTypeChange) {
|
||||
yield DevelopmentByMuscleLoadingState();
|
||||
String type = event.diagramType;
|
||||
this.diagramType = type;
|
||||
getChartData();
|
||||
yield DevelopmentByMuscleReadyState();
|
||||
} else if (event is DevelopmentByMuscleDateRateChange) {
|
||||
yield DevelopmentByMuscleLoadingState();
|
||||
String dateRate = event.dateRate;
|
||||
this.dateRate = dateRate;
|
||||
getChartData();
|
||||
yield DevelopmentByMuscleReadyState();
|
||||
}
|
||||
} on Exception catch (e) {
|
||||
yield DevelopmentByMuscleErrorState(message: e.toString());
|
||||
}
|
||||
void _onLoad(DevelopmentByMuscleLoad event, Emitter<DevelopmentByMuscleState> emit) async {
|
||||
emit(DevelopmentByMuscleLoadingState());
|
||||
Track().track(TrackingEvent.my_muscle_development);
|
||||
Cache().setActivityDonePrefs(ActivityDone.isMuscleDevelopmentSeen);
|
||||
await getData();
|
||||
emit(DevelopmentByMuscleReadyState());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ class DevelopmentByMuscleLoad extends DevelopmentByMuscleEvent {
|
||||
}
|
||||
|
||||
class DevelopmentByMuscleDateRateChange extends DevelopmentByMuscleEvent {
|
||||
final String dateRate;
|
||||
final DiagramDateType dateRate;
|
||||
const DevelopmentByMuscleDateRateChange({required this.dateRate});
|
||||
|
||||
@override
|
||||
@@ -21,7 +21,7 @@ class DevelopmentByMuscleDateRateChange extends DevelopmentByMuscleEvent {
|
||||
}
|
||||
|
||||
class DevelopmentByMuscleDiagramTypeChange extends DevelopmentByMuscleEvent {
|
||||
final String diagramType;
|
||||
final DiagramType diagramType;
|
||||
const DevelopmentByMuscleDiagramTypeChange({required this.diagramType});
|
||||
|
||||
@override
|
||||
|
||||
Reference in New Issue
Block a user