Skip to content

Commit 6b27786

Browse files
committed
fix(Profile): Properly return issue when loading profile at application startup.
Fixes SOUNDSWITCH-9T
1 parent ae4f30b commit 6b27786

File tree

4 files changed

+68
-14
lines changed

4 files changed

+68
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
namespace SoundSwitch.Framework.Profile;
2+
3+
public record ProfileError(Profile Profile, string Error) {}

SoundSwitch/Framework/Profile/ProfileManager.cs

+19-9
Original file line numberDiff line numberDiff line change
@@ -122,20 +122,31 @@ private void UnRegisterTriggers(Profile profile)
122122
/// Initialize the profile manager. Return the list of Profile that it couldn't register hotkeys for.
123123
/// </summary>
124124
/// <returns></returns>
125-
public Result<Profile[], VoidSuccess> Init()
125+
public Result<ProfileError[], VoidSuccess> Init()
126126
{
127-
var errors = AppConfigs.Configuration.Profiles.Where(profile => !RegisterTriggers(profile, true)).ToArray();
128-
127+
var errors = Array.Empty<ProfileError>();
129128
try
130129
{
131-
RegisterEvents();
130+
errors = AppConfigs.Configuration.Profiles.Select(profile => (Profile: profile, Failure: ValidateProfile(profile, true).UnwrapFailure()))
131+
.Select(tuple =>
132+
{
133+
if (tuple.Failure == null)
134+
{
135+
RegisterTriggers(tuple.Profile, true);
136+
}
132137

133-
InitializeProfileExistingProcess();
138+
return tuple;
139+
})
140+
.Where(tuple => tuple.Failure != null)
141+
.Select(tuple => new ProfileError(tuple.Profile, tuple.Failure))
142+
.ToArray();
134143

144+
RegisterEvents();
145+
InitializeProfileExistingProcess();
135146

136147
if (errors.Length > 0)
137148
{
138-
_logger.Warning("Couldn't initiate all profiles: {profiles}", errors.Select(profile => profile.Name));
149+
_logger.Warning("Couldn't initiate all profiles: {profiles}", errors);
139150
return errors;
140151
}
141152

@@ -163,7 +174,6 @@ private void RegisterEvents()
163174

164175
WindowsAPIAdapter.WindowDestroyed += (sender, @event) => { RestoreState(@event.Hwnd); };
165176
_logger.Information("Windows Destroyed Registered");
166-
167177
}
168178

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

389-
private Result<string, VoidSuccess> ValidateProfile(Profile profile)
399+
private Result<string, VoidSuccess> ValidateProfile(Profile profile, bool init = false)
390400
{
391401
if (string.IsNullOrEmpty(profile.Name))
392402
{
@@ -403,7 +413,7 @@ private Result<string, VoidSuccess> ValidateProfile(Profile profile)
403413
return SettingsStrings.profile_error_needPlaybackOrRecording;
404414
}
405415

406-
if (AppConfigs.Configuration.Profiles.Contains(profile))
416+
if (!init && AppConfigs.Configuration.Profiles.Contains(profile))
407417
{
408418
return string.Format(SettingsStrings.profile_error_name, profile.Name);
409419
}

SoundSwitch/Model/AppModel.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -304,10 +304,13 @@ public void InitializeMain(IAudioDeviceLister active, IAudioDeviceLister unplugg
304304

305305
ProfileManager
306306
.Init()
307-
.Catch<Profile[]>(settings =>
307+
.Catch(profileErrors =>
308308
{
309-
var profileNames = string.Join(", ", settings.Select((setting) => setting.Name));
310-
TrayIcon.ShowError(string.Format(SettingsStrings.profile_error_registerHotkeys, profileNames), SettingsStrings.profile_error_registerHotkeys_title);
309+
foreach (var profileError in profileErrors)
310+
{
311+
TrayIcon.ShowError($"{profileError.Profile.Name}: {profileError.Error}", SettingsStrings.profile_error_title);
312+
313+
}
311314
return Result.Success();
312315
});
313316

+40-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,53 @@
11
using System;
22
using RailSharp.Internal.Result;
33

4+
#nullable enable
5+
46
namespace SoundSwitch.Util
57
{
68
public static class ResultTypeExtension
79
{
810
public static RailSharp.Result<TFailure, TSuccess> Catch<TFailure, TSuccess>(
911
this RailSharp.Result<TFailure, TSuccess> result,
10-
Func<TFailure, RailSharp.Result<TFailure, TSuccess>> mapper)
12+
Func<TFailure, RailSharp.Result<TFailure, TSuccess>> mapper)
1113
{
12-
return !(result is Failure<TFailure, TSuccess> failure) ? result : mapper((TFailure) failure);
14+
return !(result is Failure<TFailure, TSuccess> failure) ? result : mapper((TFailure)failure);
15+
}
16+
17+
/// <summary>
18+
/// Unwrap the failure case
19+
/// </summary>
20+
/// <param name="result"></param>
21+
/// <param name="defaultValue"></param>
22+
/// <typeparam name="TFailure"></typeparam>
23+
/// <typeparam name="TSuccess"></typeparam>
24+
/// <returns></returns>
25+
public static TFailure? UnwrapFailure<TFailure, TSuccess>(this RailSharp.Result<TFailure, TSuccess> result, TFailure defaultValue = default)
26+
{
27+
if (result is Failure<TFailure, TSuccess> failure)
28+
{
29+
return failure;
30+
}
31+
32+
return defaultValue;
33+
}
34+
35+
/// <summary>
36+
/// Unwrap the success value
37+
/// </summary>
38+
/// <param name="result"></param>
39+
/// <param name="defaultValue"></param>
40+
/// <typeparam name="TFailure"></typeparam>
41+
/// <typeparam name="TSuccess"></typeparam>
42+
/// <returns></returns>
43+
public static TSuccess? UnwrapSuccess<TFailure, TSuccess>(this RailSharp.Result<TFailure, TSuccess> result, TSuccess? defaultValue = default)
44+
{
45+
if (result is Success<TFailure, TSuccess> success)
46+
{
47+
return success;
48+
}
49+
50+
return defaultValue;
1351
}
1452
}
1553
}

0 commit comments

Comments
 (0)