Skip to content

Commit

Permalink
Use Notification Manager in Profile manager
Browse files Browse the repository at this point in the history
See #539
  • Loading branch information
Belphemur committed Jan 16, 2021
1 parent 4354ae4 commit 4bae9fb
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 68 deletions.
30 changes: 21 additions & 9 deletions SoundSwitch/Framework/NotificationManager/NotificationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ namespace SoundSwitch.Framework.NotificationManager
{
public class NotificationManager
{
private readonly IAppModel _model;
private string _lastDeviceId;
private INotification _notification;
private readonly IAppModel _model;
private string _lastDeviceId;
private INotification _notification;
private readonly NotificationFactory _notificationFactory;

public NotificationManager(IAppModel model)
{
_model = model;
_model = model;
_notificationFactory = new NotificationFactory();
}

public void Init()
{
_model.DefaultDeviceChanged += ModelOnDefaultDeviceChanged;
_model.DefaultDeviceChanged += ModelOnDefaultDeviceChanged;
_model.NotificationSettingsChanged += ModelOnNotificationSettingsChanged;
SetNotification(_model.NotificationSettings);
_model.CustomSoundChanged += ModelOnCustomSoundChanged;
Expand All @@ -59,7 +59,7 @@ private void SetNotification(NotificationTypeEnum notificationTypeEnum)
_notification = _notificationFactory.Get(notificationTypeEnum);
_notification.Configuration = new NotificationConfiguration()
{
Icon = AppModel.Instance.TrayIcon.NotifyIcon,
Icon = _model.TrayIcon.NotifyIcon,
DefaultSound = Resources.NotificationSound
};
try
Expand All @@ -74,7 +74,7 @@ private void SetNotification(NotificationTypeEnum notificationTypeEnum)
}

MessageBox.Show(string.Format(SettingsStrings.audioFileNotFound, SettingsStrings.notificationOptionSound),
SettingsStrings.audioFileNotFoundCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
SettingsStrings.audioFileNotFoundCaption, MessageBoxButtons.OK, MessageBoxIcon.Error);
_model.NotificationSettings = NotificationTypeEnum.SoundNotification;
}
}
Expand All @@ -88,11 +88,23 @@ private void ModelOnDefaultDeviceChanged(object sender, DeviceDefaultChangedEven
_lastDeviceId = deviceDefaultChangedEvent.DeviceId;
}

/// <summary>
/// Notify on Profile changed
/// </summary>
public void NotifyProfileChanged(Profile.Profile profile, uint? processId)
{
if (!profile.NotifyOnActivation)
{
return;
}
_notification.NotifyProfileChanged(profile, processId);
}

~NotificationManager()
{
_model.DefaultDeviceChanged -= ModelOnDefaultDeviceChanged;
_model.DefaultDeviceChanged -= ModelOnDefaultDeviceChanged;
_model.NotificationSettingsChanged -= ModelOnNotificationSettingsChanged;
_model.CustomSoundChanged -= ModelOnCustomSoundChanged;
_model.CustomSoundChanged -= ModelOnCustomSoundChanged;
}
}
}
84 changes: 25 additions & 59 deletions SoundSwitch/Framework/Profile/ProfileManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using Markdig.Syntax.Inlines;
using NAudio.CoreAudioApi;
using RailSharp;
using RailSharp.Internal.Result;
Expand All @@ -13,29 +12,26 @@
using SoundSwitch.Audio.Manager.Interop.Com.User;
using SoundSwitch.Audio.Manager.Interop.Enum;
using SoundSwitch.Common.Framework.Audio.Device;
using SoundSwitch.Common.Framework.Icon;
using SoundSwitch.Framework.Banner;
using SoundSwitch.Framework.Configuration;
using SoundSwitch.Framework.Profile.Trigger;
using SoundSwitch.Framework.WinApi;
using SoundSwitch.Framework.WinApi.Keyboard;
using SoundSwitch.Localization;
using SoundSwitch.Model;
using SoundSwitch.Properties;
using SoundSwitch.Util;
using HotKey = SoundSwitch.Framework.WinApi.Keyboard.HotKey;
using WindowsAPIAdapter = SoundSwitch.Framework.WinApi.WindowsAPIAdapter;

namespace SoundSwitch.Framework.Profile
{
public class ProfileManager
{
public delegate void ShowError(string errorMessage, string errorTitle);

private readonly BannerManager _bannerManager = new();
private readonly WindowMonitor _windowMonitor;
private readonly AudioSwitcher _audioSwitcher;
private readonly IAudioDeviceLister _activeDeviceLister;
private readonly ShowError _showError;
private readonly TriggerFactory _triggerFactory;
private readonly WindowMonitor _windowMonitor;
private readonly AudioSwitcher _audioSwitcher;
private readonly IAudioDeviceLister _activeDeviceLister;
private readonly ShowError _showError;
private readonly TriggerFactory _triggerFactory;
private readonly NotificationManager.NotificationManager _notificationManager;

private Profile? _steamProfile;

Expand All @@ -49,17 +45,19 @@ public class ProfileManager

public IReadOnlyCollection<Profile> Profiles => AppConfigs.Configuration.Profiles;

public ProfileManager(WindowMonitor windowMonitor,
AudioSwitcher audioSwitcher,
IAudioDeviceLister activeDeviceLister,
ShowError showError,
TriggerFactory triggerFactory)
public ProfileManager(WindowMonitor windowMonitor,
AudioSwitcher audioSwitcher,
IAudioDeviceLister activeDeviceLister,
ShowError showError,
TriggerFactory triggerFactory,
NotificationManager.NotificationManager notificationManager)
{
_windowMonitor = windowMonitor;
_audioSwitcher = audioSwitcher;
_activeDeviceLister = activeDeviceLister;
_showError = showError;
_triggerFactory = triggerFactory;
_windowMonitor = windowMonitor;
_audioSwitcher = audioSwitcher;
_activeDeviceLister = activeDeviceLister;
_showError = showError;
_triggerFactory = triggerFactory;
_notificationManager = notificationManager;
}

private void RegisterTriggers(Profile profile, bool onInit = false)
Expand Down Expand Up @@ -151,7 +149,7 @@ private void RegisterEvents()
private bool HandleUwpApp(WindowMonitor.Event @event)
{
(Profile Profile, Trigger.Trigger Trigger) profileTuple;

var windowNameLowerCase = @event.WindowName.ToLower();

profileTuple = _profilesByUwpApp.FirstOrDefault(pair => windowNameLowerCase.Contains(pair.Key)).Value;
Expand All @@ -167,10 +165,9 @@ private bool HandleUwpApp(WindowMonitor.Event @event)

private bool HandleWindowName(WindowMonitor.Event @event)
{
(Profile Profile, Trigger.Trigger Trigger) profileTuple;
var windowNameLower = @event.WindowName.ToLower();
var windowNameLower = @event.WindowName.ToLower();

profileTuple = _profilesByWindowName.FirstOrDefault(pair => windowNameLower.Contains(pair.Key)).Value;
var profileTuple = _profilesByWindowName.FirstOrDefault(pair => windowNameLower.Contains(pair.Key)).Value;
if (profileTuple != default)
{
SaveCurrentState(@event.Hwnd, profileTuple.Profile, profileTuple.Trigger);
Expand Down Expand Up @@ -269,40 +266,9 @@ private bool HandleSteamBigPicture(WindowMonitor.Event @event)
};
}

private void NotifyProfileIfNeeded(Profile profile, uint? processId)
{
if (!profile.NotifyOnActivation)
{
return;
}


var icon = Resources.default_profile_image;
if (processId.HasValue)
{
try
{
var process = Process.GetProcessById((int) processId.Value);
icon = IconExtractor.Extract(process.MainModule?.FileName, 0, true).ToBitmap();
}
catch (Exception)
{
// ignored
}
}

_bannerManager.ShowNotification(new BannerData
{
Priority = 1,
Image = icon,
Title = string.Format(SettingsStrings.profile_notification_text, profile.Name),
Text = string.Join("\n", profile.Devices.Select(wrapper => wrapper.DeviceInfo.NameClean))
});
}

private void SwitchAudio(Profile profile, uint processId)
{
NotifyProfileIfNeeded(profile, processId);
_notificationManager.NotifyProfileChanged(profile, processId);
foreach (var device in profile.Devices)
{
var deviceToUse = CheckDeviceAvailable(device.DeviceInfo);
Expand All @@ -328,7 +294,7 @@ private void SwitchAudio(Profile profile, uint processId)

private void SwitchAudio(Profile profile)
{
NotifyProfileIfNeeded(profile, null);
_notificationManager.NotifyProfileChanged(profile, null);
foreach (var device in profile.Devices)
{
var deviceToUse = CheckDeviceAvailable(device.DeviceInfo);
Expand Down

0 comments on commit 4bae9fb

Please sign in to comment.