Skip to content

Commit

Permalink
fix: interface orientation should match bottom bar position (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
gladiuscode authored May 26, 2024
1 parent 4c1056d commit 7012798
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,20 +79,21 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
return mAutoRotationObserver.getLastAutoRotationStatus()
}

fun lockTo(jsOrientation: Int) {
val interfaceOrientation = mUtils.getOrientationFromJsOrientation(jsOrientation)
fun lockTo(rawJsOrientation: Int) {
val jsOrientation = mUtils.getOrientationFromJsOrientation(rawJsOrientation)
val screenOrientation =
mUtils.getActivityOrientationFrom(interfaceOrientation)
mUtils.getActivityOrientationFrom(jsOrientation)
context.currentActivity?.requestedOrientation = screenOrientation
mEventEmitter.sendInterfaceOrientationDidChange(interfaceOrientation.ordinal)
lastInterfaceOrientation = interfaceOrientation

updateIsLockedTo(true)
updateLastInterfaceOrientationTo(jsOrientation)
}

fun unlock() {
context.currentActivity?.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED

updateIsLockedTo(false)
adaptInterfaceTo(getDeviceOrientation())
adaptInterfaceTo(lastDeviceOrientation)
}

fun resetSupportedInterfaceOrientations() {
Expand Down Expand Up @@ -140,11 +141,12 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
return
}

if (lastInterfaceOrientation == deviceOrientation) {
val newInterfaceOrientation = mUtils.getInterfaceOrientationFromDeviceOrientation(deviceOrientation);
if (newInterfaceOrientation == lastInterfaceOrientation) {
return
}

updateLastInterfaceOrientationTo(deviceOrientation)
updateLastInterfaceOrientationTo(newInterfaceOrientation)
}

private fun updateIsLockedTo(value: Boolean) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,14 @@ class OrientationDirectorUtilsImpl(val context: ReactContext) {
else -> Orientation.PORTRAIT
}
}

fun getInterfaceOrientationFromDeviceOrientation(deviceOrientation: Orientation): Orientation {
return when(deviceOrientation) {
Orientation.PORTRAIT -> Orientation.PORTRAIT
Orientation.LANDSCAPE_RIGHT -> Orientation.LANDSCAPE_LEFT
Orientation.PORTRAIT_UPSIDE_DOWN -> Orientation.PORTRAIT_UPSIDE_DOWN
Orientation.LANDSCAPE_LEFT -> Orientation.LANDSCAPE_RIGHT
else -> Orientation.UNKNOWN
}
}
}
2 changes: 1 addition & 1 deletion ios/OrientationDirector.mm
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ - (void)lockTo:(double)jsOrientation
{
NSNumber *jsOrientationNumber = @(jsOrientation);
dispatch_async(dispatch_get_main_queue(), ^{
[_director lockToJsOrientation:jsOrientationNumber];
[_director lockToRawJsOrientation:jsOrientationNumber];
});
}

Expand Down
46 changes: 22 additions & 24 deletions ios/implementation/OrientationDirectorImpl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,51 +48,48 @@ import UIKit
return lastDeviceOrientation
}

@objc public func lockTo(jsOrientation: NSNumber) {
let orientation = OrientationDirectorUtils.getOrientationFrom(jsOrientation: jsOrientation)
let mask = OrientationDirectorUtils.getMaskFrom(orientation: orientation)

@objc public func lockTo(rawJsOrientation: NSNumber) {
let jsOrientation = OrientationDirectorUtils.getOrientationFrom(jsOrientation: rawJsOrientation)
let mask = OrientationDirectorUtils.getMaskFrom(jsOrientation: jsOrientation)
self.requestInterfaceUpdateTo(mask: mask)

eventManager.sendInterfaceOrientationDidChange(orientationValue: orientation.rawValue)
lastInterfaceOrientation = orientation
updateIsLockedTo(value: true)
updateLastInterfaceOrientationTo(value: jsOrientation)
}

@objc public func unlock() {
self.requestInterfaceUpdateTo(mask: UIInterfaceOrientationMask.all)

let deviceOrientation = OrientationDirectorUtils.getOrientationFrom(deviceOrientation: UIDevice.current.orientation)
updateIsLockedTo(value: false)
self.adaptInterfaceTo(deviceOrientation: deviceOrientation)
self.adaptInterfaceTo(deviceOrientation: lastDeviceOrientation)
}

@objc public func resetSupportedInterfaceOrientations() {
self.supportedInterfaceOrientations = self.initialSupportedInterfaceOrientations
self.requestInterfaceUpdateTo(mask: self.supportedInterfaceOrientations)
self.updateIsLockedTo(value: self.initIsLocked())

let lastMask = OrientationDirectorUtils.getMaskFrom(orientation: lastInterfaceOrientation)
let isLastInterfaceOrientationAlreadySupported = self.supportedInterfaceOrientations.contains(lastMask)
if (isLastInterfaceOrientationAlreadySupported) {
let lastMask = OrientationDirectorUtils.getMaskFrom(jsOrientation: lastInterfaceOrientation)
let isLastMaskSupported = self.supportedInterfaceOrientations.contains(lastMask)
if (isLastMaskSupported) {
return
}

let supportedInterfaceOrientations = OrientationDirectorUtils.readSupportedInterfaceOrientationsFromBundle()
if (supportedInterfaceOrientations.contains(UIInterfaceOrientationMask.portrait)) {
self.updateLastInterfaceOrientation(value: Orientation.PORTRAIT)
self.updateLastInterfaceOrientationTo(value: Orientation.PORTRAIT)
return
}
if (supportedInterfaceOrientations.contains(UIInterfaceOrientationMask.landscapeRight)) {
self.updateLastInterfaceOrientation(value: Orientation.LANDSCAPE_LEFT)
self.updateLastInterfaceOrientationTo(value: Orientation.LANDSCAPE_RIGHT)
return
}
if (supportedInterfaceOrientations.contains(UIInterfaceOrientationMask.landscapeLeft)) {
self.updateLastInterfaceOrientation(value: Orientation.LANDSCAPE_RIGHT)
self.updateLastInterfaceOrientationTo(value: Orientation.LANDSCAPE_LEFT)
return
}

self.updateLastInterfaceOrientation(value: Orientation.PORTRAIT_UPSIDE_DOWN)
self.updateLastInterfaceOrientationTo(value: Orientation.PORTRAIT_UPSIDE_DOWN)
}

private func initInitialSupportedInterfaceOrientations() -> UIInterfaceOrientationMask {
Expand Down Expand Up @@ -154,30 +151,31 @@ import UIKit
if (isLocked) {
return
}

if (lastInterfaceOrientation == deviceOrientation) {
return
}

if (deviceOrientation == Orientation.FACE_UP || deviceOrientation == Orientation.FACE_DOWN) {
return
}

let deviceOrientationMask = OrientationDirectorUtils.getMaskFrom(orientation: deviceOrientation)
let isDeviceOrientationMaskSupported = self.supportedInterfaceOrientations.contains(deviceOrientationMask)
if (!isDeviceOrientationMaskSupported) {
let newInterfaceOrientationMask = OrientationDirectorUtils.getMaskFrom(deviceOrientation: deviceOrientation)
let isSupported = self.supportedInterfaceOrientations.contains(newInterfaceOrientationMask)
if (!isSupported) {
return
}

updateLastInterfaceOrientation(value: deviceOrientation)
let newInterfaceOrientation = OrientationDirectorUtils.getOrientationFrom(mask: newInterfaceOrientationMask)
if (newInterfaceOrientation == lastInterfaceOrientation) {
return
}

updateLastInterfaceOrientationTo(value: newInterfaceOrientation)
}

private func updateIsLockedTo(value: Bool) {
eventManager.sendLockDidChange(value: value)
isLocked = value
}

private func updateLastInterfaceOrientation(value: Orientation) {
private func updateLastInterfaceOrientationTo(value: Orientation) {
self.eventManager.sendInterfaceOrientationDidChange(orientationValue: value.rawValue)
lastInterfaceOrientation = value
}
Expand Down
55 changes: 36 additions & 19 deletions ios/implementation/OrientationDirectorUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class OrientationDirectorUtils {
var orientation = Orientation.UNKNOWN

switch(uiInterfaceOrientation) {
case UIInterfaceOrientation.landscapeRight: // Home button on the right
orientation = Orientation.LANDSCAPE_LEFT
case UIInterfaceOrientation.landscapeRight:
orientation = Orientation.LANDSCAPE_RIGHT
case UIInterfaceOrientation.portraitUpsideDown:
orientation = Orientation.PORTRAIT_UPSIDE_DOWN
case UIInterfaceOrientation.landscapeLeft: // Home button on the left
orientation = Orientation.LANDSCAPE_RIGHT
case UIInterfaceOrientation.landscapeLeft:
orientation = Orientation.LANDSCAPE_LEFT
default:
orientation = Orientation.PORTRAIT
}
Expand Down Expand Up @@ -73,35 +73,52 @@ class OrientationDirectorUtils {
case UIInterfaceOrientationMask.portraitUpsideDown:
orientation = Orientation.PORTRAIT_UPSIDE_DOWN
case UIInterfaceOrientationMask.landscapeRight:
orientation = Orientation.LANDSCAPE_LEFT
case UIInterfaceOrientationMask.landscapeLeft:
orientation = Orientation.LANDSCAPE_RIGHT
case UIInterfaceOrientationMask.landscapeLeft:
orientation = Orientation.LANDSCAPE_LEFT
default:
orientation = Orientation.PORTRAIT
}

return orientation
}

/*
Note: .portraitUpsideDown only works for devices with home button
//https://developer.apple.com/documentation/uikit/uiviewcontroller/1621435-supportedinterfaceorientations
/**
Note: .portraitUpsideDown only works for devices with home button and iPads
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621435-supportedinterfaceorientations
*/
public static func getMaskFrom(orientation: Orientation) -> UIInterfaceOrientationMask {
var mask = UIInterfaceOrientationMask.portrait

switch(orientation) {
public static func getMaskFrom(jsOrientation: Orientation) -> UIInterfaceOrientationMask {
switch(jsOrientation) {
case Orientation.PORTRAIT:
return UIInterfaceOrientationMask.portrait
case Orientation.LANDSCAPE_RIGHT:
mask = UIInterfaceOrientationMask.landscapeLeft
return UIInterfaceOrientationMask.landscapeRight
case Orientation.PORTRAIT_UPSIDE_DOWN:
mask = UIInterfaceOrientationMask.portraitUpsideDown
return UIInterfaceOrientationMask.portraitUpsideDown
case Orientation.LANDSCAPE_LEFT:
mask = UIInterfaceOrientationMask.landscapeRight
return UIInterfaceOrientationMask.landscapeLeft
default:
mask = UIInterfaceOrientationMask.portrait
return UIInterfaceOrientationMask.all
}
}

/**
Note: .portraitUpsideDown only works for devices with home button and iPads
https://developer.apple.com/documentation/uikit/uiviewcontroller/1621435-supportedinterfaceorientations
*/
public static func getMaskFrom(deviceOrientation: Orientation) -> UIInterfaceOrientationMask {
switch(deviceOrientation) {
case Orientation.PORTRAIT:
return UIInterfaceOrientationMask.portrait
case Orientation.LANDSCAPE_RIGHT:
return UIInterfaceOrientationMask.landscapeLeft
case Orientation.PORTRAIT_UPSIDE_DOWN:
return UIInterfaceOrientationMask.portraitUpsideDown
case Orientation.LANDSCAPE_LEFT:
return UIInterfaceOrientationMask.landscapeRight
default:
return UIInterfaceOrientationMask.all
}

return mask
}

public static func getInterfaceOrientation() -> UIInterfaceOrientation {
Expand Down

0 comments on commit 7012798

Please sign in to comment.