-
-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(Device::Matching): Always match devices by their Id and their cle…
…an name. Also follow user order for display/switching. Fixes #706
- Loading branch information
Showing
6 changed files
with
96 additions
and
71 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
SoundSwitch.Common/Framework/Audio/Collection/DeviceReadOnlyCollection.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using SoundSwitch.Common.Framework.Audio.Device; | ||
|
||
namespace SoundSwitch.Common.Framework.Audio.Collection | ||
{ | ||
public class DeviceReadOnlyCollection<T> : IReadOnlyCollection<T> where T : DeviceInfo | ||
{ | ||
private readonly Dictionary<string, T> _byId = new(); | ||
private readonly Dictionary<string, T> _byName = new(); | ||
|
||
public DeviceReadOnlyCollection(IEnumerable<T> deviceInfos) | ||
{ | ||
foreach (var item in deviceInfos) | ||
{ | ||
if (item == null) | ||
{ | ||
return; | ||
} | ||
|
||
_byId[item.Id] = item; | ||
_byName[item.NameClean] = item; | ||
} | ||
} | ||
|
||
public IEnumerator<T> GetEnumerator() | ||
{ | ||
return _byId.Values.GetEnumerator(); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
|
||
/// <summary> | ||
/// Intersect with another list of <see cref="DeviceInfo"/> | ||
/// </summary> | ||
/// <param name="second"></param> | ||
/// <returns></returns> | ||
public IEnumerable<T> IntersectWith(IEnumerable<DeviceInfo> second) | ||
{ | ||
return second | ||
.Select(info => | ||
{ | ||
if (_byId.TryGetValue(info.Id, out var found)) | ||
{ | ||
return found; | ||
} | ||
|
||
if (_byId.TryGetValue(info.NameClean, out found)) | ||
{ | ||
return found; | ||
} | ||
|
||
return null; | ||
}) | ||
.Where(info => info != null) | ||
.Distinct(); | ||
} | ||
|
||
public int Count => _byId.Count; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.