Fixed #1: Settings for Analog Outputs are not remembered

And a few other null-related fixes
This commit is contained in:
Mark van Renswoude 2021-03-08 20:46:18 +01:00
parent df57d665bf
commit 217013d4cc
2 changed files with 26 additions and 23 deletions

View File

@ -132,7 +132,7 @@ namespace MassiveKnob.Core
return null; return null;
if (list[index]?.ActionInfo.Info == action) if (list[index]?.ActionInfo.Info == action)
return list[index].ActionInfo; return list[index]?.ActionInfo;
list[index]?.ActionInfo.Instance?.Dispose(); list[index]?.ActionInfo.Instance?.Dispose();
@ -175,7 +175,7 @@ namespace MassiveKnob.Core
if (analogOutputIndex >= settingsList.Count) if (analogOutputIndex >= settingsList.Count)
return new MassiveKnobSettings.DigitalToAnalogSettings(); return new MassiveKnobSettings.DigitalToAnalogSettings();
return settingsList[analogOutputIndex].DigitalToAnalog?.Clone() ?? new MassiveKnobSettings.DigitalToAnalogSettings(); return settingsList[analogOutputIndex]?.DigitalToAnalog?.Clone() ?? new MassiveKnobSettings.DigitalToAnalogSettings();
} }
} }
@ -320,18 +320,18 @@ namespace MassiveKnob.Core
} }
protected T GetActionSettings<T>(IMassiveKnobActionContext context, IMassiveKnobAction action, int index) where T : class, new() protected T GetActionSettings<T>(IMassiveKnobActionContext context, MassiveKnobActionType actionType, IMassiveKnobAction action, int index) where T : class, new()
{ {
lock (settingsLock) lock (settingsLock)
{ {
var list = GetActionMappingList(action.ActionType); var list = GetActionMappingList(actionType);
if (index >= list.Count) if (index >= list.Count)
return new T(); return new T();
if (list[index]?.Context != context) if (list[index]?.Context != context)
throw new InvalidOperationException("Caller must be the active action to retrieve the settings"); throw new InvalidOperationException("Caller must be the active action to retrieve the settings");
var settingsList = GetActionSettingsList(action.ActionType); var settingsList = GetActionSettingsList(actionType);
if (index >= settingsList.Count) if (index >= settingsList.Count)
return new T(); return new T();
@ -340,18 +340,18 @@ namespace MassiveKnob.Core
} }
protected void SetActionSettings<T>(IMassiveKnobActionContext context, IMassiveKnobAction action, int index, T actionSettings) where T : class, new() protected void SetActionSettings<T>(IMassiveKnobActionContext context, MassiveKnobActionType actionType, IMassiveKnobAction action, int index, T actionSettings) where T : class, new()
{ {
lock (settingsLock) lock (settingsLock)
{ {
var list = GetActionMappingList(action.ActionType); var list = GetActionMappingList(actionType);
if (index >= list.Count) if (index >= list.Count)
return; return;
if (list[index]?.Context != context) if (list[index]?.Context != context)
throw new InvalidOperationException("Caller must be the active action to retrieve the settings"); throw new InvalidOperationException("Caller must be the active action to retrieve the settings");
var settingsList = GetActionSettingsList(action.ActionType); var settingsList = GetActionSettingsList(actionType);
while (index >= settingsList.Count) while (index >= settingsList.Count)
settingsList.Add(null); settingsList.Add(null);
@ -752,13 +752,13 @@ namespace MassiveKnob.Core
public T GetSettings<T>() where T : class, new() public T GetSettings<T>() where T : class, new()
{ {
return owner.GetActionSettings<T>(this, action, index); return owner.GetActionSettings<T>(this, assignedActionType, action, index);
} }
public void SetSettings<T>(T settings) where T : class, new() public void SetSettings<T>(T settings) where T : class, new()
{ {
owner.SetActionSettings(this, action, index, settings); owner.SetActionSettings(this, assignedActionType, action, index, settings);
} }

View File

@ -26,7 +26,7 @@ namespace MassiveKnob.ViewModel
public string DisplayName => string.Format( public string DisplayName => string.Format(
actionType == MassiveKnobActionType.OutputAnalog || actionType == MassiveKnobActionType.OutputDigital actionType == MassiveKnobActionType.OutputAnalog || actionType == MassiveKnobActionType.OutputDigital
? Strings.OutputHeader ? Strings.OutputHeader
: Strings.InputHeader, : Strings.InputHeader,
index + 1); index + 1);
public IList<ActionViewModel> Actions { get; } public IList<ActionViewModel> Actions { get; }
@ -40,7 +40,7 @@ namespace MassiveKnob.ViewModel
if (value == selectedAction) if (value == selectedAction)
return; return;
selectedAction = value == null || value.RepresentsNull ? null : value; selectedAction = value == null || value.RepresentsNull ? Actions.Single(a => a.RepresentsNull) : value;
var actionInfo = orchestrator.SetAction(actionType, index, selectedAction?.Action); var actionInfo = orchestrator.SetAction(actionType, index, selectedAction?.Action);
OnPropertyChanged(); OnPropertyChanged();
@ -60,7 +60,7 @@ namespace MassiveKnob.ViewModel
if (actionSettingsControl is IDisposable disposable) if (actionSettingsControl is IDisposable disposable)
disposable.Dispose(); disposable.Dispose();
actionSettingsControl = value; actionSettingsControl = value;
OnPropertyChanged(); OnPropertyChanged();
} }
@ -91,6 +91,7 @@ namespace MassiveKnob.ViewModel
private readonly IDisposable digitalToAnalogChangedSubscription; private readonly IDisposable digitalToAnalogChangedSubscription;
private byte digitalToAnalogOn; private byte digitalToAnalogOn;
public byte DigitalToAnalogOn public byte DigitalToAnalogOn
{ {
get => digitalToAnalogOn; get => digitalToAnalogOn;
@ -107,6 +108,7 @@ namespace MassiveKnob.ViewModel
private byte digitalToAnalogOff; private byte digitalToAnalogOff;
public byte DigitalToAnalogOff public byte DigitalToAnalogOff
{ {
get => digitalToAnalogOff; get => digitalToAnalogOff;
@ -123,13 +125,14 @@ namespace MassiveKnob.ViewModel
// ReSharper restore UnusedMember.Global // ReSharper restore UnusedMember.Global
public InputOutputViewModel(SettingsViewModel settingsViewModel, IMassiveKnobOrchestrator orchestrator, MassiveKnobActionType actionType, int index) public InputOutputViewModel(SettingsViewModel settingsViewModel, IMassiveKnobOrchestrator orchestrator,
MassiveKnobActionType actionType, int index)
{ {
this.orchestrator = orchestrator; this.orchestrator = orchestrator;
this.actionType = actionType; this.actionType = actionType;
this.index = index; this.index = index;
// For design-time support // For design-time support
if (orchestrator == null) if (orchestrator == null)
{ {
@ -142,7 +145,7 @@ namespace MassiveKnob.ViewModel
{ {
if (actionViewModel.RepresentsNull) if (actionViewModel.RepresentsNull)
return true; return true;
if (actionViewModel.Action.ActionType == actionType) if (actionViewModel.Action.ActionType == actionType)
return true; return true;
@ -150,12 +153,12 @@ namespace MassiveKnob.ViewModel
return actionType == MassiveKnobActionType.OutputAnalog && return actionType == MassiveKnobActionType.OutputAnalog &&
actionViewModel.Action.ActionType == MassiveKnobActionType.OutputDigital; actionViewModel.Action.ActionType == MassiveKnobActionType.OutputDigital;
} }
Actions = settingsViewModel.Actions.Where(AllowAction).ToList(); Actions = settingsViewModel.Actions.Where(AllowAction).ToList();
var actionInfo = orchestrator.GetAction(actionType, index); var actionInfo = orchestrator.GetAction(actionType, index);
selectedAction = actionInfo != null selectedAction = actionInfo != null
? Actions.SingleOrDefault(a => !a.RepresentsNull && a.Action.ActionId == actionInfo.Info.ActionId) ? Actions.SingleOrDefault(a => !a.RepresentsNull && a.Action.ActionId == actionInfo.Info.ActionId)
: Actions.Single(a => a.RepresentsNull); : Actions.Single(a => a.RepresentsNull);
@ -163,9 +166,9 @@ namespace MassiveKnob.ViewModel
actionSettingsControl = actionInfo?.Instance.CreateSettingsControl(); actionSettingsControl = actionInfo?.Instance.CreateSettingsControl();
if (actionType != MassiveKnobActionType.OutputAnalog) if (actionType != MassiveKnobActionType.OutputAnalog)
return; return;
var digitalToAnalogSettings = orchestrator.GetDigitalToAnalogSettings(index); var digitalToAnalogSettings = orchestrator.GetDigitalToAnalogSettings(index);
digitalToAnalogOn = digitalToAnalogSettings.OnValue; digitalToAnalogOn = digitalToAnalogSettings.OnValue;
digitalToAnalogOff = digitalToAnalogSettings.OffValue; digitalToAnalogOff = digitalToAnalogSettings.OffValue;
@ -213,4 +216,4 @@ namespace MassiveKnob.ViewModel
{ {
} }
} }
} }