Skip to content

Commit

Permalink
Separated Gui for sending a message in an own control
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandKoenig committed Dec 3, 2020
1 parent aeedc1c commit 478e1bd
Show file tree
Hide file tree
Showing 9 changed files with 194 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace MessageCommunicator.TestGui.Tests
{
[TestClass]
public class ConnectionProfileViewModelTests
public class SendMessageViewModelTests
{
[TestMethod]
public void SendPlainMessage()
Expand All @@ -28,7 +28,8 @@ public void SendPlainMessage()
sentMessage = (string?)callInfo.Arguments[0];
});

var testObject = new ConnectionProfileViewModel(connProfile);
var testObject = new SendMessageViewModel();
testObject.CurrentConnectionProfile = connProfile;
testObject.SendFormattingMode = SendFormattingMode.Plain;
testObject.Command_SendMessage.Execute("DummyMessage \\\\");

Expand All @@ -50,7 +51,8 @@ public void SendEscapedMessage()
sentMessage = (string?)callInfo.Arguments[0];
});

var testObject = new ConnectionProfileViewModel(connProfile);
var testObject = new SendMessageViewModel();
testObject.CurrentConnectionProfile = connProfile;
testObject.SendFormattingMode = SendFormattingMode.Escaped;
testObject.Command_SendMessage.Execute("DummyMessage \\\\");

Expand All @@ -72,7 +74,8 @@ public void SendHexMessage()
sentMessage = (string?)callInfo.Arguments[0];
});

var testObject = new ConnectionProfileViewModel(connProfile);
var testObject = new SendMessageViewModel();
testObject.CurrentConnectionProfile = connProfile;
testObject.SendFormattingMode = SendFormattingMode.BinaryHex;
testObject.Command_SendMessage.Execute("41 42 43");

Expand Down
7 changes: 6 additions & 1 deletion MessageCommunicator.TestGui/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,12 @@
</StackPanel>
</HeaderedContentControl.Header>

<localViews:ConnectionProfileView DataContext="{Binding #LstProfiles.SelectedItem}" />
<DockPanel LastChildFill="True">
<localViews:SendMessageView DockPanel.Dock="Bottom"
DataContext="{Binding Path=SendMessageVM}" />
<localViews:ConnectionProfileView DataContext="{Binding Path=SelectedProfile}" />
</DockPanel>

</HeaderedContentControl>
</Grid>

Expand Down
5 changes: 5 additions & 0 deletions MessageCommunicator.TestGui/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ public ConnectionProfileViewModel? SelectedProfile
if (_selectedProfile != value)
{
_selectedProfile = value;
this.SendMessageVM.CurrentConnectionProfile = _selectedProfile?.Model;
this.RaisePropertyChanged(nameof(this.SelectedProfile));
this.RaisePropertyChanged(nameof(this.IsProfileScreenEnabled));
}
}
}

public SendMessageViewModel SendMessageVM { get; }

public bool IsProfileScreenEnabled => _selectedProfile != null;

public ReactiveCommand<object?, Unit> Command_ImportProfiles { get; }
Expand All @@ -56,6 +59,8 @@ public MainWindowViewModel()
this.Command_EditProfile = ReactiveCommand.CreateFromTask<object?>(this.EditProfileAsync);
this.Command_DeleteProfile = ReactiveCommand.CreateFromTask<object?>(this.DeleteSelectedProfile);

this.SendMessageVM = new SendMessageViewModel();

var syncContext = SynchronizationContext.Current;
if (syncContext == null) { return; }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<None Remove="MessageCommunicator.TestGui.csproj.DotSettings" />
<None Remove="ViewServices\_ExportDialog\ExportDialogControl.xaml" />
<None Remove="ViewServices\_ImportDialog\ImportDialogControl.xaml" />
<None Remove="Views\_SendMessage\SendMessageView.xaml" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MessageCommunicator\MessageCommunicator.csproj" />
Expand All @@ -65,6 +66,9 @@
<AvaloniaResource Update="ViewServices\_ImportDialog\ImportDialogControl.xaml">
<Generator>MSBuild:Compile</Generator>
</AvaloniaResource>
<AvaloniaResource Update="Views\_SendMessage\SendMessageView.xaml">
<Generator>MSBuild:Compile</Generator>
</AvaloniaResource>
</ItemGroup>
<ItemGroup>
<Compile Update="ViewServices\_ExportDialog\ExportDialogControl.xaml.cs">
Expand All @@ -73,5 +77,8 @@
<Compile Update="ViewServices\_ImportDialog\ImportDialogControl.xaml.cs">
<DependentUpon>%(Filename)</DependentUpon>
</Compile>
<Compile Update="Views\_SendMessage\SendMessageView.xaml.cs">
<DependentUpon>%(Filename)</DependentUpon>
</Compile>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,48 +7,30 @@
mc:Ignorable="d"
d:Width="400" d:Height="400"
x:Class="MessageCommunicator.TestGui.Views.ConnectionProfileView">
<DockPanel LastChildFill="True">
<DockPanel DockPanel.Dock="Bottom"
LastChildFill="True">
<Button DockPanel.Dock="Right"
Content="Send" Width="150"
IsDefault="True"
Margin="3"
Command="{Binding Path=Command_SendMessage}"
CommandParameter="{Binding #TxtSendMessage.Text}" />
<ComboBox DockPanel.Dock="Right"
Width="75" Margin="3"
SelectedItem="{Binding Path=SendFormattingMode}"
Items="{Binding Path=SendFormattingModeList}" />
<TextBox Name="TxtSendMessage"
Margin="3" />
</DockPanel>

<TabControl>
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Messages (" />
<TextBlock Text="{Binding Path=MessageLoggingViewModel.Logging.Count, FallbackValue=0}" />
<TextBlock Text=")" />
</StackPanel>
</TabItem.Header>

<local:LoggingView DataContext="{Binding Path=MessageLoggingViewModel}"
IsTypeColumnVisible="false" />
</TabItem>
<TabControl>
<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Messages (" />
<TextBlock Text="{Binding Path=MessageLoggingViewModel.Logging.Count, FallbackValue=0}" />
<TextBlock Text=")" />
</StackPanel>
</TabItem.Header>

<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Details (" />
<TextBlock Text="{Binding Path=DetailLoggingViewModel.Logging.Count, FallbackValue=0}" />
<TextBlock Text=")" />
</StackPanel>
</TabItem.Header>

<local:LoggingView DataContext="{Binding Path=DetailLoggingViewModel}" />
</TabItem>
</TabControl>
</DockPanel>
<local:LoggingView DataContext="{Binding Path=MessageLoggingViewModel}"
IsTypeColumnVisible="false" />
</TabItem>

<TabItem>
<TabItem.Header>
<StackPanel Orientation="Horizontal">
<TextBlock Text="Details (" />
<TextBlock Text="{Binding Path=DetailLoggingViewModel.Logging.Count, FallbackValue=0}" />
<TextBlock Text=")" />
</StackPanel>
</TabItem.Header>

<local:LoggingView DataContext="{Binding Path=DetailLoggingViewModel}" />
</TabItem>
</TabControl>
</UserControl>
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ public class ConnectionProfileViewModel : OwnViewModelBase

public ReactiveCommand<object?, Unit> Command_Stop { get; }

public ReactiveCommand<string?, Unit> Command_SendMessage { get; }

public bool IsRunning
{
get => _isRunning;
Expand All @@ -41,10 +39,6 @@ public bool IsRunning

public bool CanStop => this.IsRunning;

public SendFormattingMode SendFormattingMode { get; set; }

public SendFormattingMode[] SendFormattingModeList => (SendFormattingMode[])Enum.GetValues(typeof(SendFormattingMode));

public ConnectionState State
{
get => _connState;
Expand Down Expand Up @@ -97,37 +91,6 @@ public ConnectionProfileViewModel(IConnectionProfile connProfile)
await this.Model.StopAsync();
}
});
this.Command_SendMessage = ReactiveCommand.CreateFromTask<string?>(async message =>
{
try
{
message ??= string.Empty;

switch (this.SendFormattingMode)
{
case SendFormattingMode.Plain:
break;

case SendFormattingMode.Escaped:
message = Regex.Unescape(message);
break;

case SendFormattingMode.BinaryHex:
var encoding = Encoding.GetEncoding(this.Model.Parameters.RecognizerSettings.Encoding);
message = encoding.GetString(HexFormatUtil.ToByteArray(message));
break;

default:
throw new InvalidOperationException($"Unhandled {nameof(Views.SendFormattingMode)} {this.SendFormattingMode}!");
}

await this.Model.SendMessageAsync(message);
}
catch (Exception e)
{
CommonErrorHandling.Current.ShowErrorDialog(e);
}
});
}

public void RefreshData()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<UserControl xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MessageCommunicator.TestGui.Views;assembly=MessageCommunicator.TestGui"
xmlns:localRoot="clr-namespace:MessageCommunicator.TestGui;assembly=MessageCommunicator.TestGui"
mc:Ignorable="d"
d:Width="400" d:Height="400"
x:Class="MessageCommunicator.TestGui.Views.SendMessageView">
<DockPanel LastChildFill="True">
<Button DockPanel.Dock="Right"
Content="Send" Width="150"
IsDefault="True"
Margin="3"
Command="{Binding Path=Command_SendMessage}"
CommandParameter="{Binding #TxtSendMessage.Text}" />
<ComboBox DockPanel.Dock="Right"
Width="75" Margin="3"
SelectedItem="{Binding Path=SendFormattingMode}"
Items="{Binding Path=SendFormattingModeList}" />
<TextBox Name="TxtSendMessage"
Margin="3" />
</DockPanel>
</UserControl>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Avalonia.Controls;
using Avalonia.Markup.Xaml;

namespace MessageCommunicator.TestGui.Views
{
public class SendMessageView : OwnUserControl<SendMessageViewModel>
{
public SendMessageView()
{
AvaloniaXamlLoader.Load(this);
}
}
}
107 changes: 107 additions & 0 deletions MessageCommunicator.TestGui/Views/_SendMessage/SendMessageViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections.Generic;
using System.Reactive;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using MessageCommunicator.TestGui.Logic;
using MessageCommunicator.Util;
using ReactiveUI;

namespace MessageCommunicator.TestGui.Views
{
public class SendMessageViewModel : OwnViewModelBase
{
private IConnectionProfile? _currentConnectionProfile;
private SendFormattingMode _sendFormattingMode;
private string _currentMessage;

public IConnectionProfile? CurrentConnectionProfile
{
get => _currentConnectionProfile;
set
{
if (_currentConnectionProfile != value)
{
_currentConnectionProfile = value;
this.RaisePropertyChanged();
}
}
}

public SendFormattingMode SendFormattingMode
{
get => _sendFormattingMode;
set
{
if (_sendFormattingMode != value)
{
_sendFormattingMode = value;
this.RaisePropertyChanged();
}
}
}

public string CurrentMessage
{
get => _currentMessage;
set
{
if (_currentMessage != value)
{
_currentMessage = value;
this.RaisePropertyChanged();
}
}
}

public SendFormattingMode[] SendFormattingModeList => (SendFormattingMode[])Enum.GetValues(typeof(SendFormattingMode));

public ReactiveCommand<string?, Unit> Command_SendMessage { get; }

public SendMessageViewModel()
{
_sendFormattingMode = SendFormattingMode.Plain;
_currentMessage = string.Empty;

this.Command_SendMessage = ReactiveCommand.CreateFromTask<string?>(
this.OnCommand_SendMessage_Execute);
}

private async Task OnCommand_SendMessage_Execute(string? message)
{
if (_currentConnectionProfile == null) { return; }
var connProfile = _currentConnectionProfile;

try
{
message ??= string.Empty;

switch (this.SendFormattingMode)
{
case SendFormattingMode.Plain:
break;

case SendFormattingMode.Escaped:
message = Regex.Unescape(message);
break;

case SendFormattingMode.BinaryHex:
var encoding = Encoding.GetEncoding(connProfile.Parameters.RecognizerSettings.Encoding);
message = encoding.GetString(HexFormatUtil.ToByteArray(message));
break;

default:
throw new InvalidOperationException(
$"Unhandled {nameof(Views.SendFormattingMode)} {this.SendFormattingMode}!");
}

await connProfile.SendMessageAsync(message);
}
catch (Exception e)
{
CommonErrorHandling.Current.ShowErrorDialog(e);
}
}
}
}

0 comments on commit 478e1bd

Please sign in to comment.