Skip to content

Commit

Permalink
refer to constant instead of literal for Empty CatalogActionItem; don…
Browse files Browse the repository at this point in the history
…'t serialize out Empty action item;
  • Loading branch information
joekolodz committed Apr 9, 2022
1 parent 6e9ebd6 commit 047f981
Show file tree
Hide file tree
Showing 9 changed files with 54 additions and 17 deletions.
42 changes: 40 additions & 2 deletions src/CustomJsonConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,27 @@ public override bool CanConvert(Type objectType)
return objectType == typeof(HOTASButton) ||
objectType == typeof(HOTASAxis) ||
objectType == typeof(ButtonAction) ||
objectType == typeof(ActionCatalogItem) ||
objectType == typeof(IHOTASDevice);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (objectType == typeof(ActionCatalogItem))
{
var item = new ActionCatalogItem();
try
{
serializer.Populate(reader, item);
}
catch (Exception e)
{
Logging.Log.Error(e, "Failed to deserialize an ActionCatalogItem");
throw;
}
return item;
}

if (objectType == typeof(IHOTASDevice))
{
var device = new HOTASDevice();
Expand Down Expand Up @@ -126,7 +142,7 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
if (button.Type == HOTASButton.ButtonType.Button && string.CompareOrdinal(button.MapName, JoystickOffsetValues.GetName(button.MapId)) != 0 ||
button.Type == HOTASButton.ButtonType.POV && string.CompareOrdinal(button.MapName, JoystickOffsetValues.GetPOVName(button.MapId)) != 0 ||
button.ActionCatalogItem?.Actions?.Count > 0 ||
button.ShiftModePage >0 ||
button.ShiftModePage > 0 ||
button.IsShift)
{
SerializeButton(writer, serializer, button);
Expand All @@ -143,6 +159,12 @@ public override void WriteJson(JsonWriter writer, object value, JsonSerializer s
return;
}

if (value is ActionCatalogItem item)
{
if (item.Id == Guid.Empty) return;
SerializeCatalogActionItem(writer, serializer, item);
}

if (!(value is Dictionary<int, ObservableCollection<IHotasBaseMap>>)) return;

serializer.Serialize(writer, value);
Expand All @@ -160,7 +182,7 @@ private static void SerializeButton(JsonWriter writer, JsonSerializer serializer

if (prop.Name == nameof(button.ShiftModePage) && (int)prop.GetValue(button) == 0) continue;
if (prop.Name == nameof(button.IsShift) && (bool)prop.GetValue(button) == false) continue;

writer.WritePropertyName(prop.Name);
serializer.Serialize(writer, value);
}
Expand Down Expand Up @@ -207,6 +229,22 @@ private static void SerializeButtonAction(JsonWriter writer, JsonSerializer seri
}
writer.WriteEndObject();
}

private static void SerializeCatalogActionItem(JsonWriter writer, JsonSerializer serializer, ActionCatalogItem item)
{
var propList = typeof(ActionCatalogItem).GetProperties();
writer.WriteStartObject();
foreach (var prop in propList)
{
if (prop.GetCustomAttributes(true).Any(x => x is JsonIgnoreAttribute)) continue;

var value = prop.GetValue(item);

writer.WritePropertyName(prop.Name);
serializer.Serialize(writer, value);
}
writer.WriteEndObject();
}
}
}

3 changes: 1 addition & 2 deletions src/Models/ActionCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public void Add(ActionCatalogItem item, string buttonName)

public ActionCatalogItem Get(string actionName)
{
if (!Contains(actionName)) return null;
return Catalog.First(x => x.ActionName == actionName);
return Catalog.FirstOrDefault(x => x.ActionName == actionName);
}

private void AddEmptyItem()
Expand Down
2 changes: 1 addition & 1 deletion src/Models/ActionCatalogItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace SierraHOTAS.Models
public class ActionCatalogItem : INotifyPropertyChanged
{
private string _actionName;
private const string NO_ACTION_TEXT = "<No Action>";
public const string NO_ACTION_TEXT = "<No Action>";

public Guid Id { get; set; }

Expand Down
2 changes: 1 addition & 1 deletion src/Models/HOTASAxis.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ private void ClearUnassignedActions(ObservableCollection<HOTASButton> map)
{
foreach (var b in map)
{
if (b.ActionName != "<No Action>") continue;
if (b.ActionName != ActionCatalogItem.NO_ACTION_TEXT) continue;
b.ActionName = string.Empty;
b.ActionCatalogItem.Actions.Clear();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Models/HOTASButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ private void RecordKeypress(Keyboard.KeystrokeEventArgs e)

public void ClearUnassignedActions()
{
if (ActionName != "<No Action>") return;
if (ActionName != ActionCatalogItem.NO_ACTION_TEXT) return;
ActionName = string.Empty;
ActionCatalogItem.Actions.Clear();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ActionCatalogViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ActionCatalogViewModelTests
public void new_catalog_then_noActionItem_exists()
{
var catalog = new ActionCatalog();
Assert.True(catalog.Contains("<No Action>"));
Assert.True(catalog.Contains(ActionCatalogItem.NO_ACTION_TEXT));
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion tests/DeviceViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ public void clear_button_map()
Type = HOTASButton.ButtonType.Button,
ActionCatalogItem = new ActionCatalogItem()
{ Actions = new ObservableCollection<ButtonAction>() { new ButtonAction() } },
ActionName = "<No Action>"
ActionName = ActionCatalogItem.NO_ACTION_TEXT
};
list.Add(testButton);

Expand Down
4 changes: 2 additions & 2 deletions tests/HOTASAxisMapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -434,11 +434,11 @@ public void get_button_from_raw_value_backward_directional_not_multi_action()
public void clear_unassigned_actions()
{
var map = new HOTASAxis();
map.ButtonMap.Add(new HOTASButton() { ActionCatalogItem = new ActionCatalogItem() { ActionName = "<No Action>", Actions = new ObservableCollection<ButtonAction>() { new ButtonAction(), new ButtonAction() } } });
map.ButtonMap.Add(new HOTASButton() { ActionCatalogItem = new ActionCatalogItem() { ActionName = ActionCatalogItem.NO_ACTION_TEXT, Actions = new ObservableCollection<ButtonAction>() { new ButtonAction(), new ButtonAction() } } });
map.ButtonMap.Add(new HOTASButton() { ActionCatalogItem = new ActionCatalogItem() { ActionName = "item 1", Actions = new ObservableCollection<ButtonAction>() { new ButtonAction(), new ButtonAction() } } });
map.ButtonMap.Add(new HOTASButton() { ActionCatalogItem = new ActionCatalogItem() { ActionName = "item 2", Actions = new ObservableCollection<ButtonAction>() { new ButtonAction(), new ButtonAction() } } });

map.ReverseButtonMap.Add(new HOTASButton() { ActionCatalogItem = new ActionCatalogItem() { ActionName = "<No Action>", Actions = new ObservableCollection<ButtonAction>() { new ButtonAction(), new ButtonAction() } } });
map.ReverseButtonMap.Add(new HOTASButton() { ActionCatalogItem = new ActionCatalogItem() { ActionName = ActionCatalogItem.NO_ACTION_TEXT, Actions = new ObservableCollection<ButtonAction>() { new ButtonAction(), new ButtonAction() } } });
map.ReverseButtonMap.Add(new HOTASButton() { ActionCatalogItem = new ActionCatalogItem() { ActionName = "item 1", Actions = new ObservableCollection<ButtonAction>() { new ButtonAction(), new ButtonAction() } } });
map.ReverseButtonMap.Add(new HOTASButton() { ActionCatalogItem = new ActionCatalogItem() { ActionName = "item 2", Actions = new ObservableCollection<ButtonAction>() { new ButtonAction(), new ButtonAction() } } });

Expand Down
12 changes: 6 additions & 6 deletions tests/HOTASCollectionViewModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ public void file_open_command_valid_file()

//check action catalog is rebuilt with the button loaded from file
Assert.Equal(2, hotasVm.ActionCatalog.Catalog.Count);
Assert.Contains(hotasVm.ActionCatalog.Catalog, item => item.ActionName == "<No Action>");
Assert.Contains(hotasVm.ActionCatalog.Catalog, item => item.ActionName == ActionCatalogItem.NO_ACTION_TEXT);
Assert.Contains(hotasVm.ActionCatalog.Catalog, item => item.ActionName == "Release");

subDeviceList.Received().Stop();
Expand Down Expand Up @@ -603,7 +603,7 @@ public void file_open_command_valid_file_existing_device()

//check action catalog is rebuilt with the button loaded from file
Assert.Equal(2, hotasVm.ActionCatalog.Catalog.Count);
Assert.Contains(hotasVm.ActionCatalog.Catalog, item => item.ActionName == "<No Action>");
Assert.Contains(hotasVm.ActionCatalog.Catalog, item => item.ActionName == ActionCatalogItem.NO_ACTION_TEXT);
Assert.Contains(hotasVm.ActionCatalog.Catalog, item => item.ActionName == "Release");

subDeviceList.Received().Stop();
Expand Down Expand Up @@ -637,7 +637,7 @@ public void file_open_command_null_file()
Assert.NotEqual(loadedButtonMapId, hotasVm.Devices[0].ButtonMap[3].ButtonId);

Assert.Single(hotasVm.ActionCatalog.Catalog);
Assert.Equal("<No Action>", hotasVm.ActionCatalog.Catalog[0].ActionName);
Assert.Equal(ActionCatalogItem.NO_ACTION_TEXT, hotasVm.ActionCatalog.Catalog[0].ActionName);

subDeviceList.DidNotReceive().Stop();
subFileSystem.Received().FileOpenDialog();
Expand Down Expand Up @@ -665,7 +665,7 @@ public void clear_active_profile_set_command()
existingDevice.DeviceId = deviceGuid;
existingDevice.Name = "existing device";
AddHotasButtonMap(existingDevice.ButtonMap, existingButtonMapId);
AddHotasButtonMap(existingDevice.ButtonMap, 0, HOTASButton.ButtonType.Button, "Button2 - remove my actions", "<No Action>", 1);
AddHotasButtonMap(existingDevice.ButtonMap, 0, HOTASButton.ButtonType.Button, "Button2 - remove my actions", ActionCatalogItem.NO_ACTION_TEXT, 1);
subDeviceList.ModeProfileActivationButtons.Add(1, new ModeActivationItem() { ButtonId = modeActivationButtonId, DeviceId = deviceGuid });

subFileSystem.FileOpenDialog().Returns((HOTASCollection)null);
Expand Down Expand Up @@ -1169,7 +1169,7 @@ public void load_profile_add_button_list_to_catalog()

var hotasVm = CreateHotasCollectionViewModel(out var subFileSystem, out var subQuickProfilePanelVm);
Assert.Single(hotasVm.ActionCatalog.Catalog);
Assert.Equal("<No Action>", hotasVm.ActionCatalog.Catalog[0].ActionName);
Assert.Equal(ActionCatalogItem.NO_ACTION_TEXT, hotasVm.ActionCatalog.Catalog[0].ActionName);

var subHotasCollection = CreateHotasCollectionSubstitute();

Expand Down Expand Up @@ -1209,7 +1209,7 @@ public void load_profile_add_button_list_to_catalog()
hotasVm.Initialize();

Assert.Equal(3, hotasVm.ActionCatalog.Catalog.Count);
Assert.Equal("<No Action>", hotasVm.ActionCatalog.Catalog[0].ActionName);
Assert.Equal(ActionCatalogItem.NO_ACTION_TEXT, hotasVm.ActionCatalog.Catalog[0].ActionName);
Assert.Equal("action1", hotasVm.ActionCatalog.Catalog[1].ActionName);
Assert.Equal("action2", hotasVm.ActionCatalog.Catalog[2].ActionName);
}
Expand Down

0 comments on commit 047f981

Please sign in to comment.