Skip to content

Commit

Permalink
Added DI and added appsettings for sound control (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
dynamictulip authored Jan 31, 2020
1 parent f9dad64 commit b4d7442
Show file tree
Hide file tree
Showing 10 changed files with 101 additions and 32 deletions.
6 changes: 0 additions & 6 deletions Pomo-Shiny/App.config

This file was deleted.

4 changes: 1 addition & 3 deletions Pomo-Shiny/App.xaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<Application x:Class="Pomo_Shiny.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Pomo_Shiny"
StartupUri="MainWindow.xaml">
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Application.Resources>

<ResourceDictionary>
Expand Down
42 changes: 36 additions & 6 deletions Pomo-Shiny/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,46 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Threading.Tasks;
using System.IO;
using System.Windows;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;

namespace Pomo_Shiny
{
/// <summary>
/// Interaction logic for App.xaml
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
private IServiceProvider ServiceProvider { get; set; }
private IConfiguration Configuration { get; set; }

protected override void OnStartup(StartupEventArgs e)
{
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false, true);

Configuration = builder.Build();

var services = new ServiceCollection();
ConfigureServices(services);

ServiceProvider = services.BuildServiceProvider();

var mainWindow = ServiceProvider.GetRequiredService<MainWindow>();
mainWindow.Show();
}

private void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IApplicationAccessor, ApplicationAccessor>();
services.AddTransient<ICountdownTimer, CountdownTimer>();
services.AddTransient<ITimerFacade, TimerFacade>();
services.AddTransient<ISoundProvider, SoundProvider>();
services.AddTransient<MainWindowViewModel>();
services.AddTransient<MainWindow>();

services.Configure<AppSettings>(Configuration.GetSection(nameof(AppSettings)));
}
}
}
}
9 changes: 9 additions & 0 deletions Pomo-Shiny/AppSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.Extensions.Configuration;

namespace Pomo_Shiny
{
public class AppSettings
{
public bool IsSoundOn { get; set; }
}
}
8 changes: 4 additions & 4 deletions Pomo-Shiny/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Configuration;
using System.Diagnostics.CodeAnalysis;
using System.Windows;
using System.Windows.Input;

Expand All @@ -10,10 +11,9 @@ namespace Pomo_Shiny
[ExcludeFromCodeCoverage]
public partial class MainWindow : Window
{
public MainWindow()
public MainWindow(MainWindowViewModel mainWindowViewModel)
{
DataContext = new MainWindowViewModel(new ApplicationAccessor(),
new CountdownTimer(new TimerFacade(), new SoundProvider()));
DataContext = mainWindowViewModel;
InitializeComponent();
}

Expand Down
12 changes: 8 additions & 4 deletions Pomo-Shiny/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
using System;
using Microsoft.Extensions.Configuration;
using System;
using System.Windows.Input;
using Microsoft.Extensions.Options;

namespace Pomo_Shiny
{
public class MainWindowViewModel : ObservableObject
{
private readonly IApplicationAccessor _applicationAccessor;
private readonly ICountdownTimer _countdownTimer;
private readonly AppSettings _appSettings;
private TimeSpan _remainingTime;

public MainWindowViewModel(IApplicationAccessor applicationAccessor, ICountdownTimer countdownTimer)
public MainWindowViewModel(IApplicationAccessor applicationAccessor, ICountdownTimer countdownTimer, IOptions<AppSettings> appSettings)
{
_applicationAccessor = applicationAccessor;
_countdownTimer = countdownTimer;
_appSettings = appSettings.Value;
_countdownTimer.Callback = val => RemainingTime = val;

ExitCommand = new DelegateCommand(Exit);
Expand Down Expand Up @@ -48,7 +52,7 @@ private set

private void StartTimer()
{
_countdownTimer.StartCountdown(25, false);
_countdownTimer.StartCountdown(25, _appSettings.IsSoundOn);
}

private void StopTimer()
Expand All @@ -58,7 +62,7 @@ private void StopTimer()

private void StartBreakTimer()
{
_countdownTimer.StartCountdown(5, false);
_countdownTimer.StartCountdown(5, _appSettings.IsSoundOn);
}

private void Exit()
Expand Down
14 changes: 9 additions & 5 deletions Pomo-Shiny/Pomo-Shiny.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,23 @@

<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Pomo_Shiny</RootNamespace>
<UseWPF>true</UseWPF>
</PropertyGroup>

<ItemGroup>
<None Remove="Media\unicorn2.png" />
<None Remove="Media\unicorn2_Pyo_12.ico" />
<Content Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Resource Include="Media\unicorn2.png" />
<Resource Include="Media\unicorn2_Pyo_12.ico" />
</ItemGroup>

<ItemGroup>
<Resource Include="Media\unicorn2.png" />
<Resource Include="Media\unicorn2_Pyo_12.ico" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.1" />
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.1" />
</ItemGroup>

</Project>
5 changes: 5 additions & 0 deletions Pomo-Shiny/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"AppSettings": {
"SoundOn": false
}
}
30 changes: 27 additions & 3 deletions Tests/MainWindowViewModelTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using FakeItEasy;
using Microsoft.Extensions.Options;
using NUnit.Framework;
using Pomo_Shiny;

Expand All @@ -10,13 +11,16 @@ public class MainWindowViewModelTests
private IApplicationAccessor _fakeApplicationAccessor;
private ICountdownTimer _fakeCountdownTimer;
private MainWindowViewModel _sut;
private IOptions<AppSettings> _fakeAppSettings;

[SetUp]
public void Setup()
{
_fakeApplicationAccessor = A.Fake<IApplicationAccessor>();
_fakeCountdownTimer = A.Fake<ICountdownTimer>();
_sut = new MainWindowViewModel(_fakeApplicationAccessor, _fakeCountdownTimer);
_fakeAppSettings = A.Fake<IOptions<AppSettings>>();

_sut = new MainWindowViewModel(_fakeApplicationAccessor, _fakeCountdownTimer, _fakeAppSettings);
}

[Test]
Expand All @@ -32,15 +36,15 @@ public void StartTimerCommand_starts_timer_for_25_minutes()
{
_sut.StartTimerCommand.Execute(null);

A.CallTo(() => _fakeCountdownTimer.StartCountdown(25, false)).MustHaveHappened();
A.CallTo(() => _fakeCountdownTimer.StartCountdown(25, A<bool>._)).MustHaveHappened();
}

[Test]
public void StartBreakTimerCommand_starts_timer_for_5_minutes()
{
_sut.StartBreakTimerCommand.Execute(null);

A.CallTo(() => _fakeCountdownTimer.StartCountdown(5, false)).MustHaveHappened();
A.CallTo(() => _fakeCountdownTimer.StartCountdown(5, A<bool>._)).MustHaveHappened();
}

[Test]
Expand Down Expand Up @@ -102,5 +106,25 @@ public void Title_depends_on_PercentageToGo(double value, string expectedTitle)
A.CallTo(() => _fakeCountdownTimer.PercentageToGo).Returns(value);
Assert.AreEqual(expectedTitle, _sut.Title);
}

[TestCase(true)]
[TestCase(false)]
public void StartTimerCountdown_sound_controlled_by_config(bool soundIsOn)
{
_fakeAppSettings.Value.IsSoundOn = soundIsOn;

_sut.StartTimerCommand.Execute(null);
A.CallTo(() => _fakeCountdownTimer.StartCountdown(25, soundIsOn)).MustHaveHappened();
}

[TestCase(true)]
[TestCase(false)]
public void StartBreakTimerCountdown_sound_controlled_by_config(bool soundIsOn)
{
_fakeAppSettings.Value.IsSoundOn = soundIsOn;

_sut.StartBreakTimerCommand.Execute(null);
A.CallTo(() => _fakeCountdownTimer.StartCountdown(5, soundIsOn)).MustHaveHappened();
}
}
}
3 changes: 2 additions & 1 deletion Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FakeItEasy" Version="5.4.0" />
<PackageReference Include="FluentAssertions" Version="5.10.0" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.13.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.2.0" />
Expand Down

0 comments on commit b4d7442

Please sign in to comment.