Last active
December 5, 2019 01:25
-
-
Save icebeam7/6c96da8ae330ab406c71a49cf5804a16 to your computer and use it in GitHub Desktop.
HarryPotterApp: CharacterDetailsViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Windows.Input; | |
using System.Threading.Tasks; | |
using HarryPotterApp.Models; | |
using Xamarin.Forms; | |
using Acr.UserDialogs; | |
namespace HarryPotterApp.ViewModels | |
{ | |
public class CharacterDetailsViewModel : BaseViewModel | |
{ | |
private CharacterViewModel _characterVM; | |
public CharacterViewModel CharacterVM | |
{ | |
get { return _characterVM; } | |
set { _characterVM = value; OnPropertyChanged(); } | |
} | |
public ICommand SaveCharacterCommand { private set; get; } | |
public ICommand DeleteCharacterCommand { private set; get; } | |
public CharacterDetailsViewModel(CharacterViewModel character) | |
{ | |
CharacterVM = character; | |
SaveCharacterCommand = new Command(async () => await SaveCharacter()); | |
DeleteCharacterCommand = new Command(async () => await DeleteCharacter()); | |
} | |
async Task SaveCharacter() | |
{ | |
var isInsert = false; | |
if (string.IsNullOrWhiteSpace(CharacterVM._id)) | |
{ | |
CharacterVM._id = Guid.NewGuid().ToString(); | |
isInsert = true; | |
} | |
var hpCharacter = CharacterVM.GetCharacter(); | |
var success = await App.Context.SaveItemAsync<HPCharacter>(hpCharacter, isInsert); | |
await UserDialogs.Instance.AlertAsync((success > 0) ? "Success!" : "Error!", "Saving...", "OK"); | |
} | |
async Task DeleteCharacter() | |
{ | |
var confirm = await UserDialogs.Instance.ConfirmAsync("Are you sure?", "Delete?", "Yes", "No"); | |
if (confirm) | |
{ | |
var hpCharacter = CharacterVM.GetCharacter(); | |
var success = await App.Context.DeleteItemAsync<HPCharacter>(hpCharacter); | |
await UserDialogs.Instance.AlertAsync((success > 0) ? "Success!" : "Error!", "Deleting...", "OK"); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment