private async Task ExecuteLoadCommand(SuperheroListDTO superhero) { Title = superhero.AlterEgo; Id = superhero.Id; Name = superhero.Name; AlterEgo = superhero.AlterEgo; PortraitUrl = superhero.PortraitUrl; IsBusy = true; try { var(status, hero) = await _client.GetAsync <SuperheroDetailsDTO>($"superheroes/{Id}"); if (status != HttpStatusCode.OK) { await _dialog.DisplayAlertAsync("Error", $"Error from api: {status}", "OK"); } else { SetSuperhero(hero); } } catch (Exception e) { await _dialog.DisplayAlertAsync(e); } IsBusy = false; }
public void LoadCommand_populates_Items_from_api() { var batman = new SuperheroListDTO(); var superman = new SuperheroListDTO(); var heroes = new[] { batman, superman }; var navigation = new Mock <INavigationService>(); var messaging = new Mock <IMessagingCenter>(); var client = new Mock <IRestClient>(); client.Setup(s => s.GetAllAsync <SuperheroListDTO>("superheroes")).ReturnsAsync((HttpStatusCode.OK, heroes)); var service = new Mock <IAuthenticationService>(); var dialog = new Mock <IDialogService>(); var vm = new SuperheroesViewModel(navigation.Object, messaging.Object, client.Object, service.Object, dialog.Object); vm.LoadCommand.Execute(null); Assert.Collection(vm.Items, a => Assert.Equal(a, batman), a => Assert.Equal(a, superman) ); // Ensure not busy when command finished Assert.False(vm.IsBusy); }
public SuperheroDetailsPage(SuperheroListDTO superhero) { InitializeComponent(); _superhero = superhero; BindingContext = _viewModel = App.Container.GetRequiredService <SuperheroDetailsViewModel>(); }
public void EditCommand_calls_EditAsync_with_DTO() { var navigation = new Mock <INavigationService>(); var messaging = new Mock <IMessagingCenter>(); var detailsDTO = new SuperheroDetailsDTO { Id = 42, Name = "name", AlterEgo = "alterEgo", Occupation = "occupation", CityName = "cityName", PortraitUrl = "https://image.com/portrait.jpg", BackgroundUrl = "https://image.com/background.jpg", FirstAppearance = 2000, Gender = Gender.Male, Powers = new[] { "power1", "power2" } }; var client = new Mock <IRestClient>(); client.Setup(s => s.GetAsync <SuperheroDetailsDTO>("superheroes/42")).ReturnsAsync((HttpStatusCode.OK, detailsDTO)); var dialog = new Mock <IDialogService>(); var vm = new SuperheroDetailsViewModel(navigation.Object, messaging.Object, client.Object, dialog.Object); var listDTO = new SuperheroListDTO { Id = 42, AlterEgo = "alterEgo", Name = "name", PortraitUrl = "https://image.com/image.jpg" }; vm.LoadCommand.Execute(listDTO); vm.EditCommand.Execute(null); navigation.Verify(s => s.EditAsync(It.Is <SuperheroDetailsDTO>(h => h.Id == 42 && h.Name == "name" && h.AlterEgo == "alterEgo" && h.Occupation == "occupation" && h.CityName == "cityName" && h.PortraitUrl == "https://image.com/portrait.jpg" && h.BackgroundUrl == "https://image.com/background.jpg" && h.FirstAppearance == 2000 && h.Gender == Gender.Male && new HashSet <string> { "power1", "power2" }.SetEquals(h.Powers) ))); // Ensure not busy when command finished Assert.False(vm.IsBusy); }
public void LoadCommand_populates_Superhero() { var navigation = new Mock <INavigationService>(); var messaging = new Mock <IMessagingCenter>(); var dialog = new Mock <IDialogService>(); var detailsDTO = new SuperheroDetailsDTO { Id = 42, Name = "name", AlterEgo = "alterEgo", Occupation = "occupation", CityName = "cityName", PortraitUrl = "https://image.com/portrait.jpg", BackgroundUrl = "https://image.com/background.jpg", FirstAppearance = 2000, Gender = Gender.Male, Powers = new[] { "power1", "power2" } }; var client = new Mock <IRestClient>(); client.Setup(s => s.GetAsync <SuperheroDetailsDTO>("superheroes/42")).ReturnsAsync((HttpStatusCode.OK, detailsDTO)); var vm = new SuperheroDetailsViewModel(navigation.Object, messaging.Object, client.Object, dialog.Object); var listDTO = new SuperheroListDTO { Id = 42, AlterEgo = "alterEgo", Name = "name", PortraitUrl = "https://image.com/image.jpg" }; vm.LoadCommand.Execute(listDTO); Assert.Equal(42, vm.Id); Assert.Equal("alterEgo", vm.AlterEgo); Assert.Equal("name", vm.Name); Assert.Equal("occupation", vm.Occupation); Assert.Equal("cityName", vm.CityName); Assert.Equal("https://image.com/portrait.jpg", vm.PortraitUrl); Assert.Equal("https://image.com/background.jpg", vm.BackgroundUrl); Assert.Equal(2000, vm.FirstAppearance); Assert.Equal(Gender.Male, vm.Gender); Assert.Collection(vm.Powers, s => Assert.Equal("power1", s), s => Assert.Equal("power2", s) ); // Ensure not busy when command finished Assert.False(vm.IsBusy); }
private async Task ExecuteSaveCommand() { IsBusy = true; var powers = Powers?.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()) ?? new string[0]; var superhero = new SuperheroCreateDTO { Name = Name, AlterEgo = AlterEgo, Occupation = Occupation, CityName = CityName, PortraitUrl = PortraitUrl, BackgroundUrl = BackgroundUrl, FirstAppearance = FirstAppearance, Gender = Gender, Powers = new HashSet <string>(powers) }; try { var(status, uri) = await _client.PostAsync("superheroes", superhero); if (status != HttpStatusCode.Created) { await _dialog.DisplayAlertAsync("Error", $"Error from api: {status}", "OK"); } else { var id = int.Parse(uri.AbsoluteUri.Substring(uri.AbsoluteUri.LastIndexOf("/") + 1)); var superheroListDTO = new SuperheroListDTO { Id = id, Name = Name, AlterEgo = AlterEgo, PortraitUrl = PortraitUrl }; _messaging.Send(this, AddSuperhero, superheroListDTO); await _navigation.CancelAsync(); } } catch (Exception e) { await _dialog.DisplayAlertAsync(e); } IsBusy = false; }
private async Task ExecuteLoadCommand(SuperheroListDTO superhero) { Title = superhero.AlterEgo; Id = superhero.Id; Name = superhero.Name; AlterEgo = superhero.AlterEgo; PortraitUrl = superhero.PortraitUrl; IsBusy = true; SetSuperhero(await _client.GetAsync <SuperheroDetailsDTO>($"superheroes/{Id}")); IsBusy = false; }
private async Task ExecuteSaveCommand() { IsBusy = true; var powers = Powers?.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()) ?? new string[0]; var superhero = new SuperheroCreateDTO { Name = Name, AlterEgo = AlterEgo, Occupation = Occupation, CityName = CityName, PortraitUrl = PortraitUrl, BackgroundUrl = BackgroundUrl, FirstAppearance = FirstAppearance, Gender = Gender, Powers = new HashSet <string>(powers) }; var uri = await _client.PostAsync("superheroes", superhero); var id = int.Parse(uri.AbsoluteUri.Substring(uri.AbsoluteUri.LastIndexOf("/") + 1)); var superheroListDTO = new SuperheroListDTO { Id = id, Name = Name, AlterEgo = AlterEgo, PortraitUrl = PortraitUrl }; _messaging.Send(this, AddSuperhero, superheroListDTO); await _navigation.CancelAsync(); IsBusy = false; }
public async Task ViewAsync(SuperheroListDTO superhero) { await _navigation.PushAsync(new SuperheroDetailsPage(superhero)); }