Skip to content

Commit

Permalink
Merge pull request #19166 from nickodei/use-startstopeventwrapper-for…
Browse files Browse the repository at this point in the history
…-sensors
  • Loading branch information
MartinZikmund authored Jan 14, 2025
2 parents 11272fa + 5ac8688 commit 4bc6ea1
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 252 deletions.
2 changes: 1 addition & 1 deletion src/Uno.UWP/Devices/Sensors/Accelerometer.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public uint ReportInterval
{
_reportInterval = value;

if (_readingChanged != null)
if (_readingChangedWrapper.Event != null)
{
//restart reading to apply interval
StopReadingChanged();
Expand Down
74 changes: 21 additions & 53 deletions src/Uno.UWP/Devices/Sensors/Accelerometer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System;
using System.Collections.Generic;
using System.Text;
using Uno.Helpers;
using Windows.Foundation;

namespace Windows.Devices.Sensors
{
Expand All @@ -10,13 +12,13 @@ namespace Windows.Devices.Sensors
/// </summary>
public partial class Accelerometer
{
private readonly static object _syncLock = new object();
private readonly static object _syncLock = new();

private static Accelerometer _instance;
private static bool _initializationAttempted;

private Foundation.TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs> _readingChanged;
private Foundation.TypedEventHandler<Accelerometer, AccelerometerShakenEventArgs> _shaken;
private readonly StartStopTypedEventWrapper<Accelerometer, AccelerometerReadingChangedEventArgs> _readingChangedWrapper;
private readonly StartStopTypedEventWrapper<Accelerometer, AccelerometerShakenEventArgs> _shakenWrapper;

/// <summary>
/// Gets or sets the transformation that needs to be applied to sensor data. Transformations to be applied are tied to the display orientation with which to align the sensor data.
Expand All @@ -32,6 +34,14 @@ public partial class Accelerometer
/// </summary>
private Accelerometer()
{
_readingChangedWrapper = new StartStopTypedEventWrapper<Accelerometer, AccelerometerReadingChangedEventArgs>(
() => StartReadingChanged(),
() => StopReadingChanged(),
_syncLock);
_shakenWrapper = new StartStopTypedEventWrapper<Accelerometer, AccelerometerShakenEventArgs>(
() => StartShaken(),
() => StopShaken(),
_syncLock);
}

/// <summary>
Expand All @@ -58,71 +68,29 @@ public static Accelerometer GetDefault()
/// <summary>
/// Occurs each time the accelerometer reports a new sensor reading.
/// </summary>
public event Foundation.TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs> ReadingChanged
public event TypedEventHandler<Accelerometer, AccelerometerReadingChangedEventArgs> ReadingChanged
{
add
{
lock (_syncLock)
{
bool isFirstSubscriber = _readingChanged == null;
_readingChanged += value;
if (isFirstSubscriber)
{
StartReadingChanged();
}
}
}
remove
{
lock (_syncLock)
{
_readingChanged -= value;
if (_readingChanged == null)
{
StopReadingChanged();
}
}
}
add => _readingChangedWrapper.AddHandler(value);
remove => _readingChangedWrapper.RemoveHandler(value);
}

/// <summary>
/// Occurs when the accelerometer detects that the device has been shaken.
/// </summary>
public event Foundation.TypedEventHandler<Accelerometer, AccelerometerShakenEventArgs> Shaken
public event TypedEventHandler<Accelerometer, AccelerometerShakenEventArgs> Shaken
{
add
{
lock (_syncLock)
{
bool isFirstSubscriber = _shaken == null;
_shaken += value;
if (isFirstSubscriber)
{
StartShaken();
}
}
}
remove
{
lock (_syncLock)
{
_shaken -= value;
if (_shaken == null)
{
StopShaken();
}
}
}
add => _shakenWrapper.AddHandler(value);
remove => _shakenWrapper.RemoveHandler(value);
}

private void OnReadingChanged(AccelerometerReading reading)
{
_readingChanged?.Invoke(this, new AccelerometerReadingChangedEventArgs(reading));
_readingChangedWrapper.Invoke(this, new AccelerometerReadingChangedEventArgs(reading));
}

internal void OnShaken(DateTimeOffset timestamp)
{
_shaken?.Invoke(this, new AccelerometerShakenEventArgs(timestamp));
_shakenWrapper.Invoke(this, new AccelerometerShakenEventArgs(timestamp));
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UWP/Devices/Sensors/Accelerometer.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private void AttachDeviceMotion()
{
//if both delegates are not null,
//we have already started reading previously
if (_shaken == null || _readingChanged == null)
if (_shakenWrapper.Event == null || _readingChangedWrapper.Event == null)
{
NativeMethods.StartReading();
}
Expand All @@ -64,7 +64,7 @@ private void AttachDeviceMotion()
private void DetachDeviceMotion()
{
//we only stop when both are null
if (_shaken == null && _readingChanged == null)
if (_shakenWrapper.Event == null && _readingChangedWrapper.Event == null)
{
NativeMethods.StopReading();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Uno.UWP/Devices/Sensors/Barometer.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public uint ReportInterval
{
_reportInterval = value;

if (_readingChanged != null)
if (_readingChangedWrapper.Event != null)
{
//restart reading to apply interval
StopReading();
Expand Down Expand Up @@ -96,7 +96,7 @@ void ISensorEventListener.OnSensorChanged(SensorEvent? e)
var barometerReading = new BarometerReading(
values[0],
SensorHelpers.TimestampToDateTimeOffset(e.Timestamp));
_barometer._readingChanged?.Invoke(
_barometer._readingChangedWrapper?.Invoke(
_barometer,
new BarometerReadingChangedEventArgs(barometerReading));
}
Expand Down
35 changes: 10 additions & 25 deletions src/Uno.UWP/Devices/Sensors/Barometer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#if __ANDROID__ || __IOS__

using Uno.Helpers;
using Windows.Foundation;

namespace Windows.Devices.Sensors
Expand All @@ -9,17 +10,22 @@ namespace Windows.Devices.Sensors
/// </summary>
public partial class Barometer
{
private static readonly object _syncLock = new object();
private static readonly object _syncLock = new();

private static bool _initializationAttempted;
private static Barometer _instance;

private TypedEventHandler<Barometer, BarometerReadingChangedEventArgs> _readingChanged;
private readonly StartStopTypedEventWrapper<Barometer, BarometerReadingChangedEventArgs> _readingChangedWrapper;

/// <summary>
/// Hides the public parameterless constructor
/// </summary>
private Barometer()
{
_readingChangedWrapper = new StartStopTypedEventWrapper<Barometer, BarometerReadingChangedEventArgs>(
() => StartReading(),
() => StopReading(),
_syncLock);
}

/// <summary>
Expand Down Expand Up @@ -48,29 +54,8 @@ public static Barometer GetDefault()
/// </summary>
public event TypedEventHandler<Barometer, BarometerReadingChangedEventArgs> ReadingChanged
{
add
{
lock (_syncLock)
{
bool isFirstSubscriber = _readingChanged == null;
_readingChanged += value;
if (isFirstSubscriber)
{
StartReading();
}
}
}
remove
{
lock (_syncLock)
{
_readingChanged -= value;
if (_readingChanged == null)
{
StopReading();
}
}
}
add => _readingChangedWrapper.AddHandler(value);
remove => _readingChangedWrapper.RemoveHandler(value);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Devices/Sensors/Barometer.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ private void RelativeAltitudeUpdateReceived(CMAltitudeData data, NSError error)
var barometerReading = new BarometerReading(
KPaToHPa(data.Pressure.DoubleValue),
SensorHelpers.TimestampToDateTimeOffset(data.Timestamp));
_readingChanged?.Invoke(
_readingChangedWrapper.Invoke(
this,
new BarometerReadingChangedEventArgs(barometerReading));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Devices/Sensors/Compass.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public uint ReportInterval

_reportInterval = value;

if (_readingChanged != null)
if (_readingChangedWrapper.Event != null)
{
//restart reading to apply interval
StopReadingChanged();
Expand Down
34 changes: 9 additions & 25 deletions src/Uno.UWP/Devices/Sensors/Compass.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#if __IOS__ || __ANDROID__ || __WASM__
using Uno.Helpers;
using Windows.Foundation;

namespace Windows.Devices.Sensors;
Expand All @@ -14,13 +15,17 @@ public partial class Compass
private static Compass _instance;
private static bool _initializationAttempted;

private TypedEventHandler<Compass, CompassReadingChangedEventArgs> _readingChanged;
private readonly StartStopTypedEventWrapper<Compass, CompassReadingChangedEventArgs> _readingChangedWrapper;

/// <summary>
/// Hides the public parameterless constructor
/// </summary>
private Compass()
{
_readingChangedWrapper = new StartStopTypedEventWrapper<Compass, CompassReadingChangedEventArgs>(
() => StartReadingChanged(),
() => StopReadingChanged(),
_syncLock);
}

/// <summary>
Expand Down Expand Up @@ -49,34 +54,13 @@ public static Compass GetDefault()
/// </summary>
public event TypedEventHandler<Compass, CompassReadingChangedEventArgs> ReadingChanged
{
add
{
lock (_syncLock)
{
var isFirstSubscriber = _readingChanged == null;
_readingChanged += value;
if (isFirstSubscriber)
{
StartReadingChanged();
}
}
}
remove
{
lock (_syncLock)
{
_readingChanged -= value;
if (_readingChanged == null)
{
StopReadingChanged();
}
}
}
add => _readingChangedWrapper.AddHandler(value);
remove => _readingChangedWrapper.RemoveHandler(value);
}

private void OnReadingChanged(CompassReading reading)
{
_readingChanged?.Invoke(this, new CompassReadingChangedEventArgs(reading));
_readingChangedWrapper.Invoke(this, new CompassReadingChangedEventArgs(reading));
}
}
#endif
2 changes: 1 addition & 1 deletion src/Uno.UWP/Devices/Sensors/Compass.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public uint ReportInterval
return;
}

if (_readingChanged != null)
if (_readingChangedWrapper.Event != null)
{
//restart reading to apply interval
StopReadingChanged();
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Devices/Sensors/Gyrometer.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public uint ReportInterval
{
_reportInterval = value;

if (_readingChanged != null)
if (_readingChangedWrapper.Event != null)
{
//restart reading to apply interval
StopReading();
Expand Down
Loading

0 comments on commit 4bc6ea1

Please sign in to comment.