react-native-orientation-director 2.6.4 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (178) hide show
  1. package/README.md +11 -33
  2. package/android/build.gradle +19 -79
  3. package/android/src/main/AndroidManifest.xml +1 -2
  4. package/android/src/{newarch → main/java/com/orientationdirector}/OrientationDirectorModule.kt +20 -11
  5. package/android/src/main/java/com/orientationdirector/OrientationDirectorPackage.kt +15 -20
  6. package/android/src/main/java/com/orientationdirector/implementation/EventManager.kt +4 -17
  7. package/android/src/main/java/com/orientationdirector/implementation/EventManagerDelegate.kt +9 -0
  8. package/android/src/main/java/com/orientationdirector/implementation/OrientationDirectorModuleImpl.kt +23 -24
  9. package/android/src/main/java/com/orientationdirector/implementation/OrientationSensorsEventListener.kt +73 -80
  10. package/android/src/main/java/com/orientationdirector/implementation/Utils.kt +0 -54
  11. package/app.plugin.js +1 -1
  12. package/ios/OrientationDirector.h +1 -18
  13. package/ios/OrientationDirector.mm +35 -35
  14. package/ios/SharedOrientationDirectorImpl.swift +12 -0
  15. package/ios/implementation/EventManager.swift +12 -37
  16. package/ios/implementation/OrientationDirectorImpl.swift +6 -6
  17. package/lib/module/EventEmitter.js +15 -9
  18. package/lib/module/EventEmitter.js.map +1 -1
  19. package/lib/module/NativeOrientationDirector.js.map +1 -1
  20. package/lib/module/module.js +1 -2
  21. package/lib/module/module.js.map +1 -1
  22. package/lib/plugin/index.d.ts +3 -0
  23. package/lib/plugin/index.js +10 -0
  24. package/lib/plugin/withAndroidConfiguration.d.ts +3 -0
  25. package/{plugin/build/withRNOrientationMainActivity.js → lib/plugin/withAndroidConfiguration.js} +3 -3
  26. package/lib/plugin/withIosConfiguration.d.ts +3 -0
  27. package/lib/plugin/withIosConfiguration.js +62 -0
  28. package/lib/typescript/{commonjs/src → src}/EventEmitter.d.ts +5 -4
  29. package/lib/typescript/src/EventEmitter.d.ts.map +1 -0
  30. package/lib/typescript/{commonjs/src → src}/NativeOrientationDirector.d.ts +10 -3
  31. package/lib/typescript/src/NativeOrientationDirector.d.ts.map +1 -0
  32. package/lib/typescript/{module/src → src}/RNOrientationDirector.d.ts +3 -3
  33. package/lib/typescript/src/RNOrientationDirector.d.ts.map +1 -0
  34. package/lib/typescript/src/hooks/useDeviceOrientation.hook.d.ts.map +1 -0
  35. package/lib/typescript/src/hooks/useInterfaceOrientation.hook.d.ts.map +1 -0
  36. package/lib/typescript/src/hooks/useIsInterfaceOrientationLocked.hook.d.ts.map +1 -0
  37. package/lib/typescript/src/index.d.ts.map +1 -0
  38. package/lib/typescript/src/module.d.ts +4 -0
  39. package/lib/typescript/src/module.d.ts.map +1 -0
  40. package/lib/typescript/src/types/AutoRotation.enum.d.ts.map +1 -0
  41. package/lib/typescript/src/types/Event.enum.d.ts.map +1 -0
  42. package/lib/typescript/src/types/HumanReadableAutoRotationsResource.type.d.ts.map +1 -0
  43. package/lib/typescript/src/types/HumanReadableOrientationsResource.type.d.ts.map +1 -0
  44. package/lib/typescript/src/types/LockableOrientation.type.d.ts.map +1 -0
  45. package/lib/typescript/src/types/LockedEvent.interface.d.ts.map +1 -0
  46. package/lib/typescript/src/types/Orientation.enum.d.ts.map +1 -0
  47. package/lib/typescript/src/types/OrientationEvent.interface.d.ts.map +1 -0
  48. package/lib/typescript/src/types/OrientationType.enum.d.ts.map +1 -0
  49. package/package.json +72 -77
  50. package/react-native-orientation-director.podspec +4 -24
  51. package/src/EventEmitter.ts +21 -24
  52. package/src/NativeOrientationDirector.ts +8 -3
  53. package/src/module.ts +1 -3
  54. package/android/gradle.properties +0 -17
  55. package/android/src/main/AndroidManifestNew.xml +0 -2
  56. package/android/src/oldarch/OrientationDirectorModule.kt +0 -66
  57. package/android/src/test/java/com/orientationdirector/implementation/OrientationDirectorModuleImplTest.kt +0 -199
  58. package/android/src/test/java/com/orientationdirector/implementation/UtilsTest.kt +0 -314
  59. package/ios/react-native-orientation-director-Bridging-Header.h +0 -6
  60. package/lib/commonjs/EventEmitter.js +0 -48
  61. package/lib/commonjs/NativeOrientationDirector.js +0 -8
  62. package/lib/commonjs/RNOrientationDirector.js +0 -126
  63. package/lib/commonjs/hooks/useDeviceOrientation.hook.js +0 -37
  64. package/lib/commonjs/hooks/useInterfaceOrientation.hook.js +0 -37
  65. package/lib/commonjs/hooks/useIsInterfaceOrientationLocked.hook.js +0 -27
  66. package/lib/commonjs/index.js +0 -51
  67. package/lib/commonjs/module.js +0 -22
  68. package/lib/commonjs/package.json +0 -1
  69. package/lib/commonjs/types/AutoRotation.enum.js +0 -12
  70. package/lib/commonjs/types/Event.enum.js +0 -13
  71. package/lib/commonjs/types/HumanReadableAutoRotationsResource.type.js +0 -5
  72. package/lib/commonjs/types/HumanReadableOrientationsResource.type.js +0 -5
  73. package/lib/commonjs/types/LockableOrientation.type.js +0 -5
  74. package/lib/commonjs/types/LockedEvent.interface.js +0 -1
  75. package/lib/commonjs/types/Orientation.enum.js +0 -17
  76. package/lib/commonjs/types/OrientationEvent.interface.js +0 -5
  77. package/lib/commonjs/types/OrientationType.enum.js +0 -11
  78. package/lib/typescript/commonjs/example/src/App.d.ts +0 -2
  79. package/lib/typescript/commonjs/example/src/App.d.ts.map +0 -1
  80. package/lib/typescript/commonjs/example/src/AppNavigationContainer.d.ts +0 -3
  81. package/lib/typescript/commonjs/example/src/AppNavigationContainer.d.ts.map +0 -1
  82. package/lib/typescript/commonjs/example/src/screens/Explore.d.ts +0 -3
  83. package/lib/typescript/commonjs/example/src/screens/Explore.d.ts.map +0 -1
  84. package/lib/typescript/commonjs/example/src/screens/Home.d.ts +0 -3
  85. package/lib/typescript/commonjs/example/src/screens/Home.d.ts.map +0 -1
  86. package/lib/typescript/commonjs/example/src/screens/InnerExplore.d.ts +0 -3
  87. package/lib/typescript/commonjs/example/src/screens/InnerExplore.d.ts.map +0 -1
  88. package/lib/typescript/commonjs/example/src/screens/styles.d.ts +0 -51
  89. package/lib/typescript/commonjs/example/src/screens/styles.d.ts.map +0 -1
  90. package/lib/typescript/commonjs/package.json +0 -1
  91. package/lib/typescript/commonjs/src/EventEmitter.d.ts.map +0 -1
  92. package/lib/typescript/commonjs/src/NativeOrientationDirector.d.ts.map +0 -1
  93. package/lib/typescript/commonjs/src/RNOrientationDirector.d.ts +0 -62
  94. package/lib/typescript/commonjs/src/RNOrientationDirector.d.ts.map +0 -1
  95. package/lib/typescript/commonjs/src/hooks/useDeviceOrientation.hook.d.ts.map +0 -1
  96. package/lib/typescript/commonjs/src/hooks/useInterfaceOrientation.hook.d.ts.map +0 -1
  97. package/lib/typescript/commonjs/src/hooks/useIsInterfaceOrientationLocked.hook.d.ts.map +0 -1
  98. package/lib/typescript/commonjs/src/index.d.ts.map +0 -1
  99. package/lib/typescript/commonjs/src/module.d.ts +0 -6
  100. package/lib/typescript/commonjs/src/module.d.ts.map +0 -1
  101. package/lib/typescript/commonjs/src/types/AutoRotation.enum.d.ts.map +0 -1
  102. package/lib/typescript/commonjs/src/types/Event.enum.d.ts.map +0 -1
  103. package/lib/typescript/commonjs/src/types/HumanReadableAutoRotationsResource.type.d.ts.map +0 -1
  104. package/lib/typescript/commonjs/src/types/HumanReadableOrientationsResource.type.d.ts.map +0 -1
  105. package/lib/typescript/commonjs/src/types/LockableOrientation.type.d.ts.map +0 -1
  106. package/lib/typescript/commonjs/src/types/LockedEvent.interface.d.ts.map +0 -1
  107. package/lib/typescript/commonjs/src/types/Orientation.enum.d.ts.map +0 -1
  108. package/lib/typescript/commonjs/src/types/OrientationEvent.interface.d.ts.map +0 -1
  109. package/lib/typescript/commonjs/src/types/OrientationType.enum.d.ts.map +0 -1
  110. package/lib/typescript/module/example/src/App.d.ts +0 -2
  111. package/lib/typescript/module/example/src/App.d.ts.map +0 -1
  112. package/lib/typescript/module/example/src/AppNavigationContainer.d.ts +0 -3
  113. package/lib/typescript/module/example/src/AppNavigationContainer.d.ts.map +0 -1
  114. package/lib/typescript/module/example/src/screens/Explore.d.ts +0 -3
  115. package/lib/typescript/module/example/src/screens/Explore.d.ts.map +0 -1
  116. package/lib/typescript/module/example/src/screens/Home.d.ts +0 -3
  117. package/lib/typescript/module/example/src/screens/Home.d.ts.map +0 -1
  118. package/lib/typescript/module/example/src/screens/InnerExplore.d.ts +0 -3
  119. package/lib/typescript/module/example/src/screens/InnerExplore.d.ts.map +0 -1
  120. package/lib/typescript/module/example/src/screens/styles.d.ts +0 -51
  121. package/lib/typescript/module/example/src/screens/styles.d.ts.map +0 -1
  122. package/lib/typescript/module/src/EventEmitter.d.ts +0 -11
  123. package/lib/typescript/module/src/EventEmitter.d.ts.map +0 -1
  124. package/lib/typescript/module/src/NativeOrientationDirector.d.ts +0 -17
  125. package/lib/typescript/module/src/NativeOrientationDirector.d.ts.map +0 -1
  126. package/lib/typescript/module/src/RNOrientationDirector.d.ts.map +0 -1
  127. package/lib/typescript/module/src/hooks/useDeviceOrientation.hook.d.ts +0 -8
  128. package/lib/typescript/module/src/hooks/useDeviceOrientation.hook.d.ts.map +0 -1
  129. package/lib/typescript/module/src/hooks/useInterfaceOrientation.hook.d.ts +0 -8
  130. package/lib/typescript/module/src/hooks/useInterfaceOrientation.hook.d.ts.map +0 -1
  131. package/lib/typescript/module/src/hooks/useIsInterfaceOrientationLocked.hook.d.ts +0 -7
  132. package/lib/typescript/module/src/hooks/useIsInterfaceOrientationLocked.hook.d.ts.map +0 -1
  133. package/lib/typescript/module/src/index.d.ts +0 -12
  134. package/lib/typescript/module/src/index.d.ts.map +0 -1
  135. package/lib/typescript/module/src/module.d.ts +0 -6
  136. package/lib/typescript/module/src/module.d.ts.map +0 -1
  137. package/lib/typescript/module/src/types/AutoRotation.enum.d.ts +0 -6
  138. package/lib/typescript/module/src/types/AutoRotation.enum.d.ts.map +0 -1
  139. package/lib/typescript/module/src/types/Event.enum.d.ts +0 -7
  140. package/lib/typescript/module/src/types/Event.enum.d.ts.map +0 -1
  141. package/lib/typescript/module/src/types/HumanReadableAutoRotationsResource.type.d.ts +0 -3
  142. package/lib/typescript/module/src/types/HumanReadableAutoRotationsResource.type.d.ts.map +0 -1
  143. package/lib/typescript/module/src/types/HumanReadableOrientationsResource.type.d.ts +0 -3
  144. package/lib/typescript/module/src/types/HumanReadableOrientationsResource.type.d.ts.map +0 -1
  145. package/lib/typescript/module/src/types/LockableOrientation.type.d.ts +0 -3
  146. package/lib/typescript/module/src/types/LockableOrientation.type.d.ts.map +0 -1
  147. package/lib/typescript/module/src/types/LockedEvent.interface.d.ts +0 -4
  148. package/lib/typescript/module/src/types/LockedEvent.interface.d.ts.map +0 -1
  149. package/lib/typescript/module/src/types/Orientation.enum.d.ts +0 -11
  150. package/lib/typescript/module/src/types/Orientation.enum.d.ts.map +0 -1
  151. package/lib/typescript/module/src/types/OrientationEvent.interface.d.ts +0 -5
  152. package/lib/typescript/module/src/types/OrientationEvent.interface.d.ts.map +0 -1
  153. package/lib/typescript/module/src/types/OrientationType.enum.d.ts +0 -5
  154. package/lib/typescript/module/src/types/OrientationType.enum.d.ts.map +0 -1
  155. package/plugin/build/custom-mod/withBridgingHeader.d.ts +0 -12
  156. package/plugin/build/custom-mod/withBridgingHeader.js +0 -107
  157. package/plugin/build/index.d.ts +0 -3
  158. package/plugin/build/index.js +0 -27
  159. package/plugin/build/withRNOrientationAppDelegate.d.ts +0 -4
  160. package/plugin/build/withRNOrientationAppDelegate.js +0 -102
  161. package/plugin/build/withRNOrientationBridgingHeader.d.ts +0 -3
  162. package/plugin/build/withRNOrientationBridgingHeader.js +0 -27
  163. package/plugin/build/withRNOrientationMainActivity.d.ts +0 -3
  164. package/plugin/tsconfig.json +0 -9
  165. /package/lib/typescript/{module/package.json → package.json} +0 -0
  166. /package/lib/typescript/{commonjs/src → src}/hooks/useDeviceOrientation.hook.d.ts +0 -0
  167. /package/lib/typescript/{commonjs/src → src}/hooks/useInterfaceOrientation.hook.d.ts +0 -0
  168. /package/lib/typescript/{commonjs/src → src}/hooks/useIsInterfaceOrientationLocked.hook.d.ts +0 -0
  169. /package/lib/typescript/{commonjs/src → src}/index.d.ts +0 -0
  170. /package/lib/typescript/{commonjs/src → src}/types/AutoRotation.enum.d.ts +0 -0
  171. /package/lib/typescript/{commonjs/src → src}/types/Event.enum.d.ts +0 -0
  172. /package/lib/typescript/{commonjs/src → src}/types/HumanReadableAutoRotationsResource.type.d.ts +0 -0
  173. /package/lib/typescript/{commonjs/src → src}/types/HumanReadableOrientationsResource.type.d.ts +0 -0
  174. /package/lib/typescript/{commonjs/src → src}/types/LockableOrientation.type.d.ts +0 -0
  175. /package/lib/typescript/{commonjs/src → src}/types/LockedEvent.interface.d.ts +0 -0
  176. /package/lib/typescript/{commonjs/src → src}/types/Orientation.enum.d.ts +0 -0
  177. /package/lib/typescript/{commonjs/src → src}/types/OrientationEvent.interface.d.ts +0 -0
  178. /package/lib/typescript/{commonjs/src → src}/types/OrientationType.enum.d.ts +0 -0
package/README.md CHANGED
@@ -6,7 +6,10 @@
6
6
 
7
7
  A React Native library that allows you to listen to orientation changes, lock interface orientation
8
8
  to a selected one and get current orientation.
9
- Written in Kotlin, Swift and Typescript. It supports both the Old and New React Native architecture.
9
+ Written in Kotlin, Swift and Typescript.
10
+
11
+ Kindly note that this library only supports the new architecture since v3.0.0, if you are looking for a version that
12
+ supports the old architecture, please check older versions.
10
13
 
11
14
  This library takes inspiration from and builds upon the following amazing alternatives:
12
15
 
@@ -112,40 +115,20 @@ Nothing else is required for Android.
112
115
 
113
116
  #### iOS
114
117
 
115
- To properly handle interface orientation changes in iOS, you need to update your AppDelegate file. Since React Native
116
- 0.77, the AppDelegate has been migrated to Swift, so see the instructions below for both Swift and Objective-C.
118
+ To properly handle interface orientation changes in iOS, you need to update your AppDelegate file. Follow the instructions
119
+ below to set it up:
117
120
 
118
- ##### Objective-C
121
+ In your AppDelegate.swift file, implement the supportedInterfaceOrientationsFor method as follows:
119
122
 
120
- In your AppDelegate.mm file, import "OrientationDirector.h" and implement supportedInterfaceOrientationsForWindow method as follows:
123
+ ```swift
121
124
 
122
- ```objc
123
- #import <OrientationDirector.h>
125
+ import OrientationDirector
124
126
 
125
- - (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
126
- {
127
- return [OrientationDirector getSupportedInterfaceOrientationsForWindow];
127
+ func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
128
+ return SharedOrientationDirectorImpl.shared.supportedInterfaceOrientations
128
129
  }
129
- ```
130
-
131
- ##### Swift
132
-
133
- You need to create a [bridging header](https://developer.apple.com/documentation/swift/importing-objective-c-into-swift#Import-Code-Within-an-App-Target)
134
- to import the library, as shown below:
135
130
 
136
131
  ```
137
- #import "OrientationDirector.h"
138
- ```
139
-
140
- Then, in your AppDelegate.swift file, implement the supportedInterfaceOrientationsFor method as follows:
141
-
142
- ```swift
143
- override func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
144
- return OrientationDirector.getSupportedInterfaceOrientationsForWindow()
145
- }
146
- ```
147
-
148
- Note: if you are targeting react-native > 79.x, you can omit the `override` keyword.
149
132
 
150
133
  If you need help, you can check the example project.
151
134
 
@@ -212,11 +195,6 @@ differently than on iOS, mainly in the following ways:
212
195
 
213
196
  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).
214
197
 
215
- ## Roadmap
216
-
217
- - [ ] Add JS side tests
218
- - [ ] Add iOS side tests
219
-
220
198
  ## Contributing
221
199
 
222
200
  See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
@@ -1,6 +1,17 @@
1
1
  buildscript {
2
- ext.getExtOrDefault = {name ->
3
- return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties['OrientationDirector_' + name]
2
+ ext.OrientationDirector = [
3
+ kotlinVersion: "2.0.21",
4
+ minSdkVersion: 24,
5
+ compileSdkVersion: 36,
6
+ targetSdkVersion: 36
7
+ ]
8
+
9
+ ext.getExtOrDefault = { prop ->
10
+ if (rootProject.ext.has(prop)) {
11
+ return rootProject.ext.get(prop)
12
+ }
13
+
14
+ return OrientationDirector[prop]
4
15
  }
5
16
 
6
17
  repositories {
@@ -16,51 +27,19 @@ buildscript {
16
27
  }
17
28
 
18
29
 
19
- def isNewArchitectureEnabled() {
20
- return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
21
- }
22
-
23
30
  apply plugin: "com.android.library"
24
31
  apply plugin: "kotlin-android"
25
32
 
26
- if (isNewArchitectureEnabled()) {
27
- apply plugin: "com.facebook.react"
28
- }
29
-
30
- def getExtOrIntegerDefault(name) {
31
- return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["OrientationDirector_" + name]).toInteger()
32
- }
33
-
34
- def getDefault(name) {
35
- return project.properties["OrientationDirector_" + name]
36
- }
37
-
38
- def supportsNamespace() {
39
- def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
40
- def major = parsed[0].toInteger()
41
- def minor = parsed[1].toInteger()
42
-
43
- // Namespace support was added in 7.3.0
44
- return (major == 7 && minor >= 3) || major >= 8
45
- }
33
+ apply plugin: "com.facebook.react"
46
34
 
47
35
  android {
48
- if (supportsNamespace()) {
49
- namespace "com.orientationdirector"
36
+ namespace "com.orientationdirector"
50
37
 
51
- sourceSets {
52
- main {
53
- manifest.srcFile "src/main/AndroidManifestNew.xml"
54
- }
55
- }
56
- }
57
-
58
- compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
38
+ compileSdkVersion getExtOrDefault("compileSdkVersion")
59
39
 
60
40
  defaultConfig {
61
- minSdkVersion getExtOrIntegerDefault("minSdkVersion")
62
- targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
63
- buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
41
+ minSdkVersion getExtOrDefault("minSdkVersion")
42
+ targetSdkVersion getExtOrDefault("targetSdkVersion")
64
43
  }
65
44
 
66
45
  buildFeatures {
@@ -73,7 +52,7 @@ android {
73
52
  }
74
53
  }
75
54
 
76
- lintOptions {
55
+ lint {
77
56
  disable "GradleCompatible"
78
57
  }
79
58
 
@@ -81,47 +60,8 @@ android {
81
60
  sourceCompatibility JavaVersion.VERSION_1_8
82
61
  targetCompatibility JavaVersion.VERSION_1_8
83
62
  }
84
-
85
- sourceSets {
86
- main {
87
- if (isNewArchitectureEnabled()) {
88
- java.srcDirs += [
89
- "src/newarch",
90
- // This is needed to build Kotlin project with NewArch enabled
91
- "${project.buildDir}/generated/source/codegen/java"
92
- ]
93
- } else {
94
- java.srcDirs += ["src/oldarch"]
95
- }
96
- }
97
- }
98
- }
99
-
100
- repositories {
101
- mavenCentral()
102
- google()
103
63
  }
104
64
 
105
- def kotlin_version = getExtOrDefault("kotlinVersion")
106
- def junit_version = getDefault("junitVersion")
107
- def android_x_core_version = getDefault("androidXCoreVersion")
108
- def robolectric_version = getDefault("robolectricVersion")
109
- def mockito_core_version = getDefault("mockitoCoreVersion")
110
-
111
65
  dependencies {
112
66
  implementation "com.facebook.react:react-android"
113
- implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
114
-
115
- testImplementation "junit:junit:$junit_version"
116
- testImplementation "androidx.test:core:$android_x_core_version"
117
- testImplementation "org.robolectric:robolectric:$robolectric_version"
118
- testImplementation "org.mockito:mockito-core:$mockito_core_version"
119
- }
120
-
121
- if (isNewArchitectureEnabled()) {
122
- react {
123
- jsRootDir = file("../src/")
124
- libraryName = "OrientationDirector"
125
- codegenJavaPackageName = "com.orientationdirector"
126
- }
127
67
  }
@@ -1,3 +1,2 @@
1
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
2
- package="com.orientationdirector">
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
3
2
  </manifest>
@@ -2,36 +2,33 @@ package com.orientationdirector
2
2
 
3
3
  import com.facebook.react.bridge.Promise
4
4
  import com.facebook.react.bridge.ReactApplicationContext
5
+ import com.facebook.react.bridge.WritableMap
6
+ import com.orientationdirector.implementation.EventManagerDelegate
5
7
  import com.orientationdirector.implementation.OrientationDirectorModuleImpl
6
8
 
7
- class OrientationDirectorModule internal constructor(context: ReactApplicationContext) :
8
- NativeOrientationDirectorSpec(context) {
9
+ class OrientationDirectorModule(reactContext: ReactApplicationContext) :
10
+ NativeOrientationDirectorSpec(reactContext), EventManagerDelegate {
9
11
 
10
- private var implementation = OrientationDirectorModuleImpl(context)
11
-
12
- override fun getName() = OrientationDirectorModuleImpl.NAME
12
+ override fun getName() = NAME
13
13
 
14
+ private var implementation = OrientationDirectorModuleImpl(reactContext, this)
14
15
 
15
16
  override fun getInterfaceOrientation(promise: Promise) {
16
17
  promise.resolve(implementation.getInterfaceOrientation().ordinal)
17
18
  }
18
19
 
19
-
20
20
  override fun getDeviceOrientation(promise: Promise) {
21
21
  promise.resolve(implementation.getDeviceOrientation().ordinal)
22
22
  }
23
23
 
24
-
25
24
  override fun lockTo(orientation: Double) {
26
25
  implementation.lockTo(orientation.toInt())
27
26
  }
28
27
 
29
-
30
28
  override fun unlock() {
31
29
  implementation.unlock()
32
30
  }
33
31
 
34
-
35
32
  override fun resetSupportedInterfaceOrientations() {
36
33
  implementation.resetSupportedInterfaceOrientations()
37
34
  }
@@ -52,8 +49,20 @@ class OrientationDirectorModule internal constructor(context: ReactApplicationCo
52
49
  return implementation.disableOrientationSensors()
53
50
  }
54
51
 
55
- override fun addListener(eventName: String) {}
52
+ override fun sendOnDeviceOrientationChanged(params: WritableMap) {
53
+ emitOnDeviceOrientationChanged(params)
54
+ }
56
55
 
57
- override fun removeListeners(count: Double) {}
56
+ override fun sendOnInterfaceOrientationChanged(params: WritableMap) {
57
+ emitOnInterfaceOrientationChanged(params)
58
+ }
59
+
60
+ override fun sendOnLockChanged(params: WritableMap) {
61
+ emitOnLockChanged(params)
62
+ }
63
+
64
+ companion object {
65
+ const val NAME = OrientationDirectorModuleImpl.NAME
66
+ }
58
67
 
59
68
  }
@@ -1,35 +1,30 @@
1
1
  package com.orientationdirector
2
2
 
3
3
  import com.facebook.react.BaseReactPackage
4
- import com.facebook.react.bridge.ReactApplicationContext
5
4
  import com.facebook.react.bridge.NativeModule
6
- import com.facebook.react.module.model.ReactModuleInfoProvider
5
+ import com.facebook.react.bridge.ReactApplicationContext
7
6
  import com.facebook.react.module.model.ReactModuleInfo
8
- import com.orientationdirector.implementation.OrientationDirectorModuleImpl
9
- import java.util.HashMap
7
+ import com.facebook.react.module.model.ReactModuleInfoProvider
10
8
 
11
9
  class OrientationDirectorPackage : BaseReactPackage() {
12
- override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
13
- return if (name == OrientationDirectorModuleImpl.NAME) {
10
+
11
+ override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? =
12
+ if (name == OrientationDirectorModule.NAME) {
14
13
  OrientationDirectorModule(reactContext)
15
14
  } else {
16
15
  null
17
16
  }
18
- }
19
17
 
20
- override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
21
- return ReactModuleInfoProvider {
22
- val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
23
- val isTurboModule: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
24
- moduleInfos[OrientationDirectorModuleImpl.NAME] = ReactModuleInfo(
25
- OrientationDirectorModuleImpl.NAME,
26
- OrientationDirectorModuleImpl.NAME,
27
- false, // canOverrideExistingModule
28
- false, // needsEagerInit
29
- false, // isCxxModule
30
- isTurboModule // isTurboModule
18
+ override fun getReactModuleInfoProvider() = ReactModuleInfoProvider {
19
+ mapOf(
20
+ OrientationDirectorModule.NAME to ReactModuleInfo(
21
+ name = OrientationDirectorModule.NAME,
22
+ className = OrientationDirectorModule.NAME,
23
+ canOverrideExistingModule = false,
24
+ needsEagerInit = false,
25
+ isCxxModule = false,
26
+ isTurboModule = true
31
27
  )
32
- moduleInfos
33
- }
28
+ )
34
29
  }
35
30
  }
@@ -5,39 +5,26 @@ import com.facebook.react.bridge.ReactApplicationContext
5
5
  import com.facebook.react.bridge.WritableMap
6
6
  import com.facebook.react.modules.core.DeviceEventManagerModule
7
7
 
8
- enum class Event {
9
- DeviceOrientationDidChange,
10
- InterfaceOrientationDidChange,
11
- LockDidChange,
12
- }
13
-
14
- class EventManager(private val context: ReactApplicationContext) {
15
-
8
+ class EventManager(private val delegate: EventManagerDelegate) {
16
9
  fun sendDeviceOrientationDidChange(orientationValue: Int) {
17
10
  val params = Arguments.createMap().apply {
18
11
  putInt("orientation", orientationValue)
19
12
  }
20
- sendEvent(Event.DeviceOrientationDidChange, params)
13
+ delegate.sendOnDeviceOrientationChanged(params)
21
14
  }
22
15
 
23
16
  fun sendInterfaceOrientationDidChange(orientationValue: Int) {
24
17
  val params = Arguments.createMap().apply {
25
18
  putInt("orientation", orientationValue)
26
19
  }
27
- sendEvent(Event.InterfaceOrientationDidChange, params)
20
+ delegate.sendOnInterfaceOrientationChanged(params)
28
21
  }
29
22
 
30
23
  fun sendLockDidChange(value: Boolean) {
31
24
  val params = Arguments.createMap().apply {
32
25
  putBoolean("locked", value)
33
26
  }
34
- sendEvent(Event.LockDidChange, params)
35
- }
36
-
37
- private fun sendEvent(eventName: Event, params: WritableMap?) {
38
- context
39
- .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
40
- .emit(eventName.name, params)
27
+ delegate.sendOnLockChanged(params)
41
28
  }
42
29
 
43
30
  companion object {
@@ -0,0 +1,9 @@
1
+ package com.orientationdirector.implementation
2
+
3
+ import com.facebook.react.bridge.WritableMap
4
+
5
+ interface EventManagerDelegate {
6
+ fun sendOnDeviceOrientationChanged(params: WritableMap)
7
+ fun sendOnInterfaceOrientationChanged(params: WritableMap)
8
+ fun sendOnLockChanged(params: WritableMap)
9
+ }
@@ -5,9 +5,9 @@ import android.os.Handler
5
5
  import android.os.Looper
6
6
  import com.facebook.react.bridge.ReactApplicationContext
7
7
 
8
- class OrientationDirectorModuleImpl internal constructor(private val context: ReactApplicationContext) {
8
+ class OrientationDirectorModuleImpl internal constructor(private val context: ReactApplicationContext, eventManagerDelegate: EventManagerDelegate) {
9
9
  private var mUtils = Utils(context)
10
- private var mEventManager = EventManager(context)
10
+ private var mEventManager = EventManager(eventManagerDelegate)
11
11
  private var mOrientationSensorsEventListener = OrientationSensorsEventListener(context)
12
12
  private var mAutoRotationObserver = AutoRotationObserver(
13
13
  context, Handler(
@@ -26,8 +26,8 @@ class OrientationDirectorModuleImpl internal constructor(private val context: Re
26
26
  private var didComputeInitialDeviceOrientation = false;
27
27
 
28
28
  init {
29
- mOrientationSensorsEventListener.setOnOrientationAnglesChangedCallback { orientation ->
30
- onOrientationAnglesChanged(orientation)
29
+ mOrientationSensorsEventListener.setOnDeviceOrientationChangedCallback { orientation ->
30
+ onDeviceOrientationChanged(orientation)
31
31
  }
32
32
 
33
33
  mAutoRotationObserver.enable()
@@ -36,6 +36,9 @@ class OrientationDirectorModuleImpl internal constructor(private val context: Re
36
36
  checkInterfaceOrientation(false)
37
37
  }
38
38
 
39
+ // NOTE(1.init): This is needed to compute the initial device orientation
40
+ mOrientationSensorsEventListener.enable()
41
+
39
42
  context.addLifecycleEventListener(mLifecycleListener)
40
43
  mLifecycleListener.setOnHostResumeCallback {
41
44
  if (!didComputeInitialDeviceOrientation || areOrientationSensorsEnabled) {
@@ -45,17 +48,17 @@ class OrientationDirectorModuleImpl internal constructor(private val context: Re
45
48
  mBroadcastReceiver.register()
46
49
  }
47
50
  mLifecycleListener.setOnHostPauseCallback {
48
- if (initialized && areOrientationSensorsEnabled) {
51
+ if (!didComputeInitialDeviceOrientation || areOrientationSensorsEnabled) {
49
52
  mOrientationSensorsEventListener.disable()
50
- mAutoRotationObserver.disable()
51
53
  }
54
+ mAutoRotationObserver.disable()
52
55
  mBroadcastReceiver.unregister()
53
56
  }
54
57
  mLifecycleListener.setOnHostDestroyCallback {
55
- if (areOrientationSensorsEnabled) {
58
+ if (!didComputeInitialDeviceOrientation || areOrientationSensorsEnabled) {
56
59
  mOrientationSensorsEventListener.disable()
57
- mAutoRotationObserver.disable()
58
60
  }
61
+ mAutoRotationObserver.disable()
59
62
  mBroadcastReceiver.unregister()
60
63
  }
61
64
 
@@ -148,35 +151,31 @@ class OrientationDirectorModuleImpl internal constructor(private val context: Re
148
151
  ).contains(activity.requestedOrientation)
149
152
  }
150
153
 
151
- private fun onOrientationAnglesChanged(orientationAngles: FloatArray) {
152
- val deviceOrientation = mUtils.convertToDeviceOrientationFrom(orientationAngles)
153
- if (deviceOrientation == Orientation.UNKNOWN) {
154
- return
155
- }
154
+ private fun onDeviceOrientationChanged(deviceOrientation: Orientation) {
155
+ if (deviceOrientation == Orientation.UNKNOWN) return
156
156
 
157
- if (lastDeviceOrientation == deviceOrientation) {
158
- return
159
- }
157
+ if (lastDeviceOrientation == deviceOrientation) return
160
158
 
161
159
  mEventManager.sendDeviceOrientationDidChange(deviceOrientation.ordinal)
162
160
  lastDeviceOrientation = deviceOrientation
163
161
 
164
162
  checkInterfaceOrientation()
165
163
 
166
- if (!didComputeInitialDeviceOrientation) {
167
- didComputeInitialDeviceOrientation = true
164
+ // NOTE(2.init): This is needed to disable sensors if they were needed just for the initial
165
+ // device computation.
166
+ if (didComputeInitialDeviceOrientation) return
167
+
168
+ didComputeInitialDeviceOrientation = true
169
+
170
+ if (!areOrientationSensorsEnabled) {
168
171
  mOrientationSensorsEventListener.disable()
169
172
  }
170
173
  }
171
174
 
172
175
  private fun checkInterfaceOrientation(skipIfAutoRotationIsDisabled: Boolean = true) {
173
- if (skipIfAutoRotationIsDisabled && !mAutoRotationObserver.getLastAutoRotationStatus()) {
174
- return
175
- }
176
+ if (skipIfAutoRotationIsDisabled && !mAutoRotationObserver.getLastAutoRotationStatus()) return
176
177
 
177
- if (isLocked) {
178
- return
179
- }
178
+ if (isLocked) return
180
179
 
181
180
  if (lastDeviceOrientation != Orientation.LANDSCAPE_RIGHT && lastDeviceOrientation != Orientation.LANDSCAPE_LEFT) {
182
181
  val rotation = mUtils.getInterfaceRotation()