async Task onDeleteTodoItemCommand(object obj) { if (IsBusy) { return; } IsBusy = true; try { if (obj == null) { return; } ToDoItem itemToDelete = (ToDoItem)obj; await _service.DeleteItem(itemToDelete.Key); await Application.Current.MainPage.DisplayAlert("Delete OK", $"ToDo item {itemToDelete.Name} has been deleted correctly", "OK"); } catch (Exception ex) { await Application.Current.MainPage.DisplayAlert("Item Not Deleted", ex.Message, "OK"); } finally { IsBusy = false; FetchItemsCommand.Execute(null); } }
async Task onEditTodoItemCommand() { if (IsBusy) { return; } IsBusy = true; try { if (_selectedItem == null) { return; } _selectedItem.IsComplete = !_selectedItem.IsComplete; await _service.UpdateItem(_selectedItem); } catch (Exception ex) { await Application.Current.MainPage.DisplayAlert("Item Not Updated", ex.Message, "OK"); } finally { IsBusy = false; FetchItemsCommand.Execute(null); } }
public MainPageViewModel() { Title = "Todo List"; _service = ServiceLocator.Instance.Resolve <IApiService>(); _navigationService = ServiceLocator.Instance.Resolve <INavigationService>(); // Subscribe to events from the Task Detail Page MessagingCenter.Subscribe <ToDoViewModel>(this, "ItemChanged", async(sender) => { await onFetchFieldsCommand(); }); FetchItemsCommand.Execute(null); }