private async Task DownloadData()
        {
            if (!_navigationService.IsNetworkAvailable)
            {
                RetryIsVisible = true;

                return;
            }

            RetryIsVisible = false;

            try
            {
                ProgressIsVisible = true;

                var genresResponse = await _musicClient.GetGenresAsync();

                if (genresResponse.Error != null || (genresResponse.Result == null))
                {
                    Log.ErrorException("Error getting genres", genresResponse.Error);

                    // TODO: Display an error
                    RetryIsVisible    = true;
                    ProgressIsVisible = false;

                    return;
                }

                var genres = genresResponse.Result;

                await _asyncStorageService.WriteAllTextAsync(Constants.GenreDataFile, await JsonConvert.SerializeObjectAsync(genres));

                Log.Info("Genres written to IsolatedStorage");

                var tracks = new List <Product>();
                foreach (var genre in genres.Where(x => !x.Name.Equals("Comedy")))
                {
                    var trackResponse = await _musicClient.SearchGenreAsync(genre.Id, category : Category.Track, startIndex : 0, itemsPerPage : 100);

                    if (trackResponse.Error == null)
                    {
                        Debug.WriteLine("TopProductsForGenre ({0}): {1} tracks", genre.Name, trackResponse.Result.Count);
                        tracks.AddRange(trackResponse.Result.Cast <Product>());
                    }
                }

                await _asyncStorageService.WriteAllTextAsync(Constants.GameDataFile, await JsonConvert.SerializeObjectAsync(tracks));

                Log.Info("Tracks written to IsolatedStorage");

                NavigateTo(Constants.Pages.MainPage + Constants.ClearBackStack);
            }
            catch (Exception ex)
            {
                Log.ErrorException("Error download data", ex);
            }

            ProgressIsVisible = false;
        }
예제 #2
0
        private async Task <bool> CheckForGameData()
        {
            var genres = _settingsService.Get("Genres", default(List <Genre>));

            if (genres != default(List <Genre>))
            {
                await _asyncStorageService.WriteAllTextAsync(Constants.GenreDataFile, await JsonConvert.SerializeObjectAsync(genres));

                _settingsService.Reset("Genres");
            }
            else
            {
                if (!await _asyncStorageService.FileExistsAsync(Constants.GenreDataFile))
                {
                    return(false);
                }

                var genreJson = await _asyncStorageService.ReadAllTextAsync(Constants.GenreDataFile);

                genres = await JsonConvert.DeserializeObjectAsync <List <Genre> >(genreJson);

                if (genres == null || !genres.Any())
                {
                    return(false);
                }

                var allGenreCheck = genres.FirstOrDefault(x => x.Name.Equals(GameViewModel.AllGenres));
                if (allGenreCheck == default(Genre))
                {
                    genres.Insert(0, new Genre {
                        Name = GameViewModel.AllGenres
                    });
                }

                var comedyGenreCheck = genres.FirstOrDefault(x => x.Name.Equals("Comedy"));
                if (comedyGenreCheck != default(Genre))
                {
                    genres.Remove(comedyGenreCheck);
                }
            }

            Genres = genres;

            App.SettingsWrapper.AppSettings.DefaultGenre = Genres.FirstOrDefault(x => x.Name.Equals(App.SettingsWrapper.AppSettings.DefaultGenre.Name));

            if (SimpleIoc.Default.GetInstance <GameViewModel>() == null)
            {
                return(false);
            }

            Messenger.Default.Send(new NotificationMessage(genres, Constants.Messages.HereAreTheGenresMsg));

            if (!await _asyncStorageService.FileExistsAsync(Constants.GameDataFile))
            {
                return(false);
            }

            var tracksJson = await _asyncStorageService.ReadAllTextAsync(Constants.GameDataFile);

            try
            {
                var tracks = await JsonConvert.DeserializeObjectAsync <List <Product> >(tracksJson);

                if (tracks.Any())
                {
                    Messenger.Default.Send(new NotificationMessage(tracks, Constants.Messages.HereAreTheTracksMsg));

                    return(true);
                }
            }
            catch (Exception ex)
            {
                Log.ErrorException("Deserializing tracks", ex);
            }

            return(false);
        }