Skip to content

Commit

Permalink
Migrate plugin to use CommunityToolkit.Mvvm.Input for RelayCommands
Browse files Browse the repository at this point in the history
  • Loading branch information
daleghent committed Mar 1, 2024
1 parent 11c5098 commit 84a470a
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 35 deletions.
32 changes: 10 additions & 22 deletions GroundStation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This Source Code Form is subject to the terms of the Mozilla Public

#endregion "copyright"

using CommunityToolkit.Mvvm.Input;
using DaleGhent.NINA.GroundStation.Mqtt;
using DaleGhent.NINA.GroundStation.TTS;
using DaleGhent.NINA.GroundStation.Utilities;
Expand All @@ -30,12 +31,11 @@ This Source Code Form is subject to the terms of the Mozilla Public
using System.Security;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;

namespace DaleGhent.NINA.GroundStation {

[Export(typeof(IPluginManifest))]
public class GroundStation : PluginBase, ISettings, INotifyPropertyChanged {
public partial class GroundStation : PluginBase, ISettings, INotifyPropertyChanged {
private MqttClient mqttClient;

[ImportingConstructor]
Expand All @@ -45,16 +45,6 @@ public GroundStation() {
Properties.Settings.Default.UpgradeSettings = false;
CoreUtil.SaveSettings(Properties.Settings.Default);
}

PushoverTestCommand = new AsyncCommand<bool>(PushoverTest);
EmailTestCommand = new AsyncCommand<bool>(EmailTest);
TelegramTestCommand = new AsyncCommand<bool>(TelegramTest);
MQTTTestCommand = new AsyncCommand<bool>(MQTTTest);
IFTTTTestCommand = new AsyncCommand<bool>(IFTTTTest);
TtsTestCommand = new AsyncCommand<bool>(TtsTest);

SelectDefaultSoundFileCommand = new RelayCommand(OpenSelectDefaultSoundFileDialog);
SelectDefaultFailureSoundFileCommand = new RelayCommand(OpenSelectDefaultFailureSoundFileDialog);
}

public override Task Initialize() {
Expand All @@ -75,6 +65,7 @@ public override async Task Teardown() {
return;
}

[RelayCommand]
private async Task<bool> PushoverTest(object arg) {
var send = new SendToPushover.SendToPushover() {
Message = "Test Message",
Expand All @@ -93,6 +84,7 @@ private async Task<bool> PushoverTest(object arg) {
}
}

[RelayCommand]
private async Task<bool> EmailTest(object arg) {
var send = new SendToEmail.SendToEmail() {
Subject = "Test Subject",
Expand All @@ -116,6 +108,7 @@ private async Task<bool> EmailTest(object arg) {
}
}

[RelayCommand]
private async Task<bool> TelegramTest(object arg) {
var send = new SendToTelegram.SendToTelegram() {
Message = "Test Message",
Expand All @@ -133,6 +126,7 @@ private async Task<bool> TelegramTest(object arg) {
}
}

[RelayCommand]
private async Task<bool> MQTTTest(object arg) {
var send = new SendToMqtt.SendToMqtt() {
Topic = "Test Topic",
Expand All @@ -151,6 +145,7 @@ private async Task<bool> MQTTTest(object arg) {
}
}

[RelayCommand]
private async Task<bool> IFTTTTest(object arg) {
var send = new SendToIftttWebhook.SendToIftttWebhook() {
Value1 = "Test Value1",
Expand All @@ -170,6 +165,7 @@ private async Task<bool> IFTTTTest(object arg) {
}
}

[RelayCommand]
private async Task<bool> TtsTest(object arg) {
var send = new SendToTTS() {
Message = TtsTestMessage,
Expand Down Expand Up @@ -217,13 +213,6 @@ private async Task LwtStopWorker() {
return;
}

public IAsyncCommand PushoverTestCommand { get; }
public IAsyncCommand EmailTestCommand { get; }
public IAsyncCommand IFTTTTestCommand { get; }
public IAsyncCommand TelegramTestCommand { get; }
public IAsyncCommand MQTTTestCommand { get; }
public IAsyncCommand TtsTestCommand { get; }

public string IFTTTWebhookKey {
get => Security.Decrypt(Properties.Settings.Default.IFTTTWebhookKey);
set {
Expand Down Expand Up @@ -680,6 +669,7 @@ protected void RaisePropertyChanged([CallerMemberName] string propertyName = nul
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

[RelayCommand]
internal void OpenSelectDefaultSoundFileDialog(object obj) {
Microsoft.Win32.OpenFileDialog dialog = new() {
FileName = string.Empty,
Expand All @@ -691,6 +681,7 @@ internal void OpenSelectDefaultSoundFileDialog(object obj) {
}
}

[RelayCommand]
internal void OpenSelectDefaultFailureSoundFileDialog(object obj) {
Microsoft.Win32.OpenFileDialog dialog = new() {
FileName = string.Empty,
Expand All @@ -701,8 +692,5 @@ internal void OpenSelectDefaultFailureSoundFileDialog(object obj) {
PlaySoundDefaultFailureFile = dialog.FileName;
}
}

public ICommand SelectDefaultSoundFileCommand { get; private set; }
public ICommand SelectDefaultFailureSoundFileCommand { get; private set; }
}
}
4 changes: 2 additions & 2 deletions Options.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Command="{Binding SelectDefaultSoundFileCommand}">
Command="{Binding OpenSelectDefaultSoundFileDialogCommand}">
<Button.Content>
<TextBlock Foreground="{StaticResource ButtonForegroundBrush}" Text="..." />
</Button.Content>
Expand Down Expand Up @@ -988,7 +988,7 @@
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Command="{Binding SelectDefaultFailureSoundFileCommand}">
Command="{Binding OpenSelectDefaultFailureSoundFileDialogCommand}">
<Button.Content>
<TextBlock Foreground="{StaticResource ButtonForegroundBrush}" Text="..." />
</Button.Content>
Expand Down
8 changes: 3 additions & 5 deletions PlaySound/PlaySound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This Source Code Form is subject to the terms of the Mozilla Public

#endregion "copyright"

using CommunityToolkit.Mvvm.Input;
using NetCoreAudio;
using Newtonsoft.Json;
using NINA.Core.Model;
Expand All @@ -22,7 +23,6 @@ This Source Code Form is subject to the terms of the Mozilla Public
using System.IO;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;

namespace DaleGhent.NINA.GroundStation.PlaySound {

Expand All @@ -32,14 +32,13 @@ namespace DaleGhent.NINA.GroundStation.PlaySound {
[ExportMetadata("Category", "Ground Station")]
[Export(typeof(ISequenceItem))]
[JsonObject(MemberSerialization.OptIn)]
public class PlaySound : SequenceItem, IValidatable {
public partial class PlaySound : SequenceItem, IValidatable {
private string soundFile = string.Empty;
private bool waitUntilFinished = true;

[ImportingConstructor]
public PlaySound() {
SoundFile = Properties.Settings.Default.PlaySoundDefaultFile;
SelectSoundFileCommand = new RelayCommand(OpenSelectSoundFileDialog);

Validate();
}
Expand Down Expand Up @@ -118,6 +117,7 @@ public override string ToString() {
return $"Category: {Category}, Item: {nameof(PlaySound)}, SoundFile: {soundFile}, WaitUntilFinished: {WaitUntilFinished}";
}

[RelayCommand]
internal void OpenSelectSoundFileDialog(object obj) {
Microsoft.Win32.OpenFileDialog dialog = new() {
FileName = string.Empty,
Expand All @@ -128,7 +128,5 @@ internal void OpenSelectSoundFileDialog(object obj) {
SoundFile = dialog.FileName;
}
}

public ICommand SelectSoundFileCommand { get; private set; }
}
}
7 changes: 3 additions & 4 deletions PlaySound/PlaySoundOnFailureTrigger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ This Source Code Form is subject to the terms of the Mozilla Public

#endregion "copyright"

using CommunityToolkit.Mvvm.Input;
using NetCoreAudio;
using Newtonsoft.Json;
using NINA.Core.Enum;
Expand Down Expand Up @@ -37,14 +38,13 @@ namespace DaleGhent.NINA.GroundStation.PlaySoundOnFailureTrigger {
[ExportMetadata("Category", "Ground Station")]
[Export(typeof(ISequenceTrigger))]
[JsonObject(MemberSerialization.OptIn)]
public class PlaySoundOnFailureTrigger : SequenceTrigger, IValidatable {
public partial class PlaySoundOnFailureTrigger : SequenceTrigger, IValidatable {
private string soundFile = string.Empty;
private ISequenceRootContainer failureHook;

[ImportingConstructor]
public PlaySoundOnFailureTrigger() {
SoundFile = Properties.Settings.Default.PlaySoundDefaultFailureFile;
SelectSoundFileCommand = new RelayCommand(OpenSelectSoundFileDialog);

Validate();
}
Expand Down Expand Up @@ -157,6 +157,7 @@ public override string ToString() {
return $"Category: {Category}, Item: {nameof(PlaySoundOnFailureTrigger)}, SoundFile: {soundFile}";
}

[RelayCommand]
internal void OpenSelectSoundFileDialog(object obj) {
Microsoft.Win32.OpenFileDialog dialog = new() {
FileName = string.Empty,
Expand All @@ -167,7 +168,5 @@ internal void OpenSelectSoundFileDialog(object obj) {
SoundFile = dialog.FileName;
}
}

public ICommand SelectSoundFileCommand { get; private set; }
}
}
2 changes: 1 addition & 1 deletion PlaySound/PlaySoundOnFailureTriggerTemplate.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Command="{Binding SelectSoundFileCommand}">
Command="{Binding OpenSelectSoundFileDialogCommand}">
<Button.Content>
<TextBlock Foreground="{StaticResource ButtonForegroundBrush}" Text="..." />
</Button.Content>
Expand Down
2 changes: 1 addition & 1 deletion PlaySound/PlaySoundTemplate.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
Margin="5"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Command="{Binding SelectSoundFileCommand}">
Command="{Binding OpenSelectSoundFileDialogCommand}">
<Button.Content>
<TextBlock Foreground="{StaticResource ButtonForegroundBrush}" Text="..." />
</Button.Content>
Expand Down

0 comments on commit 84a470a

Please sign in to comment.