Esempio n. 1
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();

            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)
            {
                var trainingDayScenario = new TrainingDayScenario()
                {
                    ManageExercise = true
                };
                var trainingDayUpdated = await TrainingDayWebService.UpdateTrainingDayAsync(_trainingDay, trainingDayScenario);

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

            return(result);
        }
        internal TrainingWeek UpdateTrainingWeek(TrainingWeek trainingWeek, TrainingWeekScenario trainingWeekScenario)
        {
            DbContext.ChangeTracker.AutoDetectChangesEnabled = false;
            TrainingWeek trainingWeekResult = null;

            trainingWeekResult = _trainingWeekModule.Update(trainingWeek);

            if (trainingWeekScenario != null && trainingWeekScenario.ManageTrainingDay)
            {
                var trainingDayCriteria = new TrainingDayCriteria()
                {
                    UserId = new StringCriteria()
                    {
                        Equal = trainingWeek.UserId
                    },
                    Year = new IntegerCriteria()
                    {
                        Equal = trainingWeek.Year
                    },
                    WeekOfYear = new IntegerCriteria()
                    {
                        Equal = trainingWeek.WeekOfYear
                    }
                };
                var userUnit       = AppUtils.GetUserUnit(_userInfosService, trainingWeek.UserId);
                var trainingDaysDb = _trainingDayService.FindTrainingDay(userUnit, trainingDayCriteria, trainingWeekScenario.TrainingDayScenario);
                if (trainingDaysDb != null && trainingDaysDb.Count > 0)
                {
                    foreach (var trainingDayDb in trainingDaysDb)
                    {
                        _trainingDayService.DeleteTrainingDay(trainingDayDb);
                    }
                }

                if (trainingWeek.TrainingDays != null)
                {
                    trainingWeekResult.TrainingDays = new List <TrainingDay>();
                    foreach (var trainingDay in trainingWeek.TrainingDays)
                    {
                        trainingWeekResult.TrainingDays.Add(_trainingDayService.UpdateTrainingDay(trainingDay, trainingWeekScenario.TrainingDayScenario));
                    }
                }
            }
            DbContext.ChangeTracker.DetectChanges();
            DbContext.SaveChanges();
            DbContext.ChangeTracker.AutoDetectChangesEnabled = true;
            return(trainingWeekResult);
        }
        internal TrainingWeek UpdateTrainingWeek(TrainingWeek trainingWeek, TrainingWeekScenario trainingWeekScenario)
        {
            TrainingWeek trainingWeekResult = null;

            trainingWeekResult = _trainingWeekModule.Update(trainingWeek);

            if (trainingWeekScenario != null && trainingWeekScenario.ManageTrainingDay)
            {
                var trainingDayCriteria = new TrainingDayCriteria()
                {
                    UserId = new StringCriteria()
                    {
                        Equal = trainingWeek.UserId
                    },
                    Year = new IntegerCriteria()
                    {
                        Equal = trainingWeek.Year
                    },
                    WeekOfYear = new IntegerCriteria()
                    {
                        Equal = trainingWeek.WeekOfYear
                    }
                };
                var trainingDaysDb = _trainingDayService.FindTrainingDay(trainingDayCriteria, trainingWeekScenario.TrainingDayScenario);
                if (trainingDaysDb != null && trainingDaysDb.Count > 0)
                {
                    foreach (var trainingDayDb in trainingDaysDb)
                    {
                        _trainingDayService.DeleteTrainingDay(trainingDayDb);
                    }
                }

                if (trainingWeek.TrainingDays != null)
                {
                    trainingWeekResult.TrainingDays = new List <TrainingDay>();
                    foreach (var trainingDay in trainingWeek.TrainingDays)
                    {
                        trainingWeekResult.TrainingDays.Add(_trainingDayService.UpdateTrainingDay(trainingDay, trainingWeekScenario.TrainingDayScenario));
                    }
                }
            }
            return(trainingWeekResult);
        }
Esempio n. 4
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);
        }
        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));
            }
        }
Esempio n. 6
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));
            }
        }