Skip to content

Commit 85a3024

Browse files
authored
fix: Fix orientation updating to portrait when phone is flat (mrousavy#3423)
* fix: Fix orientation updating to `portrait` when phone is flat * fix: Make `[weak self]`
1 parent 64c3f7c commit 85a3024

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

package/ios/Core/Extensions/CMAccelerometerData+deviceOrientation.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ import Foundation
1010

1111
extension CMAccelerometerData {
1212
/**
13-
Get the current device orientation from the given acceleration/gyro data.
13+
Get the current device orientation from the given acceleration/gyro data, or `nil` if it is unknown.
14+
The orientation can only be unknown if the phone is flat, in which case it is not clear which orientation the phone is held in.
1415
*/
15-
var deviceOrientation: Orientation {
16+
var deviceOrientation: Orientation? {
1617
let acceleration = acceleration
1718
let xNorm = abs(acceleration.x)
1819
let yNorm = abs(acceleration.y)

package/ios/Core/OrientationManager.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,12 +161,17 @@ final class OrientationManager {
161161
stopDeviceOrientationListener()
162162
if motionManager.isAccelerometerAvailable {
163163
motionManager.accelerometerUpdateInterval = 0.2
164-
motionManager.startAccelerometerUpdates(to: operationQueue) { accelerometerData, error in
164+
motionManager.startAccelerometerUpdates(to: operationQueue) { [weak self] accelerometerData, error in
165+
guard let self else { return }
165166
if let error {
166167
VisionLogger.log(level: .error, message: "Failed to get Accelerometer data! \(error)")
167168
}
168169
if let accelerometerData {
169-
self.deviceOrientation = accelerometerData.deviceOrientation
170+
// There is accelerometer data available
171+
if let deviceOrientation = accelerometerData.deviceOrientation {
172+
// The orientation can actually be determined - it is not `nil` (flat)! Update it
173+
self.deviceOrientation = deviceOrientation
174+
}
170175
}
171176
}
172177
}

0 commit comments

Comments
 (0)