Beispiel #1
0
        public async Task<IList<WorkoutDetailViewModel>> LoadAlternatives(string name)
        {
            var list = new List<WorkoutDetailViewModel>();

            string url = @"https://api.freeletics.com/v2/coach/workouts.json?base_name=" + name;
            var request = GetRequest(url);

            using (var response = await request.GetResponseAsync())
            {
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    var content = reader.ReadToEnd();
                    dynamic data = JObject.Parse(content);

                    var exos = new List<ExercisesViewModel>();
                    foreach (var ex in data.exercises)
                    {
                        string video = "";
                        try { video = ex.video_urls.mp4; } catch { }

                        var exercice = new ExercisesViewModel()
                        {
                            Slug = ex.slug,
                            Title = ex.title,
                            Image = ex.picture_urls.small_mobile,
                            Video = video
                        };
                        exos.Add(exercice);
                    }

                    foreach (var w in data.workouts)
                    {
                        var detail = new WorkoutDetailViewModel()
                        {
                            Name = w.base_name,
                            Title = GetWorkoutDisplayName(w),
                            Slug = w.slug,
                            Variant = w.fitness_variant,
                        };

                        if (w.category_slug == "home") detail.Variant = "2x2 " + detail.Variant;

                        var idx = 0;
                        foreach (var round in w.rounds)
                        {
                            detail.Exercises.Add(new ExerciesSeparatorViewModel() { Title = "Round " + ++idx });
                            foreach (var ex in round)
                            {
                                var exid = (string)ex.exercise_slug;
                                if (!string.IsNullOrWhiteSpace(exid))
                                {
                                    var e = exos.FirstOrDefault(x => x.Slug == exid).Clone();
                                    e.Quantity = ex.quantity;
                                    e.Repetition = ex.quantity_description;
                                    e.Title = e.Repetition + " " + e.Title;

                                    detail.Exercises.Add(e);
                                }
                            }
                        }

                        list.Add(detail);
                    }
                }
            }

            return list;
        }
Beispiel #2
0
        public async Task<IList<WorkoutDetailViewModel>> GetAvailableWorkouts(string type = null)
        {
            if (type == null)
            {
                return FreeWorkoutsCache;
            }

            FreeWorkoutsCache.Clear();
            
            string url;
            if (type == "WORKOUTS")
            {
                url = @"https://api.freeletics.com/v2/coach/workouts.json?base_volume=true&category_slugs=regular&fitness_variants=standard";
            } 
            else if (type == "EXERCICES")
            {
                url = @"https://api.freeletics.com/v2/coach/workouts.json?base_volume=true&category_slugs%5B%5D=exercise_with_distance&category_slugs%5B%5D=exercise_with_repetitions";
            }
            else
            {
                url = @"https://api.freeletics.com/v2/coach/workouts.json?category_slugs=distance_run";
            }
            
            var request = GetRequest(url);

            using (var response = await request.GetResponseAsync())
            {
                using (var reader = new StreamReader(response.GetResponseStream()))
                {
                    var content = reader.ReadToEnd();
                    dynamic data = JObject.Parse(content);

                    var exos = new List<ExercisesViewModel>();
                    foreach (var ex in data.exercises)
                    {
                        string video = "";
                        try { video = ex.video_urls.mp4; } catch { }

                        var exercice = new ExercisesViewModel()
                        {
                            Slug = ex.slug,
                            Title = ex.title,
                            Image = ex.picture_urls.small_mobile,
                            Video = video
                        };
                        exos.Add(exercice);
                    }
                    
                    foreach (var w in data.workouts)
                    {
                        string title;
                        if (type == "WORKOUTS")
                        {
                            title = GetWorkoutDisplayName(w);
                        }
                        else
                        {
                            title = w.title;
                            title = title.ToUpper();
                        }

                        var detail = new WorkoutDetailViewModel()
                        {
                            Name = w.base_name,
                            Title = title,
                            Slug = w.slug,
                            Variant = w.fitness_variant,
                            TrainingType = type,
                            Points = w.points
                        };

                        if (w.category_slug == "home") detail.Variant = "2x2 " + detail.Variant;

                        var idx = 0;
                        foreach (var round in w.rounds)
                        {
                            detail.Exercises.Add(new ExerciesSeparatorViewModel() { Title = "Round " + ++idx });
                            foreach (var ex in round)
                            {
                                var exid = (string)ex.exercise_slug;
                                if (!string.IsNullOrWhiteSpace(exid))
                                {
                                    var e = exos.FirstOrDefault(x => x.Slug == exid).Clone();
                                    e.Quantity = ex.quantity;
                                    e.Repetition = ex.quantity_description;
                                    e.Title = e.Repetition + " " + e.Title;

                                    detail.Exercises.Add(e);
                                }
                            }
                        }

                        FreeWorkoutsCache.Add(detail);
                    }
                }
            }

            if (type == "RUN") FreeWorkoutsCache = FreeWorkoutsCache.OrderBy(w => w.Points).ToList();
            return FreeWorkoutsCache;
        }