Added module (the first major plugin type) view(model) creation

This commit is contained in:
SpoinkyNL committed 2018-01-08 20:41:21 +01:00
1 parent f7fe57676d
commit 3bea4463dc
26 files changed
+172 -1038

No files matched your search

+2
View File
@@ -93,6 +93,8 @@
</ApplicationDefinition>
<Compile Include="Bootstrapper.cs" />
<Compile Include="Ninject\UIModule.cs" />
<Compile Include="Services\Interfaces\IArtemisUIService.cs" />
<Compile Include="Stylet\ArtemisViewManager.cs" />
<Compile Include="Stylet\NinjectBootstrapper.cs" />
<Compile Include="ViewModels\Interfaces\IHomeViewModel.cs" />
<Compile Include="ViewModels\Interfaces\IArtemisViewModel.cs" />
+12 -1
View File
@@ -1,4 +1,5 @@
using Artemis.UI.ViewModels.Interfaces;
using Artemis.UI.Services.Interfaces;
using Artemis.UI.ViewModels.Interfaces;
using Ninject.Extensions.Conventions;
using Ninject.Modules;
@@ -17,6 +18,16 @@ namespace Artemis.UI.Ninject
.InheritedFrom<IArtemisViewModel>()
.BindAllInterfaces();
});
// Bind all UI services as singletons
Kernel.Bind(x =>
{
x.FromThisAssembly()
.SelectAllClasses()
.InheritedFrom<IArtemisUIService>()
.BindAllInterfaces()
.Configure(c => c.InSingletonScope());
});
}
}
}
@@ -0,0 +1,8 @@
namespace Artemis.UI.Services.Interfaces
{
// ReSharper disable once InconsistentNaming
public interface IArtemisUIService
{
}
}
@@ -0,0 +1,46 @@
using System.IO;
using System.Windows;
using System.Windows.Markup;
using Artemis.Core.Exceptions;
using Artemis.Plugins.Interfaces;
using Stylet;
namespace Artemis.UI.Stylet
{
public class ArtemisViewManager : ViewManager
{
public ArtemisViewManager(ViewManagerConfig config) : base(config)
{
}
public override UIElement CreateViewForModel(object model)
{
if (model is IPluginViewModel)
return CreateViewForPlugin(model);
return base.CreateViewForModel(model);
}
private UIElement CreateViewForPlugin(object model)
{
var pluginInfo = ((IPluginViewModel) model).PluginInfo;
var viewPath = pluginInfo.Folder + pluginInfo.ViewModel.Replace("ViewModel", "View").Replace(".cs", ".xaml");
// There doesn't have to be a view so make sure one exists
if (!File.Exists(viewPath))
return null;
// Compile the view if found, must be done on UI thread sadly
object view = null;
Execute.OnUIThread(() => view = XamlReader.Parse(File.ReadAllText(viewPath)));
var viewType = view.GetType();
// Make sure it's a valid UIElement
if (viewType.IsAbstract || !typeof(UIElement).IsAssignableFrom(viewType))
{
var e = new ArtemisPluginException(pluginInfo, $"Found type for view: {viewType.Name}, but it wasn't a class derived from UIElement");
throw e;
}
return (UIElement) view;
}
}
}
+1 -1
View File
@@ -31,7 +31,7 @@ namespace Artemis.UI.Stylet
ViewFactory = GetInstance,
ViewAssemblies = new List<Assembly> {GetType().Assembly}
};
kernel.Bind<IViewManager>().ToConstant(new ViewManager(viewManagerConfig));
kernel.Bind<IViewManager>().ToConstant(new ArtemisViewManager(viewManagerConfig));
kernel.Bind<IWindowManagerConfig>().ToConstant(this).InTransientScope();
kernel.Bind<IWindowManager>().ToMethod(c => new WindowManager(c.Kernel.Get<IViewManager>(),
@@ -1,6 +1,5 @@
using System;
using System.Diagnostics;
using Artemis.Core.Services.Interfaces;
using Artemis.UI.ViewModels.Interfaces;
using Stylet;
@@ -8,15 +7,6 @@ namespace Artemis.UI.ViewModels
{
public class HomeViewModel : Screen, IHomeViewModel
{
private readonly IPluginService _pluginService;
public HomeViewModel(IPluginService pluginService)
{
_pluginService = pluginService;
_pluginService.FinishedLoadedPlugins += PluginServiceOnFinishedLoadedPlugins;
}
public string Title => "Home";
public void OpenUrl(string url)
@@ -25,15 +15,5 @@ namespace Artemis.UI.ViewModels
if (Uri.IsWellFormedUriString(url, UriKind.RelativeOrAbsolute))
Process.Start(url);
}
/// <summary>
/// Populates the sidebar with plugins when they are finished loading
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
private void PluginServiceOnFinishedLoadedPlugins(object sender, EventArgs eventArgs)
{
throw new NotImplementedException();
}
}
}
@@ -1,6 +1,8 @@
namespace Artemis.UI.ViewModels.Interfaces
using Stylet;
namespace Artemis.UI.ViewModels.Interfaces
{
public interface IArtemisViewModel
public interface IArtemisViewModel : IScreen
{
string Title { get; }
}
+44 -3
View File
@@ -1,24 +1,57 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Linq;
using Artemis.Core.Services.Interfaces;
using Artemis.Plugins.Interfaces;
using Artemis.Plugins.Models;
using Artemis.UI.Services.Interfaces;
using Artemis.UI.ViewModels.Interfaces;
using Stylet;
namespace Artemis.UI.ViewModels
{
public class RootViewModel : Conductor<IArtemisViewModel>.Collection.OneActive
public class RootViewModel : Conductor<IScreen>.Collection.OneActive
{
private readonly ICollection<IArtemisViewModel> _artemisViewModels;
private readonly IPluginService _pluginService;
public RootViewModel(ICollection<IArtemisViewModel> artemisViewModels)
public RootViewModel(ICollection<IArtemisViewModel> artemisViewModels, IPluginService pluginService)
{
_artemisViewModels = artemisViewModels;
_pluginService = pluginService;
// 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<PluginInfo>();
LoadingPlugins = _pluginService.LoadingPlugins;
_pluginService.StartedLoadingPlugins += PluginServiceOnStartedLoadingPlugins;
_pluginService.FinishedLoadedPlugins += PluginServiceOnFinishedLoadedPlugins;
if (!LoadingPlugins)
Modules.AddRange(_pluginService.Plugins.Where(p => p.Plugin is IModule));
}
public IObservableCollection<PluginInfo> Modules { get; set; }
public bool MenuOpen { get; set; }
public bool LoadingPlugins { get; set; }
private void PluginServiceOnStartedLoadingPlugins(object sender, EventArgs eventArgs)
{
LoadingPlugins = true;
Modules.Clear();
}
private void PluginServiceOnFinishedLoadedPlugins(object sender, EventArgs eventArgs)
{
LoadingPlugins = false;
Modules.AddRange(_pluginService.Plugins.Where(p => p.Plugin is IModule));
NavigateToModule(Modules.First());
}
public void NavigateToHome()
{
@@ -39,5 +72,13 @@ namespace Artemis.UI.ViewModels
ActivateItem(_artemisViewModels.First(v => v.GetType() == typeof(SettingsViewModel)));
MenuOpen = false;
}
public async void NavigateToModule(PluginInfo pluginInfo)
{
// Create a view model for the given plugin info (which will be a module)
var viewModel = await _pluginService.GetPluginViewModel(pluginInfo);
// Tell Stylet to active the view model, the view manager will compile and show the XAML
ActivateItem(viewModel);
}
}
}