Skip to content

Commit

Permalink
Migrate MVVM light to MVVM Toolkit (#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
torbacz authored Apr 10, 2023
1 parent f8d3139 commit 840f7bf
Show file tree
Hide file tree
Showing 25 changed files with 1,952 additions and 173 deletions.
28 changes: 19 additions & 9 deletions VisualStudio.Extension-2019/AutomaticUpdates/UpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@
// See LICENSE file in the project root for full license information.
//

using GalaSoft.MvvmLight.Messaging;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.VisualStudio.Shell;
using nanoFramework.Tools.Debugger;
using nanoFramework.Tools.VisualStudio.Extension.FirmwareUpdate;
using nanoFramework.Tools.VisualStudio.Extension.Messages;
using nanoFramework.Tools.VisualStudio.Extension.ToolWindow.ViewModel;
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Task = System.Threading.Tasks.Task;

namespace nanoFramework.Tools.VisualStudio.Extension.AutomaticUpdates
{
public class UpdateManager
public class UpdateManager : IRecipient<NanoDeviceIsConnectedMessage>, IRecipient<NanoDeviceHasDepartedMessage>
{
private static UpdateManager s_instance;
private ViewModelLocator ViewModelLocator;
Expand All @@ -32,16 +32,26 @@ private UpdateManager(Package package)
}

public static void Initialize(
AsyncPackage package,
AsyncPackage package,
ViewModelLocator vmLocator)
{
s_instance = new UpdateManager(package)
{
ViewModelLocator = vmLocator
};

Messenger.Default.Register<NotificationMessage>(s_instance, DeviceExplorerViewModel.MessagingTokens.LaunchFirmwareUpdateForNanoDevice, (message) => s_instance.LaunchUpdate(message.Notification));
Messenger.Default.Register<NotificationMessage>(s_instance, DeviceExplorerViewModel.MessagingTokens.NanoDeviceHasDeparted, (message) => s_instance.ProcessNanoDeviceDeparture(message.Notification));
WeakReferenceMessenger.Default.Register<NanoDeviceIsConnectedMessage>(s_instance);
WeakReferenceMessenger.Default.Register<NanoDeviceHasDepartedMessage>(s_instance);
}

public void Receive(NanoDeviceHasDepartedMessage message)
{
s_instance.ProcessNanoDeviceDeparture(message.DeviceId);
}

public void Receive(NanoDeviceIsConnectedMessage message)
{
s_instance.LaunchUpdate(message.DeviceId);
}

private void ProcessNanoDeviceDeparture(string deviceId)
Expand Down Expand Up @@ -159,7 +169,7 @@ private void LaunchUpdate(string deviceId)
Version currentClrVersion = null;

// try to store CLR version
if(nanoDevice.DebugEngine.IsConnectedTonanoCLR)
if (nanoDevice.DebugEngine.IsConnectedTonanoCLR)
{
if (nanoDevice.DeviceInfo.Valid)
{
Expand Down Expand Up @@ -206,7 +216,7 @@ private void LaunchUpdate(string deviceId)
}

// check if the device is still there
if(ViewModelLocator.DeviceExplorer.AvailableDevices.FirstOrDefault(d => d.DeviceUniqueId == deviceUniqueId) == null)
if (ViewModelLocator.DeviceExplorer.AvailableDevices.FirstOrDefault(d => d.DeviceUniqueId == deviceUniqueId) == null)
{
#if DEBUG
Console.WriteLine($"[Automatic Updates] {nanoDevice.TargetName} is not available anymore.");
Expand Down Expand Up @@ -351,7 +361,7 @@ private void LaunchUpdate(string deviceId)
}

internal static async Task<FirmwarePackage> GetFirmwarePackageAsync(
string targetName,
string targetName,
string platformName)
{
if (platformName.StartsWith("STM32"))
Expand Down
5 changes: 2 additions & 3 deletions VisualStudio.Extension-2019/NanoFrameworkPackage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
// See LICENSE file in the project root for full license information.
//

using GalaSoft.MvvmLight.Ioc;
using CommunityToolkit.Mvvm.DependencyInjection;
using Microsoft;
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.ProjectSystem.VS;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;
Expand Down Expand Up @@ -534,7 +533,7 @@ protected override async Task InitializeAsync(CancellationToken cancellationToke
Application.Current.Resources.Add("Locator", viewModelLocator);
}

SimpleIoc.Default.GetInstance<DeviceExplorerViewModel>().Package = this;
Ioc.Default.GetRequiredService<DeviceExplorerViewModel>().Package = this;

await MessageCentre.InitializeAsync(this, ".NET nanoFramework Extension");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@

namespace nanoFramework.Tools.VisualStudio.Extension
{
using GalaSoft.MvvmLight.Messaging;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.VisualStudio.Shell;
using nanoFramework.Tools.Debugger;
using nanoFramework.Tools.VisualStudio.Extension.Messages;
using nanoFramework.Tools.VisualStudio.Extension.ToolWindow.ViewModel;
using System;
using System.Windows.Controls;

/// <summary>
/// Interaction logic for DeviceExplorerControl.
/// </summary>
public partial class DeviceExplorerControl : UserControl
public partial class DeviceExplorerControl : UserControl, IRecipient<ForceSelectionOfNanoDeviceMessage>
{
// strongly-typed view models enable x:bind
public DeviceExplorerViewModel ViewModel => DataContext as DeviceExplorerViewModel;
Expand All @@ -30,7 +31,7 @@ public DeviceExplorerControl()
Loaded += DeviceExplorerControl_Loaded;

deviceTreeView.SelectedItemChanged += DevicesTreeView_SelectedItemChanged;
Messenger.Default.Register<NotificationMessage>(this, DeviceExplorerViewModel.MessagingTokens.ForceSelectionOfNanoDevice, (message) => ForceSelectionOfNanoDeviceHandlerAsync().ConfigureAwait(false));
WeakReferenceMessenger.Default.Register<ForceSelectionOfNanoDeviceMessage>(this);
}

private void DeviceExplorerControl_Loaded(object sender, System.Windows.RoutedEventArgs e)
Expand Down Expand Up @@ -120,14 +121,19 @@ private async System.Threading.Tasks.Task ForceSelectionOfNanoDeviceHandlerAsync
}
while (tryCount-- > 0);

Messenger.Default.Send(new NotificationMessage(""), DeviceExplorerViewModel.MessagingTokens.SelectedNanoDeviceHasChanged);
WeakReferenceMessenger.Default.Send(new SelectedNanoDeviceHasChangedMessage());

// force redrawing to show selection
deviceTreeView.InvalidateVisual();
deviceTreeView.UpdateLayout();
deviceTreeView.InvalidateVisual();
}

public void Receive(ForceSelectionOfNanoDeviceMessage message)
{
ForceSelectionOfNanoDeviceHandlerAsync().ConfigureAwait(false);
}

#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@

namespace nanoFramework.Tools.VisualStudio.Extension
{
using GalaSoft.MvvmLight.Messaging;
using CommunityToolkit.Mvvm.Messaging;
using Microsoft.VisualStudio.PlatformUI;
using Microsoft.VisualStudio.Shell;
using nanoFramework.Tools.VisualStudio.Extension.ToolWindow.ViewModel;
using nanoFramework.Tools.VisualStudio.Extension.Messages;
using System.Collections.Generic;
using System.Net;
using System.Windows.Controls.Primitives;
using System.Windows.Forms;


/// <summary>
/// Interaction logic for DeviceExplorerControl.
/// </summary>
public partial class SettingsDialog : DialogWindow
public partial class SettingsDialog : DialogWindow, IRecipient<VirtualDeviceOperationExecutingMessage>
{
private const string _stopVirtualDeviceLabel = "Stop Virtual Device";
private const string _startVirtualDeviceLabel = "Start Virtual Device";
Expand All @@ -41,7 +42,7 @@ public SettingsDialog()
// init controls
private void InitControls()
{
Messenger.Default.Register<NotificationMessage>(this, DeviceExplorerViewModel.MessagingTokens.VirtualDeviceOperationExecuting, (message) => this.UpdateStartStopAvailabilityAsync(message.Notification).ConfigureAwait(false));
WeakReferenceMessenger.Default.Register<VirtualDeviceOperationExecutingMessage>(this);

// set controls according to stored preferences
GenerateDeploymentImage.IsChecked = NanoFrameworkPackage.SettingGenerateDeploymentImage;
Expand Down Expand Up @@ -95,11 +96,11 @@ private void InitControls()
CloseButton.Focus();
}

private async System.Threading.Tasks.Task UpdateStartStopAvailabilityAsync(string installCompleted)
private async System.Threading.Tasks.Task UpdateStartStopAvailabilityAsync(bool installCompleted)
{
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

StartStopDevice.IsEnabled = !bool.Parse(installCompleted)
StartStopDevice.IsEnabled = !installCompleted
&& NanoFrameworkPackage.VirtualDeviceService.NanoClrIsInstalled
&& NanoFrameworkPackage.VirtualDeviceService.CanStartVirtualDevice;
}
Expand Down Expand Up @@ -266,5 +267,10 @@ private void AutoUpdateNanoClrImage_Checked(object sender, System.Windows.Routed
// save new state
NanoFrameworkPackage.SettingVirtualDeviceAutoUpdateNanoClrImage = (sender as ToggleButton).IsChecked ?? false;
}

public void Receive(VirtualDeviceOperationExecutingMessage message)
{
UpdateStartStopAvailabilityAsync(message.InstallCompleted).ConfigureAwait(false);
}
}
}
11 changes: 7 additions & 4 deletions VisualStudio.Extension-2019/VisualStudio.Extension-vs2019.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,9 @@
<PackageReference Include="CliWrap">
<Version>3.5.0</Version>
</PackageReference>
<PackageReference Include="CommunityToolkit.Mvvm">
<Version>8.1.0</Version>
</PackageReference>
<PackageReference Include="Extended.Wpf.Toolkit">
<Version>4.3.0</Version>
</PackageReference>
Expand Down Expand Up @@ -508,6 +511,9 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.DependencyInjection">
<Version>7.0.0</Version>
</PackageReference>
<PackageReference Include="Microsoft.VisualStudio.Composition">
<Version>16.9.20</Version>
</PackageReference>
Expand All @@ -532,9 +538,6 @@
<PackageReference Include="Microsoft.Win32.Registry">
<Version>5.0.0</Version>
</PackageReference>
<PackageReference Include="MvvmLightLibsStd10">
<Version>5.4.1.1</Version>
</PackageReference>
<PackageReference Include="nanoFramework.CoreLibrary" GeneratePathProperty="true">
<Version>1.14.2</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down Expand Up @@ -641,4 +644,4 @@
<Target Name="BeforeBuild">
</Target>
-->
</Project>
</Project>
Loading

0 comments on commit 840f7bf

Please sign in to comment.