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.
84 lines
2.5 KiB
84 lines
2.5 KiB
using ToolKit;
|
|
using System.Windows.Input;
|
|
using System.Diagnostics;
|
|
using Model;
|
|
|
|
namespace BookApp.ViewModel
|
|
{
|
|
public class ViewModelNavigation : BaseViewModel
|
|
{
|
|
private ViewModelMenuItem _selectedItem;
|
|
public ViewModelMenuItem SelectedItem
|
|
{
|
|
get { return _selectedItem; }
|
|
set { SetProperty(ref _selectedItem, value); }
|
|
}
|
|
|
|
private Book _selectedBook;
|
|
public Book SelectedBook
|
|
{
|
|
get { return _selectedBook; }
|
|
set { SetProperty(ref _selectedBook, value); }
|
|
}
|
|
public INavigation Navigation { get; set; }
|
|
|
|
public ICommand MenuItemsFiltre { get; private set; }
|
|
public ICommand ItemSelectedCommandMain { get; private set; }
|
|
public ICommand BookSelected { get; private set; }
|
|
|
|
public ICommand BackButtonCommand { get; private set; }
|
|
|
|
public ViewModelNavigation()
|
|
{
|
|
MenuItemsFiltre = new Command(async () => await OnItemSelected(SelectedItem));
|
|
ItemSelectedCommandMain = new Command(async () => await OnItemSelected(SelectedItem));
|
|
|
|
BackButtonCommand = new Command(async () => await BackButton(this, EventArgs.Empty));
|
|
|
|
BookSelected = new Command(async () => await OnBookSelected(SelectedBook));
|
|
}
|
|
|
|
private async Task OnItemSelected(ViewModelMenuItem selectedItem)
|
|
{
|
|
SelectedItem = 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 OnBookSelected(Book SelectedBook)
|
|
{
|
|
if (SelectedBook == null)
|
|
{
|
|
return;
|
|
}
|
|
try
|
|
{
|
|
await Shell.Current.GoToAsync("DetailBookPage");
|
|
}
|
|
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();
|
|
}
|
|
}
|
|
}
|
|
}
|