-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Compass.shared.cs
318 lines (272 loc) · 9.31 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#nullable enable
using System;
using Microsoft.Maui.ApplicationModel;
namespace Microsoft.Maui.Devices.Sensors
{
/// <summary>
/// Monitor changes to the orientation of the user's device.
/// </summary>
public interface ICompass
{
/// <summary>
/// Gets a value indicating whether reading the compass is supported on this device.
/// </summary>
bool IsSupported { get; }
/// <summary>
/// Gets if compass is actively being monitored.
/// </summary>
bool IsMonitoring { get; }
/// <summary>
/// Start monitoring for changes to the compass.
/// </summary>
/// <param name="sensorSpeed">The speed to monitor for changes.</param>
void Start(SensorSpeed sensorSpeed);
/// <summary>
/// Start monitoring for changes to the compass.
/// </summary>
/// <param name="sensorSpeed">The speed to monitor for changes.</param>
/// <param name="applyLowPassFilter">Whether or not to apply a moving average filter (only used on Android).</param>
void Start(SensorSpeed sensorSpeed, bool applyLowPassFilter);
/// <summary>
/// Stop monitoring for changes to the compass.
/// </summary>
void Stop();
/// <summary>
/// Occurs when compass reading changes.
/// </summary>
event EventHandler<CompassChangedEventArgs> ReadingChanged;
}
/// <summary>
/// Platform-specific APIs for use with <see cref="ICompass"/>.
/// </summary>
public interface IPlatformCompass
{
#if IOS || MACCATALYST
/// <summary>
/// Gets or sets if heading calibration should be shown.
/// </summary>
bool ShouldDisplayHeadingCalibration { get; set; }
#endif
}
/// <summary>
/// Monitor changes to the orientation of the user's device.
/// </summary>
public static class Compass
{
/// <summary>
/// Occurs when compass reading changes.
/// </summary>
public static event EventHandler<CompassChangedEventArgs> ReadingChanged
{
add => Current.ReadingChanged += value;
remove => Current.ReadingChanged -= value;
}
/// <summary>
/// Gets a value indicating whether reading the compass is supported on this device.
/// </summary>
public static bool IsSupported
=> Current.IsSupported;
/// <summary>
/// Gets a value indicating whether the compass is actively being monitored.
/// </summary>
public static bool IsMonitoring
=> Current.IsMonitoring;
/// <summary>
/// Start monitoring for changes to the compass.
/// </summary>
/// <remarks>
/// Will throw <see cref="FeatureNotSupportedException"/> if not supported on device.
/// Will throw <see cref="InvalidOperationException"/> if <see cref="IsMonitoring"/> is <see langword="true"/>.
/// </remarks>
/// <param name="sensorSpeed">The speed to monitor for changes.</param>
public static void Start(SensorSpeed sensorSpeed)
=> Start(sensorSpeed, true);
/// <summary>
/// Start monitoring for changes to the compass.
/// </summary>
/// <remarks>
/// Will throw <see cref="FeatureNotSupportedException"/> if not supported on device.
/// Will throw <see cref="InvalidOperationException"/> if <see cref="IsMonitoring"/> is <see langword="true"/>.
/// </remarks>
/// <param name="sensorSpeed">The speed to monitor for changes.</param>
/// <param name="applyLowPassFilter">Whether or not to apply a moving average filter (only used on Android).</param>
public static void Start(SensorSpeed sensorSpeed, bool applyLowPassFilter)
=> Current.Start(sensorSpeed, applyLowPassFilter);
/// <summary>
/// Stop monitoring for changes to the compass.
/// </summary>
public static void Stop()
=> Current.Stop();
#if IOS || MACCATALYST
/// <summary>
/// Gets or sets a value specifying whether the calibration screen should be displayed.
/// </summary>
/// <remarks>Only available on iOS.</remarks>
public static bool ShouldDisplayHeadingCalibration
{
get
{
if (Current is IPlatformCompass c)
return c.ShouldDisplayHeadingCalibration;
return false;
}
set
{
if (Current is IPlatformCompass c)
c.ShouldDisplayHeadingCalibration = value;
}
}
#endif
static ICompass Current => Devices.Sensors.Compass.Default;
static ICompass? defaultImplementation;
/// <summary>
/// Provides the default implementation for static usage of this API.
/// </summary>
public static ICompass Default =>
defaultImplementation ??= new CompassImplementation();
internal static void SetDefault(ICompass? implementation) =>
defaultImplementation = implementation;
}
/// <summary>
/// This class contains static extension methods for use with <see cref="ICompass"/>.
/// </summary>
public static class CompassExtensions
{
#if IOS || MACCATALYST
/// <summary>
/// Gets or sets a value specifying whether the calibration screen should be displayed.
/// </summary>
/// <remarks>Only available on iOS.</remarks>
public static void SetShouldDisplayHeadingCalibration(this ICompass compass, bool shouldDisplay)
{
if (compass is IPlatformCompass platform)
{
platform.ShouldDisplayHeadingCalibration = shouldDisplay;
}
}
#endif
}
/// <summary>
/// Event arguments when compass reading changes.
/// </summary>
public class CompassChangedEventArgs : EventArgs
{
/// <summary>
/// Initializes a new instance of the <see cref="CompassChangedEventArgs"/> class.
/// </summary>
/// <param name="reading">The compass data reading.</param>
public CompassChangedEventArgs(CompassData reading) =>
Reading = reading;
/// <summary>
/// The current values of the compass.
/// </summary>
public CompassData Reading { get; }
}
/// <summary>
/// Contains the orientation of the user's device.
/// </summary>
public readonly struct CompassData : IEquatable<CompassData>
{
/// <summary>
/// Initializes a new instance of the <see cref="CompassData"/> class.
/// </summary>
/// <param name="headingMagneticNorth">A reading of compass data for heading magnetic north.</param>
public CompassData(double headingMagneticNorth) =>
HeadingMagneticNorth = headingMagneticNorth;
/// <summary>
/// The heading (measured in degrees) relative to magnetic north.
/// </summary>
public double HeadingMagneticNorth { get; }
/// <summary>
/// Compares the underlying <see cref="CompassData"/> instances.
/// </summary>
/// <param name="obj">Object to compare with.</param>
/// <returns><see langword="true"/> if they are equal, otherwise <see langword="false"/>.</returns>
public override bool Equals(object? obj) =>
(obj is CompassData data) && Equals(data);
/// <summary>
/// Compares the underlying <see cref="CompassData.HeadingMagneticNorth"/> instances.
/// </summary>
/// <param name="other">Object to compare with.</param>
/// <returns><see langword="true"/> if they are equal, otherwise <see langword="false"/>.</returns>
public bool Equals(CompassData other) =>
HeadingMagneticNorth.Equals(other.HeadingMagneticNorth);
/// <summary>
/// Equality operator for equals.
/// </summary>
/// <param name="left">Left to compare.</param>
/// <param name="right">Right to compare.</param>
/// <returns><see langword="true"/> if objects are equal, otherwise <see langword="false"/>.</returns>
public static bool operator ==(CompassData left, CompassData right) =>
left.Equals(right);
/// <summary>
/// Inequality operator.
/// </summary>
/// <param name="left">Left to compare.</param>
/// <param name="right">Right to compare.</param>
/// <returns><see langword="true"/> if objects are not equal, otherwise <see langword="false"/>.</returns>
public static bool operator !=(CompassData left, CompassData right) =>
!left.Equals(right);
/// <inheritdoc cref="object.GetHashCode"/>
public override int GetHashCode() =>
HeadingMagneticNorth.GetHashCode();
/// <summary>
/// Returns a string representation of the current values of <see cref="CompassData.HeadingMagneticNorth"/>.
/// </summary>
/// <returns>A string representation of this instance in the format of <c>HeadingMagneticNorth: {value}</c>.</returns>
public override string ToString() =>
$"{nameof(HeadingMagneticNorth)}: {HeadingMagneticNorth}";
}
partial class CompassImplementation : ICompass
{
bool UseSyncContext => SensorSpeed == SensorSpeed.Default || SensorSpeed == SensorSpeed.UI;
public event EventHandler<CompassChangedEventArgs>? ReadingChanged;
public bool IsSupported
=> PlatformIsSupported;
public bool IsMonitoring { get; private set; }
SensorSpeed SensorSpeed { get; set; }
public void Start(SensorSpeed sensorSpeed) => Start(sensorSpeed, true);
public void Start(SensorSpeed sensorSpeed, bool applyLowPassFilter)
{
if (!PlatformIsSupported)
throw new FeatureNotSupportedException();
if (IsMonitoring)
throw new InvalidOperationException("Compass has already been started.");
IsMonitoring = true;
try
{
PlatformStart(sensorSpeed, applyLowPassFilter);
}
catch
{
IsMonitoring = false;
throw;
}
}
public void Stop()
{
if (!PlatformIsSupported)
throw new FeatureNotSupportedException();
if (!IsMonitoring)
return;
IsMonitoring = false;
try
{
PlatformStop();
}
catch
{
IsMonitoring = true;
throw;
}
}
internal void RaiseReadingChanged(CompassData data)
{
var args = new CompassChangedEventArgs(data);
if (UseSyncContext)
MainThread.BeginInvokeOnMainThread(() => ReadingChanged?.Invoke(null, args));
else
ReadingChanged?.Invoke(null, args);
}
}
}