示例#1
0
        public async Task CopyTrainingDayActionAsync(DayOfWeek dayOfWeek)
        {
            if (TrainingWeek == null)
            {
                return;
            }
            try
            {
                var trainingDayKey = new TrainingDayKey()
                {
                    UserId     = TrainingWeek.UserId,
                    Year       = TrainingWeek.Year,
                    WeekOfYear = TrainingWeek.WeekOfYear,
                    DayOfWeek  = (int)dayOfWeek
                };
                List <GenericData> datas = new List <GenericData>();
                foreach (DayOfWeek dayOfWeekTmp in Enum.GetValues(typeof(DayOfWeek)))
                {
                    if (dayOfWeekTmp != dayOfWeek)
                    {
                        datas.Add(new GenericData()
                        {
                            Tag = (int)dayOfWeekTmp, Name = Translation.Get(dayOfWeekTmp.ToString().ToUpper())
                        });
                    }
                }

                var result = await ListViewModel.ShowGenericListAsync(Translation.Get(TRS.DAY_OF_WEEK), datas, null, this);

                if (result.Validated && result.SelectedData != null)
                {
                    int dayOfWeekSelected = (int)result.SelectedData.Tag;
                    //Copy day on server
                    await TrainingDayWebService.CopyDayOfTrainingDay(trainingDayKey, dayOfWeekSelected);

                    //Reload updated TrainingWeek on server
                    var updatedTrainingWeek = await TrainingWeekWebService.GetTrainingWeekAsync(TrainingWeek, true);

                    if (updatedTrainingWeek != null)
                    {
                        var trainingWeekService  = new TrainingWeekService(DbContext);
                        var trainingWeekScenario = new TrainingWeekScenario()
                        {
                            ManageTrainingDay = true
                        };
                        trainingWeekService.UpdateTrainingWeek(updatedTrainingWeek, trainingWeekScenario);
                        //Update UI
                        TrainingWeek = updatedTrainingWeek;
                        await SynchronizeDataAsync();
                    }
                }
            }
            catch (Exception except)
            {
                await _userDialog.AlertAsync(except.Message, Translation.Get(TRS.ERROR), Translation.Get(TRS.OK));
            }
        }
 public CopyTrainingWeekViewModel() : base()
 {
     _dbContext           = Resolver.Resolve <ISQLite>().GetConnection();
     _trainingWeekService = new TrainingWeekService(_dbContext);
 }
示例#3
0
 public TrainingJournalViewModel() : base()
 {
     _userDialog          = Resolver.Resolve <IUserDialogs>();
     _dbContext           = Resolver.Resolve <ISQLite> ().GetConnection();
     _trainingWeekService = new TrainingWeekService(_dbContext);
 }
示例#4
0
        public static async Task <bool> SynchronizeTrainingWeeksAsync(SQLiteConnection dbContext)
        {
            bool result = false;

            try
            {
                //Synchronize TrainingWeek with server (with trainingday and exercise)
                var criteria = new TrainingWeekCriteria();
                criteria.UserId = new StringCriteria()
                {
                    Equal = UserData.Instance.UserInfo.UserId
                };
                TrainingWeekScenario trainingWeekScenario = new TrainingWeekScenario()
                {
                    ManageTrainingDay = false
                };
                var trainingWeekService = new TrainingWeekService(dbContext);
                //retreive local data
                var localTrainingWeekList = trainingWeekService.FindTrainingWeek(criteria, trainingWeekScenario);
                //retreive online data
                var criteriaList = new CriteriaList <TrainingWeekCriteria>()
                {
                    criteria
                };
                var onlineTrainingWeekList = await TrainingWeekWebService.FindTrainingWeeksAsync(criteriaList, trainingWeekScenario);

                bool found;
                //Delete local data if not found on server
                if (localTrainingWeekList != null)
                {
                    var deletedTrainingWeekList = new List <TrainingWeek>();
                    foreach (var localTrainingWeek in localTrainingWeekList)
                    {
                        found = false;
                        if (onlineTrainingWeekList != null)
                        {
                            foreach (var onlineTrainingWeek in onlineTrainingWeekList)
                            {
                                if (TrainingWeekKey.IsEqualByKey(onlineTrainingWeek, localTrainingWeek))
                                {
                                    found = true;
                                    break;
                                }
                            }
                        }
                        if (!found)
                        {
                            deletedTrainingWeekList.Add(localTrainingWeek);
                        }
                    }
                    if (deletedTrainingWeekList.Count > 0)
                    {
                        var deletedTrainingWeekKeyList = new List <TrainingWeekKey>();
                        foreach (var key in deletedTrainingWeekList)
                        {
                            deletedTrainingWeekKeyList.Add(key);
                        }
                        //Delete in local database
                        trainingWeekService.DeleteTrainingWeekList(deletedTrainingWeekKeyList);
                        foreach (var deleteTrainingWeek in deletedTrainingWeekList)
                        {
                            localTrainingWeekList.Remove(deleteTrainingWeek);
                        }
                    }
                }
                //if modification date online != local, get full trainingWeek online data and save them on local database
                var synchronizeTrainingWeekList = new List <TrainingWeek>();
                if (onlineTrainingWeekList != null)
                {
                    foreach (var onlineTrainingWeek in onlineTrainingWeekList)
                    {
                        found = false;
                        if (localTrainingWeekList != null)
                        {
                            foreach (var localTrainingWeek in localTrainingWeekList)
                            {
                                //Same trainingWeek
                                if (TrainingWeekKey.IsEqualByKey(onlineTrainingWeek, localTrainingWeek))
                                {
                                    if (onlineTrainingWeek.ModificationDate.ToUniversalTime() != localTrainingWeek.ModificationDate.ToUniversalTime()) //ToUniversalTime for security...
                                    {
                                        synchronizeTrainingWeekList.Add(onlineTrainingWeek);
                                    }
                                    found = true;
                                    break;
                                }
                            }
                        }
                        if (!found)
                        {
                            synchronizeTrainingWeekList.Add(onlineTrainingWeek);
                        }
                    }
                }

                //Synchronize all trainingWeek data
                trainingWeekScenario = new TrainingWeekScenario()
                {
                    ManageTrainingDay   = true,
                    TrainingDayScenario = new TrainingDayScenario()
                    {
                        ManageExercise = true
                    }
                };
                if (synchronizeTrainingWeekList.Count > 0)
                {
                    criteriaList.Clear();
                    foreach (var trainingWeek in synchronizeTrainingWeekList)
                    {
                        criteria        = new TrainingWeekCriteria();
                        criteria.UserId = new StringCriteria()
                        {
                            Equal = trainingWeek.UserId
                        };
                        criteria.Year = new IntegerCriteria()
                        {
                            Equal = trainingWeek.Year
                        };
                        criteria.WeekOfYear = new IntegerCriteria()
                        {
                            Equal = trainingWeek.WeekOfYear
                        };
                        criteriaList.Add(criteria);
                    }
                    onlineTrainingWeekList = await TrainingWeekWebService.FindTrainingWeeksAsync(criteriaList, trainingWeekScenario);

                    if (onlineTrainingWeekList != null && onlineTrainingWeekList.Count > 0)
                    {
                        trainingWeekService.UpdateTrainingWeekList(onlineTrainingWeekList, trainingWeekScenario);
                    }
                }
                result = true;
            }
            catch (Exception except)
            {
                ILogger.Instance.Error("Unable to synchronize training weeks", except);
            }
            return(result);
        }