Make core Ticker abstract and public (#116)

* Make core Ticker abstract and public
Make the core Ticker abstract and public so it can be inherited by platform
implementations; remove now-unused parts of original Ticker; add CreateTicker
to IPlatformServices; remove unused CreateTimer methods from IPlatformServices

* Add docs for Ticker

* Remove unnecessary Ticker.Default set

* Move Ticker into Internals

* Update Ticker docs

* Remove old Ticker docs

* Remove commented code
This commit is contained in:
E.Z. Hart 2016-04-25 13:57:44 -06:00 коммит произвёл Rui Marinho
Родитель 07df05ced2
Коммит 843bc4727a
22 изменённых файлов: 190 добавлений и 133 удалений

Просмотреть файл

@ -9,6 +9,8 @@ using Xamarin.Forms;
using Xamarin.Forms.Core.UnitTests;
using System.Security.Cryptography;
using System.Text;
using Xamarin.Forms.Internals;
#if WINDOWS_PHONE
using Xamarin.Forms.Platform.WinPhone;
#endif
@ -88,6 +90,11 @@ namespace Xamarin.Forms.Core.UnitTests
invokeOnMainThread (action);
}
public Ticker CreateTicker()
{
return new MockTicker();
}
public void StartTimer (TimeSpan interval, Func<bool> callback)
{
Timer timer = null;
@ -267,4 +274,24 @@ namespace Xamarin.Forms.Core.UnitTests
{
}
}
internal class MockTicker : Ticker
{
bool _enabled;
protected override void EnableTimer()
{
_enabled = true;
while (_enabled)
{
SendSignals(16);
}
}
protected override void DisableTimer()
{
_enabled = false;
}
}
}

Просмотреть файл

@ -1,25 +1,26 @@
using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Core.UnitTests
{
internal class BlockingTicker : Ticker
{
bool enabled;
bool _enabled;
protected override void EnableTimer ()
{
enabled = true;
_enabled = true;
while (enabled) {
while (_enabled) {
SendSignals (16);
}
}
protected override void DisableTimer ()
{
enabled = false;
_enabled = false;
}
}

Просмотреть файл

@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Core.UnitTests
{

Просмотреть файл

@ -4,6 +4,7 @@ using System.Collections.Specialized;
using System.Threading.Tasks;
using NUnit.Framework;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Core.UnitTests
{

Просмотреть файл

@ -26,6 +26,7 @@
using System;
using System.Collections.Generic;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms
{

Просмотреть файл

@ -3,6 +3,7 @@ using System.IO;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms
{
@ -12,12 +13,7 @@ namespace Xamarin.Forms
void BeginInvokeOnMainThread(Action action);
//this will go once Timer is included in Pcl profiles
ITimer CreateTimer(Action<object> callback);
ITimer CreateTimer(Action<object> callback, object state, int dueTime, int period);
ITimer CreateTimer(Action<object> callback, object state, long dueTime, long period);
ITimer CreateTimer(Action<object> callback, object state, TimeSpan dueTime, TimeSpan period);
ITimer CreateTimer(Action<object> callback, object state, uint dueTime, uint period);
Ticker CreateTicker();
Assembly[] GetAssemblies();

Просмотреть файл

@ -2,26 +2,21 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
namespace Xamarin.Forms
namespace Xamarin.Forms.Internals
{
internal class Ticker
public abstract class Ticker
{
static Ticker s_ticker;
readonly Stopwatch _stopwatch;
readonly SynchronizationContext _sync;
readonly List<Tuple<int, Func<long, bool>>> _timeouts;
readonly ITimer _timer;
int _count;
bool _enabled;
internal Ticker()
{
_sync = SynchronizationContext.Current;
_count = 0;
_timer = Device.PlatformServices.CreateTimer(HandleElapsed, null, Timeout.Infinite, Timeout.Infinite);
_timeouts = new List<Tuple<int, Func<long, bool>>>();
_stopwatch = new Stopwatch();
@ -30,7 +25,7 @@ namespace Xamarin.Forms
public static Ticker Default
{
internal set { s_ticker = value; }
get { return s_ticker ?? (s_ticker = new Ticker()); }
get { return s_ticker ?? (s_ticker = Device.PlatformServices.CreateTicker()); }
}
public virtual int Insert(Func<long, bool> timeout)
@ -61,16 +56,10 @@ namespace Xamarin.Forms
});
}
protected virtual void DisableTimer()
{
_timer.Change(Timeout.Infinite, Timeout.Infinite);
}
protected virtual void EnableTimer()
{
_timer.Change(16, 16);
}
protected abstract void DisableTimer();
protected abstract void EnableTimer();
protected void SendSignals(int timestep = -1)
{
long step = timestep >= 0 ? timestep : _stopwatch.ElapsedMilliseconds;
@ -103,15 +92,5 @@ namespace Xamarin.Forms
_stopwatch.Start();
EnableTimer();
}
void HandleElapsed(object state)
{
if (_timeouts.Count > 0)
{
_sync.Post(o => SendSignals(), null);
_stopwatch.Reset();
_stopwatch.Start();
}
}
}
}

Просмотреть файл

@ -25,6 +25,7 @@
// THE SOFTWARE.
using System;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms
{

Просмотреть файл

@ -232,7 +232,7 @@
<Compile Include="TextChangedEventArgs.cs" />
<Compile Include="TextKeyboard.cs" />
<Compile Include="ThicknessTypeConverter.cs" />
<Compile Include="Ticker.cs" />
<Compile Include="Internals\Ticker.cs" />
<Compile Include="ToggledEventArgs.cs" />
<Compile Include="ToolbarItemEventArgs.cs" />
<Compile Include="ToolbarItemOrder.cs" />

Просмотреть файл

@ -1,5 +1,6 @@
using System;
using Android.Animation;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms.Platform.Android
{

Просмотреть файл

@ -15,6 +15,7 @@ using Android.Content;
using Android.Content.Res;
using Android.OS;
using Android.Util;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.Android;
using Resource = Android.Resource;
using Trace = System.Diagnostics.Trace;
@ -247,29 +248,9 @@ namespace Xamarin.Forms
activity.RunOnUiThread(action);
}
public ITimer CreateTimer(Action<object> callback)
public Ticker CreateTicker()
{
return new _Timer(new Timer(o => callback(o)));
}
public ITimer CreateTimer(Action<object> callback, object state, int dueTime, int period)
{
return new _Timer(new Timer(o => callback(o), state, dueTime, period));
}
public ITimer CreateTimer(Action<object> callback, object state, long dueTime, long period)
{
return new _Timer(new Timer(o => callback(o), state, dueTime, period));
}
public ITimer CreateTimer(Action<object> callback, object state, TimeSpan dueTime, TimeSpan period)
{
return new _Timer(new Timer(o => callback(o), state, dueTime, period));
}
public ITimer CreateTimer(Action<object> callback, object state, uint dueTime, uint period)
{
return new _Timer(new Timer(o => callback(o), state, dueTime, period));
return new AndroidTicker();
}
public Assembly[] GetAssemblies()

Просмотреть файл

@ -7,6 +7,7 @@ using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
using Microsoft.Phone.Controls;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.WinPhone;
using Expression = System.Linq.Expressions.Expression;

Просмотреть файл

@ -9,6 +9,7 @@ using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using Windows.System;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.WinPhone;
namespace Xamarin.Forms
@ -22,29 +23,9 @@ namespace Xamarin.Forms
Deployment.Current.Dispatcher.BeginInvoke(action);
}
public ITimer CreateTimer(Action<object> callback)
public Ticker CreateTicker()
{
return new _Timer(new Timer(o => callback(o)));
}
public ITimer CreateTimer(Action<object> callback, object state, int dueTime, int period)
{
return new _Timer(new Timer(o => callback(o), state, dueTime, period));
}
public ITimer CreateTimer(Action<object> callback, object state, long dueTime, long period)
{
return new _Timer(new Timer(o => callback(o), state, dueTime, period));
}
public ITimer CreateTimer(Action<object> callback, object state, TimeSpan dueTime, TimeSpan period)
{
return new _Timer(new Timer(o => callback(o), state, dueTime, period));
}
public ITimer CreateTimer(Action<object> callback, object state, uint dueTime, uint period)
{
return new _Timer(new Timer(o => callback(o), state, dueTime, period));
return new WinPhoneTicker();
}
public Assembly[] GetAssemblies()

Просмотреть файл

@ -1,5 +1,6 @@
using System;
using System.Windows.Threading;
using Xamarin.Forms.Internals;
namespace Xamarin.Forms
{

Просмотреть файл

@ -6,6 +6,7 @@ using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;
using Xamarin.Forms.Internals;
using Xamarin.Forms.Platform.WinRT;
namespace Xamarin.Forms

Просмотреть файл

@ -59,8 +59,6 @@ namespace Xamarin.Forms
break;
}
#endif
Ticker.Default = new WindowsTicker();
ExpressionSearch.Default = new WindowsExpressionSearch();
#if WINDOWS_UWP

Просмотреть файл

@ -17,6 +17,7 @@ using Windows.Storage.Streams;
using Windows.System;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Xamarin.Forms.Internals;
#if WINDOWS_UWP
@ -28,7 +29,7 @@ namespace Xamarin.Forms.Platform.WinRT
{
internal abstract class WindowsBasePlatformServices : IPlatformServices
{
CoreDispatcher _dispatcher;
CoreDispatcher _dispatcher;
public WindowsBasePlatformServices(CoreDispatcher dispatcher)
{
@ -43,29 +44,9 @@ namespace Xamarin.Forms.Platform.WinRT
_dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action()).WatchForError();
}
public ITimer CreateTimer(Action<object> callback)
public Ticker CreateTicker()
{
return new WindowsTimer(new Timer(o => callback(o), null, Timeout.Infinite, Timeout.Infinite));
}
public ITimer CreateTimer(Action<object> callback, object state, int dueTime, int period)
{
return new WindowsTimer(new Timer(o => callback(o), state, dueTime, period));
}
public ITimer CreateTimer(Action<object> callback, object state, long dueTime, long period)
{
return CreateTimer(callback, state, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period));
}
public ITimer CreateTimer(Action<object> callback, object state, TimeSpan dueTime, TimeSpan period)
{
return new WindowsTimer(new Timer(o => callback(o), state, dueTime, period));
}
public ITimer CreateTimer(Action<object> callback, object state, uint dueTime, uint period)
{
return CreateTimer(callback, state, TimeSpan.FromMilliseconds(dueTime), TimeSpan.FromMilliseconds(period));
return new WindowsTicker();
}
public virtual Assembly[] GetAssemblies()

Просмотреть файл

@ -1,8 +1,8 @@
using System;
using Windows.UI.Xaml;
using Xamarin.Forms.Internals;
#if WINDOWS_UWP
namespace Xamarin.Forms.Platform.UWP
#else
@ -11,7 +11,7 @@ namespace Xamarin.Forms.Platform.WinRT
{
internal class WindowsTicker : Ticker
{
DispatcherTimer _timer;
readonly DispatcherTimer _timer;
public WindowsTicker()
{

Просмотреть файл

@ -1,6 +1,7 @@
using System;
using System.Collections.Concurrent;
using System.Threading;
using Xamarin.Forms.Internals;
#if __UNIFIED__
using UIKit;
using CoreAnimation;

Просмотреть файл

@ -11,6 +11,7 @@ using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Xamarin.Forms.Internals;
#if __UNIFIED__
using UIKit;
using CoreFoundation;
@ -86,7 +87,6 @@ namespace Xamarin.Forms
Device.PlatformServices = new IOSPlatformServices();
Device.Info = new IOSDeviceInfo();
Ticker.Default = new CADisplayLinkTicker();
Registrar.RegisterAll(new[] { typeof(ExportRendererAttribute), typeof(ExportCellAttribute), typeof(ExportImageSourceHandlerAttribute) });
Device.Idiom = UIDevice.CurrentDevice.UserInterfaceIdiom == UIUserInterfaceIdiom.Pad ? TargetIdiom.Tablet : TargetIdiom.Phone;
@ -173,29 +173,9 @@ namespace Xamarin.Forms
NSRunLoop.Main.BeginInvokeOnMainThread(action.Invoke);
}
public ITimer CreateTimer(Action<object> callback)
public Ticker CreateTicker()
{
return new _Timer(new Timer(o => callback(o)));
}
public ITimer CreateTimer(Action<object> callback, object state, int dueTime, int period)
{
return new _Timer(new Timer(o => callback(o), state, dueTime, period));
}
public ITimer CreateTimer(Action<object> callback, object state, long dueTime, long period)
{
return new _Timer(new Timer(o => callback(o), state, dueTime, period));
}
public ITimer CreateTimer(Action<object> callback, object state, TimeSpan dueTime, TimeSpan period)
{
return new _Timer(new Timer(o => callback(o), state, dueTime, period));
}
public ITimer CreateTimer(Action<object> callback, object state, uint dueTime, uint period)
{
return new _Timer(new Timer(o => callback(o), state, dueTime, period));
return new CADisplayLinkTicker();
}
public Assembly[] GetAssemblies()

Просмотреть файл

@ -630,4 +630,4 @@
<Error Condition="!Exists('$(SolutionDir)\.nuget\NuGet.targets')" Text="$([System.String]::Format('$(ErrorText)', '$(SolutionDir)\.nuget\NuGet.targets'))" />
</Target>
<ItemGroup />
</Project>
</Project>

Просмотреть файл

@ -0,0 +1,124 @@
<Type Name="Ticker" FullName="Xamarin.Forms.Internals.Ticker">
<TypeSignature Language="C#" Value="public abstract class Ticker" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi abstract beforefieldinit Ticker extends System.Object" />
<AssemblyInfo>
<AssemblyName>Xamarin.Forms.Core</AssemblyName>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Base>
<BaseTypeName>System.Object</BaseTypeName>
</Base>
<Interfaces />
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
<Members>
<Member MemberName="Default">
<MemberSignature Language="C#" Value="public static Xamarin.Forms.Internals.Ticker Default { get; }" />
<MemberSignature Language="ILAsm" Value=".property class Xamarin.Forms.Internals.Ticker Default" />
<MemberType>Property</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>Xamarin.Forms.Internals.Ticker</ReturnType>
</ReturnValue>
<Docs>
<summary>To be added.</summary>
<value>To be added.</value>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="DisableTimer">
<MemberSignature Language="C#" Value="protected abstract void DisableTimer ();" />
<MemberSignature Language="ILAsm" Value=".method familyhidebysig newslot virtual instance void DisableTimer() cil managed" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="EnableTimer">
<MemberSignature Language="C#" Value="protected abstract void EnableTimer ();" />
<MemberSignature Language="ILAsm" Value=".method familyhidebysig newslot virtual instance void EnableTimer() cil managed" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters />
<Docs>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="Insert">
<MemberSignature Language="C#" Value="public virtual int Insert (Func&lt;long,bool&gt; timeout);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig newslot virtual instance int32 Insert(class System.Func`2&lt;int64, bool&gt; timeout) cil managed" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Int32</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="timeout" Type="System.Func&lt;System.Int64,System.Boolean&gt;" />
</Parameters>
<Docs>
<param name="timeout">To be added.</param>
<summary>To be added.</summary>
<returns>To be added.</returns>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="Remove">
<MemberSignature Language="C#" Value="public virtual void Remove (int handle);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig newslot virtual instance void Remove(int32 handle) cil managed" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="handle" Type="System.Int32" />
</Parameters>
<Docs>
<param name="handle">To be added.</param>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName="SendSignals">
<MemberSignature Language="C#" Value="protected void SendSignals (int timestep = -1);" />
<MemberSignature Language="ILAsm" Value=".method familyhidebysig instance void SendSignals(int32 timestep) cil managed" />
<MemberType>Method</MemberType>
<AssemblyInfo>
<AssemblyVersion>2.0.0.0</AssemblyVersion>
</AssemblyInfo>
<ReturnValue>
<ReturnType>System.Void</ReturnType>
</ReturnValue>
<Parameters>
<Parameter Name="timestep" Type="System.Int32" />
</Parameters>
<Docs>
<param name="timestep">To be added.</param>
<summary>To be added.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>
</Members>
</Type>