78 lines
1.8 KiB
C#
78 lines
1.8 KiB
C#
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;
|
|
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;
|
|
}
|
|
|
|
await Shell.Current.GoToAsync(
|
|
nameof(TodoDetailsPage),
|
|
true,
|
|
new Dictionary<string, object>()
|
|
{
|
|
{ nameof(Todo), todo }
|
|
});
|
|
}
|
|
}
|
|
}
|