Skip to content

Commit

Permalink
Add a UI scheduler for Android
Browse files Browse the repository at this point in the history
  • Loading branch information
anaisbetts committed Oct 23, 2012
1 parent a68f4cb commit cb0a2c9
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 16 deletions.
69 changes: 69 additions & 0 deletions ReactiveUI.Android/AndroidUIScheduler.cs
@@ -0,0 +1,69 @@
using System;
using System.Linq;
using System.Collections.Generic;
using System.Reactive;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using ReactiveUI;
using Android.App;

namespace ReactiveUI.Android
{
public class AndroidUIScheduler : IScheduler, IEnableLogger
{
Activity activity;

public AndroidUIScheduler(Activity activity)
{
this.activity = activity;
}

public DateTimeOffset Now {
get { return DateTimeOffset.Now; }
}

public IDisposable Schedule<TState>(TState state, Func<IScheduler, TState, IDisposable> action)
{
IDisposable innerDisp = Disposable.Empty;
activity.RunOnUiThread(new Action(() => innerDisp = action(this, state)));

return innerDisp;
}

public IDisposable Schedule<TState>(TState state, DateTimeOffset dueTime, Func<IScheduler, TState, IDisposable> action)
{
if (dueTime <= Now) {
return Schedule(state, action);
}

return Schedule(state, dueTime - Now, action);
}

public IDisposable Schedule<TState>(TState state, TimeSpan dueTime, Func<IScheduler, TState, IDisposable> action)
{
bool isCancelled = false;

var disp = Scheduler.TaskPool.Schedule(state, dueTime, (sched, st) => {
IDisposable innerDisp = Disposable.Empty;
if (!isCancelled) {
activity.RunOnUiThread(() => {
if (!isCancelled) innerDisp = action(this, state);
});
}
return Disposable.Create(() => {
isCancelled = true;
innerDisp.Dispose();
});
});

return Disposable.Create(() => {
isCancelled = true;
disp.Dispose();
});
}
}
}

// vim: tw=120 ts=4 sw=4 et :
78 changes: 78 additions & 0 deletions ReactiveUI.Android/ReactiveUI.Android.csproj
@@ -0,0 +1,78 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>10.0.0</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{55A6E11B-B074-4A0B-B937-267D840E31DF}</ProjectGuid>
<ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<RootNamespace>ReactiveUI.Android</RootNamespace>
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<AssemblyName>ReactiveUI.Android</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>True</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>False</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<AndroidLinkMode>None</AndroidLinkMode>
<ConsolePause>False</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>True</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>False</ConsolePause>
<AndroidLinkMode>SdkOnly</AndroidLinkMode>
<AndroidUseSharedRuntime>False</AndroidUseSharedRuntime>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="Mono.Android" />
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Novell\Novell.MonoDroid.CSharp.targets" />
<ItemGroup>
<ProjectReference Include="..\ext\mono.reactive\System.Reactive.Core\System.Reactive.Core.csproj">
<Project>{AD27C5E8-8A0C-454D-B7AA-76A54793B81F}</Project>
<Name>System.Reactive.Core</Name>
</ProjectReference>
<ProjectReference Include="..\ext\mono.reactive\System.Reactive.Interfaces\System.Reactive.Interfaces.csproj">
<Project>{755E549E-1BDE-4153-A58B-7C969C902CAD}</Project>
<Name>System.Reactive.Interfaces</Name>
</ProjectReference>
<ProjectReference Include="..\ext\mono.reactive\System.Reactive.Linq\System.Reactive.Linq.csproj">
<Project>{F0395470-330B-4DBF-B1A5-D873E597DD05}</Project>
<Name>System.Reactive.Linq</Name>
</ProjectReference>
<ProjectReference Include="..\ext\mono.reactive\System.Reactive.PlatformServices\System.Reactive.PlatformServices.csproj">
<Project>{AEEBFC85-91B8-410F-BBB7-BD12BBC61BE6}</Project>
<Name>System.Reactive.PlatformServices</Name>
</ProjectReference>
<ProjectReference Include="..\ext\mono.reactive\System.Reactive.Providers2\System.Reactive.Providers2.csproj">
<Project>{9652D18A-CF8D-40D7-BC1D-2090E58EC1FF}</Project>
<Name>System.Reactive.Providers2</Name>
</ProjectReference>
<ProjectReference Include="..\ReactiveUI\ReactiveUI_Android.csproj">
<Project>{292A477B-BB94-43C1-984E-E177EF9FEDB7}</Project>
<Name>ReactiveUI_Android</Name>
</ProjectReference>
<ProjectReference Include="..\ReactiveUI.Xaml\ReactiveUI.Xaml_Android.csproj">
<Project>{761AC9BA-4A9C-440C-9B6C-2569978F0610}</Project>
<Name>ReactiveUI.Xaml_Android</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Compile Include="AndroidUIScheduler.cs" />
</ItemGroup>
</Project>
1 change: 1 addition & 0 deletions ReactiveUI/RxApp.cs
Expand Up @@ -391,6 +391,7 @@ static IEnumerable<string> attemptToEarlyLoadReactiveUIDLLs()
"ReactiveUI.Xaml",
"ReactiveUI.Gtk",
"ReactiveUI.Cocoa",
"ReactiveUI.Android",
"ReactiveUI.NLog",
};

Expand Down
32 changes: 16 additions & 16 deletions ReactiveUI_Mono.sln
Expand Up @@ -34,14 +34,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Reactive.Linq", "ext
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "System.Reactive.Providers2", "ext\mono.reactive\System.Reactive.Providers2\System.Reactive.Providers2.csproj", "{9652D18A-CF8D-40D7-BC1D-2090E58EC1FF}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Mono.Reactive.Testing2", "ext\mono.reactive\Mono.Reactive.Testing2\Mono.Reactive.Testing2.csproj", "{25A4B33D-EE55-4F25-98A5-3148A5875247}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReactiveUI_Android", "ReactiveUI\ReactiveUI_Android.csproj", "{292A477B-BB94-43C1-984E-E177EF9FEDB7}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReactiveUI.Xaml_Android", "ReactiveUI.Xaml\ReactiveUI.Xaml_Android.csproj", "{761AC9BA-4A9C-440C-9B6C-2569978F0610}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReactiveUI.Routing_Android", "ReactiveUI.Routing\ReactiveUI.Routing_Android.csproj", "{67038157-092E-4D72-BAF4-049EB1532264}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ReactiveUI.Android", "ReactiveUI.Android\ReactiveUI.Android.csproj", "{55A6E11B-B074-4A0B-B937-267D840E31DF}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -67,20 +67,6 @@ Global
{0913BF79-061F-4667-ADF9-8E6CDA6D1213}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{0913BF79-061F-4667-ADF9-8E6CDA6D1213}.Release|x86.ActiveCfg = Release|Any CPU
{0913BF79-061F-4667-ADF9-8E6CDA6D1213}.Release|x86.Build.0 = Release|Any CPU
{25A4B33D-EE55-4F25-98A5-3148A5875247}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
{25A4B33D-EE55-4F25-98A5-3148A5875247}.AppStore|Any CPU.Build.0 = Debug|Any CPU
{25A4B33D-EE55-4F25-98A5-3148A5875247}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{25A4B33D-EE55-4F25-98A5-3148A5875247}.Debug|Any CPU.Build.0 = Debug|Any CPU
{25A4B33D-EE55-4F25-98A5-3148A5875247}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{25A4B33D-EE55-4F25-98A5-3148A5875247}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{25A4B33D-EE55-4F25-98A5-3148A5875247}.Debug|x86.ActiveCfg = Debug|Any CPU
{25A4B33D-EE55-4F25-98A5-3148A5875247}.Debug|x86.Build.0 = Debug|Any CPU
{25A4B33D-EE55-4F25-98A5-3148A5875247}.Release|Any CPU.ActiveCfg = Release|Any CPU
{25A4B33D-EE55-4F25-98A5-3148A5875247}.Release|Any CPU.Build.0 = Release|Any CPU
{25A4B33D-EE55-4F25-98A5-3148A5875247}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{25A4B33D-EE55-4F25-98A5-3148A5875247}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{25A4B33D-EE55-4F25-98A5-3148A5875247}.Release|x86.ActiveCfg = Release|Any CPU
{25A4B33D-EE55-4F25-98A5-3148A5875247}.Release|x86.Build.0 = Release|Any CPU
{292A477B-BB94-43C1-984E-E177EF9FEDB7}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
{292A477B-BB94-43C1-984E-E177EF9FEDB7}.AppStore|Any CPU.Build.0 = Debug|Any CPU
{292A477B-BB94-43C1-984E-E177EF9FEDB7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -119,6 +105,20 @@ Global
{482DE1CC-A08C-49F7-AB36-445DC9724482}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{482DE1CC-A08C-49F7-AB36-445DC9724482}.Release|x86.ActiveCfg = Release|Any CPU
{482DE1CC-A08C-49F7-AB36-445DC9724482}.Release|x86.Build.0 = Release|Any CPU
{55A6E11B-B074-4A0B-B937-267D840E31DF}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
{55A6E11B-B074-4A0B-B937-267D840E31DF}.AppStore|Any CPU.Build.0 = Debug|Any CPU
{55A6E11B-B074-4A0B-B937-267D840E31DF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{55A6E11B-B074-4A0B-B937-267D840E31DF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{55A6E11B-B074-4A0B-B937-267D840E31DF}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{55A6E11B-B074-4A0B-B937-267D840E31DF}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{55A6E11B-B074-4A0B-B937-267D840E31DF}.Debug|x86.ActiveCfg = Debug|Any CPU
{55A6E11B-B074-4A0B-B937-267D840E31DF}.Debug|x86.Build.0 = Debug|Any CPU
{55A6E11B-B074-4A0B-B937-267D840E31DF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{55A6E11B-B074-4A0B-B937-267D840E31DF}.Release|Any CPU.Build.0 = Release|Any CPU
{55A6E11B-B074-4A0B-B937-267D840E31DF}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{55A6E11B-B074-4A0B-B937-267D840E31DF}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{55A6E11B-B074-4A0B-B937-267D840E31DF}.Release|x86.ActiveCfg = Release|Any CPU
{55A6E11B-B074-4A0B-B937-267D840E31DF}.Release|x86.Build.0 = Release|Any CPU
{67038157-092E-4D72-BAF4-049EB1532264}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
{67038157-092E-4D72-BAF4-049EB1532264}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
{67038157-092E-4D72-BAF4-049EB1532264}.AppStore|Any CPU.Build.0 = Debug|Any CPU
Expand Down

0 comments on commit cb0a2c9

Please sign in to comment.