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
5 changes: 0 additions & 5 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,6 @@ jobs:
restore-keys: |
${{ runner.os }}-gradle-

- name: Run test
if: env.turbo_cache_hit != 1
run: |
cd example/android && ./gradlew react-native-orientation-director:testDebugUnitTest

- name: Build example for Android
env:
JAVA_OPTS: "-XX:MaxHeapSize=6g"
Expand Down
5 changes: 0 additions & 5 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ jobs:
restore-keys: |
${{ runner.os }}-gradle-

- name: Run test
if: env.turbo_cache_hit != 1
run: |
cd example/android && ./gradlew react-native-orientation-director:testDebugUnitTest

- name: Build example for Android
env:
JAVA_OPTS: "-XX:MaxHeapSize=6g"
Expand Down
44 changes: 11 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

A React Native library that allows you to listen to orientation changes, lock interface orientation
to a selected one and get current orientation.
Written in Kotlin, Swift and Typescript. It supports both the Old and New React Native architecture.
Written in Kotlin, Swift and Typescript.

Kindly note that this library only supports the new architecture since v3.0.0, if you are looking for a version that
supports the old architecture, please check older versions.

This library takes inspiration from and builds upon the following amazing alternatives:

Expand Down Expand Up @@ -112,40 +115,20 @@ Nothing else is required for Android.

#### iOS

To properly handle interface orientation changes in iOS, you need to update your AppDelegate file. Since React Native
0.77, the AppDelegate has been migrated to Swift, so see the instructions below for both Swift and Objective-C.
To properly handle interface orientation changes in iOS, you need to update your AppDelegate file. Follow the instructions
below to set it up:

##### Objective-C
In your AppDelegate.swift file, implement the supportedInterfaceOrientationsFor method as follows:

In your AppDelegate.mm file, import "OrientationDirector.h" and implement supportedInterfaceOrientationsForWindow method as follows:
```swift

```objc
#import <OrientationDirector.h>
import OrientationDirector

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return [OrientationDirector getSupportedInterfaceOrientationsForWindow];
func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return SharedOrientationDirectorImpl.shared.supportedInterfaceOrientations
}
```

##### Swift

You need to create a [bridging header](https://developer.apple.com/documentation/swift/importing-objective-c-into-swift#Import-Code-Within-an-App-Target)
to import the library, as shown below:

```
#import "OrientationDirector.h"
```

Then, in your AppDelegate.swift file, implement the supportedInterfaceOrientationsFor method as follows:

```swift
override func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
return OrientationDirector.getSupportedInterfaceOrientationsForWindow()
}
```

Note: if you are targeting react-native > 79.x, you can omit the `override` keyword.

If you need help, you can check the example project.

Expand Down Expand Up @@ -212,11 +195,6 @@ differently than on iOS, mainly in the following ways:

This behavior allows us to follow Google's best practices related to the Sensors Framework. More [here](https://developer.android.com/develop/sensors-and-location/sensors/sensors_overview#sensors-practices).

## Roadmap

- [ ] Add JS side tests
- [ ] Add iOS side tests

## Contributing

See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
Expand Down
98 changes: 19 additions & 79 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
buildscript {
ext.getExtOrDefault = {name ->
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['OrientationDirector_' + name]
ext.OrientationDirector = [
kotlinVersion: "2.0.21",
minSdkVersion: 24,
compileSdkVersion: 36,
targetSdkVersion: 36
]

ext.getExtOrDefault = { prop ->
if (rootProject.ext.has(prop)) {
return rootProject.ext.get(prop)
}

return OrientationDirector[prop]
}

repositories {
Expand All @@ -16,51 +27,19 @@ buildscript {
}


def isNewArchitectureEnabled() {
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
}

apply plugin: "com.android.library"
apply plugin: "kotlin-android"

if (isNewArchitectureEnabled()) {
apply plugin: "com.facebook.react"
}

def getExtOrIntegerDefault(name) {
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["OrientationDirector_" + name]).toInteger()
}

def getDefault(name) {
return project.properties["OrientationDirector_" + name]
}

def supportsNamespace() {
def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
def major = parsed[0].toInteger()
def minor = parsed[1].toInteger()

// Namespace support was added in 7.3.0
return (major == 7 && minor >= 3) || major >= 8
}
apply plugin: "com.facebook.react"

android {
if (supportsNamespace()) {
namespace "com.orientationdirector"
namespace "com.orientationdirector"

sourceSets {
main {
manifest.srcFile "src/main/AndroidManifestNew.xml"
}
}
}

compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
compileSdkVersion getExtOrDefault("compileSdkVersion")

defaultConfig {
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
minSdkVersion getExtOrDefault("minSdkVersion")
targetSdkVersion getExtOrDefault("targetSdkVersion")
}

buildFeatures {
Expand All @@ -73,55 +52,16 @@ android {
}
}

lintOptions {
lint {
disable "GradleCompatible"
}

compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}

sourceSets {
main {
if (isNewArchitectureEnabled()) {
java.srcDirs += [
"src/newarch",
// This is needed to build Kotlin project with NewArch enabled
"${project.buildDir}/generated/source/codegen/java"
]
} else {
java.srcDirs += ["src/oldarch"]
}
}
}
}

repositories {
mavenCentral()
google()
}

def kotlin_version = getExtOrDefault("kotlinVersion")
def junit_version = getDefault("junitVersion")
def android_x_core_version = getDefault("androidXCoreVersion")
def robolectric_version = getDefault("robolectricVersion")
def mockito_core_version = getDefault("mockitoCoreVersion")

dependencies {
implementation "com.facebook.react:react-android"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"

testImplementation "junit:junit:$junit_version"
testImplementation "androidx.test:core:$android_x_core_version"
testImplementation "org.robolectric:robolectric:$robolectric_version"
testImplementation "org.mockito:mockito-core:$mockito_core_version"
}

if (isNewArchitectureEnabled()) {
react {
jsRootDir = file("../src/")
libraryName = "OrientationDirector"
codegenJavaPackageName = "com.orientationdirector"
}
}
17 changes: 0 additions & 17 deletions android/gradle.properties

This file was deleted.

3 changes: 1 addition & 2 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.orientationdirector">
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
</manifest>
2 changes: 0 additions & 2 deletions android/src/main/AndroidManifestNew.xml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,33 @@ package com.orientationdirector

import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.WritableMap
import com.orientationdirector.implementation.EventManagerDelegate
import com.orientationdirector.implementation.OrientationDirectorModuleImpl

class OrientationDirectorModule internal constructor(context: ReactApplicationContext) :
NativeOrientationDirectorSpec(context) {
class OrientationDirectorModule(reactContext: ReactApplicationContext) :
NativeOrientationDirectorSpec(reactContext), EventManagerDelegate {

private var implementation = OrientationDirectorModuleImpl(context)

override fun getName() = OrientationDirectorModuleImpl.NAME
override fun getName() = NAME

private var implementation = OrientationDirectorModuleImpl(reactContext, this)

override fun getInterfaceOrientation(promise: Promise) {
promise.resolve(implementation.getInterfaceOrientation().ordinal)
}


override fun getDeviceOrientation(promise: Promise) {
promise.resolve(implementation.getDeviceOrientation().ordinal)
}


override fun lockTo(orientation: Double) {
implementation.lockTo(orientation.toInt())
}


override fun unlock() {
implementation.unlock()
}


override fun resetSupportedInterfaceOrientations() {
implementation.resetSupportedInterfaceOrientations()
}
Expand All @@ -52,8 +49,20 @@ class OrientationDirectorModule internal constructor(context: ReactApplicationCo
return implementation.disableOrientationSensors()
}

override fun addListener(eventName: String) {}
override fun sendOnDeviceOrientationChanged(params: WritableMap) {
emitOnDeviceOrientationChanged(params)
}

override fun removeListeners(count: Double) {}
override fun sendOnInterfaceOrientationChanged(params: WritableMap) {
emitOnInterfaceOrientationChanged(params)
}

override fun sendOnLockChanged(params: WritableMap) {
emitOnLockChanged(params)
}

companion object {
const val NAME = OrientationDirectorModuleImpl.NAME
}

}
Original file line number Diff line number Diff line change
@@ -1,35 +1,30 @@
package com.orientationdirector

import com.facebook.react.BaseReactPackage
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.NativeModule
import com.facebook.react.module.model.ReactModuleInfoProvider
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.module.model.ReactModuleInfo
import com.orientationdirector.implementation.OrientationDirectorModuleImpl
import java.util.HashMap
import com.facebook.react.module.model.ReactModuleInfoProvider

class OrientationDirectorPackage : BaseReactPackage() {
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
return if (name == OrientationDirectorModuleImpl.NAME) {

override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? =
if (name == OrientationDirectorModule.NAME) {
OrientationDirectorModule(reactContext)
} else {
null
}
}

override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
return ReactModuleInfoProvider {
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
val isTurboModule: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
moduleInfos[OrientationDirectorModuleImpl.NAME] = ReactModuleInfo(
OrientationDirectorModuleImpl.NAME,
OrientationDirectorModuleImpl.NAME,
false, // canOverrideExistingModule
false, // needsEagerInit
false, // isCxxModule
isTurboModule // isTurboModule
override fun getReactModuleInfoProvider() = ReactModuleInfoProvider {
mapOf(
OrientationDirectorModule.NAME to ReactModuleInfo(
name = OrientationDirectorModule.NAME,
className = OrientationDirectorModule.NAME,
canOverrideExistingModule = false,
needsEagerInit = false,
isCxxModule = false,
isTurboModule = true
)
moduleInfos
}
)
}
}
Loading