Skip to content

Commit

Permalink
fix(Profile): Properly return issue when loading profile at applicati…
Browse files Browse the repository at this point in the history
…on startup.

Fixes SOUNDSWITCH-9T
  • Loading branch information
Belphemur committed Oct 9, 2021
1 parent ae4f30b commit 6b27786
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 14 deletions.
3 changes: 3 additions & 0 deletions SoundSwitch/Framework/Profile/ProfileError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace SoundSwitch.Framework.Profile;

public record ProfileError(Profile Profile, string Error) {}
28 changes: 19 additions & 9 deletions SoundSwitch/Framework/Profile/ProfileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,20 +122,31 @@ private void UnRegisterTriggers(Profile profile)
/// Initialize the profile manager. Return the list of Profile that it couldn't register hotkeys for.
/// </summary>
/// <returns></returns>
public Result<Profile[], VoidSuccess> Init()
public Result<ProfileError[], VoidSuccess> Init()
{
var errors = AppConfigs.Configuration.Profiles.Where(profile => !RegisterTriggers(profile, true)).ToArray();

var errors = Array.Empty<ProfileError>();
try
{
RegisterEvents();
errors = AppConfigs.Configuration.Profiles.Select(profile => (Profile: profile, Failure: ValidateProfile(profile, true).UnwrapFailure()))
.Select(tuple =>
{
if (tuple.Failure == null)
{
RegisterTriggers(tuple.Profile, true);
}

InitializeProfileExistingProcess();
return tuple;
})
.Where(tuple => tuple.Failure != null)
.Select(tuple => new ProfileError(tuple.Profile, tuple.Failure))
.ToArray();

RegisterEvents();
InitializeProfileExistingProcess();

if (errors.Length > 0)
{
_logger.Warning("Couldn't initiate all profiles: {profiles}", errors.Select(profile => profile.Name));
_logger.Warning("Couldn't initiate all profiles: {profiles}", errors);
return errors;
}

Expand Down Expand Up @@ -163,7 +174,6 @@ private void RegisterEvents()

WindowsAPIAdapter.WindowDestroyed += (sender, @event) => { RestoreState(@event.Hwnd); };
_logger.Information("Windows Destroyed Registered");

}

private bool HandleUwpApp(WindowMonitor.Event @event)
Expand Down Expand Up @@ -386,7 +396,7 @@ public Result<string, VoidSuccess> UpdateProfile(Profile oldProfile, Profile new
});
}

private Result<string, VoidSuccess> ValidateProfile(Profile profile)
private Result<string, VoidSuccess> ValidateProfile(Profile profile, bool init = false)
{
if (string.IsNullOrEmpty(profile.Name))
{
Expand All @@ -403,7 +413,7 @@ private Result<string, VoidSuccess> ValidateProfile(Profile profile)
return SettingsStrings.profile_error_needPlaybackOrRecording;
}

if (AppConfigs.Configuration.Profiles.Contains(profile))
if (!init && AppConfigs.Configuration.Profiles.Contains(profile))
{
return string.Format(SettingsStrings.profile_error_name, profile.Name);
}
Expand Down
9 changes: 6 additions & 3 deletions SoundSwitch/Model/AppModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,13 @@ public void InitializeMain(IAudioDeviceLister active, IAudioDeviceLister unplugg

ProfileManager
.Init()
.Catch<Profile[]>(settings =>
.Catch(profileErrors =>
{
var profileNames = string.Join(", ", settings.Select((setting) => setting.Name));
TrayIcon.ShowError(string.Format(SettingsStrings.profile_error_registerHotkeys, profileNames), SettingsStrings.profile_error_registerHotkeys_title);
foreach (var profileError in profileErrors)
{
TrayIcon.ShowError($"{profileError.Profile.Name}: {profileError.Error}", SettingsStrings.profile_error_title);

}
return Result.Success();
});

Expand Down
42 changes: 40 additions & 2 deletions SoundSwitch/Util/ResultTypeExtension.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,53 @@
using System;
using RailSharp.Internal.Result;

#nullable enable

namespace SoundSwitch.Util
{
public static class ResultTypeExtension
{
public static RailSharp.Result<TFailure, TSuccess> Catch<TFailure, TSuccess>(
this RailSharp.Result<TFailure, TSuccess> result,
Func<TFailure, RailSharp.Result<TFailure, TSuccess>> mapper)
Func<TFailure, RailSharp.Result<TFailure, TSuccess>> mapper)
{
return !(result is Failure<TFailure, TSuccess> failure) ? result : mapper((TFailure) failure);
return !(result is Failure<TFailure, TSuccess> failure) ? result : mapper((TFailure)failure);
}

/// <summary>
/// Unwrap the failure case
/// </summary>
/// <param name="result"></param>
/// <param name="defaultValue"></param>
/// <typeparam name="TFailure"></typeparam>
/// <typeparam name="TSuccess"></typeparam>
/// <returns></returns>
public static TFailure? UnwrapFailure<TFailure, TSuccess>(this RailSharp.Result<TFailure, TSuccess> result, TFailure defaultValue = default)
{
if (result is Failure<TFailure, TSuccess> failure)
{
return failure;
}

return defaultValue;
}

/// <summary>
/// Unwrap the success value
/// </summary>
/// <param name="result"></param>
/// <param name="defaultValue"></param>
/// <typeparam name="TFailure"></typeparam>
/// <typeparam name="TSuccess"></typeparam>
/// <returns></returns>
public static TSuccess? UnwrapSuccess<TFailure, TSuccess>(this RailSharp.Result<TFailure, TSuccess> result, TSuccess? defaultValue = default)
{
if (result is Success<TFailure, TSuccess> success)
{
return success;
}

return defaultValue;
}
}
}

0 comments on commit 6b27786

Please sign in to comment.