Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
HarryPotterApp: CharacterDetailsViewModel.cs
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
You can’t perform that action at this time.