Restructured the VM/Vs folder layout while it's still feasible

This commit is contained in:
Robert committed 2019-11-15 15:53:55 +01:00
1 parent 7443c0eda7
commit 368b73ce9c
48 files changed
+397 -469

No files matched your search

+122
View File
@@ -0,0 +1,122 @@
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Controls;
using Artemis.Core.Events;
using Artemis.Core.Services.Interfaces;
using Artemis.UI.Ninject.Factories;
using Artemis.UI.Screens.Home;
using Artemis.UI.Screens.News;
using Artemis.UI.Screens.Settings;
using Artemis.UI.Screens.SurfaceEditor;
using Artemis.UI.Screens.Workshop;
using Stylet;
namespace Artemis.UI.Screens
{
public class RootViewModel : Conductor<IScreen>.Collection.OneActive
{
private readonly ICollection<IScreenViewModel> _artemisViewModels;
private readonly IModuleViewModelFactory _moduleViewModelFactory;
private readonly IPluginService _pluginService;
public RootViewModel(ICollection<IScreenViewModel> artemisViewModels, IPluginService pluginService, IModuleViewModelFactory moduleViewModelFactory)
{
_artemisViewModels = artemisViewModels;
_pluginService = pluginService;
_moduleViewModelFactory = moduleViewModelFactory;
// Add the built-in items
Items.AddRange(artemisViewModels);
// Activate the home item
ActiveItem = _artemisViewModels.First(v => v.GetType() == typeof(HomeViewModel));
// Sync up with the plugin service
Modules = new BindableCollection<Core.Plugins.Abstract.Module>();
Modules.AddRange(_pluginService.GetPluginsOfType<Core.Plugins.Abstract.Module>());
_pluginService.PluginEnabled += PluginServiceOnPluginEnabled;
_pluginService.PluginDisabled += PluginServiceOnPluginDisabled;
PropertyChanged += OnSelectedModuleChanged;
PropertyChanged += OnSelectedPageChanged;
}
public IObservableCollection<Core.Plugins.Abstract.Module> Modules { get; set; }
public bool MenuOpen { get; set; }
public ListBoxItem SelectedPage { get; set; }
public Core.Plugins.Abstract.Module SelectedModule { get; set; }
public async Task NavigateToSelectedModule()
{
if (SelectedModule == null)
return;
// Create a view model for the given plugin info (which will be a module)
var viewModel = await Task.Run(() => _moduleViewModelFactory.CreateModuleViewModel(SelectedModule));
ActivateItem(viewModel);
SelectedPage = null;
MenuOpen = false;
}
private void PluginServiceOnPluginEnabled(object sender, PluginEventArgs e)
{
var existing = Modules.FirstOrDefault(m => _pluginService.GetPluginInfo(m)?.Guid == e.PluginInfo.Guid);
if (existing != null)
{
if (SelectedModule == existing && SelectedModule != null)
SelectedModule = null;
Modules.Remove(existing);
}
if (e.PluginInfo.Instance is Core.Plugins.Abstract.Module module)
Modules.Add(module);
}
private void PluginServiceOnPluginDisabled(object sender, PluginEventArgs e)
{
var existing = Modules.FirstOrDefault(m => _pluginService.GetPluginInfo(m)?.Guid == e.PluginInfo.Guid);
if (existing != null)
{
if (SelectedModule == existing && SelectedModule != null)
SelectedModule = null;
Modules.Remove(existing);
}
}
private async void OnSelectedModuleChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(SelectedModule))
await NavigateToSelectedModule();
}
private void OnSelectedPageChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName != "SelectedPage" || SelectedPage == null)
return;
switch (SelectedPage.Name)
{
case "Home":
ActivateItem(_artemisViewModels.First(v => v is HomeViewModel));
break;
case "News":
ActivateItem(_artemisViewModels.First(v => v is NewsViewModel));
break;
case "Workshop":
ActivateItem(_artemisViewModels.First(v => v is WorkshopViewModel));
break;
case "SurfaceEditor":
ActivateItem(_artemisViewModels.First(v => v is SurfaceEditorViewModel));
break;
case "Settings":
ActivateItem(_artemisViewModels.First(v => v is SettingsViewModel));
break;
}
SelectedModule = null;
MenuOpen = false;
}
}
}