Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add payload etc. to persisted state for publish items #52

Merged
merged 1 commit into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Source/Controls/ErrorBox/ErrorBox.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ void OnButtonCloseClicked(object? sender, RoutedEventArgs e)

void OnButtonCopyClicked(object? sender, RoutedEventArgs e)
{
_ = Application.Current.Clipboard.SetTextAsync(Message);
_ = Application.Current?.Clipboard?.SetTextAsync(Message);
}
}
2 changes: 1 addition & 1 deletion Source/Controls/UserProperties/UserPropertiesView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Orientation="Horizontal">
<Button Classes="tool_bar_button"
ToolTip.Tip="Add user property"
Command="{Binding AddItem}">
Command="{Binding AddEmptyItem}">
<PathIcon Data="{StaticResource add_square_regular}" />
</Button>
<Button Classes="tool_bar_button"
Expand Down
15 changes: 13 additions & 2 deletions Source/Controls/UserProperties/UserPropertiesViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,20 @@ public bool IsReadOnly

public ObservableCollection<UserPropertyViewModel> Items { get; } = new();

public void AddItem()
public void AddEmptyItem()
{
Items.Add(new UserPropertyViewModel(this));
AddItem(string.Empty, string.Empty);
}

public void AddItem(string name, string value)
{
var item = new UserPropertyViewModel(this)
{
Name = name,
Value = value
};

Items.Add(item);
}

public void ClearItems()
Expand Down
2 changes: 1 addition & 1 deletion Source/Pages/Connection/ConnectionItemViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public ConnectionItemViewModel(ConnectionPageViewModel ownerPage)
{
OwnerPage = ownerPage ?? throw new ArgumentNullException(nameof(ownerPage));

SessionOptions.UserProperties.AddItem();
SessionOptions.UserProperties.AddEmptyItem();
}

public string Name
Expand Down
10 changes: 10 additions & 0 deletions Source/Pages/Publish/PayloadInputFormat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace MQTTnetApp.Pages.Publish;

public enum PayloadInputFormat
{
PlainText,

Base64String,

FilePath
}
37 changes: 36 additions & 1 deletion Source/Pages/Publish/PayloadInputFormatSelectorViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ public sealed class PayloadInputFormatSelectorViewModel : BaseViewModel
{
bool _isBase64String;
bool _isFilePath;
bool _isPlainText = true;
bool _isPlainText;

public PayloadInputFormatSelectorViewModel()
{
Value = PayloadInputFormat.PlainText;
}

public bool IsBase64String
{
Expand All @@ -30,6 +35,36 @@ public bool IsPlainText
set => this.RaiseAndSetIfChanged(ref _isPlainText, value);
}

public PayloadInputFormat Value
{
get
{
if (IsPlainText)
{
return PayloadInputFormat.PlainText;
}

if (IsBase64String)
{
return PayloadInputFormat.Base64String;
}

if (IsFilePath)
{
return PayloadInputFormat.FilePath;
}

throw new NotSupportedException();
}

set
{
IsPlainText = value == PayloadInputFormat.PlainText;
IsBase64String = value == PayloadInputFormat.Base64String;
IsFilePath = value == PayloadInputFormat.FilePath;
}
}

public byte[] ConvertPayloadInput(string? payloadInput)
{
if (string.IsNullOrEmpty(payloadInput))
Expand Down
2 changes: 1 addition & 1 deletion Source/Pages/Publish/PublishPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void AddItem()

// Prepare the UI with at lest one user property.
// It will not be send when the name is empty.
newItem.UserProperties.AddItem();
newItem.UserProperties.AddEmptyItem();

Items.Collection.Add(newItem);
Items.SelectedItem = newItem;
Expand Down
24 changes: 21 additions & 3 deletions Source/Pages/Publish/State/PublishPageStateFactory.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using MQTTnetApp.Services.State.Model;

namespace MQTTnetApp.Pages.Publish.State;

Expand All @@ -15,15 +16,32 @@ public static PublishPageState Create(PublishPageViewModel viewModel)

foreach (var item in viewModel.Items.Collection)
{
state.Publishes.Add(new PublishState
var publishState = new PublishState
{
Name = item.Name,
Topic = item.Topic,
Retain = item.Retain,
ContentType = item.ContentType,
ResponseTopic = item.ResponseTopic,
SubscriptionIdentifier = item.SubscriptionIdentifier,
TopicAlias = item.TopicAlias,
MessageExpiryInterval = item.MessageExpiryInterval,
PayloadFormatIndicator = item.PayloadFormatIndicator.Value,
QualityOfServiceLevel = item.QualityOfServiceLevel.Value
});
QualityOfServiceLevel = item.QualityOfServiceLevel.Value,
PayloadInputFormat = item.PayloadInputFormat.Value,
Payload = item.Payload
};

foreach (var userProperty in item.UserProperties.Items)
{
publishState.UserProperties.Add(new UserProperty
{
Name = userProperty.Name,
Value = userProperty.Value
});
}

state.Publishes.Add(publishState);
}

return state;
Expand Down
22 changes: 20 additions & 2 deletions Source/Pages/Publish/State/PublishPageStateLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,33 @@ static PublishItemViewModel CreateItem(PublishPageViewModel ownerPage, PublishSt
var item = new PublishItemViewModel(ownerPage)
{
Name = publishState.Name ?? string.Empty,
Topic = publishState.Topic,
Topic = publishState.Topic ?? string.Empty,
Retain = publishState.Retain,
ContentType = publishState.ContentType,
ContentType = publishState.ContentType ?? string.Empty,
ResponseTopic = publishState.ResponseTopic ?? string.Empty,
SubscriptionIdentifier = publishState.SubscriptionIdentifier,
TopicAlias = publishState.TopicAlias,
MessageExpiryInterval = publishState.MessageExpiryInterval,
Payload = publishState.Payload ?? string.Empty,
PayloadFormatIndicator =
{
Value = publishState.PayloadFormatIndicator
},
QualityOfServiceLevel =
{
Value = publishState.QualityOfServiceLevel
},
PayloadInputFormat =
{
Value = publishState.PayloadInputFormat
}
};

foreach (var userProperty in publishState.UserProperties)
{
item.UserProperties.AddItem(userProperty.Name ?? string.Empty, userProperty.Value ?? string.Empty);
}

return item;
}
}
16 changes: 16 additions & 0 deletions Source/Pages/Publish/State/PublishState.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
using System.Collections.Generic;
using MQTTnet.Protocol;
using MQTTnetApp.Services.State.Model;

namespace MQTTnetApp.Pages.Publish.State;

public sealed class PublishState
{
public string? ContentType { get; set; }

public uint MessageExpiryInterval { get; set; }

public string? Name { get; set; }

public string? Payload { get; set; }

public MqttPayloadFormatIndicator PayloadFormatIndicator { get; set; }

public PayloadInputFormat PayloadInputFormat { get; set; }

public MqttQualityOfServiceLevel QualityOfServiceLevel { get; set; }

public string? ResponseTopic { get; set; }

public bool Retain { get; set; }

public uint SubscriptionIdentifier { get; set; }

public string? Topic { get; set; }

public ushort TopicAlias { get; set; }

public List<UserProperty> UserProperties { get; set; } = new();
}
2 changes: 1 addition & 1 deletion Source/Pages/Subscriptions/SubscriptionsPageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public void AddItem()
Name = "Untitled"
};

newItem.UserProperties.AddItem();
newItem.UserProperties.AddEmptyItem();

Items.Collection.Add(newItem);
}
Expand Down
8 changes: 8 additions & 0 deletions Source/Services/State/Model/UserProperty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace MQTTnetApp.Services.State.Model;

public sealed class UserProperty
{
public string? Name { get; set; }

public string? Value { get; set; }
}