private async Task LoadGames()
        {
            Games.Clear();
            IsBusy = true;
            var games = await GamesStore.GetItemsAsync();

            foreach (Game game in games)
            {
                Games.Add(game);
            }
            IsBusy = false;
        }
Beispiel #2
0
        private async void OnSaveCommand(Game obj)
        {
            var game = new Game()
            {
                Id       = Guid.NewGuid().ToString(),
                Date     = date,
                PlayerId = player.Id,
                Placar   = placar
            };
            var games = await GamesStore.GetItemsAsync();

            if (games.Count() > 0)
            {
                int maxRecord = games.Max(x => x.Placar);
                int minRecord = games.Min(x => x.Placar);
                int minQuebra = games.Last().MinRecordBroken;
                int maxQuebra = games.Last().MaxRecordBroken;
                if (placar > maxRecord)
                {
                    game.MaxRecord       = placar;
                    game.MaxRecordBroken = maxQuebra + 1;
                }
                else
                {
                    game.MaxRecord       = maxRecord;
                    game.MaxRecordBroken = maxQuebra;
                }

                if (placar < minRecord)
                {
                    game.MinRecord       = placar;
                    game.MinRecordBroken = minQuebra + 1;
                }
                else
                {
                    game.MinRecord       = minRecord;
                    game.MinRecordBroken = minQuebra;
                }
            }
            else
            {
                game.MaxRecord       = placar;
                game.MaxRecordBroken = 0;
                game.MinRecord       = placar;
                game.MinRecordBroken = 0;
            }
            try
            {
                await GamesStore.AddItemAsync(game);

                await Shell.Current.DisplayAlert("Sucesso", "Jogo adicionado com sucesso", "OK");

                Placar = 0;
                Player = null;
                Date   = DateTime.Today;
            }
            catch (Exception exc)
            {
                Debug.WriteLine(exc.StackTrace);
                await Shell.Current.DisplayAlert("ERRO FATAL", "Não foi possível salvar os dados", "OK");
            }
        }