예제 #1
0
        public async Task <IList <Workout> > GetWorkouts(WorkoutType workoutType)
        {
            //if (_cachedWorkouts != null && (DateTime.Now - _lastUpdated).TotalHours <= 4)
            //	return _cachedWorkouts;

            var targetUrl = string.Concat(RemoteUrl, workoutType.ToString().ToLower());
            var remoteUri = new Uri(targetUrl, UriKind.Absolute);
            var workouts  = new List <Workout>();

            try
            {
                var httpRequest = (HttpWebRequest)WebRequest.Create(remoteUri);
                httpRequest.ContentType = "application/json";
                httpRequest.Method      = "GET";

                using (var response = await httpRequest.GetResponseAsync() as HttpWebResponse)
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        Debug.WriteLine("Error fetching data from server.");
                        return(null);
                    }

                    var s = response.GetResponseStream();
                    if (s != null)
                    {
                        var j = JsonObject.Load(s);

                        // Create workouts from json data.
                        var results = from result in j[0] as JsonArray
                                      let jresult = result as JsonObject
                                                    select new Workout()
                        {
                            Id           = jresult["id"],
                            DisplayId    = jresult["displayId"],
                            Name         = jresult["name"],
                            ExerciseType = workoutType,
                            Level        = (WorkoutLevel)Enum.Parse(typeof(WorkoutLevel), jresult["level"])
                        };

                        _cachedWorkouts = results.ToList();
                        _lastUpdated    = DateTime.Now;

                        return(_cachedWorkouts);
                    }
                }

                return(workouts);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return(null);
            }
        }
예제 #2
0
 public Task AddOrUpdate(DB db)
 {
     return(db.Database.InsertOrReplaceAsync(new ScheduleDBModel_
     {
         Id = Id,
         Interval = (int)Interval.TotalMinutes,
         IsActive = IsActive,
         When = (int)(When - StablePointInTime).TotalMinutes,
         WorkoutId = WorkoutId,
         WorkoutType = WorkoutType.ToString()
     }, typeof(ScheduleDBModel_)));
 }
예제 #3
0
        public async Task ExecuteWorkout(DB db)
        {
            var workout = await db.Database.FindAsync <MarathonDBModel>(Id);

            string type = WorkoutType.ToString();
            var    pw   = await db.Database.Table <ListWorkoutDBModel>().Where(p => p.WorkoutId == Id && p.WorkoutType.Equals(type)).ToListAsync();

            var playlists = await db.Database.Table <PlaylistDBModel>().ToListAsync();

            App.Debug(new string(playlists.SelectMany(p => p.ToString()).ToArray()));
            await App.Current.MainPage.Navigation.PushAsync(new MarathonWorkouter(workout,
                                                                                  playlists.Where(p => pw.Any(x => x.PlaylistId == p.Id)).ToList()));
        }
		public async Task<IList<Workout>> GetWorkouts(WorkoutType workoutType)
		{
			//if (_cachedWorkouts != null && (DateTime.Now - _lastUpdated).TotalHours <= 4)
			//	return _cachedWorkouts;

			var targetUrl = string.Concat (RemoteUrl, workoutType.ToString().ToLower());
			var remoteUri = new Uri(targetUrl, UriKind.Absolute);
			var workouts = new List<Workout>();

			try
			{
				var httpRequest = (HttpWebRequest) WebRequest.Create(remoteUri);
				httpRequest.ContentType = "application/json";
				httpRequest.Method = "GET";

				using (var response = await httpRequest.GetResponseAsync() as HttpWebResponse) 
				{
					if (response.StatusCode != HttpStatusCode.OK)
					{
						Debug.WriteLine("Error fetching data from server.");
						return null;
					}

					var s = response.GetResponseStream();
					if (s != null)
					{
						var j = JsonObject.Load(s);

						// Create workouts from json data.
						var results = from result in j[0] as JsonArray
							let jresult = result as JsonObject
								select new Workout() 
							{
								Id = jresult["id"],
								DisplayId = jresult["displayId"],
								Name = jresult["name"],
								ExerciseType = workoutType,
								Level = (WorkoutLevel) Enum.Parse(typeof(WorkoutLevel), jresult["level"])
							};

						_cachedWorkouts = results.ToList();
						_lastUpdated = DateTime.Now;

						return _cachedWorkouts;
					}
				}

				return workouts;
			} 
			catch (Exception ex)
			{
				Debug.WriteLine (ex.Message);
				return null;
			}
		}
		public async Task<Workout> GetWorkoutById(WorkoutType workoutType, int id)
		{
			var targetUrl = string.Format("{0}{1}/{2}", RemoteUrl, workoutType.ToString().ToLower(), id);
			var remoteUri = new Uri(targetUrl, UriKind.Absolute);

			if (_cachedWorkouts == null)
				return null;

			var workout = _cachedWorkouts.FirstOrDefault(w => w.Id == id);
			if (workout == null)
				return null;

			if (workout.Stages.Count > 0)
				return workout;

			// Populates the stages collection with new data.
			try
			{
				var stages = new List<Stage>();
				var httpRequest = (HttpWebRequest) WebRequest.Create(remoteUri);
				httpRequest.ContentType = "application/json";
				httpRequest.Method = "GET";

				using (var response = await httpRequest.GetResponseAsync() as HttpWebResponse) 
				{
					if (response.StatusCode != HttpStatusCode.OK)
					{
						Debug.WriteLine("Error fetching data from server.");
						return null;
					}

					var s = response.GetResponseStream();
					if (s != null)
					{
						var j = JsonObject.Load(s);

						// Create workouts from json data.
						var results = from result in j[0] as JsonArray
							let jresult = result as JsonObject
								select new Stage() 
							{
								Id = jresult["stageId"],
								Duration = TimeSpan.Parse(jresult["duration"]),
								CrossRamp = Range<int>.Parse(jresult["crossRamp"]),
								Resistance = Range<int>.Parse(jresult["resistance"]),
								Strides = Range<int>.Parse(jresult["strides"]),
								Message = jresult["message"]
							};

						stages = results.ToList();

						workout.Stages = stages;
					}
				}

				return workout;
			} 
			catch (Exception ex)
			{
				Debug.WriteLine (ex.Message);
				return null;
			}
		}
예제 #6
0
        public async Task <Workout> GetWorkoutById(WorkoutType workoutType, int id)
        {
            var targetUrl = string.Format("{0}{1}/{2}", RemoteUrl, workoutType.ToString().ToLower(), id);
            var remoteUri = new Uri(targetUrl, UriKind.Absolute);

            if (_cachedWorkouts == null)
            {
                return(null);
            }

            var workout = _cachedWorkouts.FirstOrDefault(w => w.Id == id);

            if (workout == null)
            {
                return(null);
            }

            if (workout.Stages.Count > 0)
            {
                return(workout);
            }

            // Populates the stages collection with new data.
            try
            {
                var stages      = new List <Stage>();
                var httpRequest = (HttpWebRequest)WebRequest.Create(remoteUri);
                httpRequest.ContentType = "application/json";
                httpRequest.Method      = "GET";

                using (var response = await httpRequest.GetResponseAsync() as HttpWebResponse)
                {
                    if (response.StatusCode != HttpStatusCode.OK)
                    {
                        Debug.WriteLine("Error fetching data from server.");
                        return(null);
                    }

                    var s = response.GetResponseStream();
                    if (s != null)
                    {
                        var j = JsonObject.Load(s);

                        // Create workouts from json data.
                        var results = from result in j[0] as JsonArray
                                      let jresult = result as JsonObject
                                                    select new Stage()
                        {
                            Id         = jresult["stageId"],
                            Duration   = TimeSpan.Parse(jresult["duration"]),
                            CrossRamp  = Range <int> .Parse(jresult["crossRamp"]),
                            Resistance = Range <int> .Parse(jresult["resistance"]),
                            Strides    = Range <int> .Parse(jresult["strides"]),
                            Message    = jresult["message"]
                        };

                        stages = results.ToList();

                        workout.Stages = stages;
                    }
                }

                return(workout);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return(null);
            }
        }
예제 #7
0
 public override string ToString()
 {
     return(type.ToString());
 }