zhaw-dnet2/Tasks/Lab12/TodoDetails/ViewModel/TodosViewModel.cs

78 lines
1.8 KiB
C#
Raw Normal View History

using CommunityToolkit.Mvvm.Input;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TodoDetails.Model;
using TodoDetails.Services;
2024-06-06 21:55:29 +00:00
using TodoDetails.View;
namespace TodoDetails.ViewModel
{
public partial class TodosViewModel : BaseViewModel
{
public ObservableCollection<Todo> Todos { get; } = new ObservableCollection<Todo>();
TodoService todoService;
public TodosViewModel(TodoService todoService)
{
Title = "Todos";
this.todoService = todoService;
}
[RelayCommand]
async Task GetTodosAsync()
{
if (IsBusy)
{
return;
}
try
{
IsBusy = true;
var todos = await todoService.GetTodos();
if (Todos.Count != 0)
{
Todos.Clear();
}
foreach (var todo in todos)
{
Todos.Add(todo);
}
}
catch (Exception e)
{
Debug.WriteLine($"UInable to get todos: {e.Message}");
await Shell.Current.DisplayAlert("Error!", e.Message, "OK");
}
finally
{
IsBusy = false;
}
}
[RelayCommand]
async Task GoToDetails(Todo todo)
{
if (todo == null)
{
return;
}
2024-06-06 21:55:29 +00:00
await Shell.Current.GoToAsync(
nameof(TodoDetailsPage),
true,
new Dictionary<string, object>()
{
{ nameof(Todo), todo }
});
}
}
}