Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore - Improve codebase quality and readability #18

Merged
merged 16 commits into from
May 29, 2024
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 @@ -3,10 +3,9 @@ package com.orientationdirector.implementation
import android.database.ContentObserver
import android.os.Handler
import android.provider.Settings
import android.util.Log
import com.facebook.react.bridge.ReactContext

class OrientationAutoRotationObserver(val context: ReactContext, handler: Handler?) : ContentObserver(handler) {
class AutoRotationObserver(private val context: ReactContext, handler: Handler?) : ContentObserver(handler) {
private var lastAutoRotationStatus: Boolean = isAutoRotationEnabled()

fun getLastAutoRotationStatus(): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ enum class Event {
LockDidChange,
}

class OrientationEventManager(private val context: ReactApplicationContext) {
class EventManager(private val context: ReactApplicationContext) {

fun sendDeviceOrientationDidChange(orientationValue: Int) {
val params = Arguments.createMap().apply {
Expand All @@ -36,15 +36,13 @@ class OrientationEventManager(private val context: ReactApplicationContext) {
}

private fun sendEvent(eventName: Event, params: WritableMap?) {
Log.d(NAME, "sendEvent - $eventName")
Log.d(NAME, "sendEvent - $params")
context
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
.emit(eventName.name, params)
}

companion object {
val NAME = "OrientationEventManager"
const val NAME = "OrientationEventManager"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.orientationdirector.implementation

import com.facebook.react.bridge.LifecycleEventListener

class OrientationLifecycleListener() : LifecycleEventListener {
class LifecycleListener() : LifecycleEventListener {

private var onHostResumeCallback: (() -> Unit)? = null
private var onHostPauseCallback: (() -> Unit)? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ import android.os.Looper
import com.facebook.react.bridge.ReactApplicationContext

class OrientationDirectorImpl internal constructor(private val context: ReactApplicationContext) {
private var mUtils = OrientationDirectorUtilsImpl(context)
private var mEventEmitter = OrientationEventManager(context)
private var mSensorListener = OrientationSensorListener(context)
private var mAutoRotationObserver = OrientationAutoRotationObserver(
private var mUtils = Utils(context)
private var mEventManager = EventManager(context)
private var mSensorListener = SensorListener(context)
private var mAutoRotationObserver = AutoRotationObserver(
context, Handler(
Looper.getMainLooper()
)
)
private var mLifecycleListener = OrientationLifecycleListener()
private var mLifecycleListener = LifecycleListener()

private var initialSupportedInterfaceOrientations = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED
private var lastInterfaceOrientation = Orientation.UNKNOWN
Expand Down Expand Up @@ -79,10 +79,10 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
return mAutoRotationObserver.getLastAutoRotationStatus()
}

fun lockTo(rawJsOrientation: Int) {
val jsOrientation = mUtils.getOrientationFromJsOrientation(rawJsOrientation)
fun lockTo(jsValue: Int) {
val jsOrientation = mUtils.convertToOrientationFromJsValue(jsValue)
val screenOrientation =
mUtils.getActivityOrientationFrom(jsOrientation)
mUtils.convertToActivityOrientationFrom(jsOrientation)
context.currentActivity?.requestedOrientation = screenOrientation

updateIsLockedTo(true)
Expand All @@ -104,14 +104,14 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp

private fun initInterfaceOrientation(): Orientation {
val rotation = mUtils.getInterfaceRotation()
return mUtils.getOrientationFromRotation(rotation)
return mUtils.convertToOrientationFromScreenRotation(rotation)
}

private fun initDeviceOrientation(): Orientation {
val lastRotationDetected = mSensorListener.getLastRotationDetected()
?: return Orientation.UNKNOWN

return mUtils.getDeviceOrientationFrom(lastRotationDetected)
return mUtils.convertToDeviceOrientationFrom(lastRotationDetected)
}

private fun initIsLocked(): Boolean {
Expand All @@ -126,8 +126,8 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
}

private fun onOrientationChanged(rawDeviceOrientation: Int) {
val deviceOrientation = mUtils.getDeviceOrientationFrom(rawDeviceOrientation)
mEventEmitter.sendDeviceOrientationDidChange(deviceOrientation.ordinal)
val deviceOrientation = mUtils.convertToDeviceOrientationFrom(rawDeviceOrientation)
mEventManager.sendDeviceOrientationDidChange(deviceOrientation.ordinal)
lastDeviceOrientation = deviceOrientation
adaptInterfaceTo(deviceOrientation)
}
Expand All @@ -141,7 +141,7 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
return
}

val newInterfaceOrientation = mUtils.getInterfaceOrientationFromDeviceOrientation(deviceOrientation);
val newInterfaceOrientation = mUtils.convertToInterfaceOrientationFrom(deviceOrientation);
if (newInterfaceOrientation == lastInterfaceOrientation) {
return
}
Expand All @@ -150,13 +150,13 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
}

private fun updateIsLockedTo(value: Boolean) {
mEventEmitter.sendLockDidChange(value)
mEventManager.sendLockDidChange(value)
isLocked = value
}

private fun updateLastInterfaceOrientationTo(value: Orientation) {
lastInterfaceOrientation = value
mEventEmitter.sendInterfaceOrientationDidChange(value.ordinal)
mEventManager.sendInterfaceOrientationDidChange(value.ordinal)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.hardware.SensorManager
import android.view.OrientationEventListener
import com.facebook.react.bridge.ReactApplicationContext

class OrientationSensorListener(
class SensorListener(
context: ReactApplicationContext,
) : OrientationEventListener(context, SensorManager.SENSOR_DELAY_UI) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ package com.orientationdirector.implementation
import android.content.Context
import android.content.pm.ActivityInfo
import android.os.Build
import android.provider.Settings
import android.view.Surface
import android.view.WindowManager
import com.facebook.react.bridge.ReactContext

class OrientationDirectorUtilsImpl(val context: ReactContext) {
class Utils(private val context: ReactContext) {

fun getInterfaceRotation(): Int {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
Expand All @@ -20,52 +19,50 @@ class OrientationDirectorUtilsImpl(val context: ReactContext) {
}
}

fun getDeviceOrientationFrom(rotation: Int): Orientation {
var orientation = Orientation.UNKNOWN

if (rotation == -1) {
orientation = Orientation.UNKNOWN
} else if (rotation > 355 || rotation < 5) {
orientation = Orientation.PORTRAIT
} else if (rotation in 86..94) {
orientation = Orientation.LANDSCAPE_RIGHT
} else if (rotation in 176..184) {
orientation = Orientation.PORTRAIT_UPSIDE_DOWN
} else if (rotation in 266..274) {
orientation = Orientation.LANDSCAPE_LEFT
fun convertToDeviceOrientationFrom(deviceRotation: Int): Orientation {
return if (deviceRotation == -1) {
Orientation.UNKNOWN
} else if (deviceRotation > 355 || deviceRotation < 5) {
Orientation.PORTRAIT
} else if (deviceRotation in 86..94) {
Orientation.LANDSCAPE_RIGHT
} else if (deviceRotation in 176..184) {
Orientation.PORTRAIT_UPSIDE_DOWN
} else if (deviceRotation in 266..274) {
Orientation.LANDSCAPE_LEFT
} else {
return Orientation.UNKNOWN
}

return orientation
}

fun getActivityOrientationFrom(interfaceOrientation: Orientation): Int {
return when (interfaceOrientation) {
fun convertToActivityOrientationFrom(orientation: Orientation): Int {
return when (orientation) {
Orientation.LANDSCAPE_RIGHT -> ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
Orientation.PORTRAIT_UPSIDE_DOWN -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT
Orientation.LANDSCAPE_LEFT -> ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
else -> ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
}

fun getOrientationFromJsOrientation(jsOrientation: Int): Orientation {
return when (jsOrientation) {
fun convertToOrientationFromJsValue(jsValue: Int): Orientation {
return when (jsValue) {
2 -> Orientation.LANDSCAPE_RIGHT
3 -> Orientation.PORTRAIT_UPSIDE_DOWN
4 -> Orientation.LANDSCAPE_LEFT
else -> Orientation.PORTRAIT
}
}

fun getOrientationFromRotation(rotation: Int): Orientation {
return when(rotation) {
fun convertToOrientationFromScreenRotation(screenRotation: Int): Orientation {
return when(screenRotation) {
Surface.ROTATION_270 -> Orientation.LANDSCAPE_RIGHT
Surface.ROTATION_90 -> Orientation.LANDSCAPE_LEFT
Surface.ROTATION_180 -> Orientation.PORTRAIT_UPSIDE_DOWN
else -> Orientation.PORTRAIT
}
}

fun getInterfaceOrientationFromDeviceOrientation(deviceOrientation: Orientation): Orientation {
fun convertToInterfaceOrientationFrom(deviceOrientation: Orientation): Orientation {
return when(deviceOrientation) {
Orientation.PORTRAIT -> Orientation.PORTRAIT
Orientation.LANDSCAPE_RIGHT -> Orientation.LANDSCAPE_LEFT
Expand Down
33 changes: 23 additions & 10 deletions ios/OrientationDirector.mm
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ -(void)stopObserving {
}

- (NSArray<NSString *> *)supportedEvents {
return [OrientationEventManager supportedEvents];
return [EventManager supportedEvents];
}

- (void)sendEventWithName:(NSString * _Nonnull)name params:(NSDictionary *)params {
Expand Down Expand Up @@ -91,6 +91,15 @@ - (void)getDeviceOrientation:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseR
});
}

#ifdef RCT_NEW_ARCH_ENABLED
- (NSNumber *)isLocked
#else
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(isLocked)
#endif
{
return @([_director getIsLocked]);
}

#ifdef RCT_NEW_ARCH_ENABLED
- (void)lockTo:(double)jsOrientation
#else
Expand All @@ -99,7 +108,7 @@ - (void)lockTo:(double)jsOrientation
{
NSNumber *jsOrientationNumber = @(jsOrientation);
dispatch_async(dispatch_get_main_queue(), ^{
[_director lockToRawJsOrientation:jsOrientationNumber];
[_director lockToJsValue:jsOrientationNumber];
});
}

Expand All @@ -115,26 +124,30 @@ - (void)unlock
}

#ifdef RCT_NEW_ARCH_ENABLED
- (NSNumber *)isLocked
- (void)resetSupportedInterfaceOrientations
#else
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(isLocked)
RCT_EXPORT_METHOD(resetSupportedInterfaceOrientations)
#endif
{
return @([_director isLocked]);
dispatch_async(dispatch_get_main_queue(), ^{
[_director resetSupportedInterfaceOrientations];
});
}

/**
This method is a pure stub since we cannot access auto rotation setting in iOS
*/
#ifdef RCT_NEW_ARCH_ENABLED
- (void)resetSupportedInterfaceOrientations
- (NSNumber *)isAutoRotationEnabled
#else
RCT_EXPORT_METHOD(resetSupportedInterfaceOrientations)
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(isAutoRotationEnabled)
#endif
{
dispatch_async(dispatch_get_main_queue(), ^{
[_director resetSupportedInterfaceOrientations];
});
return @(NO);
}



// Don't compile this code when we build for the old architecture.
#ifdef RCT_NEW_ARCH_ENABLED
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
Expand Down
45 changes: 45 additions & 0 deletions ios/implementation/BundleManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// BundleManager.swift
// react-native-orientation-director
//
// Created by gladiuscode on 26/05/2024.
//

import Foundation

class BundleManager {

private var supportedInterfaceOrientations: [UIInterfaceOrientationMask] = [UIInterfaceOrientationMask.all]

init() {
supportedInterfaceOrientations = readSupportedInterfaceOrientations()
}

func getSupportedInterfaceOrientations() -> [UIInterfaceOrientationMask] {
return supportedInterfaceOrientations
}

private func readSupportedInterfaceOrientations() -> [UIInterfaceOrientationMask] {
let orientations = Bundle.main.object(forInfoDictionaryKey: "UISupportedInterfaceOrientations") as? [String]

guard let orientations = orientations else {
return [UIInterfaceOrientationMask.all]
}

return orientations.compactMap { orientation in
switch orientation {
case "UIInterfaceOrientationPortrait":
return UIInterfaceOrientationMask.portrait
case "UIInterfaceOrientationLandscapeLeft":
return UIInterfaceOrientationMask.landscapeLeft
case "UIInterfaceOrientationLandscapeRight":
return UIInterfaceOrientationMask.landscapeRight
case "UIInterfaceOrientationPortraitUpsideDown":
return UIInterfaceOrientationMask.portraitUpsideDown
default:
return UIInterfaceOrientationMask.allButUpsideDown
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

@objc public class OrientationEventManager : NSObject {
@objc public class EventManager : NSObject {
@objc public weak var delegate: OrientationEventEmitterDelegate? = nil

func sendDeviceOrientationDidChange(orientationValue: Int) {
Expand Down Expand Up @@ -56,7 +56,7 @@ import Foundation
func sendEvent(name: String, params: NSDictionary)
}

public extension OrientationEventManager {
public extension EventManager {

enum Event: String, CaseIterable {
case DeviceOrientationDidChange
Expand Down
Loading
Loading