using System.Collections.Generic; using detect.gui.Commons; using ReactiveUI; using System.Linq; namespace detect.gui.Classes; public class ViewModelItem { private readonly RoutingState _router; public string? ViewModelTypeName { get; init; } public string? Text { get; init; } public ViewModelItem? Parent { get; init; } public List Children { get; set; } = new List(); public bool IsAuto { get; init; } public bool IsActived { get; set; } public object? Param { get; set; } public ViewModelItem(RoutingState router) { _router = router; } public void Goto(object? param = null) { var v = RouterHelper.GetViewModel(ViewModelTypeName!); if (v == null) return; IsActived = true; Parent?.Children.ForEach(i => { if (ViewModelTypeName != i.ViewModelTypeName) i.IsActived = false; }); var count = _router.NavigationStack.Count(x => x.GetType().Name == ViewModelTypeName); if (count != 0) { var r = _router.NavigationStack.First(x => x.GetType().Name == ViewModelTypeName); v.GetType().GetProperty("Item")!.SetValue(r, param); _router.NavigationStack.Remove(r); _router.Navigate.Execute(r); } else { if (Parent != null) { Parent.IsActived = true; var parentType = Parent.ViewModelTypeName?.GetTypeByName(); var parentModel = parentType?.Assembly.CreateInstance(parentType.FullName!); if (parentModel != null) { _router.NavigationStack.Add((IRoutableViewModel)parentModel); } } v.GetType().GetProperty("Item")!.SetValue(v, param); _router.Navigate.Execute(v); } var child = Children.FirstOrDefault(x => x.IsActived); if (child == null && Children.Any() && IsAuto) Children[0].Goto(); else if (child != null && Children.Any()) { var c = _router.NavigationStack.First(x => x.GetType().Name == child.ViewModelTypeName); _router.NavigationStack.Remove(c); _router.Navigate.Execute(c); } } public void Goback() { IsActived = false; var r = _router.NavigationStack.First(x => x.GetType().Name == ViewModelTypeName); _router.NavigationStack.Remove(r); if (Parent == null) return; var p = _router.NavigationStack.First(x => x.GetType().Name == Parent.ViewModelTypeName); _router.NavigationStack.Remove(p); _router.Navigate.Execute(p); } }