public Task<GetMoviesResponse> GetTopRatedMoviesAsync () { var tcs = new TaskCompletionSource<GetMoviesResponse> (); Task.Factory.StartNew (async () => { var urlString = String.Format (StringResources.GetTopRatedMoviesUriFormatString, StringResources.ApiKey); var cacheKey = this.getCacheKey(urlString); if (this.topRatedMovies != null) { tcs.TrySetResult (this.topRatedMovies); } else { if (cache.ContainsKey(cacheKey)) { var response = cache.Get<GetMoviesResponse>(cacheKey); this.topRatedMovies = response; tcs.TrySetResult (this.topRatedMovies); } 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.topRatedMovies = result; cache.Set<GetMoviesResponse> (cacheKey, this.topRatedMovies); tcs.TrySetResult (this.topRatedMovies); } } }); return tcs.Task; }
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; }