From d1312c38e764e80eef78fe5d3488f70114b1257a Mon Sep 17 00:00:00 2001 From: Antoine Aflalo Date: Sat, 11 Sep 2021 15:19:30 -0400 Subject: [PATCH] fix(Profile::Validation): Be sure we're not creating/updating a profile with the exact same trigger Fixes #753 Fixes SOUNDSWITCH-AE --- .../Framework/Profile/ProfileManager.cs | 24 ++++++++++++++++++- .../Framework/Profile/Trigger/Trigger.cs | 12 +++++----- 2 files changed, 29 insertions(+), 7 deletions(-) diff --git a/SoundSwitch/Framework/Profile/ProfileManager.cs b/SoundSwitch/Framework/Profile/ProfileManager.cs index 98ad619d0f..41ba7faedc 100644 --- a/SoundSwitch/Framework/Profile/ProfileManager.cs +++ b/SoundSwitch/Framework/Profile/ProfileManager.cs @@ -98,7 +98,6 @@ private bool RegisterTriggers(Profile profile, bool onInit = false) return true; }, () => true); - } return success; @@ -404,6 +403,29 @@ private Result ValidateProfile(Profile profile) return string.Format(SettingsStrings.profile_error_name, profile.Name); } + //Only hotkey doesn't need validation since you can have multiple profile with the same hotkey + foreach (var groups in profile.Triggers.Where(trigger => trigger.Type != TriggerFactory.Enum.HotKey).GroupBy(trigger => trigger.Type).Where(triggers => triggers.Count() > 1)) + { + //has different trigger of the same type, not a problem + if (groups.Distinct().Count() > 1) + { + continue; + } + var trigger = groups.First(); + + var error = groups.Key.Match(() => null, + () => string.Format(SettingsStrings.profile_error_window, trigger.WindowName), + () => string.Format(SettingsStrings.profile_error_application, trigger.ApplicationPath), + () => SettingsStrings.profile_error_steam, + () => null, + () => string.Format(SettingsStrings.profile_error_window, trigger.WindowName), + () => null); + if (error != null) + { + return error; + } + } + foreach (var trigger in profile.Triggers) { var error = trigger.Type.Match(() => diff --git a/SoundSwitch/Framework/Profile/Trigger/Trigger.cs b/SoundSwitch/Framework/Profile/Trigger/Trigger.cs index 3b137d86a5..271bc21aea 100644 --- a/SoundSwitch/Framework/Profile/Trigger/Trigger.cs +++ b/SoundSwitch/Framework/Profile/Trigger/Trigger.cs @@ -10,10 +10,10 @@ public Trigger(TriggerFactory.Enum type) Type = type; } - public TriggerFactory.Enum Type { get; } - public string WindowName { get; set; } - public string ApplicationPath { get; set; } - public HotKey HotKey { get; set; } + public TriggerFactory.Enum Type { get; } + public string WindowName { get; set; } + public string ApplicationPath { get; set; } + public HotKey HotKey { get; set; } /// /// Should this trigger restore the devices after the app is closed @@ -37,14 +37,14 @@ public override bool Equals(object obj) if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; if (obj.GetType() != this.GetType()) return false; - return Equals((Trigger) obj); + return Equals((Trigger)obj); } public override int GetHashCode() { unchecked { - var hashCode = (int) Type; + var hashCode = (int)Type; hashCode = (hashCode * 397) ^ (WindowName != null ? WindowName.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (ApplicationPath != null ? ApplicationPath.GetHashCode() : 0); hashCode = (hashCode * 397) ^ (HotKey != null ? HotKey.GetHashCode() : 0);