public override void OnNavigatedTo(INavigationParameters parameters) { Device.BeginInvokeOnMainThread(async() => { UserDialogs.Instance.ShowLoading(null, MaskType.Clear); if (parameters != null && parameters.Count != 0) { GroupId = (int)parameters["GrupoId"]; GruposRegion = await _gruposRegionRepository.GetDataById(GroupId, await SecureStorage.GetAsync("UserId"), string.Empty); GroupName = GruposRegion.GrupoName; GroupType = GruposRegion.GrupoTipo; PokedexDescription = GruposRegion.PokedexDescription; Region = GruposRegion.Region; var poks = await _grupoPokemonsRepository.GetDataByGrupoId(GroupId); if (poks.Count() > 0) { IsEmpty = false; PokemonsCount = poks.Count(); PokemonsAdded = new ObservableCollection <GrupoPokemons>(poks); UserDialogs.Instance.HideLoading(); } else { IsEmpty = true; PokemonsCount = 0; UserDialogs.Instance.HideLoading(); } } UserDialogs.Instance.HideLoading(); }); }
public void GoToView(GruposRegion values) { Device.BeginInvokeOnMainThread(async() => { var navigationParams = new NavigationParameters(); navigationParams.Add("GrupoId", values.GrupoId); await _navigationService.NavigateAsync("GrupoDetailsView", navigationParams); }); }
public PokemonRegionViewModel(INavigationService navigationService, IAPIService apiService, IGruposRegionRepository gruposRegionRepository, IGrupoPokemonsRepository grupoPokemonsRepository) : base(navigationService) { _apiService = apiService; _navigationService = navigationService; _gruposRegionRepository = gruposRegionRepository; _grupoPokemonsRepository = grupoPokemonsRepository; #region Commands Logic CancelCreation = new Command(async() => { await _navigationService.GoBackAsync(); }); SaveGroup = new Command(async() => { try { UserDialogs.Instance.ShowLoading(null, MaskType.None); //validate pokemons number if (PokemonsCounter < 3 || PokemonsCounter > 6) { UserDialogs.Instance.HideLoading(); await App.Current.MainPage.DisplayAlert("Error", "You must add at min. 3 pokemons or max. 6 pokemons", "ok"); return; } var result1 = false; var result2 = false; if (CrossConnectivity.Current.IsConnected) { if (IsCreate) { //Create Group group first var group = new GruposRegion { GrupoId = await _gruposRegionRepository.GetLastID() + 1, GrupoName = GroupName, GrupoTipo = GroupType, PokedexDescription = PokedexDescription, Image = "", Region = PokedexInfo.FirstOrDefault().name, UserId = await SecureStorage.GetAsync("UserId"), Token = Convert.ToBase64String(Guid.NewGuid().ToByteArray()), GrupoIdFather = null }; result1 = await _gruposRegionRepository.SaveData(group); if (result1) { //then we add pokemons related var data = PokemonList.Where(x => x.IsSelected).Select(x => new GrupoPokemons { GroupId = group.GrupoId, Pokemon = x.name }); result2 = await _grupoPokemonsRepository.SaveDataRange(data); } if (result1 && result2) { await App.Current.MainPage.DisplayAlert("Success", "Your group was created successfully", "ok"); var navigationParams = new NavigationParameters(); navigationParams.Add("RegionName", group.Region); await navigationService.GoBackAsync(navigationParams); } else { UserDialogs.Instance.HideLoading(); ErrorAlert(); await navigationService.GoBackAsync(); } } else { GruposRegion.GrupoName = GroupName; GruposRegion.GrupoTipo = GroupType; GruposRegion.PokedexDescription = PokedexDescription; result1 = await _gruposRegionRepository.UpdateData(GruposRegion); if (result1) { //then we add pokemons related and delete pokemons non related var oldValues = (await _grupoPokemonsRepository.GetDataByGrupoId(GruposRegion.GrupoId)).ToList(); var data = PokemonList.Where(x => x.IsSelected).Select(x => new GrupoPokemons { GroupId = GruposRegion.GrupoId, Pokemon = x.name }); //if old data does not appear, it means it was no unselected foreach (var item in oldValues) { if (!data.Select(x => x.Pokemon).Contains(item.Pokemon)) { await _grupoPokemonsRepository.DeleteData(item.Id, string.Empty, string.Empty); } } //if new data does not appear, it means it must be added foreach (var item in data) { if (!oldValues.Select(x => x.Pokemon).Contains(item.Pokemon)) { item.Id = await _gruposRegionRepository.GetLastID() + 1; await _grupoPokemonsRepository.SaveData(item); } } result2 = true; } if (result1 && result2) { UserDialogs.Instance.HideLoading(); await App.Current.MainPage.DisplayAlert("Success", "Your group was modified successfully", "ok"); var navigationParams = new NavigationParameters(); navigationParams.Add("GrupoId", GruposRegion.GrupoId); await navigationService.GoBackAsync(navigationParams); } else { UserDialogs.Instance.HideLoading(); ErrorAlert(); await navigationService.GoBackAsync(); } } } else { NoInternetAlert(); } } catch (Exception ex) { ErrorAlert(); await navigationService.GoBackAsync(); } }); #endregion }