Clean up dial action to make sure all top level code paths have a try catch so the plugin won't crash

This commit is contained in:
dlprows 2023-09-13 20:46:55 -06:00
parent 06266daa92
commit f9b23a62a3

View File

@ -73,54 +73,56 @@ public class DialAction : EncoderBase
_ = UpdateStateIfNeeded(); _ = UpdateStateIfNeeded();
} }
public override void Dispose()
{
Logger.Instance.LogMessage(TracingLevel.DEBUG, "Disposing");
if (_foregroundWindowChangedEvent != IntPtr.Zero)
{
Native.UnhookWinEvent(_foregroundWindowChangedEvent);
}
_dispatcher.InvokeShutdown();
}
public override async void DialDown(DialPayload payload) public override async void DialDown(DialPayload payload)
{ {
//dial pressed down try
{
Logger.Instance.LogMessage(TracingLevel.INFO, "Dial Down"); Logger.Instance.LogMessage(TracingLevel.INFO, "Dial Down");
await ToggleMuteAsync(); await ToggleMuteAsync();
} }
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unexpected Error in DialDown:\n {ex}");
}
}
public override void DialUp(DialPayload payload) { }
public override async void TouchPress(TouchpadPressPayload payload) public override async void TouchPress(TouchpadPressPayload payload)
{
try
{ {
Logger.Instance.LogMessage(TracingLevel.INFO, "Touch Press"); Logger.Instance.LogMessage(TracingLevel.INFO, "Touch Press");
if (payload.IsLongPress) if (payload.IsLongPress)
{ {
_audioHelper.ResetAll(); await ResetAllAsync();
} }
else else
{ {
await ToggleMuteAsync(); await ToggleMuteAsync();
} }
} }
async Task ToggleMuteAsync()
{
try
{
if (_currentAudioSession != null)
{
_currentAudioSession.ToggleMute();
await UpdateStateIfNeeded();
}
else
{
await Connection.ShowAlert();
}
}
catch (Exception ex) catch (Exception ex)
{ {
await Connection.ShowAlert(); Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unexpected Error in TouchPress:\n {ex}");
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unable to toggle mute: {ex.Message}");
} }
} }
public override async void DialRotate(DialRotatePayload payload) public override async void DialRotate(DialRotatePayload payload)
{ {
Logger.Instance.LogMessage(TracingLevel.INFO, "Dial Rotate");
//dial rotated. ticks positive for right, negative for left
try try
{ {
Logger.Instance.LogMessage(TracingLevel.INFO, "Dial Rotate");
//dial rotated. ticks positive for right, negative for left
if (_currentAudioSession != null) if (_currentAudioSession != null)
{ {
_currentAudioSession.IncrementVolumeLevel(settings.StepSize, payload.Ticks); _currentAudioSession.IncrementVolumeLevel(settings.StepSize, payload.Ticks);
@ -133,24 +135,46 @@ public class DialAction : EncoderBase
} }
catch (Exception ex) catch (Exception ex)
{ {
_audioHelper.ResetCache();
await Connection.ShowAlert(); await Connection.ShowAlert();
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unable to toggle mute: {ex.Message}"); Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unable to increment volume:\n {ex}");
} }
} }
public override void DialUp(DialPayload payload) async Task ResetAllAsync()
{ {
//dial unpressed try
{
_audioHelper.ResetAll();
}
catch
{
_audioHelper.ResetCache();
await Connection.ShowAlert();
throw;
}
} }
public override void Dispose() async Task ToggleMuteAsync()
{ {
Logger.Instance.LogMessage(TracingLevel.DEBUG, "Disposing"); try
if (_foregroundWindowChangedEvent != IntPtr.Zero)
{ {
Native.UnhookWinEvent(_foregroundWindowChangedEvent); if (_currentAudioSession != null)
{
_currentAudioSession.ToggleMute();
await UpdateStateIfNeeded();
}
else
{
await Connection.ShowAlert();
}
}
catch
{
_audioHelper.ResetCache();
await Connection.ShowAlert();
throw;
} }
_dispatcher.InvokeShutdown();
} }
public override async void OnTick() public override async void OnTick()
@ -169,7 +193,8 @@ public class DialAction : EncoderBase
} }
catch (Exception ex) catch (Exception ex)
{ {
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Exception on WinEventProc\n {ex}"); _audioHelper.ResetCache();
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Exception on Tick:\n {ex}");
} }
} }
@ -177,7 +202,6 @@ public class DialAction : EncoderBase
{ {
try try
{ {
if (_currentAudioSession != null) if (_currentAudioSession != null)
{ {
@ -213,20 +237,40 @@ public class DialAction : EncoderBase
public override void ReceivedSettings(ReceivedSettingsPayload payload) public override void ReceivedSettings(ReceivedSettingsPayload payload)
{
try
{ {
Tools.AutoPopulateSettings(settings, payload.Settings); Tools.AutoPopulateSettings(settings, payload.Settings);
SaveSettings(); SaveSettings();
} }
catch (Exception ex)
private Task SaveSettings()
{ {
return Connection.SetSettingsAsync(JObject.FromObject(settings)); Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unexpected Error in SaveSettings:\n {ex}");
}
}
private async Task SaveSettings()
{
try
{
await Connection.SetSettingsAsync(JObject.FromObject(settings));
}
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unexpected Error in SaveSettings:\n {ex}");
}
} }
public void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime) public void WinEventProc(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime)
{
try
{ {
OnTick(); OnTick();
} }
catch (Exception ex)
{
Logger.Instance.LogMessage(TracingLevel.ERROR, $"Unexpected Error in DialDown:\n {ex}");
}
}
} }