Example #1
0
		public Task<GetMoviesResponse> GetUpcomingMoviesAsync () {
			var tcs = new TaskCompletionSource<GetMoviesResponse> ();
			Task.Factory.StartNew (async () => {
				var urlString = String.Format (SharedConstants.GetUpcomingMoviesUriFormatString, SharedConstants.ApiKey);
				var cacheKey = this.getCacheKey (urlString);
				if (this.upcomingMovies != null) {
					tcs.TrySetResult (this.upcomingMovies);
				} else {
					if (cache.ContainsKey(cacheKey)) {
						var response = cache.Get<GetMoviesResponse>(cacheKey);
						this.upcomingMovies = response;
						tcs.TrySetResult (this.upcomingMovies);
					} else {
						var client = this.httpClientFactory.Create ();
						var response = await client.GetAsync (urlString);
						var json = await response.Content.ReadAsStringAsync ();
						var result = JsonConvert.DeserializeObject<GetMoviesResponse> (json);
						this.upcomingMovies = result;
						cache.Set<GetMoviesResponse> (cacheKey, this.upcomingMovies);
						tcs.TrySetResult (this.upcomingMovies);
					}
				}
			});
			return tcs.Task;
		}
Example #2
0
		public Task<List<MovieCategory>> GetMoviesByCategoryAsync () {
			var tcs = new TaskCompletionSource<List<MovieCategory>> ();
			Task.Factory.StartNew (async () => {
				this.topRatedMovies = await Data.Current.GetTopRatedMoviesAsync ();
				this.popularMovies = await Data.Current.GetPopularMoviesAsync ();
				this.nowPlayingMovies = await Data.Current.GetNowPlayingMoviesAsync ();
				this.upcomingMovies = await Data.Current.GetUpcomingMoviesAsync ();
				this.favorites = await Data.Current.GetFavoritesAsync ();
				var result = new List<MovieCategory> ();
				result.Add (new MovieCategory ("Top Rated", this.topRatedMovies.Results)); 
				result.Add (new MovieCategory ("Popular", this.popularMovies.Results));
				result.Add (new MovieCategory ("Now Playing", this.nowPlayingMovies.Results));
				result.Add (new MovieCategory ("Upcoming", this.upcomingMovies.Results));
				result.Add (new MovieCategory ("Your Favorites", this.favorites));
				tcs.TrySetResult (result);
			});
			return tcs.Task;
		}