예제 #1
0
        public async Task PullSync()
        {
            var LastSyncTime = Barrel.Current.Get <DateTime>(key: "sync");

            if (LastSyncTime != null)
            {
                var LocalUser = Barrel.Current.Get <LoginResponse>(key: "user").User;
                //var ApiUser = new User();

                try
                {
                    var ApiUser = await _fitnessService.GetUserById(LocalUser.Id);

                    if (ApiUser.LastUpdated > LocalUser.LastUpdated)
                    {
                        var loginResponse = Barrel.Current.Get <LoginResponse>(key: "user");
                        loginResponse.User = ApiUser;

                        Barrel.Current.Add(key: "user", data: loginResponse, expireIn: TimeSpan.FromHours(1));
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }


                try
                {
                    var ApiWorkouts = await _fitnessService.GetUserWorkouts(LocalUser.Id);

                    if (ApiWorkouts.Count != 0)
                    {
                        foreach (var w in ApiWorkouts)
                        {
                            var localWorkout = await _localDatabase.GetWorkoutByID(w.WorkoutID);

                            if (localWorkout == null)
                            {
                                await _localDatabase.AddWorkout(w);
                            }
                            else if (w.LastUpdated > localWorkout.LastUpdated)
                            {
                                w.WorkoutExercises = localWorkout.WorkoutExercises;

                                await _localDatabase.UpdateWorkout(w);
                            }
                        }

                        foreach (var w in ApiWorkouts)
                        {
                            var apiExercises = await _fitnessService.GetWorkoutExercises(w.WorkoutID);

                            foreach (var e in apiExercises)
                            {
                                if (_localDatabase.ExerciseExists(e.ExerciseID) == null)
                                {
                                    await _fitnessService.AddWorkoutExercise(w.WorkoutID, e);
                                }
                                else
                                {
                                    var localExercise = await _localDatabase.ExerciseExists(e.ExerciseID);

                                    if (e.LastUpdated > localExercise.LastUpdated)
                                    {
                                        e.Workout   = localExercise.Workout;
                                        e.WorkoutID = localExercise.WorkoutID;

                                        await _localDatabase.UpdateExercise(e);
                                    }
                                }
                            }
                        }
                    }

                    Barrel.Current.Add(key: "sync", data: TimeZoneInfo.ConvertTimeToUtc(DateTime.Now, TimeZoneInfo.Local), expireIn: TimeSpan.FromMinutes(5));
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }



                //List<Exercise> ApiExercises = new List<Exercise>();
            }
        }