Multiprocotol Terminalprogram (BAT)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MultiTerm/MultiTerm.Protocols/ProtocolSettingsViewModel.cs

62 lines
2.2 KiB

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MultiTerm.Protocols.Types;
namespace MultiTerm.Protocols;
/// <summary>
/// Class that represents all connection and usage settings for a specific <see cref="CommunicationProtocol"/>.
/// Can be bound to UI using MVVM pattern.
/// </summary>
public abstract partial class ProtocolSettingsViewModel : ObservableObject, IProtocolSettingsViewModel
{
private const string connectedStateButtonText = "Disconnect";
private const string disconnectedStateButtonText = "Connect";
public event EventHandler? ConnectRequested;
public event EventHandler? DisconnectRequested;
public abstract ProtocolType ProtocolType { get; }
/// <summary>
/// Indicates wether the settings can currently be edited.
/// Initially are editable
/// </summary>
public bool AreEditable = true;
[ObservableProperty]
private string connectDisconnectButtonText = disconnectedStateButtonText;
/// <summary>
/// Binding to Connect/Disconnect button.
/// Button text is provided in <see cref="ConnectDisconnectButtonText"/>
/// </summary>
[RelayCommand(AllowConcurrentExecutions = false)]
private async Task ConnectDisconnectAsync()
{
await Task.Factory.StartNew(() =>
{
// if currently disconnected
if (this.ConnectDisconnectButtonText == disconnectedStateButtonText)
{
// CONNECT
this.AreEditable = false;
this.ConnectRequested?.Invoke(this, EventArgs.Empty);
this.ConnectDisconnectButtonText = connectedStateButtonText;
}
// if currently connected
else if (this.ConnectDisconnectButtonText == connectedStateButtonText)
{
// DISCONNECT
this.DisconnectRequested?.Invoke(this, EventArgs.Empty);
this.ConnectDisconnectButtonText = disconnectedStateButtonText;
this.AreEditable = true;
}
else
{
throw new Exception($"'{nameof(ConnectDisconnectAsync)}()' has not recognized button text, cannot identify state of connection.");
}
});
}
}