Skip to content

Commit

Permalink
[Core] More MVVM-ish way to manage documents
Browse files Browse the repository at this point in the history
  • Loading branch information
BAndysc committed Jan 10, 2021
1 parent 250c643 commit 598712c
Show file tree
Hide file tree
Showing 21 changed files with 279 additions and 243 deletions.
21 changes: 20 additions & 1 deletion WDE.Blueprints/Editor/ViewModels/BlueprintEditorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using WDE.Blueprints.Data;
using WDE.Common.History;
using WDE.Common.Managers;
using WDE.Common.Utils;

namespace WDE.Blueprints.Editor.ViewModels
{
public class BlueprintEditorViewModel : BindableBase
public class BlueprintEditorViewModel : BindableBase, IDocument
{
private BlueprintSolutionItem solutionItem;

Expand Down Expand Up @@ -47,5 +51,20 @@ public BlueprintEditorViewModel(BlueprintSolutionItem solutionItem, NodesViewMod
GraphViewModel.AddElement(new NodeViewModel("Node 3", Enums.NodeType.Expression, 1, 1), 10200, 10000);
GraphViewModel.AddElement(new NodeViewModel("Node 4", Enums.NodeType.Statement, 4, 1), 10300, 10000);
}

public void Dispose()
{
}

public string Title { get; } = "Blueprints";
public ICommand Undo { get; } = AlwaysDisabledCommand.Command;
public ICommand Redo { get; }= AlwaysDisabledCommand.Command;
public ICommand Copy { get; }= AlwaysDisabledCommand.Command;
public ICommand Cut { get; }= AlwaysDisabledCommand.Command;
public ICommand Paste { get; }= AlwaysDisabledCommand.Command;
public ICommand Save { get; }= AlwaysDisabledCommand.Command;
public ICommand CloseCommand { get; set; } = null;
public bool CanClose { get; } = true;
public IHistoryManager History { get; } = null;
}
}
15 changes: 2 additions & 13 deletions WDE.Blueprints/Providers/BlueprintItemEditorProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,9 @@ public BlueprintItemEditorProvider(IBlueprintDefinitionsRegistry blueprintDefini
this.blueprintDefinitionsRegistry = blueprintDefinitionsRegistry;
}

public Document GetEditor(BlueprintSolutionItem item)
public IDocument GetEditor(BlueprintSolutionItem item)
{
var view = new BlueprintEditorView();
var vm = new BlueprintEditorViewModel(item, new NodesViewModel(blueprintDefinitionsRegistry));
view.DataContext = vm;

Document editor = new Document
{
Title = "Blueprints",
Content = view,
CanClose = true
};

return editor;
return new BlueprintEditorViewModel(item, new NodesViewModel(blueprintDefinitionsRegistry));
}
}
}
13 changes: 6 additions & 7 deletions WDE.HistoryWindow/HistoryWindowModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using WDE.HistoryWindow.Views;
using Prism.Ioc;
using Prism.Events;
using WDE.Common.Managers;
using WDE.Module.Attributes;
using WDE.Module;

Expand All @@ -27,17 +28,15 @@ public HistoryWindowModule(IEventAggregator eventAggregator)
this.eventAggregator = eventAggregator;
}

public ContentControl GetView()
{
var view = new HistoryView();
view.DataContext = new HistoryViewModel(eventAggregator);
return view;
}

public bool AllowMultiple => false;

public string Name => "History view";

public ITool Provide()
{
return new HistoryViewModel(eventAggregator);
}

public bool CanOpenOnStart => false;
}
}
13 changes: 7 additions & 6 deletions WDE.HistoryWindow/ViewModels/HistoryViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WDE.Common.Events;
using WDE.Common.History;
using Prism.Ioc;
Expand All @@ -21,19 +22,19 @@ internal class HistoryEvent
public bool IsFromFuture { get; set; }
}

internal class HistoryViewModel : BindableBase
internal class HistoryViewModel : BindableBase, ITool
{
public ObservableCollection<HistoryEvent> Items { get; set; } = new ObservableCollection<HistoryEvent>();

private IDocument previousDocument;

private string _title;
public string Title
public string Title => "History";
private Visibility _visibility;
public Visibility Visibility
{
get { return _title; }
set { SetProperty(ref _title, value); }
get => _visibility;
set => SetProperty(ref _visibility, value);
}

public HistoryViewModel(IEventAggregator eventAggregator)
{
eventAggregator.GetEvent<EventActiveDocumentChanged>().Subscribe(doc =>
Expand Down
15 changes: 2 additions & 13 deletions WDE.SQLEditor/Providers/MetaSqlSolutionItemEditorProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,9 @@ public MetaSqlSolutionItemEditorProvider(Lazy<ISolutionItemSqlGeneratorRegistry>
this.sqlGeneratorsRegistry = sqlGeneratorsRegistry;
}

public Document GetEditor(MetaSolutionSQL item)
public IDocument GetEditor(MetaSolutionSQL item)
{
var view = new SqlEditorView();
var vm = new SqlEditorViewModel(sqlGeneratorsRegistry.Value.GenerateSql(item as MetaSolutionSQL));
view.DataContext = vm;

Document editor = new Document();
editor.Title = "Sql output";
editor.Content = view;
editor.CanClose = true;
editor.Undo = new DelegateCommand(() => { }, () => false);
editor.Redo = new DelegateCommand(() => { }, () => false);

return editor;
return new SqlEditorViewModel(sqlGeneratorsRegistry.Value.GenerateSql(item as MetaSolutionSQL));
}
}
}
25 changes: 20 additions & 5 deletions WDE.SQLEditor/ViewModels/SqlEditorViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
using Prism.Commands;
using System.Windows.Input;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using WDE.Common.History;
using WDE.Common.Managers;
using WDE.Common.Utils;

namespace WDE.SQLEditor.ViewModels
{
public class SqlEditorViewModel : BindableBase
public class SqlEditorViewModel : BindableBase, IDocument
{
public string Code { get; set; }

public SqlEditorViewModel(string sql)
{
Code = sql;
}

public void Dispose()
{
}

public string Title { get; } = "SQL Output";
public ICommand Undo { get; } = AlwaysDisabledCommand.Command;
public ICommand Redo { get; } = AlwaysDisabledCommand.Command;
public ICommand Copy { get; } = AlwaysDisabledCommand.Command;
public ICommand Cut { get; } = AlwaysDisabledCommand.Command;
public ICommand Paste { get; } = AlwaysDisabledCommand.Command;
public ICommand Save { get; } = AlwaysDisabledCommand.Command;
public ICommand CloseCommand { get; set; } = null;
public bool CanClose { get; } = true;
public IHistoryManager History { get; } = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
using System.Diagnostics;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;
using WDE.Common.Managers;
using WDE.SmartScriptEditor.Editor.UserControls;

namespace WDE.SmartScriptEditor.Editor.ViewModels
{
public class SmartScriptEditorViewModel : BindableBase, System.IDisposable
public class SmartScriptEditorViewModel : BindableBase, IDocument, System.IDisposable
{
private readonly IDatabaseProvider database;
private readonly IHistoryManager history;
Expand All @@ -44,7 +46,8 @@ public class SmartScriptEditorViewModel : BindableBase, System.IDisposable
public ObservableCollection<SmartEvent> Events => script.Events;

public CompositeCollection Together { get; }= new CompositeCollection();


public bool CanClose { get; } = true;
public IHistoryManager History => history;

public SmartEvent SelectedItem => Events.FirstOrDefault(ev => ev.IsSelected);
Expand Down Expand Up @@ -82,7 +85,24 @@ public class SmartScriptEditorViewModel : BindableBase, System.IDisposable
public DelegateCommand SelectionLeft { get; set; }
public DelegateCommand SelectAll { get; set; }

public SmartScriptEditorViewModel(IHistoryManager history, IDatabaseProvider database, IEventAggregator eventAggregator, ISmartDataManager smartDataManager, ISmartFactory smartFactory, IItemFromListProvider itemFromListProvider, ISmartTypeListProvider smartTypeListProvider, ISolutionItemNameRegistry itemNameRegistry)
public string Title { get; set; }
public ICommand Undo => UndoCommand;
public ICommand Redo => RedoCommand;

public ICommand Copy => CopyCommand;
public ICommand Cut => CutCommand;
public ICommand Paste => PasteCommand;
public ICommand Save => SaveCommand;
public ICommand CloseCommand { get; set; }

public SmartScriptEditorViewModel(IHistoryManager history,
IDatabaseProvider database,
IEventAggregator eventAggregator,
ISmartDataManager smartDataManager,
ISmartFactory smartFactory,
IItemFromListProvider itemFromListProvider,
ISmartTypeListProvider smartTypeListProvider,
ISolutionItemNameRegistry itemNameRegistry)
{
this.history = history;
this.database = database;
Expand All @@ -91,7 +111,7 @@ public SmartScriptEditorViewModel(IHistoryManager history, IDatabaseProvider dat
this.itemFromListProvider = itemFromListProvider;
this.smartTypeListProvider = smartTypeListProvider;
this.itemNameRegistry = itemNameRegistry;

EditEvent = new DelegateCommand(EditEventCommand);
DeselectActions = new DelegateCommand(() =>
{
Expand Down Expand Up @@ -460,6 +480,7 @@ internal void SetSolutionItem(SmartScriptSolutionItem item)
{
Debug.Assert(_item == null);
_item = item;
Title = itemNameRegistry.GetName(item);

var lines = database.GetScriptFor(_item.Entry, _item.SmartType);
script = new SmartScript(_item, smartFactory);
Expand Down
23 changes: 3 additions & 20 deletions WDE.SmartScriptEditor/Providers/SmartScriptEditorProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,12 @@ public SmartScriptEditorProvider(ISolutionItemNameRegistry solutionItemNameRegis
this.containerProvider = containerProvider;
}

public Document GetEditor(SmartScriptSolutionItem item)
public IDocument GetEditor(SmartScriptSolutionItem item)
{
var view = new SmartScriptEditorView();
var vm = containerProvider.Resolve<SmartScriptEditorViewModel>();
vm.SetSolutionItem(item);
view.DataContext = vm;

Document editor = new Document
{
Title = solutionItemNameRegistry.GetName(item),
Content = view,
Undo = vm.UndoCommand,
Redo = vm.RedoCommand,
Save = vm.SaveCommand,
Copy = vm.CopyCommand,
Paste = vm.PasteCommand,
Cut = vm.CutCommand,
History = vm.History,
CanClose = true
};
editor.OnDispose += () => vm.Dispose();

return editor;

return vm;
}
}
}
13 changes: 11 additions & 2 deletions WDE.Solutions/Explorer/ViewModels/SolutionExplorerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;

using System.Windows.Controls;
using Prism.Events;
using WDE.Common;
using WDE.Common.Events;
using WDE.Common.Solution;
using Prism.Ioc;
using WDE.Common.Managers;

namespace WDE.Solutions.Explorer.ViewModels
{
public class SolutionExplorerViewModel : BindableBase
public class SolutionExplorerViewModel : BindableBase, ITool
{
private readonly ISolutionItemNameRegistry itemNameRegistry;
private readonly ISolutionManager _solutionManager;
Expand Down Expand Up @@ -113,5 +114,13 @@ private void AddItemToRoot(ISolutionItem item)
_itemToViewmodel.Add(item, viewModel);
Root.Add(viewModel);
}

public string Title { get; } = "Solution explorer";
private Visibility _visibility;
public Visibility Visibility
{
get => _visibility;
set => SetProperty(ref _visibility, value);
}
}
}
4 changes: 2 additions & 2 deletions WDE.Solutions/Manager/SolutionItemEditorRegistry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ private void Register<T>(ISolutionItemEditorProvider<T> provider) where T : ISol
editorProviders.Add(typeof(T), provider);
}

private Document GetEditor<T>(T item) where T : ISolutionItem
private IDocument GetEditor<T>(T item) where T : ISolutionItem
{
var x = editorProviders[item.GetType()] as ISolutionItemEditorProvider<T>;
return x.GetEditor(item);
}

public Document GetEditor(ISolutionItem item)
public IDocument GetEditor(ISolutionItem item)
{
return GetEditor((dynamic)item);
}
Expand Down
17 changes: 10 additions & 7 deletions WDE.Solutions/SolutionExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,32 @@
using System.Threading.Tasks;
using System.Windows.Controls;
using WDE.Common;
using WDE.Common.Managers;
using WDE.Module.Attributes;
using WDE.Common.Windows;
using WDE.Solutions.Explorer.ViewModels;
using WDE.Solutions.Explorer.Views;

namespace WDE.Solutions
{
[AutoRegister, SingleInstance]
public class SolutionExplorer : ISolutionExplorer, IToolProvider
{
private readonly SolutionExplorerViewModel _solutionExplorerViewModel;
public bool AllowMultiple => false;

public string Name => "Solution explorer";

public bool CanOpenOnStart => true;

public ContentControl GetSolutionExplorerView()
public SolutionExplorer(SolutionExplorerViewModel solutionExplorerViewModel)
{
return new SolutionExplorerView();
_solutionExplorerViewModel = solutionExplorerViewModel;
}

public ContentControl GetView()
public ITool Provide()
{
return GetSolutionExplorerView();
return _solutionExplorerViewModel;
}

public bool CanOpenOnStart => true;
}
}
Loading

0 comments on commit 598712c

Please sign in to comment.