forked from reactiveui/ReactiveUI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAndroidUIScheduler.cs
69 lines (56 loc) · 2.07 KB
/
AndroidUIScheduler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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 :