private async Task ValidateActionAsync()
        {
            try
            {
                if (BindingTrainingExerciseSetReps != null && BindingTrainingExerciseSetReps.Count > 0 &&
                    _trainingExercise != null)
                {
                    var trainingExercise = _trainingExercise.Clone();
                    if (trainingExercise.TrainingExerciseSets == null)
                    {
                        trainingExercise.TrainingExerciseSets = new List <TrainingExerciseSet>();
                    }
                    else
                    {
                        trainingExercise.TrainingExerciseSets.Clear(); // empty sets for replacing
                    }
                    trainingExercise.RestTime = RestTime;              // don't forget restime...
                    trainingExercise.EccentricContractionTempo  = EccentricContractionTempo;
                    trainingExercise.StretchPositionTempo       = StretchPositionTempo;
                    trainingExercise.ConcentricContractionTempo = ConcentricContractionTempo;
                    trainingExercise.ContractedPositionTempo    = ContractedPositionTempo;
                    int    nbSet = 0, currentRepValue = 0;
                    var    tupleSetRepList = new List <Tuple <int, int, double> >();
                    int    repValue;
                    double weightValue, currentWeightValue = 0;
                    for (int i = 0; i < BindingTrainingExerciseSetReps.Count; i++)
                    {
                        repValue    = BindingTrainingExerciseSetReps[i].Reps;
                        weightValue = BindingTrainingExerciseSetReps[i].Weights;
                        if (repValue == 0)
                        {
                            continue;
                        }

                        if (weightValue == currentWeightValue && repValue == currentRepValue)
                        {
                            nbSet++;
                        }
                        else
                        {
                            if (nbSet != 0)
                            {
                                tupleSetRepList.Add(new Tuple <int, int, double>(nbSet, currentRepValue, currentWeightValue));
                            }
                            currentRepValue    = repValue;
                            currentWeightValue = weightValue;
                            nbSet = 1;
                        }
                    }

                    //last data
                    if (nbSet != 0)
                    {
                        tupleSetRepList.Add(new Tuple <int, int, double>(nbSet, currentRepValue, currentWeightValue));
                    }

                    var unit = await GetExerciseUnit();

                    int id = 1;
                    foreach (Tuple <int, int, double> tupleSetRep in tupleSetRepList)
                    {
                        trainingExercise.TrainingExerciseSets.Add(new TrainingExerciseSet()
                        {
                            UserId             = trainingExercise.UserId,
                            Year               = trainingExercise.Year,
                            WeekOfYear         = trainingExercise.WeekOfYear,
                            DayOfWeek          = trainingExercise.DayOfWeek,
                            TrainingDayId      = trainingExercise.TrainingDayId,
                            TrainingExerciseId = trainingExercise.Id,
                            Id           = id,
                            NumberOfSets = tupleSetRep.Item1,
                            NumberOfReps = tupleSetRep.Item2,
                            Weight       = tupleSetRep.Item3,
                            Unit         = unit
                        });
                        id++;
                    }

                    //Save in server
                    var trainingDayKey = new TrainingDayKey()
                    {
                        UserId        = trainingExercise.UserId,
                        Year          = trainingExercise.Year,
                        WeekOfYear    = trainingExercise.WeekOfYear,
                        DayOfWeek     = trainingExercise.DayOfWeek,
                        TrainingDayId = trainingExercise.TrainingDayId
                    };
                    var trainingDayScenario = new TrainingDayScenario()
                    {
                        ManageExercise = true
                    };
                    var trainingDay = await TrainingDayWebService.GetTrainingDayAsync(trainingDayKey, trainingDayScenario);

                    //modify datas
                    var trainingExerciseTmp = trainingDay.TrainingExercises.Where(t => TrainingExerciseKey.IsEqualByKey(t, trainingExercise)).FirstOrDefault();
                    var indexOf             = trainingDay.TrainingExercises.IndexOf(trainingExerciseTmp);
                    if (indexOf != -1)
                    {
                        //Replace exercise and sets
                        trainingDay.TrainingExercises[indexOf] = trainingExercise;
                        //update to server
                        trainingDay = await TrainingDayWebService.UpdateTrainingDayAsync(trainingDay, trainingDayScenario);

                        //Save modified data in local database
                        _trainingDayService.UpdateTrainingDay(trainingDay, trainingDayScenario);

                        CloseViewModel();
                    }
                }
            }
            catch (Exception except)
            {
                ILogger.Instance.Error("Unable to validate training rep/set", except);
                await _userDialog.AlertAsync(except.Message, Translation.Get(TRS.ERROR), Translation.Get(TRS.OK));
            }
        }
Ejemplo n.º 2
0
        private async Task <bool> SaveDataAsync()
        {
            bool result = false;

            _trainingDay.BeginHour = DateTime.Now.Date.Add(BindingTrainingDay.BeginTime).ToUniversalTime();
            _trainingDay.EndHour   = DateTime.Now.Date.Add(BindingTrainingDay.EndTime).ToUniversalTime();
            _trainingDay.Unit      = BindingTrainingDay.Unit;

            if (_editMode == TEditMode.Create)
            {
                _trainingDay.TrainingDayId = 0; // force calculate id
                var trainingDayCreated = await TrainingDayWebService.CreateTrainingDaysAsync(_trainingDay);

                if (trainingDayCreated != null)
                {
                    _trainingDay.TrainingDayId = trainingDayCreated.TrainingDayId;
                    var trainingDayScenario = new TrainingDayScenario()
                    {
                        ManageExercise = true
                    };
                    _trainingDayService.UpdateTrainingDay(trainingDayCreated, trainingDayScenario);
                    result = true;
                }
            }
            else if (_editMode == TEditMode.Edit)
            {
                if (BindingTrainingDay.AutomaticalUnitConvertion)
                {
                    var trainingDayScenario = new TrainingDayScenario()
                    {
                        ManageExercise = true
                    };
                    var onlineTrainingDay = await TrainingDayWebService.GetTrainingDayAsync(_trainingDay, trainingDayScenario);

                    _trainingDay.TrainingExercises = onlineTrainingDay.TrainingExercises; // Replace excercise
                    _trainingDayService.ChangeUnitForTrainingExercises(_trainingDay, onlineTrainingDay.Unit);
                    var trainingDayUpdated = await TrainingDayWebService.UpdateTrainingDayAsync(_trainingDay, trainingDayScenario);

                    if (trainingDayUpdated != null)
                    {
                        _trainingDayService.UpdateTrainingDay(trainingDayUpdated, trainingDayScenario);
                        result = true;
                    }
                }
                else
                {
                    var trainingDayScenario = new TrainingDayScenario()
                    {
                        ManageExercise = false
                    };
                    var trainingDayUpdated = await TrainingDayWebService.UpdateTrainingDayAsync(_trainingDay, trainingDayScenario);

                    if (trainingDayUpdated != null)
                    {
                        _trainingDayService.UpdateTrainingDay(trainingDayUpdated, trainingDayScenario);
                        result = true;
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 3
0
        private async Task AddExerciseActionAsync(TrainingDay trainingDay)
        {
            if (trainingDay == null)
            {
                return;
            }
            try
            {
                int indexOfTrainingDay = _trainingDays.IndexOf(trainingDay);
                if (indexOfTrainingDay != -1)
                {
                    var selectTrainingExercisesViewModelResut = await SelectTrainingExercisesViewModel.ShowAsync(trainingDay, this, async (trainingDayKey, selectedBodyExerciseList) => {
                        // Validate by upload data on server
                        if (trainingDayKey != null && selectedBodyExerciseList != null)
                        {
                            var trainingDayScenario = new TrainingDayScenario()
                            {
                                ManageExercise = true
                            };
                            var modifiedTrainingDay = await TrainingDayWebService.GetTrainingDayAsync(trainingDayKey, trainingDayScenario);

                            if (modifiedTrainingDay.TrainingExercises == null)
                            { // New training exercises
                                modifiedTrainingDay.TrainingExercises = new List <TrainingExercise>();
                            }

                            //AddExerciseActionAsync new exercises
                            int nextIdTrainingExercise = 1;
                            if (modifiedTrainingDay.TrainingExercises.Count > 0)
                            {
                                nextIdTrainingExercise = modifiedTrainingDay.TrainingExercises.Max(te => te.Id) + 1;
                            }
                            foreach (var bodyExercise in selectedBodyExerciseList)
                            {
                                var trainingExercise = new TrainingExercise()
                                {
                                    Year           = modifiedTrainingDay.Year,
                                    WeekOfYear     = modifiedTrainingDay.WeekOfYear,
                                    DayOfWeek      = modifiedTrainingDay.DayOfWeek,
                                    UserId         = modifiedTrainingDay.UserId,
                                    TrainingDayId  = modifiedTrainingDay.TrainingDayId,
                                    BodyExerciseId = bodyExercise.Id,
                                    Id             = nextIdTrainingExercise
                                };
                                trainingExercise.RestTime = 0;
                                trainingExercise.EccentricContractionTempo  = 1;
                                trainingExercise.StretchPositionTempo       = 0;
                                trainingExercise.ConcentricContractionTempo = 1;
                                trainingExercise.ContractedPositionTempo    = 0;
                                modifiedTrainingDay.TrainingExercises.Add(trainingExercise);
                                nextIdTrainingExercise++;
                            }
                            //synchronise to server
                            modifiedTrainingDay = await TrainingDayWebService.UpdateTrainingDayAsync(modifiedTrainingDay, trainingDayScenario);
                            //local update
                            _trainingDayService.UpdateTrainingDay(modifiedTrainingDay, trainingDayScenario);
                            return(true);
                        }
                        return(false);
                    });

                    if (selectTrainingExercisesViewModelResut.Result)
                    {
                        //reload local data
                        var trainingDayScenario = new TrainingDayScenario()
                        {
                            ManageExercise = true
                        };
                        trainingDay = _trainingDayService.GetTrainingDay(trainingDay, trainingDayScenario);
                        //Change modified trainingday in list of trainingdays
                        _trainingDays[indexOfTrainingDay] = trainingDay;
                        //Binding trainingDay for refresh view
                        CreateOrReplaceBindingTrainingDay(trainingDay);
                    }
                }
            }
            catch (Exception except)
            {
                ILogger.Instance.Error("Unable to add exercise", except);
                await _userDialog.AlertAsync(except.Message, Translation.Get(TRS.ERROR), Translation.Get(TRS.OK));
            }
        }