You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.5 KiB
C#

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Avalonia.Controls;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.Extensions.DependencyInjection;
using Sln.Wcs.UI.ViewModels.Base;
using Sln.Wcs.UI.ViewModels.Device;
using Sln.Wcs.UI.ViewModels.Path;
using Sln.Wcs.UI.ViewModels.Task;
using Sln.Wcs.UI.Views;
namespace Sln.Wcs.UI.ViewModels;
public partial class NavigationViewModel : ObservableObject
{
private readonly IServiceProvider _sp;
private readonly Dictionary<Type, ICrudPageViewModel> _vmCache = new();
private readonly Dictionary<Type, Control> _viewCache = new();
private Control? _homeView;
public ObservableCollection<TopMenuItem> TopMenuItems { get; } = new();
public event Action<Control?>? PageChanged;
public NavigationViewModel(IServiceProvider sp)
{
_sp = sp;
BuildMenu();
}
public void LoadDefaultPage() => ShowHome();
private void BuildMenu()
{
TopMenuItems.Clear();
TopMenuItems.Add(new TopMenuItem("首页", ShowHome));
TopMenuItems.Add(new TopMenuItem("基础数据", new List<SubMenuItem>
{
new("库位信息", () => NavigateTo<LocationInfoViewModel>()),
new("物料信息", () => NavigateTo<MaterialInfoViewModel>()),
new("仓库信息", () => NavigateTo<StoreInfoViewModel>()),
}));
TopMenuItems.Add(new TopMenuItem("设备管理", new List<SubMenuItem>
{
new("设备主机", () => NavigateTo<DeviceHostViewModel>()),
new("设备信息", () => NavigateTo<DeviceInfoViewModel>()),
new("设备参数", () => NavigateTo<DeviceParamViewModel>()),
}));
TopMenuItems.Add(new TopMenuItem("路径管理", new List<SubMenuItem>
{
new("路径信息", () => NavigateTo<PathInfoViewModel>()),
new("路径明细", () => NavigateTo<PathDetailsViewModel>()),
}));
TopMenuItems.Add(new TopMenuItem("任务管理", new List<SubMenuItem>
{
new("任务队列", () => NavigateTo<TaskQueueViewModel>()),
new("任务明细", () => NavigateTo<TaskDetailViewModel>()),
}));
}
private void ShowHome()
{
if (_homeView == null)
{
var vm = _sp.GetRequiredService<HomePageViewModel>();
_homeView = new HomePageView { DataContext = vm };
}
PageChanged?.Invoke(_homeView);
}
private void NavigateTo<T>() where T : ICrudPageViewModel
{
var type = typeof(T);
if (!_vmCache.TryGetValue(type, out var vm))
{
vm = _sp.GetRequiredService<T>();
_vmCache[type] = vm;
}
if (!_viewCache.TryGetValue(type, out var view))
{
view = vm.CreateView();
view.DataContext = vm;
_viewCache[type] = view;
}
PageChanged?.Invoke(view);
vm.Load();
}
}
public class TopMenuItem
{
public string Label { get; set; }
public List<SubMenuItem>? Children { get; set; }
public Action? Action { get; set; }
public TopMenuItem(string label, Action action)
{
Label = label; Action = action;
}
public TopMenuItem(string label, List<SubMenuItem> children)
{
Label = label; Children = children;
}
}
public class SubMenuItem
{
public string Label { get; set; }
public Action Action { get; set; }
public SubMenuItem(string label, Action action)
{
Label = label; Action = action;
}
}