Skip to content

Commit

Permalink
refactoring: separation of concerns
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasConstant committed Nov 13, 2015
1 parent ed6749f commit 8a523fe
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 40 deletions.
8 changes: 8 additions & 0 deletions SpotifySleepModeStopper/Contracts/IAppStateReporting.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SpotifyTools.Contracts
{
public interface IAppStatusReporting
{
void NotifyAntiSleepingModeIsActivated();
void NotifyAntiSleepingModeIsDisabled();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SpotifyTools.Contracts;

namespace SpotifyTools.Domain.AppStatesManagement
{
public class AppStateReporting : IAppStatusReporting
{
private readonly Action _whenAntiSleepModeActivated;
private readonly Action _whenAntiSleepModeDisabled;

public AppStateReporting(Action whenAntiSleepModeActivated, Action whenAntiSleepModeDisabled)
{
_whenAntiSleepModeActivated = whenAntiSleepModeActivated;
_whenAntiSleepModeDisabled = whenAntiSleepModeDisabled;
}

public void NotifyAntiSleepingModeIsActivated()
{
_whenAntiSleepModeActivated();
}

public void NotifyAntiSleepingModeIsDisabled()
{
_whenAntiSleepModeDisabled();
}
}
}
12 changes: 9 additions & 3 deletions SpotifySleepModeStopper/Domain/SpotifySaveModeStopper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,20 @@ public class SpotifySaveModeStopper
private readonly IMessageDisplayer _messageDisplayer;
private readonly IPreventSleepScreen _preventSleepScreen;
private readonly ISoundAnalyser _soundAnalyser;
private readonly IAppStatusReporting _appState;

private bool _spotifyRunning;
private bool _spotifyPlaying;

private CancellationTokenSource _cancellationTokenSource;
private Task _analyst;

public SpotifySaveModeStopper(IMessageDisplayer messageDisplayer, IPreventSleepScreen preventSleepScreen, ISoundAnalyser soundAnalyser)
public SpotifySaveModeStopper(IMessageDisplayer messageDisplayer, IPreventSleepScreen preventSleepScreen, ISoundAnalyser soundAnalyser, IAppStatusReporting appState1)
{
_messageDisplayer = messageDisplayer;
_preventSleepScreen = preventSleepScreen;
_soundAnalyser = soundAnalyser;
_appState = appState1;
}

public void StartListening()
Expand Down Expand Up @@ -77,12 +79,16 @@ private void AnalyseSpotifyStatus()
{
if (_spotifyRunning && _spotifyPlaying)
{
_messageDisplayer.OutputMessage("SleepModeEnabled");
_messageDisplayer.OutputMessage("Anti Sleep Mode is Enabled");
_appState.NotifyAntiSleepingModeIsActivated();

_preventSleepScreen.EnableConstantDisplayAndPower(true);
}
else
{
_messageDisplayer.OutputMessage("SleepModeDisabled");
_messageDisplayer.OutputMessage("Anti Sleep Mode is Disabled");
_appState.NotifyAntiSleepingModeIsDisabled();

_preventSleepScreen.EnableConstantDisplayAndPower(false);
}
}
Expand Down
2 changes: 2 additions & 0 deletions SpotifySleepModeStopper/SpotifySleepModeStopper.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Contracts\IAppStateReporting.cs" />
<Compile Include="Contracts\IMessageDisplayer.cs" />
<Compile Include="Contracts\IPreventSleepScreen.cs" />
<Compile Include="Contracts\ISoundAnalyser.cs" />
<Compile Include="Domain\AppStatesManagement\AppStateReporting.cs" />
<Compile Include="Tools\Repeat.cs" />
<Compile Include="Domain\SpotifySaveModeStopper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
34 changes: 0 additions & 34 deletions SpotifySleepModeStopperGui/IconChanger.cs

This file was deleted.

5 changes: 3 additions & 2 deletions SpotifySleepModeStopperGui/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Windows.Shapes;
using System.Windows.Threading;
using SpotifyTools.Domain;
using SpotifyTools.Domain.AppStatesManagement;
using SpotifyTools.Domain.AudioManagement;
using SpotifyTools.Domain.MessageManagement;
using SpotifyTools.Domain.PowerManagement;
Expand Down Expand Up @@ -70,8 +71,8 @@ public MainWindow()
_notifyIcon.ContextMenu = contextMenu;
#endregion

var iconChanger = new IconChanger(SetPlaying, SetNotPlaying);
_analyser = new SpotifySaveModeStopper(iconChanger, new PowerRequestContextHandler(), new CsCoreSoundAnalyser());
var iconChanger = new AppStateReporting(SetPlaying, SetNotPlaying);
_analyser = new SpotifySaveModeStopper(new DummyMessageDisplayer(), new PowerRequestContextHandler(), new CsCoreSoundAnalyser(), iconChanger);
_analyser.StartListening();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="IconChanger.cs" />
<Compile Include="MainWindow.xaml.cs">
<DependentUpon>MainWindow.xaml</DependentUpon>
<SubType>Code</SubType>
Expand Down

0 comments on commit 8a523fe

Please sign in to comment.