Skip to content

Commit

Permalink
Comprehensive use of the datetimeservice
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonyCorbett committed Nov 30, 2019
1 parent 99ce0f5 commit 0319b02
Show file tree
Hide file tree
Showing 35 changed files with 278 additions and 81 deletions.
27 changes: 23 additions & 4 deletions OnlyT.AnalogueClock/ClockControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using System.Windows.Shapes;
using System.Windows.Threading;

public class ClockControl : Control
public sealed class ClockControl : Control
{
public static readonly DependencyProperty IsFlatProperty =
DependencyProperty.Register(
Expand Down Expand Up @@ -67,7 +67,7 @@ public class ClockControl : Control
typeof(DurationSector),
typeof(ClockControl),
new FrameworkPropertyMetadata(DurationSectorPropertyChanged));

private static readonly double ClockRadius = 250;
private static readonly double SectorRadius = 230;
private static readonly Point ClockOrigin = new Point(ClockRadius, ClockRadius);
Expand Down Expand Up @@ -126,6 +126,8 @@ public ClockControl()
}
}

public event EventHandler<DateTimeQueryEventArgs> QueryDateTimeEvent;

public bool DigitalTimeFormat24Hours
{
// ReSharper disable once PossibleNullReferenceException
Expand Down Expand Up @@ -350,7 +352,7 @@ private void PositionClockHand(Line hand, double angle, double shadowOpacity)

private void TimerCallback(object sender, EventArgs eventArgs)
{
var now = DateTime.Now;
var now = GetNow();

PositionClockHand(_secondHand, CalculateAngleSeconds(now), _secondHandDropShadowOpacity);
PositionClockHand(_minuteHand, CalculateAngleMinutes(now), _minuteHandDropShadowOpacity);
Expand Down Expand Up @@ -414,7 +416,7 @@ private void StartupAnimation()

private AnglesOfHands GenerateTargetAngles()
{
DateTime targetTime = DateTime.Now;
var targetTime = GetNow();

return new AnglesOfHands
{
Expand Down Expand Up @@ -706,5 +708,22 @@ private void SetShowElapsedSector(bool value)
}
}
}

private DateTime GetNow()
{
var args = new DateTimeQueryEventArgs();
OnQueryDateTime(args);
return args.DateTime;
}

private void OnQueryDateTime(DateTimeQueryEventArgs e)
{
if (QueryDateTimeEvent == null)
{
e.DateTime = DateTime.Now;
}

QueryDateTimeEvent?.Invoke(this, e);
}
}
}
9 changes: 9 additions & 0 deletions OnlyT.AnalogueClock/DateTimeQueryEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace OnlyT.AnalogueClock
{
using System;

public class DateTimeQueryEventArgs : EventArgs
{
public DateTime DateTime { get; set; }
}
}
1 change: 1 addition & 0 deletions OnlyT.AnalogueClock/OnlyT.AnalogueClock.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="DateTimeQueryEventArgs.cs" />
<Compile Include="DurationSector.cs" />
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
Expand Down
5 changes: 5 additions & 0 deletions OnlyT.Common/Services/DateTime/DateTimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,10 @@ public System.DateTime UtcNow()
{
return System.DateTime.UtcNow;
}

public System.DateTime Today()
{
return System.DateTime.Today;
}
}
}
2 changes: 2 additions & 0 deletions OnlyT.Common/Services/DateTime/IDateTimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ public interface IDateTimeService
System.DateTime Now();

System.DateTime UtcNow();

System.DateTime Today();
}
}
25 changes: 22 additions & 3 deletions OnlyT.CountdownTimer/CountdownControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public CountdownControl()
_timer.Tick += TimerFire;
}

public event EventHandler<UtcDateTimeQueryEventArgs> QueryUtcDateTimeEvent;

public event EventHandler TimeUpEvent;

public int CountdownDurationMins
Expand All @@ -87,7 +89,7 @@ public override void OnApplyTemplate()

public void Start(int secsElapsed)
{
_start = DateTime.UtcNow.AddSeconds(-secsElapsed);
_start = GetNowUtc().AddSeconds(-secsElapsed);
_timer.Start();
}

Expand Down Expand Up @@ -159,7 +161,7 @@ private void TimerFire(object sender, EventArgs e)
if (_start != default(DateTime))
{
var secsInCountdown = _countdownDurationMins * 60;
var secondsElapsed = (DateTime.UtcNow - _start).TotalSeconds;
var secondsElapsed = (GetNowUtc() - _start).TotalSeconds;
var secondsLeft = secsInCountdown - secondsElapsed;

if (secondsLeft >= 0)
Expand Down Expand Up @@ -411,7 +413,7 @@ private string GetTimeText(int? secsLeft = null)
else
{
var secsInCountdown = _countdownDurationMins * 60;
var secondsElapsed = (DateTime.UtcNow - _start).TotalSeconds;
var secondsElapsed = (GetNowUtc() - _start).TotalSeconds;
secondsLeft = secsInCountdown - secondsElapsed + 1;
}
}
Expand All @@ -437,5 +439,22 @@ private Size GetTextSize(string text, bool useExtent)

return new Size(formattedText.Width, useExtent ? formattedText.Extent : formattedText.Height);
}

private DateTime GetNowUtc()
{
var args = new UtcDateTimeQueryEventArgs();
OnQueryDateTime(args);
return args.UtcDateTime;
}

private void OnQueryDateTime(UtcDateTimeQueryEventArgs e)
{
if (QueryUtcDateTimeEvent == null)
{
e.UtcDateTime = DateTime.UtcNow;
}

QueryUtcDateTimeEvent?.Invoke(this, e);
}
}
}
1 change: 1 addition & 0 deletions OnlyT.CountdownTimer/OnlyT.CountdownTimer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="AnnulusSize.cs" />
<Compile Include="UtcDateTimeQueryEventArgs.cs" />
<Compile Include="ElementsToShow.cs" />
<Compile Include="SecondsBall.cs" />
<Page Include="Themes\Generic.xaml">
Expand Down
9 changes: 9 additions & 0 deletions OnlyT.CountdownTimer/UtcDateTimeQueryEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace OnlyT.CountdownTimer
{
using System;

public class UtcDateTimeQueryEventArgs : EventArgs
{
public DateTime UtcDateTime { get; set; }
}
}
5 changes: 5 additions & 0 deletions OnlyT.Tests/Mocks/MockDateTimeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,10 @@ public DateTime UtcNow()
{
return Now();
}

public DateTime Today()
{
return Now().Date;
}
}
}
11 changes: 10 additions & 1 deletion OnlyT.Tests/TestTimingReport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,11 @@ private MeetingTimes StoreMidweekData(

if (week == weekCount - 1)
{
var file = TimingReportGeneration.ExecuteAsync(service, null).Result;
var file = TimingReportGeneration.ExecuteAsync(
service,
dateTimeService,
null).Result;

Assert.IsNotNull(file);
}

Expand Down Expand Up @@ -256,6 +260,11 @@ public DateTime UtcNow()
{
return Now();
}

public DateTime Today()
{
return Now().Date;
}
}
}
}
5 changes: 4 additions & 1 deletion OnlyT.Tests/TestViewModels.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Mocks;
using Moq;
using OnlyT.Common.Services.DateTime;
using OnlyT.Services.CommandLine;
using OnlyT.Services.Report;
using OnlyT.Services.Snackbar;
Expand Down Expand Up @@ -32,6 +33,7 @@ public void TestOperatorViewStartStop()
Mock<ICommandLineService> commandLineService = new Mock<ICommandLineService>();
Mock<ILocalTimingDataStoreService> timingDataService = new Mock<ILocalTimingDataStoreService>();
Mock<ISnackbarService> snackbarService = new Mock<ISnackbarService>();
IDateTimeService dateTimeService = new MockDateTimeService();

var vm = new OperatorPageViewModel(
timerService.Object,
Expand All @@ -41,7 +43,8 @@ public void TestOperatorViewStartStop()
commandLineService.Object,
bellService.Object,
timingDataService.Object,
snackbarService.Object);
snackbarService.Object,
dateTimeService);

Assert.IsFalse(vm.IsRunning);
Assert.IsFalse(vm.IsManualMode);
Expand Down
1 change: 1 addition & 0 deletions OnlyT.sln.DotSettings
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<s:Boolean x:Key="/Default/UserDictionary/Words/=countup/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=DISPLAYCONFIG/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=donut/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Enquire/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=hwnd/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=initialised/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Localised/@EntryIndexedValue">True</s:Boolean>
Expand Down
19 changes: 10 additions & 9 deletions OnlyT/MeetingTalkTimesFeed/TimesFeed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using OnlyT.Common.Services.DateTime;
using Serilog;
using Utils;

Expand All @@ -21,9 +22,9 @@ public TimesFeed()
_localFeedFile = Path.Combine(FileUtils.GetAppDataFolder(), "feed.json");
}

public Meeting GetMeetingDataForToday()
public Meeting GetMeetingDataForToday(IDateTimeService dateTimeService)
{
LoadFile();
LoadFile(dateTimeService.Now().Date);
return GetMeetingDataForTodayInternal(_meetingData);
}

Expand Down Expand Up @@ -70,14 +71,14 @@ public Meeting GetSampleMidweekMeetingDataForTesting(DateTime theDate)
return result;
}

private bool LocalFileTooOld()
private bool LocalFileTooOld(DateTime today)
{
bool tooOld = true;
var tooOld = true;

if (File.Exists(_localFeedFile))
{
var created = File.GetLastWriteTime(_localFeedFile);
var diff = DateTime.Now - created;
var diff = today - created;
if (diff.TotalDays <= _tooOldDays)
{
tooOld = false;
Expand All @@ -87,19 +88,19 @@ private bool LocalFileTooOld()
return tooOld;
}

private void LoadFile()
private void LoadFile(DateTime today)
{
if (_meetingData == null)
{
_meetingData = LoadFileInternal();
_meetingData = LoadFileInternal(today);
}
}

private IReadOnlyCollection<Meeting> LoadFileInternal()
private IReadOnlyCollection<Meeting> LoadFileInternal(DateTime today)
{
List<Meeting> result = null;

var needRefresh = LocalFileTooOld();
var needRefresh = LocalFileTooOld(today);

if (!needRefresh)
{
Expand Down
9 changes: 9 additions & 0 deletions OnlyT/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions OnlyT/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -673,4 +673,8 @@
<value>Flat clock</value>
<comment>See Settings =&gt; Timer window. Refres to whether the analogue clock is flat or 3D</comment>
</data>
<data name="WEEKEND_INCLUDES_FRIDAY" xml:space="preserve">
<value>Weekend includes Friday</value>
<comment>See Settings =&gt; Automatic mode</comment>
</data>
</root>
8 changes: 6 additions & 2 deletions OnlyT/Services/Automate/AutomateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System;
using System.Diagnostics;
using System.Windows.Threading;
using OnlyT.Common.Services.DateTime;
using OnlyT.Models;
using OnlyT.Services.Options;
using OnlyT.Services.TalkSchedule;
Expand All @@ -15,6 +16,7 @@ internal class AutomateService : IAutomateService
private readonly IOptionsService _optionsService;
private readonly ITalkTimerService _timerService;
private readonly ITalkScheduleService _scheduleService;
private readonly IDateTimeService _dateTimeService;
private readonly DispatcherTimer _timer = new DispatcherTimer();
private readonly Stopwatch _stopwatch = new Stopwatch();
private readonly Random _random = new Random();
Expand All @@ -25,11 +27,13 @@ internal class AutomateService : IAutomateService
public AutomateService(
IOptionsService optionsService,
ITalkTimerService timerService,
ITalkScheduleService scheduleService)
ITalkScheduleService scheduleService,
IDateTimeService dateTimeService)
{
_optionsService = optionsService;
_timerService = timerService;
_scheduleService = scheduleService;
_dateTimeService = dateTimeService;
}

public void Execute()
Expand All @@ -46,7 +50,7 @@ public void Execute()

private void TimerTick(object sender, System.EventArgs e)
{
var now = DateTime.Now;
var now = _dateTimeService.Now();

if (!_stopwatch.IsRunning)
{
Expand Down
Loading

0 comments on commit 0319b02

Please sign in to comment.