Skip to content

Commit

Permalink
Get rid of SharedViewModel, and make VMs work with a DeviceViewModel …
Browse files Browse the repository at this point in the history
…and ILogger instead
  • Loading branch information
jskeet committed May 24, 2020
1 parent 7f6b139 commit 6716a69
Show file tree
Hide file tree
Showing 13 changed files with 128 additions and 152 deletions.
16 changes: 9 additions & 7 deletions Drums/VDrumExplorer.Gui/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ namespace VDrumExplorer.Gui
/// </summary>
public partial class App : Application
{
private SharedViewModel sharedViewModel;
private DeviceViewModel deviceViewModel;
private LogViewModel logViewModel;

public App()
{
sharedViewModel = new SharedViewModel();
deviceViewModel = new DeviceViewModel();
logViewModel = new LogViewModel();
DispatcherUnhandledException += (sender, args) =>
{
sharedViewModel.Log("Unhandled exception", args.Exception);
logViewModel.Log("Unhandled exception", args.Exception);
args.Handled = true;
};
}
Expand All @@ -32,17 +34,17 @@ protected override async void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);

var viewModel = new ExplorerHomeViewModel(ViewServices.Instance, sharedViewModel);
var viewModel = new ExplorerHomeViewModel(ViewServices.Instance, logViewModel, deviceViewModel);
MainWindow = new ExplorerHome { DataContext = viewModel };
MainWindow.Show();
sharedViewModel.LogVersion(GetType());
await sharedViewModel.DetectModule();
logViewModel.LogVersion(GetType());
await deviceViewModel.DetectModule(logViewModel.Logger);
}

protected override void OnExit(ExitEventArgs e)
{
base.OnExit(e);
sharedViewModel?.ConnectedDevice?.Dispose();
deviceViewModel?.ConnectedDevice?.Dispose();
}

// TODO: Need to get at this somewhere...
Expand Down
2 changes: 1 addition & 1 deletion Drums/VDrumExplorer.Gui/DataExplorer.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<Button Margin="{StaticResource ValueMargin}" Command="{Binding CancelEditCommand}">Cancel changes</Button>
</StackPanel>

<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Visibility="{Binding SharedViewModel.DeviceConnected, Converter={StaticResource VisibilityConverter}}" Margin="{StaticResource PanelMargin}">
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Visibility="{Binding DeviceViewModel.DeviceConnected, Converter={StaticResource VisibilityConverter}}" Margin="{StaticResource PanelMargin}">
<Button Margin="{StaticResource ValueMargin}" Command="{Binding CopyDataToDeviceCommand}" CommandParameter="{Binding SelectedNode}" Content="{Binding CopyDataTitle}" VerticalAlignment="Center"/>
<TextBlock Margin="{StaticResource ValueMargin}" Visibility="{Binding IsKitExplorer, Converter={StaticResource VisibilityConverter}}" VerticalAlignment="Center">Kit to copy to:</TextBlock>
<TextBox Margin="{StaticResource ValueMargin}" Visibility="{Binding IsKitExplorer, Converter={StaticResource VisibilityConverter}}" VerticalAlignment="Center" Text="{Binding KitCopyTargetNumber, ValidatesOnExceptions=True, UpdateSourceTrigger=PropertyChanged}" Width="30"/>
Expand Down
6 changes: 3 additions & 3 deletions Drums/VDrumExplorer.Gui/ExplorerHome.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
<DockPanel>
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="5">
<TextBlock>Connected device: </TextBlock>
<TextBlock Text="{Binding SharedViewModel.ConnectedDeviceSchema.Identifier.Name}" Margin="{StaticResource ValueMargin}"/>
<TextBlock Text="{Binding DeviceViewModel.ConnectedDeviceSchema.Identifier.Name}" Margin="{StaticResource ValueMargin}"/>
</StackPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" Margin="3,3,3,3">
<Button Command="{Binding LoadFileCommand}">Load file</Button>
<Button Command="{Binding SaveLogCommand}" Margin="{StaticResource ValueMargin}">Save log file</Button>
</StackPanel>
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" IsEnabled="{Binding SharedViewModel.DeviceConnected}" Margin="3,3,3,3">
<StackPanel Orientation="Horizontal" DockPanel.Dock="Top" IsEnabled="{Binding DeviceViewModel.DeviceConnected}" Margin="3,3,3,3">
<!-- Note: primary reason for using labels instead of text blocks is that they reflect enabled state more simply. -->
<Label Content="Device options:" />
<Button Margin="{StaticResource ValueMargin}" Command="{Binding LoadModuleFromDeviceCommand}" Content="Load all data"/>
Expand All @@ -29,7 +29,7 @@
<Separator DockPanel.Dock="Top" />
<GroupBox Header="Log" Margin="5,0,5,5">
<ScrollViewer>
<ItemsControl ItemsSource="{Binding Log.LogEntries}">
<ItemsControl ItemsSource="{Binding LogViewModel.LogEntries}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel Grid.IsSharedSizeScope="True" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by the Apache License 2.0,
// as found in the LICENSE.txt file.

using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
Expand All @@ -12,9 +13,6 @@ namespace VDrumExplorer.ViewModel.Audio
{
public class InstrumentAudioExplorerViewModel : ViewModelBase<ModuleAudio>
{
private readonly IViewServices viewServices;
private readonly SharedViewModel shared;

public string Title { get; }

public IReadOnlyList<string> OutputDevices { get; }
Expand Down Expand Up @@ -56,10 +54,8 @@ public InstrumentAudio? SelectedAudio
set => SetProperty(ref selectedAudio, value);
}

public InstrumentAudioExplorerViewModel(IViewServices viewServices, SharedViewModel shared, ModuleAudio model, string? file) : base(model)
public InstrumentAudioExplorerViewModel(ModuleAudio model, string? file) : base(model)
{
this.viewServices = viewServices;
this.shared = shared;
Title = file is null
? $"Instrument Audio Explorer ({model.Schema.Identifier.Name})"
: $"Instrument Audio Explorer ({model.Schema.Identifier.Name}) - {file}";
Expand Down
16 changes: 8 additions & 8 deletions Drums/VDrumExplorer.ViewModel/Data/DataExplorerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by the Apache License 2.0,
// as found in the LICENSE.txt file.

using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.IO;
Expand All @@ -14,10 +15,11 @@ namespace VDrumExplorer.ViewModel.Data
{
public abstract class DataExplorerViewModel : ViewModelBase<ModuleData>
{
protected ILogger Logger { get; }
protected IViewServices ViewServices { get; }
public DeviceViewModel DeviceViewModel { get; }
private readonly ModuleData data;

public SharedViewModel SharedViewModel { get; }
public DelegateCommand EditCommand { get; }
public DelegateCommand CommitCommand { get; }
public DelegateCommand CancelEditCommand { get; }
Expand Down Expand Up @@ -84,26 +86,24 @@ public int Attack

private ModuleDataSnapshot? snapshot;

public DataExplorerViewModel(IViewServices viewServices, SharedViewModel shared, ModuleData data) : base(data)
public DataExplorerViewModel(IViewServices viewServices, ILogger logger, DeviceViewModel deviceViewModel, ModuleData data) : base(data)
{
Logger = logger;
this.DeviceViewModel = deviceViewModel;
this.ViewServices = viewServices;
SharedViewModel = shared;
this.data = data;
readOnly = true;
EditCommand = new DelegateCommand(EnterEditMode, readOnly);
CommitCommand = new DelegateCommand(CommitEdit, !readOnly);
CancelEditCommand = new DelegateCommand(CancelEdit, !readOnly);
PlayNoteCommand = new DelegateCommand(PlayNote, shared.DeviceConnected);
PlayNoteCommand = new DelegateCommand(PlayNote, deviceViewModel.DeviceConnected);
SaveFileCommand = new DelegateCommand(SaveFile, true);
SaveFileAsCommand = new DelegateCommand(SaveFileAs, true);
CopyDataToDeviceCommand = new DelegateCommand(CopyDataToDevice, true);
Root = SingleItemCollection.Of(new DataTreeNodeViewModel(data.LogicalRoot, this));
SelectedNode = Root[0];
}

public void Log(string text) => SharedViewModel.Log(text);
public void Log(string text, Exception exception) => SharedViewModel.Log(text, exception);

private bool readOnly;
public bool ReadOnly
{
Expand Down Expand Up @@ -183,7 +183,7 @@ public IReadOnlyList<IDataNodeDetailViewModel>? SelectedNodeDetails

private void PlayNote()
{
var midiClient = SharedViewModel.ConnectedDevice;
var midiClient = DeviceViewModel.ConnectedDevice;
if (midiClient is null)
{
return;
Expand Down
4 changes: 3 additions & 1 deletion Drums/VDrumExplorer.ViewModel/Data/KitExplorerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by the Apache License 2.0,
// as found in the LICENSE.txt file.

using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Windows.Input;
Expand Down Expand Up @@ -32,7 +33,8 @@ public int KitCopyTargetNumber
set => SetProperty(ref kitCopyTargetNumber, Kit.Schema.ValidateKitNumber(value));
}

public KitExplorerViewModel(IViewServices viewServices, SharedViewModel shared, Kit kit) : base(viewServices, shared, kit.Data)
public KitExplorerViewModel(IViewServices viewServices, ILogger logger, DeviceViewModel deviceViewModel, Kit kit)
: base(viewServices, logger, deviceViewModel, kit.Data)
{
Kit = kit;
kitCopyTargetNumber = kit.DefaultKitNumber;
Expand Down
13 changes: 7 additions & 6 deletions Drums/VDrumExplorer.ViewModel/Data/ModuleExplorerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by the Apache License 2.0,
// as found in the LICENSE.txt file.

using Microsoft.Extensions.Logging;
using System;
using System.IO;
using System.Windows.Input;
Expand All @@ -15,8 +16,8 @@ public class ModuleExplorerViewModel : DataExplorerViewModel
{
public Module Module { get; }

public ModuleExplorerViewModel(IViewServices viewServices, SharedViewModel shared, Module module)
: base(viewServices, shared, module.Data)
public ModuleExplorerViewModel(IViewServices viewServices, ILogger logger, DeviceViewModel deviceViewModel, Module module)
: base(viewServices, logger, deviceViewModel, module.Data)
{
Module = module;
OpenCopyInKitExplorerCommand = new DelegateCommand<DataTreeNodeViewModel>(OpenCopyInKitExplorer, true);
Expand All @@ -38,7 +39,7 @@ public ModuleExplorerViewModel(IViewServices viewServices, SharedViewModel share
private void OpenCopyInKitExplorer(DataTreeNodeViewModel kitNode)
{
var kit = Module.ExportKit(kitNode.KitNumber!.Value);
var viewModel = new KitExplorerViewModel(ViewServices, SharedViewModel, kit);
var viewModel = new KitExplorerViewModel(ViewServices, Logger, DeviceViewModel, kit);
ViewServices.ShowKitExplorer(viewModel);
}

Expand Down Expand Up @@ -67,18 +68,18 @@ private void ImportKitFromFile(DataTreeNodeViewModel kitNode)
}
catch (Exception ex)
{
Log($"Error loading {file}", ex);
Logger.LogError($"Error loading {file}", ex);
return;
}
if (!(loaded is Kit kit))
{
Log("Loaded file was not a kit");
Logger.LogError("Loaded file was not a kit");
return;
}

if (!kit.Schema.Identifier.Equals(Module.Schema.Identifier))
{
Log($"Kit was from {kit.Schema.Identifier.Name}; this module is {Module.Schema.Identifier.Name}");
Logger.LogError($"Kit was from {kit.Schema.Identifier.Name}; this module is {Module.Schema.Identifier.Name}");
return;
}
Module.ImportKit(kit, kitNode.KitNumber!.Value);
Expand Down
44 changes: 44 additions & 0 deletions Drums/VDrumExplorer.ViewModel/DeviceViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2020 Jon Skeet. All rights reserved.
// Use of this source code is governed by the Apache License 2.0,
// as found in the LICENSE.txt file.

using Microsoft.Extensions.Logging;
using System.Threading.Tasks;
using VDrumExplorer.Midi;
using VDrumExplorer.Model;

namespace VDrumExplorer.ViewModel
{
public sealed class DeviceViewModel : ViewModelBase
{
private ModuleSchema? connectedDeviceSchema;
public ModuleSchema? ConnectedDeviceSchema
{
get => connectedDeviceSchema;
set => SetProperty(ref connectedDeviceSchema, value);
}

private RolandMidiClient? connectedDevice;
public RolandMidiClient? ConnectedDevice
{
get => connectedDevice;
set
{
if (SetProperty(ref connectedDevice, value))
{
RaisePropertyChanged(nameof(DeviceConnected));
}
}
}

public bool DeviceConnected => connectedDevice is object;

public async Task DetectModule(ILogger logger)
{
ConnectedDevice = await MidiDevices.DetectSingleRolandMidiClientAsync(logger, ModuleSchema.KnownSchemas.Keys);
ConnectedDeviceSchema = ConnectedDevice is null ? null : ModuleSchema.KnownSchemas[ConnectedDevice.Identifier].Value;
RaisePropertyChanged(nameof(ConnectedDevice));
RaisePropertyChanged(nameof(ConnectedDeviceSchema));
}
}
}
14 changes: 7 additions & 7 deletions Drums/VDrumExplorer.ViewModel/Dialogs/DataTransferViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Use of this source code is governed by the Apache License 2.0,
// as found in the LICENSE.txt file.

using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Threading;
Expand All @@ -15,8 +16,7 @@ namespace VDrumExplorer.ViewModel.Dialogs
{
public sealed class DataTransferViewModel : ViewModelBase
{
private SharedViewModel shared;

private readonly ILogger logger;
public ICommand CancelCommand { get; }
public string Title { get; }

Expand All @@ -41,9 +41,9 @@ public string CurrentItem
private set => SetProperty(ref currentItem, value);
}

public DataTransferViewModel(SharedViewModel shared, string title)
public DataTransferViewModel(ILogger logger, string title)
{
this.shared = shared;
this.logger = logger;
Title = title;
CancelCommand = new DelegateCommand(Cancel, true);
}
Expand Down Expand Up @@ -96,12 +96,12 @@ private async Task<DataSegment> LoadSegment(RolandMidiClient client, FieldContai
}
catch (OperationCanceledException) when (timerToken.IsCancellationRequested)
{
shared.Log($"Device didn't respond for container {container.Path}; aborting.");
logger.LogError($"Device didn't respond for container {container.Path}; aborting.");
throw;
}
catch
catch (Exception e)
{
shared.Log($"Failure while loading {container.Path}");
logger.LogError($"Failure while loading {container.Path}", e);
throw;
}
}
Expand Down
Loading

0 comments on commit 6716a69

Please sign in to comment.