From 7061568cda484fb7735c240ddf97b3de95e18239 Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Thu, 4 May 2023 09:32:30 +0200 Subject: [PATCH] Implemented MultiFormatDataView for SentData, separated Display settings in SendReceiveView implemented Send Data Textbox not cleared when sending was not successful, worked on formatting, removed selector in MultiFormatDataView, cleanup --- .../ViewModel/SendReceiveViewModel.cs | 20 ++---- MultiTerm.Core/ViewModel/TerminalViewModel.cs | 7 +- MultiTerm.Protocols/CommunicationProtocol.cs | 19 +++-- MultiTerm.Protocols/ICommunicationProtocol.cs | 3 +- .../ExtendedTabControl.xaml | 4 +- .../MultiFormatDataView.cs | 71 +------------------ .../MultiFormatDataView.xaml | 23 ++---- .../MultiFormatTextBox.xaml | 29 +------- MultiTerm.Wpf/MainWindow.xaml | 3 +- MultiTerm.Wpf/View/SendReceiveView.xaml | 56 +++++++++++---- .../View/SettingsView/SerialSettingsView.xaml | 16 +++-- 11 files changed, 92 insertions(+), 159 deletions(-) diff --git a/MultiTerm.Core/ViewModel/SendReceiveViewModel.cs b/MultiTerm.Core/ViewModel/SendReceiveViewModel.cs index 4a37290..6f30c42 100644 --- a/MultiTerm.Core/ViewModel/SendReceiveViewModel.cs +++ b/MultiTerm.Core/ViewModel/SendReceiveViewModel.cs @@ -5,7 +5,6 @@ using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Messaging; using MultiTerm.Core.Model; using MultiTerm.Core.Types; -using System.Diagnostics; namespace MultiTerm.Core.ViewModel; @@ -19,12 +18,6 @@ public partial class SendReceiveViewModel : TerminalViewModel [ObservableProperty] private MultiFormatString sendableData = new(); - /// - /// Temporary sent data property, for testing purposes. - /// - [ObservableProperty] - private string tempSentDataString = string.Empty; - /// /// Constructor. /// @@ -37,14 +30,13 @@ public partial class SendReceiveViewModel : TerminalViewModel [RelayCommand] private void Send() { - // Temp - //var items = this.CommunicationData.SelectedReceivedData; - //Debugger.Break(); - this.TempSentDataString = this.SendableData.ToAsciiEncodedString(); // send data - this.SendToCommunicationProtocol(this.SendableData.GetBytes()); + bool successfullySent = this.SendToCommunicationProtocol(this.SendableData.GetBytes()); - // clear textbox - this.SendableData.Clear(); + if (successfullySent) + { + // clear textbox + this.SendableData.Clear(); + } } } diff --git a/MultiTerm.Core/ViewModel/TerminalViewModel.cs b/MultiTerm.Core/ViewModel/TerminalViewModel.cs index 0b25d36..ee584c6 100644 --- a/MultiTerm.Core/ViewModel/TerminalViewModel.cs +++ b/MultiTerm.Core/ViewModel/TerminalViewModel.cs @@ -155,7 +155,8 @@ public abstract partial class TerminalViewModel : ObservableObject, ITerminalVie /// Appends configured to the end of string. /// /// data in form of enumerable (e.g. array) - protected void SendToCommunicationProtocol(IEnumerable data) + /// when data could be sent successfully + protected bool SendToCommunicationProtocol(IEnumerable data) { // guard null values if(this.CommunicationProtocol == null) { throw new NullReferenceException($"'{nameof(SendToCommunicationProtocol)}()' was called but {nameof(CommunicationProtocol)} is null."); } @@ -164,7 +165,7 @@ public abstract partial class TerminalViewModel : ObservableObject, ITerminalVie if (this.CommunicationProtocol.IsConnected == false) { this.messenger.Send(new ProtocolNotConnectedMessage("Cannot send message")); - return; + return false; } // add newline sequence to end of data @@ -182,7 +183,7 @@ public abstract partial class TerminalViewModel : ObservableObject, ITerminalVie var dataWithNewlineSequence = data.Concat(newlineSequence); // send - this.CommunicationProtocol.SendBytes(dataWithNewlineSequence.ToArray()); + return this.CommunicationProtocol.SendBytes(dataWithNewlineSequence.ToArray()); } #region Protocol Settings diff --git a/MultiTerm.Protocols/CommunicationProtocol.cs b/MultiTerm.Protocols/CommunicationProtocol.cs index 2d383af..50a065f 100644 --- a/MultiTerm.Protocols/CommunicationProtocol.cs +++ b/MultiTerm.Protocols/CommunicationProtocol.cs @@ -66,17 +66,28 @@ public abstract class CommunicationProtocol : ICommunicationProtocol /// true if the data was sent successfully protected abstract bool InternalSendBytes(byte[] bytes); - public void SendBytes(byte[] bytes) + public bool SendBytes(byte[] bytes) { + bool success = true; + // guard is not connected => log warning. user of this function shall only use SendBytes if IsConnected is true - if (this.IsConnected == false) { this.logger.LogWarn($"'{nameof(SendBytes)}()' was reached with {nameof(IsConnected)} being false", nameof(CommunicationProtocol)); } + if (this.IsConnected == false) + { + this.logger.LogWarn($"'{nameof(SendBytes)}()' was reached with {nameof(IsConnected)} being false", nameof(CommunicationProtocol)); + return false; // return and do not send + } - // send bytes and if the sending was cancelled report error - if (this.InternalSendBytes(bytes) == false) + // send bytes + success = this.InternalSendBytes(bytes); + + // if the sending was cancelled report error + if (success == false) { this.logger.LogError($"'{nameof(SendBytes)}()' failed to send during {nameof(InternalSendBytes)}.", nameof(CommunicationProtocol)); this.messenger.Send(new GenericUserInterfaceMessage("Failed to send message", MessageImportance.High)); } + + return success; } /// diff --git a/MultiTerm.Protocols/ICommunicationProtocol.cs b/MultiTerm.Protocols/ICommunicationProtocol.cs index 6e32808..58aa487 100644 --- a/MultiTerm.Protocols/ICommunicationProtocol.cs +++ b/MultiTerm.Protocols/ICommunicationProtocol.cs @@ -57,5 +57,6 @@ public interface ICommunicationProtocol /// Send data to the connected device. /// /// data to send, as an array of bytes - void SendBytes(byte[] bytes); + /// true if the sending was successful + bool SendBytes(byte[] bytes); } diff --git a/MultiTerm.Wpf.CustomControl/ExtendedTabControl/ExtendedTabControl.xaml b/MultiTerm.Wpf.CustomControl/ExtendedTabControl/ExtendedTabControl.xaml index 0c6b29e..c93fdcd 100644 --- a/MultiTerm.Wpf.CustomControl/ExtendedTabControl/ExtendedTabControl.xaml +++ b/MultiTerm.Wpf.CustomControl/ExtendedTabControl/ExtendedTabControl.xaml @@ -26,7 +26,9 @@ - + diff --git a/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs b/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs index e82a44d..1b85a03 100644 --- a/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs +++ b/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.cs @@ -17,8 +17,7 @@ public class MultiFormatDataView : Control { private static readonly Dictionary itemParentPairs = new(); private const string itemsControlTemplateName = "PART_ItemsControl"; - private const string buttonClearTemplateName = "btnClear"; - private const string selectorTemplateName = "comboBoxSelector"; + private const string buttonClearTemplateName = "PART_ButtonClear"; private const string textBoxCharOnlyViewTemplateName = "PART_CharOnlyTextBox"; private ListBox? itemsControl; private TextBox? tbCharOnlyView; @@ -30,21 +29,6 @@ public class MultiFormatDataView : Control typeof(ICommunicationDataViewModel), typeof(MultiFormatDataView), new PropertyMetadata(null, OnDataSourcePropertyChanged)); - public static readonly DependencyProperty SelectorItemsSourceProperty = - DependencyProperty.Register("SelectorItemsSource", - typeof(IEnumerable), typeof(MultiFormatDataView), - new PropertyMetadata(null, OnSelectorItemsSourceChanged)); - - public static readonly DependencyProperty SelectorSelectedItemProperty = - DependencyProperty.Register("SelectorSelectedItem", - typeof(object), typeof(MultiFormatDataView), - new PropertyMetadata(null, OnSelectorSelectedItemChanged)); - - public static readonly DependencyProperty SelectorDescriptionProperty = - DependencyProperty.Register("SelectorDescription", - typeof(string), typeof(MultiFormatDataView), - new PropertyMetadata(string.Empty, OnSelectorDescriptionChanged)); - public static readonly DependencyProperty RealizedItemsCountProperty = DependencyProperty.Register("RealizedItemsCount", typeof(uint), typeof(MultiFormatDataView), @@ -75,36 +59,6 @@ public class MultiFormatDataView : Control set { SetValue(DataSourceProperty, value); } } - /// - /// .NET Property for SelectorItemsSource. - /// - [Bindable(true)] - public IEnumerable SelectorItemsSource - { - get { return (IEnumerable)GetValue(SelectorItemsSourceProperty); } - set { SetValue(SelectorItemsSourceProperty, value); } - } - - /// - /// .NET Property for SelectorSelectedItem. - /// - [Bindable(true)] - public string SelectorSelectedItem - { - get { return (string)GetValue(SelectorSelectedItemProperty); } - set { SetValue(SelectorSelectedItemProperty, value); } - } - - /// - /// .NET Property for SelectorDescription. - /// - [Bindable(true)] - public string SelectorDescription - { - get { return (string)GetValue(SelectorDescriptionProperty); } - set { SetValue(SelectorDescriptionProperty, value); } - } - /// /// .NET Property for RealizedItemsCount. /// @@ -312,29 +266,6 @@ public class MultiFormatDataView : Control } #endregion - #region Selector (ComboBox) handling - private static void OnSelectorItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - // extract instance and guard null - //if (d is not MultiFormatDataView mfdv) { return; } - // extract instance of new Value and guard null - //if (e.NewValue is not IEnumerable enumerable) { return; } - - // nothing to do - } - - private static void OnSelectorDescriptionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - // nothing to do - } - - private static void OnSelectorSelectedItemChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) - { - // nothing to do - } - - #endregion - #region Realized Item Count private static void OnRealizedItemsCountChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { diff --git a/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.xaml b/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.xaml index 45193bf..0b1ffeb 100644 --- a/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.xaml +++ b/MultiTerm.Wpf.CustomControl/MultiFormatDataView/MultiFormatDataView.xaml @@ -128,22 +128,13 @@ - + - - - + + + -