Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ class HybridDeviceOrientationManager : HybridOrientationManagerSpec() {

override fun startOrientationUpdates(onChanged: (orientation: CameraOrientation) -> Unit) {
orientationListener?.disable()
currentOrientation?.let(onChanged)
orientationListener =
object : OrientationEventListener(context) {
override fun onOrientationChanged(rotationDegrees: Int) {
if (orientationListener !== this) return
if (rotationDegrees == ORIENTATION_UNKNOWN) {
// phone is laying flat - orientation is unknown! Avoid sending out event.
return
Expand All @@ -51,6 +53,8 @@ class HybridDeviceOrientationManager : HybridOrientationManagerSpec() {
}

override fun stopOrientationUpdates() {
orientationListener?.disable()
val listener = orientationListener
orientationListener = null
listener?.disable()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ class HybridInterfaceOrientationManager : HybridOrientationManagerSpec() {
listener?.let { listener ->
displayManager.unregisterDisplayListener(listener)
}
val defaultDisplay = displayManager.displays.firstOrNull()
if (defaultDisplay != null) {
currentOrientation = CameraOrientation.fromSurfaceRotation(defaultDisplay.rotation)
}
currentOrientation?.let(onChanged)
val listener =
object : DisplayManager.DisplayListener {
override fun onDisplayAdded(displayId: Int) = Unit

override fun onDisplayRemoved(displayId: Int) = Unit

override fun onDisplayChanged(displayId: Int) {
if (this@HybridInterfaceOrientationManager.listener !== this) return
val display = displayManager.getDisplay(displayId) ?: return
val surfaceRotation = display.rotation
val orientation = CameraOrientation.fromSurfaceRotation(surfaceRotation)
Expand All @@ -54,9 +60,8 @@ class HybridInterfaceOrientationManager : HybridOrientationManagerSpec() {
}

override fun stopOrientationUpdates() {
listener?.let { listener ->
displayManager.unregisterDisplayListener(listener)
}
val currentListener = listener
listener = null
currentListener?.let(displayManager::unregisterDisplayListener)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ final class HybridDeviceOrientationManager: HybridOrientationManagerSpec {
if motionManager.isAccelerometerActive {
motionManager.stopAccelerometerUpdates()
}
if let currentOrientation {
onChanged(currentOrientation)
}

if motionManager.isAccelerometerAvailable {
motionManager.startAccelerometerUpdates(to: operationQueue) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ final class HybridInterfaceOrientationManager: HybridOrientationManagerSpec {
// Start new listener (beginGeneratingDeviceOrientationNotifications() can be nested)
UIDevice.current.beginGeneratingDeviceOrientationNotifications()

let interfaceOrientation = UIApplication.shared.interfaceOrientation
if interfaceOrientation != .unknown {
let orientation = CameraOrientation(interfaceOrientation: interfaceOrientation)
self.currentOrientation = orientation
onChanged(orientation)
}

self.observer = NotificationCenter.default.addObserver(
forName: UIDevice.orientationDidChangeNotification,
object: nil,
Expand All @@ -60,6 +67,7 @@ final class HybridInterfaceOrientationManager: HybridOrientationManagerSpec {
if let observer = self.observer {
logger.info("Stopping interface orientation updates...")
NotificationCenter.default.removeObserver(observer)
self.observer = nil
UIDevice.current.endGeneratingDeviceOrientationNotifications()
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export function useOrientation(
): CameraOrientation | undefined {
const orientationManager = useOrientationManager(source)
const currentOrientation = useRef(orientationManager?.currentOrientation)
currentOrientation.current = orientationManager?.currentOrientation

const subscribe = useCallback(
(onStoreChange: () => void) => {
Expand Down
Loading