You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PocketBook/Sources/BookApp/ViewModel/ViewModelNavigation.cs

85 lines
2.5 KiB

using ToolKit;
using System.Windows.Input;
using System.Diagnostics;
using Model;
namespace BookApp.ViewModel
{
public class ViewModelNavigation : BaseViewModel
{
public INavigation Navigation { get; set; }
// Command Pop
public ICommand _backButtonCommand;
public ICommand BackButtonCommand =>
_backButtonCommand ??= new CommandPersonnal(
async () => await BackButton(this, EventArgs.Empty)
);
// Command Collection livre
public ICommand _bookDetailCommand;
public ICommand BookDetailCommand =>
_bookDetailCommand ??= new CommandPersonnal<string>(
async (book) => await OnToItemDetail(book)
);
// Command Collection menu
private ICommand _menuItemsCommand;
public ICommand MenuItemsCommand =>
_menuItemsCommand ??= new CommandPersonnal<ViewModelMenuItem>(
async (item) => await OnItemSelected(item)
);
public ICommand _menuItemsFiltreCommand;
public ICommand MenuItemsFiltreCommand =>
_menuItemsFiltreCommand ??= new CommandPersonnal<ViewModelMenuItem>(
async (item) => await OnItemSelected(item)
);
public ViewModelNavigation() { }
private async Task OnItemSelected(ViewModelMenuItem selectedItem)
{
if (string.IsNullOrEmpty(selectedItem?.Route))
{
return;
}
try
{
await Shell.Current.GoToAsync(selectedItem.Route);
}
catch (Exception ex)
{
Debug.WriteLine($"Navigation failed: {ex.Message}");
}
}
private async Task OnToItemDetail(string bookId)
{
if (bookId == null)
{
Debug.WriteLine("Book is null, cannot navigate to details page.");
return;
}
try
{
await Shell.Current.GoToAsync($"DetailBookPage?BookId={bookId}");
}
catch (Exception ex)
{
Debug.WriteLine($"Navigation failed: {ex.Message}");
}
}
async Task BackButton(object sender, EventArgs args)
{
if (Shell.Current.Navigation.NavigationStack.Count > 1)
{
await Shell.Current.Navigation.PopAsync();
}
}
}
}