Skip to content

Commit

Permalink
fix(iOS): add start/stop MotionManager events
Browse files Browse the repository at this point in the history
  • Loading branch information
ajpinedam committed Nov 15, 2023
1 parent 3706fc9 commit 8bf4e78
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/Uno.UWP/Devices/Sensors/SimpleOrientationSensor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ public static SimpleOrientationSensor GetDefault()

private static partial SimpleOrientationSensor TryCreateInstance();

partial void StartListeningOrientationChanged();

partial void StopListeningOrientationChanged();

#endregion

Expand Down
63 changes: 61 additions & 2 deletions src/Uno.UWP/Devices/Sensors/SimpleOrientationSensor.iOS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,43 @@ public partial class SimpleOrientationSensor
: null;
}

partial void StartListeningOrientationChanged()
{
_motionManager ??= new();

if (!_motionManager.DeviceMotionAvailable)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().Error("SimpleOrientationSensor failed to Start. CoreMotion is not available");
}
}

if (this.Log().IsEnabled(LogLevel.Information))
{
this.Log().Info("DeviceMotion is available");
}

var operationQueue = (NSOperationQueue.CurrentQueue == null || NSOperationQueue.CurrentQueue == NSOperationQueue.MainQueue)
? new NSOperationQueue()
: NSOperationQueue.CurrentQueue;

_motionManager.DeviceMotionUpdateInterval = _updateInterval;

_motionManager.StartDeviceMotionUpdates(operationQueue, OnMotionChanged);
}

partial void StopListeningOrientationChanged()
{
if (_motionManager is null)
{
return;
}

_motionManager.StopDeviceMotionUpdates();
_motionManager.Dispose();
_motionManager = null;
}

partial void Initialize()
{
Expand Down Expand Up @@ -66,7 +103,7 @@ partial void Initialize()
return;
}
OnMotionChanged(motion);
OnMotionChanged(motion, error);
});
}
else // For iOS devices that don't support CoreMotion
Expand All @@ -78,9 +115,31 @@ partial void Initialize()
}
}

private void OnMotionChanged(CMDeviceMotion motion)
private void OnMotionChanged(CMDeviceMotion? motion, NSError? error)
{
// Motion and Error can be null: https://developer.apple.com/documentation/coremotion/cmdevicemotionhandler
if (error is not null)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().Error($"SimpleOrientationSensor returned error when reading Device Motion updates. {error.Description}");
}

return;
}

if (motion is null)
{
if (this.Log().IsEnabled(LogLevel.Error))
{
this.Log().Error($"SimpleOrientationSensor failed read Device Motion updates.");
}

return;
}

var orientation = ToSimpleOrientation(motion.Gravity.X, motion.Gravity.Y, motion.Gravity.Z, _threshold, _previousOrientation);

_previousOrientation = orientation;
SetCurrentOrientation(orientation);
}
Expand Down

0 comments on commit 8bf4e78

Please sign in to comment.