Skip to content

Commit

Permalink
fix!: iOS create instance only if motion is available
Browse files Browse the repository at this point in the history
  • Loading branch information
ajpinedam committed Nov 15, 2023
1 parent c72a877 commit 3706fc9
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
20 changes: 17 additions & 3 deletions src/Uno.UWP/Devices/Sensors/SimpleOrientationSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,31 @@ public partial class SimpleOrientationSensor
#region Static

private static SimpleOrientationSensor _instance;
private static bool _initialized;
private readonly static object _syncLock = new();

public static SimpleOrientationSensor GetDefault()
{
if (_instance == null)
if (_initialized)
{
_instance = new SimpleOrientationSensor();
return _instance;
}

return _instance;
lock (_syncLock)
{
if (!_initialized)
{
_instance = TryCreateInstance();
_initialized = true;
}

return _instance;
}
}

private static partial SimpleOrientationSensor TryCreateInstance();


#endregion

#pragma warning disable CS0649 // Field 'SimpleOrientationSensor._currentOrientation' is never assigned to, and will always have its default value - Assigned only in Android and iOS.
Expand Down
12 changes: 11 additions & 1 deletion src/Uno.UWP/Devices/Sensors/SimpleOrientationSensor.iOS.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#nullable enable
#nullable enable
using CoreMotion;
using Foundation;
using Uno.Extensions;
Expand All @@ -13,6 +13,16 @@ public partial class SimpleOrientationSensor
private const double _updateInterval = 0.5;
private const double _threshold = 0.5;

private static partial SimpleOrientationSensor? TryCreateInstance()
{
_motionManager ??= new CMMotionManager();

return _motionManager.DeviceMotionAvailable
? new SimpleOrientationSensor()
: null;
}


partial void Initialize()
{
if (_motionManager is { DeviceMotionAvailable: true })
Expand Down

0 comments on commit 3706fc9

Please sign in to comment.