forked from xamarin/Essentials
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Compass.shared.cs
90 lines (70 loc) · 2.21 KB
/
Compass.shared.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System;
namespace Xamarin.Essentials
{
public static partial class Compass
{
static bool useSyncContext;
public static event CompassChangedEventHandler ReadingChanged;
public static bool IsMonitoring { get; private set; }
public static bool ApplyLowPassFilter { get; set; }
public static void Start(SensorSpeed sensorSpeed)
{
if (!IsSupported)
throw new FeatureNotSupportedException();
if (IsMonitoring)
return;
IsMonitoring = true;
useSyncContext = sensorSpeed == SensorSpeed.Normal || sensorSpeed == SensorSpeed.UI;
try
{
PlatformStart(sensorSpeed);
}
catch
{
IsMonitoring = false;
throw;
}
}
public static void Stop()
{
if (!IsSupported)
throw new FeatureNotSupportedException();
if (!IsMonitoring)
return;
IsMonitoring = false;
try
{
PlatformStop();
}
catch
{
IsMonitoring = true;
throw;
}
}
internal static void OnChanged(CompassData reading)
=> OnChanged(new CompassChangedEventArgs(reading));
internal static void OnChanged(CompassChangedEventArgs e)
{
var handler = ReadingChanged;
if (handler == null)
return;
if (useSyncContext)
MainThread.BeginInvokeOnMainThread(() => handler?.Invoke(e));
else
handler?.Invoke(e);
}
}
public delegate void CompassChangedEventHandler(CompassChangedEventArgs e);
public class CompassChangedEventArgs : EventArgs
{
internal CompassChangedEventArgs(CompassData reading) => Reading = reading;
public CompassData Reading { get; }
}
public struct CompassData
{
internal CompassData(double headingMagneticNorth) =>
HeadingMagneticNorth = headingMagneticNorth;
public double HeadingMagneticNorth { get; }
}
}