Skip to content

Commit

Permalink
feat(dispatcherQueueTimer): Add support of the IsReapeting property
Browse files Browse the repository at this point in the history
  • Loading branch information
dr1rrb committed Aug 3, 2020
1 parent 6fe45f1 commit 556e4ae
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
19 changes: 9 additions & 10 deletions src/Uno.UWP/System/DispatcherQueueTimer.cs
Expand Up @@ -7,8 +7,7 @@

namespace Windows.System
{
public partial class
DispatcherQueueTimer
public partial class DispatcherQueueTimer
{
private static class States
{
Expand Down Expand Up @@ -45,12 +44,7 @@ public TimeSpan Interval

public bool IsRunning => _state == States.Running;

public bool IsRepeating
{
get => true;
[global::Uno.NotImplemented]
set => global::Windows.Foundation.Metadata.ApiInformation.TryRaiseNotImplemented("Windows.System.DispatcherQueueTimer", "bool DispatcherQueueTimer.IsRepeating");
}
public bool IsRepeating { get; set; }

/// <summary>
/// An internal state that can be used to store a value in order to prevent a closure in the click handler.
Expand Down Expand Up @@ -106,7 +100,7 @@ private void Restart(TimeSpan interval)
var elapsed = now - _lastTick;
if (elapsed >= interval)
{
RaiseTick();
RaiseTick(isTickForRestart: true);

if (IsRunning) // be sure to not self restart if the timer was Stopped by the Tick event handler
{
Expand All @@ -119,7 +113,7 @@ private void Restart(TimeSpan interval)
}
}

private void RaiseTick()
private void RaiseTick(bool isTickForRestart = false)
{
try
{
Expand All @@ -137,6 +131,11 @@ private void RaiseTick()
this.Log().Error("Raising tick failed", e);
}
}

if (!isTickForRestart && !IsRepeating)
{
Stop();
}
}

~DispatcherQueueTimer()
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/System/DispatcherQueueTimer.iOSmacOS.cs
Expand Up @@ -12,7 +12,7 @@ partial class DispatcherQueueTimer

private void StartNative(TimeSpan interval)
{
Start(NSTimer.CreateScheduledTimer(interval.TotalSeconds, repeats: true, block: OnRecurentTick));
Start(NSTimer.CreateScheduledTimer(interval.TotalSeconds, repeats: IsRepeating, block: OnRecurentTick));
}

private void StartNative(TimeSpan dueTime, TimeSpan interval)
Expand Down
26 changes: 23 additions & 3 deletions src/Uno.UWP/System/DispatcherQueueTimer.skia.cs
Expand Up @@ -2,26 +2,46 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Windows.System.Threading;
using Windows.UI.Core;

namespace Windows.System
{
partial class DispatcherQueueTimer
{
Timer _timer;

private void StartNative(TimeSpan interval)
{
throw new NotImplementedException("DispatcherQueueTimer not supported");
if (_timer == null)
{
_timer = new Timer(_ => DispatchRaiseTick());
}

_timer.Change(interval, interval);
}

private void StartNative(TimeSpan dueTime, TimeSpan interval)
{
throw new NotImplementedException("DispatcherQueueTimer not supported");
if (_timer == null)
{
_timer = new Timer(_ => DispatchRaiseTick());
}

_timer.Change(dueTime, interval);
}

private void DispatchRaiseTick()
{
CoreDispatcher.Main.RunAsync(CoreDispatcherPriority.Normal, _ => RaiseTick());
}

private void StopNative()
{
throw new NotImplementedException("DispatcherQueueTimer not supported");
_timer.Dispose();
_timer = null;
}
}
}

0 comments on commit 556e4ae

Please sign in to comment.