From dfb671a7ee4912fd0a3dd6fd3a163faa2573ca15 Mon Sep 17 00:00:00 2001 From: Stefan Roelofs Date: Sat, 27 Mar 2021 23:26:33 +0100 Subject: [PATCH] Update views --- UserInterface/Views/AddCommandDialog.axaml | 34 +++--- UserInterface/Views/AddCommandDialog.axaml.cs | 93 +++++++++++----- UserInterface/Views/AddSensorDialog.axaml | 40 +++---- UserInterface/Views/AddSensorDialog.axaml.cs | 76 +++++++++---- UserInterface/Views/CommandSettings.axaml | 2 +- UserInterface/Views/CommandSettings.axaml.cs | 103 ++++++++++-------- UserInterface/Views/SensorSettings.axaml | 2 +- UserInterface/Views/SensorSettings.axaml.cs | 38 ++++--- 8 files changed, 239 insertions(+), 149 deletions(-) diff --git a/UserInterface/Views/AddCommandDialog.axaml b/UserInterface/Views/AddCommandDialog.axaml index dc2e967..30bdcf7 100644 --- a/UserInterface/Views/AddCommandDialog.axaml +++ b/UserInterface/Views/AddCommandDialog.axaml @@ -5,24 +5,26 @@ mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="UserInterface.Views.AddCommandDialog" SizeToContent="WidthAndHeight" - Title="Add command"> - - Command type + Title="Add / edit command"> + + Command type - - - - Name - + + + + Name + - - Command - - Key - - - - + + Command + + Key + + + + + + diff --git a/UserInterface/Views/AddCommandDialog.axaml.cs b/UserInterface/Views/AddCommandDialog.axaml.cs index fbf0974..45005e2 100644 --- a/UserInterface/Views/AddCommandDialog.axaml.cs +++ b/UserInterface/Views/AddCommandDialog.axaml.cs @@ -7,7 +7,6 @@ using hass_workstation_service.Communication.NamedPipe; using JKang.IpcServiceFramework.Client; using Microsoft.Extensions.DependencyInjection; using System; -using System.Dynamic; using System.Linq; using System.Text.Json; using UserInterface.Util; @@ -17,16 +16,25 @@ namespace UserInterface.Views { public class AddCommandDialog : Window { - private readonly IIpcClient client; - public ComboBox comboBox { get; set; } - public ComboBox detectionModecomboBox { get; set; } + private readonly IIpcClient _client; + public ComboBox ComboBox { get; set; } + public ComboBox DetectionModecomboBox { get; set; } + public Guid CommandId { get; } + + public AddCommandDialog(Guid commandId) : this() + { + CommandId = commandId; + GetCommandInfo(CommandId); + Title = "Edit command"; + } + public AddCommandDialog() { - this.InitializeComponent(); + InitializeComponent(); DataContext = new AddCommandViewModel(); - this.comboBox = this.FindControl("ComboBox"); - this.comboBox.Items = Enum.GetValues(typeof(AvailableCommands)).Cast().OrderBy(v => v.ToString()); - this.comboBox.SelectedIndex = 0; + ComboBox = this.FindControl("ComboBox"); + ComboBox.Items = Enum.GetValues(typeof(AvailableCommands)).Cast().OrderBy(v => v.ToString()); + ComboBox.SelectedIndex = 0; // register IPC clients ServiceProvider serviceProvider = new ServiceCollection() @@ -38,22 +46,53 @@ namespace UserInterface.Views .GetRequiredService>(); // create client - this.client = clientFactory.CreateClient("addCommand"); + _client = clientFactory.CreateClient("addCommand"); + Title = "Add sensor"; + + } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } + + private async void GetCommandInfo(Guid commandId) + { + var command = await _client.InvokeAsync(x => x.GetConfiguredCommand(commandId)); + + ComboBox.SelectedItem = command.Type; + FillDefaultValues(); + ComboBox.IsEnabled = false; + var item = (AddCommandViewModel)DataContext; + item.SelectedType = command.Type; + item.Name = command.Name; + //item.UpdateInterval = command.UpdateInterval; + //item.WindowName = + //item.Query = } public async void Save(object sender, RoutedEventArgs args) { - var item = ((AddCommandViewModel)this.DataContext); - dynamic model = new { item.Name, item.Command, item.Key}; + var item = (AddCommandViewModel)DataContext; + dynamic model = new { item.Name, item.Command, item.Key }; string json = JsonSerializer.Serialize(model); - await this.client.InvokeAsync(x => x.AddCommand(item.SelectedType, json)); + if (CommandId == Guid.Empty) + await _client.InvokeAsync(x => x.AddCommand(item.SelectedType, json)); + else + await _client.InvokeAsync(x => x.UpdateCommandById(CommandId, json)); + Close(); } public void ComboBoxClosed(object sender, SelectionChangedEventArgs args) { - var item = ((AddCommandViewModel)this.DataContext); - switch (this.comboBox.SelectedItem) + FillDefaultValues(); + } + + private void FillDefaultValues() + { + var item = (AddCommandViewModel)DataContext; + switch (ComboBox.SelectedItem) { case AvailableCommands.CustomCommand: item.Description = "This command lets you execute any command you want. It will run in a Windows Command Prompt silently. "; @@ -129,28 +168,26 @@ namespace UserInterface.Views break; } } - public void OpenInfo(object sender, RoutedEventArgs args) + + public void OpenInfo(object sender, RoutedEventArgs args) { - var item = ((AddCommandViewModel)this.DataContext); + var item = (AddCommandViewModel)DataContext; BrowserUtil.OpenBrowser(item.MoreInfoLink); } public void Test(object sender, RoutedEventArgs args) { - var item = ((AddCommandViewModel)this.DataContext); + var item = (AddCommandViewModel)DataContext; - System.Diagnostics.Process process = new System.Diagnostics.Process(); - System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); - startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal; - startInfo.FileName = "cmd.exe"; - startInfo.Arguments = $"/k {"echo You won't see this window normally. &&" + item.Command}"; + var process = new System.Diagnostics.Process(); + var startInfo = new System.Diagnostics.ProcessStartInfo + { + WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal, + FileName = "cmd.exe", + Arguments = $"/k {"echo You won't see this window normally. &&" + item.Command}" + }; process.StartInfo = startInfo; process.Start(); } - - private void InitializeComponent() - { - AvaloniaXamlLoader.Load(this); - } } -} +} \ No newline at end of file diff --git a/UserInterface/Views/AddSensorDialog.axaml b/UserInterface/Views/AddSensorDialog.axaml index 10d62ec..8b3b295 100644 --- a/UserInterface/Views/AddSensorDialog.axaml +++ b/UserInterface/Views/AddSensorDialog.axaml @@ -5,27 +5,27 @@ mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="UserInterface.Views.AddSensorDialog" SizeToContent="WidthAndHeight" - Title="Add sensor"> - - Sensor type + Title="Add / edit sensor"> + + Sensor type - - - - Name - + + + + Name + - Update interval - - - - - - Query - - Window name - This is case-insensitive and loosely matched. A window called "Spotify Premium" will match "spotify" or "premium". - + Update interval + + + + + + Query + + Window name + This is case-insensitive and loosely matched. A window called "Spotify Premium" will match "spotify" or "premium". + - + diff --git a/UserInterface/Views/AddSensorDialog.axaml.cs b/UserInterface/Views/AddSensorDialog.axaml.cs index 024e78a..9b5d82f 100644 --- a/UserInterface/Views/AddSensorDialog.axaml.cs +++ b/UserInterface/Views/AddSensorDialog.axaml.cs @@ -7,7 +7,6 @@ using hass_workstation_service.Communication.NamedPipe; using JKang.IpcServiceFramework.Client; using Microsoft.Extensions.DependencyInjection; using System; -using System.Dynamic; using System.Linq; using System.Text.Json; using UserInterface.Util; @@ -17,16 +16,25 @@ namespace UserInterface.Views { public class AddSensorDialog : Window { - private readonly IIpcClient client; - public ComboBox comboBox { get; set; } - public ComboBox detectionModecomboBox { get; set; } + private readonly IIpcClient _client; + public ComboBox ComboBox { get; set; } + public ComboBox DetectionModecomboBox { get; set; } + public Guid SensorId { get; } + + public AddSensorDialog(Guid sensorId) : this() + { + SensorId = sensorId; + GetSensorInfo(SensorId); + Title = "Edit sensor"; + } + public AddSensorDialog() { - this.InitializeComponent(); + InitializeComponent(); DataContext = new AddSensorViewModel(); - this.comboBox = this.FindControl("ComboBox"); - this.comboBox.Items = Enum.GetValues(typeof(AvailableSensors)).Cast().OrderBy(v => v.ToString()); - this.comboBox.SelectedIndex = 0; + ComboBox = this.FindControl("ComboBox"); + ComboBox.Items = Enum.GetValues(typeof(AvailableSensors)).Cast().OrderBy(v => v.ToString()); + ComboBox.SelectedIndex = 0; // register IPC clients ServiceProvider serviceProvider = new ServiceCollection() @@ -38,22 +46,52 @@ namespace UserInterface.Views .GetRequiredService>(); // create client - this.client = clientFactory.CreateClient("addsensor"); + _client = clientFactory.CreateClient("addsensor"); + Title = "Add sensor"; + } + + private void InitializeComponent() + { + AvaloniaXamlLoader.Load(this); + } + + private async void GetSensorInfo(Guid sensorId) + { + var sensor = await _client.InvokeAsync(x => x.GetConfiguredSensor(sensorId)); + + ComboBox.SelectedItem = sensor.Type; + FillDefaultValues(); + ComboBox.IsEnabled = false; + var item = (AddSensorViewModel)DataContext; + item.SelectedType = sensor.Type; + item.Name = sensor.Name; + item.UpdateInterval = sensor.UpdateInterval; + //item.WindowName = + //item.Query = } public async void Save(object sender, RoutedEventArgs args) { - var item = ((AddSensorViewModel)this.DataContext); - dynamic model = new { item.Name, item.Query, item.UpdateInterval, item.WindowName}; + var item = (AddSensorViewModel)DataContext; + dynamic model = new { item.Name, item.Query, item.UpdateInterval, item.WindowName }; string json = JsonSerializer.Serialize(model); - await this.client.InvokeAsync(x => x.AddSensor(item.SelectedType, json)); + if (SensorId == Guid.Empty) + await _client.InvokeAsync(x => x.AddSensor(item.SelectedType, json)); + else + await _client.InvokeAsync(x => x.UpdateSensorById(SensorId, json)); + Close(); } public void ComboBoxClosed(object sender, SelectionChangedEventArgs args) { - var item = ((AddSensorViewModel)this.DataContext); - switch (this.comboBox.SelectedItem) + FillDefaultValues(); + } + + private void FillDefaultValues() + { + var item = (AddSensorViewModel)DataContext; + switch (ComboBox.SelectedItem) { case AvailableSensors.UserNotificationStateSensor: item.Description = "This sensor watches the UserNotificationState. This is normally used in applications to determine if it is appropriate to send a notification but we can use it to expose this state. \n "; @@ -181,15 +219,11 @@ namespace UserInterface.Views break; } } + public void OpenInfo(object sender, RoutedEventArgs args) { - var item = ((AddSensorViewModel)this.DataContext); + var item = (AddSensorViewModel)DataContext; BrowserUtil.OpenBrowser(item.MoreInfoLink); } - - private void InitializeComponent() - { - AvaloniaXamlLoader.Load(this); - } } -} +} \ No newline at end of file diff --git a/UserInterface/Views/CommandSettings.axaml b/UserInterface/Views/CommandSettings.axaml index 6998808..0bc4aac 100644 --- a/UserInterface/Views/CommandSettings.axaml +++ b/UserInterface/Views/CommandSettings.axaml @@ -22,7 +22,7 @@