コード例 #1
0
        public async Task LikeGameAsync(int gameId)
        {
            var    gameDetails = new GameDetailedResponse();
            string path        = Path.Combine(FileSystem.AppDataDirectory, FileName);

            if (File.Exists(path))
            {
                var file = File.ReadAllText(path);
                if (!string.IsNullOrEmpty(file))
                {
                    FavoriteGames = JsonConvert.DeserializeObject <Dictionary <int, GameDetailedResponse> >(file);
                }

                if (!FavoriteGames.ContainsKey(gameId))
                {
                    gameDetails = await _gameApiClient.GetGameByIdAsync(gameId);

                    gameDetails.IsLiked = true;
                    await SaveFavoriteGameToFileAsync(gameDetails);
                }
            }
            else
            {
                gameDetails = await _gameApiClient.GetGameByIdAsync(gameId);

                gameDetails.IsLiked = true;
                await SaveFavoriteGameToFileAsync(gameDetails);
            }
        }
コード例 #2
0
        public async Task <GameDetailedResponse> GetGameByIdAsync(int id)
        {
            GameDetailedResponse game = null;
            var requestUri            = $"{urlApi}/games/{id}";
            var json = await _httpClient.GetStringAsync(requestUri);

            if (json != null)
            {
                game = JsonConvert.DeserializeObject <GameDetailedResponse>(json);
                string pattern = @"<(.|\n)*?/>";
                game.description = Regex.Replace(game.description, pattern, string.Empty);
            }
            return(game);
        }
コード例 #3
0
        public async Task <bool> IsGameInFavorites(int id)
        {
            var favoriteGame = new GameDetailedResponse();

            if (FavoriteGames.Count == 0)
            {
                FavoriteGames = (await GetAllFavoriteGamesAsync()).ToDictionary(x => x.id, x => x);
                if (FavoriteGames.ContainsKey(id))
                {
                    favoriteGame = FavoriteGames[id];
                }
            }
            else
            {
                if (FavoriteGames.ContainsKey(id))
                {
                    favoriteGame = FavoriteGames[id];
                }
            }
            return(favoriteGame.IsLiked);
        }
コード例 #4
0
 private async Task SaveFavoriteGameToFileAsync(GameDetailedResponse game)
 {
     FavoriteGames.Add(game.id, game);
     await SaveFileAsync(FavoriteGames);
 }