Skip to content

Commit

Permalink
Merge pull request #19278 from nickodei/use-lazy-instance-for-all-sen…
Browse files Browse the repository at this point in the history
…sor-classes
  • Loading branch information
MartinZikmund authored Jan 22, 2025
2 parents b9eacda + eac2858 commit 7f57344
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 181 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 @@ -27,7 +27,7 @@ public uint ReportInterval
return;
}

lock (_syncLock)
lock (_readingChangedWrapper.SyncLock)
{
_reportInterval = value;

Expand Down
30 changes: 6 additions & 24 deletions src/Uno.UWP/Devices/Sensors/Accelerometer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if __IOS__ || __ANDROID__ || __WASM__
#nullable enable

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

private static Accelerometer _instance;
private static bool _initializationAttempted;
private readonly static Lazy<Accelerometer?> _instance = new Lazy<Accelerometer?>(() => TryCreateInstance());

private readonly StartStopTypedEventWrapper<Accelerometer, AccelerometerReadingChangedEventArgs> _readingChangedWrapper;
private readonly StartStopTypedEventWrapper<Accelerometer, AccelerometerShakenEventArgs> _shakenWrapper;
Expand All @@ -36,34 +35,17 @@ private Accelerometer()
{
_readingChangedWrapper = new StartStopTypedEventWrapper<Accelerometer, AccelerometerReadingChangedEventArgs>(
() => StartReadingChanged(),
() => StopReadingChanged(),
_syncLock);
() => StopReadingChanged());
_shakenWrapper = new StartStopTypedEventWrapper<Accelerometer, AccelerometerShakenEventArgs>(
() => StartShaken(),
() => StopShaken(),
_syncLock);
() => StopShaken());
}

/// <summary>
/// Returns the default accelerometer.
/// </summary>
/// <returns>The default accelerometer or null if no integrated accelerometers are found.</returns>
public static Accelerometer GetDefault()
{
if (_initializationAttempted)
{
return _instance;
}
lock (_syncLock)
{
if (!_initializationAttempted)
{
_instance = TryCreateInstance();
_initializationAttempted = true;
}
return _instance;
}
}
public static Accelerometer? GetDefault() => _instance.Value;

/// <summary>
/// Occurs each time the accelerometer reports a new sensor reading.
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Devices/Sensors/Accelerometer.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public uint ReportInterval

internal static void HandleShake()
{
_instance?.OnShaken(DateTimeOffset.UtcNow);
_instance.Value?.OnShaken(DateTimeOffset.UtcNow);
}

private static Accelerometer? TryCreateInstance()
Expand Down
10 changes: 5 additions & 5 deletions src/Uno.UWP/Devices/Sensors/Accelerometer.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,22 @@ private void DetachDeviceMotion()
/// <returns>0 - needed to bind method from WASM</returns>
internal static int DispatchReading(float x, float y, float z)
{
if (_instance == null)
if (_instance.Value == null)
{
throw new InvalidOperationException("Accelerometer:DispatchReading can be called only after Accelerometer is initialized");
}
var now = DateTimeOffset.UtcNow;
if ((now - _instance._lastReading).TotalMilliseconds >= _instance.ReportInterval * 0.8)
if ((now - _instance.Value._lastReading).TotalMilliseconds >= _instance.Value.ReportInterval * 0.8)
{
_instance._lastReading = now;
_instance.OnReadingChanged(
_instance.Value._lastReading = now;
_instance.Value.OnReadingChanged(
new AccelerometerReading(
x / Gravity * -1,
y / Gravity * -1,
z / Gravity * -1,
now));
}
_instance._shakeDetector?.OnSensorChanged(x, y, z, DateTimeOffset.UtcNow);
_instance.Value._shakeDetector?.OnSensorChanged(x, y, z, DateTimeOffset.UtcNow);
return 0;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Devices/Sensors/Barometer.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public uint ReportInterval
return;
}

lock (_syncLock)
lock (_readingChangedWrapper.SyncLock)
{
_reportInterval = value;

Expand Down
27 changes: 5 additions & 22 deletions src/Uno.UWP/Devices/Sensors/Barometer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#if __ANDROID__ || __IOS__
#nullable enable

using System;
using Uno.Helpers;
using Windows.Foundation;

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

private static bool _initializationAttempted;
private static Barometer _instance;
private readonly static Lazy<Barometer?> _instance = new Lazy<Barometer?>(() => TryCreateInstance());

private readonly StartStopTypedEventWrapper<Barometer, BarometerReadingChangedEventArgs> _readingChangedWrapper;

Expand All @@ -24,30 +23,14 @@ private Barometer()
{
_readingChangedWrapper = new StartStopTypedEventWrapper<Barometer, BarometerReadingChangedEventArgs>(
() => StartReading(),
() => StopReading(),
_syncLock);
() => StopReading());
}

/// <summary>
/// Returns the default barometer sensor.
/// </summary>
/// <returns>If no barometer sensor is available, this method will return null.</returns>
public static Barometer GetDefault()
{
if (_initializationAttempted)
{
return _instance;
}
lock (_syncLock)
{
if (!_initializationAttempted)
{
_instance = TryCreateInstance();
_initializationAttempted = true;
}
return _instance;
}
}
public static Barometer? GetDefault() => _instance.Value;

/// <summary>
/// Occurs each time the barometer reports a new sensor reading.
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 @@ -27,7 +27,7 @@ public uint ReportInterval
get => _reportInterval;
set
{
lock (_syncLock)
lock (_readingChangedWrapper.SyncLock)
{
if (_reportInterval == value)
{
Expand Down
28 changes: 6 additions & 22 deletions src/Uno.UWP/Devices/Sensors/Compass.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#if __IOS__ || __ANDROID__ || __WASM__
#nullable enable

using System;
using Uno.Helpers;
using Windows.Foundation;

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

private static Compass _instance;
private static bool _initializationAttempted;
private readonly static Lazy<Compass?> _instance = new Lazy<Compass?>(() => TryCreateInstance());

private readonly StartStopTypedEventWrapper<Compass, CompassReadingChangedEventArgs> _readingChangedWrapper;

Expand All @@ -24,30 +24,14 @@ private Compass()
{
_readingChangedWrapper = new StartStopTypedEventWrapper<Compass, CompassReadingChangedEventArgs>(
() => StartReadingChanged(),
() => StopReadingChanged(),
_syncLock);
() => StopReadingChanged());
}

/// <summary>
/// Returns the default compass.
/// </summary>
/// <returns>The default compass or null if no integrated compasses are found.</returns>
public static Compass GetDefault()
{
if (_initializationAttempted)
{
return _instance;
}
lock (_syncLock)
{
if (!_initializationAttempted)
{
_instance = TryCreateInstance();
_initializationAttempted = true;
}
return _instance;
}
}
public static Compass? GetDefault() => _instance.Value;

/// <summary>
/// Occurs each time the compass reports a new sensor reading.
Expand Down
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 @@ -28,7 +28,7 @@ public uint ReportInterval
return;
}

lock (_syncLock)
lock (_readingChangedWrapper.SyncLock)
{
_reportInterval = value;

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 @@ -27,7 +27,7 @@ public uint ReportInterval
return;
}

lock (_syncLock)
lock (_readingChangedWrapper.SyncLock)
{
_reportInterval = value;

Expand Down
28 changes: 6 additions & 22 deletions src/Uno.UWP/Devices/Sensors/Gyrometer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#if __IOS__ || __ANDROID__ || __WASM__
#nullable enable

using System;
using Uno.Extensions;
using Uno.Foundation.Logging;
using Uno.Helpers;
Expand All @@ -11,10 +14,7 @@ namespace Windows.Devices.Sensors
/// </summary>
public partial class Gyrometer
{
private readonly static object _syncLock = new();

private static Gyrometer _instance;
private static bool _initializationAttempted;
private readonly static Lazy<Gyrometer?> _instance = new Lazy<Gyrometer?>(() => TryCreateInstance());

private readonly StartStopTypedEventWrapper<Gyrometer, GyrometerReadingChangedEventArgs> _readingChangedWrapper;

Expand All @@ -25,30 +25,14 @@ private Gyrometer()
{
_readingChangedWrapper = new StartStopTypedEventWrapper<Gyrometer, GyrometerReadingChangedEventArgs>(
() => StartReading(),
() => StopReading(),
_syncLock);
() => StopReading());
}

/// <summary>
/// Returns the default gyrometer.
/// </summary>
/// <returns>Null if no integrated gyrometers are found.</returns>
public static Gyrometer GetDefault()
{
if (_initializationAttempted)
{
return _instance;
}
lock (_syncLock)
{
if (!_initializationAttempted)
{
_instance = TryCreateInstance();
_initializationAttempted = true;
}
return _instance;
}
}
public static Gyrometer? GetDefault() => _instance.Value;

/// <summary>
/// Occurs each time the gyrometer reports the current sensor reading.
Expand Down
8 changes: 4 additions & 4 deletions src/Uno.UWP/Devices/Sensors/Gyrometer.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ private void StopReading()
[JSExport]
internal static int DispatchReading(float x, float y, float z)
{
if (_instance == null)
if (_instance.Value == null)
{
throw new InvalidOperationException("Gyrometer:DispatchReading can be called only after Gyrometer is initialized");
}
var now = DateTimeOffset.UtcNow;
if ((now - _instance._lastReading).TotalMilliseconds >= _instance.ReportInterval * 0.8)
if ((now - _instance.Value._lastReading).TotalMilliseconds >= _instance.Value.ReportInterval * 0.8)
{
_instance._lastReading = now;
_instance.OnReadingChanged(
_instance.Value._lastReading = now;
_instance.Value.OnReadingChanged(
new GyrometerReading(
x * SensorConstants.RadToDeg,
y * SensorConstants.RadToDeg,
Expand Down
2 changes: 1 addition & 1 deletion src/Uno.UWP/Devices/Sensors/Magnetometer.Android.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public uint ReportInterval
return;
}

lock (_syncLock)
lock (_readingChangedWrapper.SyncLock)
{
_reportInterval = value;

Expand Down
27 changes: 5 additions & 22 deletions src/Uno.UWP/Devices/Sensors/Magnetometer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#if __IOS__ || __ANDROID__ || __WASM__
#nullable enable

using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -14,10 +16,7 @@ namespace Windows.Devices.Sensors
/// </summary>
public partial class Magnetometer
{
private readonly static object _syncLock = new();

private static Magnetometer _instance;
private static bool _initializationAttempted;
private readonly static Lazy<Magnetometer?> _instance = new Lazy<Magnetometer?>(() => TryCreateInstance());

private readonly StartStopTypedEventWrapper<Magnetometer, MagnetometerReadingChangedEventArgs> _readingChangedWrapper;

Expand All @@ -28,30 +27,14 @@ private Magnetometer()
{
_readingChangedWrapper = new StartStopTypedEventWrapper<Magnetometer, MagnetometerReadingChangedEventArgs>(
() => StartReading(),
() => StopReading(),
_syncLock);
() => StopReading());
}

/// <summary>
/// Returns the default magnetometer.
/// </summary>
/// <returns>The default magnetometer.</returns>
public static Magnetometer GetDefault()
{
if (_initializationAttempted)
{
return _instance;
}
lock (_syncLock)
{
if (!_initializationAttempted)
{
_instance = TryCreateInstance();
_initializationAttempted = true;
}
return _instance;
}
}
public static Magnetometer? GetDefault() => _instance.Value;

/// <summary>
/// Occurs each time the compass reports a new sensor reading.
Expand Down
8 changes: 4 additions & 4 deletions src/Uno.UWP/Devices/Sensors/Magnetometer.wasm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ private void StopReading()
[JSExport]
internal static int DispatchReading(float x, float y, float z)
{
if (_instance == null)
if (_instance.Value == null)
{
throw new InvalidOperationException("Magnetometer:DispatchReading can be called only after Magnetometer is initialized");
}
var now = DateTimeOffset.UtcNow;
if ((now - _instance._lastReading).TotalMilliseconds >= _instance.ReportInterval * 0.8)
if ((now - _instance.Value._lastReading).TotalMilliseconds >= _instance.Value.ReportInterval * 0.8)
{
_instance._lastReading = now;
_instance.OnReadingChanged(
_instance.Value._lastReading = now;
_instance.Value.OnReadingChanged(
new MagnetometerReading(
x,
y,
Expand Down
Loading

0 comments on commit 7f57344

Please sign in to comment.