-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separated Gui for sending a message in an own control
- Loading branch information
1 parent
aeedc1c
commit 478e1bd
Showing
9 changed files
with
194 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
MessageCommunicator.TestGui/Views/_SendMessage/SendMessageView.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
13 changes: 13 additions & 0 deletions
13
MessageCommunicator.TestGui/Views/_SendMessage/SendMessageView.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
107
MessageCommunicator.TestGui/Views/_SendMessage/SendMessageViewModel.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |