Skip to content

Commit e5d4e11

Browse files
authored
feat: reset to initial supported interface orientation (#11)
1 parent 3815b4d commit e5d4e11

10 files changed

Lines changed: 96 additions & 9 deletions

File tree

android/src/main/java/com/orientationdirector/OrientationDirectorModule.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class OrientationDirectorModule internal constructor(context: ReactApplicationCo
3838
orientationDirectorImpl.unlock()
3939
}
4040

41+
@ReactMethod()
42+
override fun resetSupportedInterfaceOrientations() {
43+
orientationDirectorImpl.resetSupportedInterfaceOrientations()
44+
}
45+
4146
@ReactMethod(isBlockingSynchronousMethod = true)
4247
override fun isLocked(): Boolean {
4348
return orientationDirectorImpl.getIsLocked()

android/src/main/java/com/orientationdirector/implementation/OrientationDirectorImpl.kt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
9595
adaptInterfaceTo(getDeviceOrientation())
9696
}
9797

98+
fun resetSupportedInterfaceOrientations() {
99+
context.currentActivity?.requestedOrientation = initialSupportedInterfaceOrientations
100+
updateIsLockedTo(initIsLocked())
101+
updateLastInterfaceOrientationTo(initInterfaceOrientation())
102+
}
103+
98104
private fun initInterfaceOrientation(): Orientation {
99105
val rotation = mUtils.getInterfaceRotation()
100106
return mUtils.getOrientationFromRotation(rotation)
@@ -138,15 +144,19 @@ class OrientationDirectorImpl internal constructor(private val context: ReactApp
138144
return
139145
}
140146

141-
lastInterfaceOrientation = deviceOrientation
142-
mEventEmitter.sendInterfaceOrientationDidChange(lastInterfaceOrientation.ordinal)
147+
updateLastInterfaceOrientationTo(deviceOrientation)
143148
}
144149

145150
private fun updateIsLockedTo(value: Boolean) {
146151
mEventEmitter.sendLockDidChange(value)
147152
isLocked = value
148153
}
149154

155+
private fun updateLastInterfaceOrientationTo(value: Orientation) {
156+
lastInterfaceOrientation = value
157+
mEventEmitter.sendInterfaceOrientationDidChange(value.ordinal)
158+
}
159+
150160
companion object {
151161
const val NAME = "OrientationDirectorImpl"
152162
}

android/src/oldarch/OrientationDirectorSpec.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ abstract class OrientationDirectorSpec internal constructor(context: ReactApplic
1010
abstract fun getDeviceOrientation(promise: Promise)
1111
abstract fun lockTo(orientation: Double)
1212
abstract fun unlock()
13+
abstract fun resetSupportedInterfaceOrientations()
1314
abstract fun isLocked(): Boolean
1415
abstract fun isAutoRotationEnabled(): Boolean
1516
abstract fun addListener(eventName: String)

example/ios/Podfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,7 @@ PODS:
935935
- React-Mapbuffer (0.74.1):
936936
- glog
937937
- React-debug
938-
- react-native-orientation-director (1.0.1):
938+
- react-native-orientation-director (1.1.0):
939939
- DoubleConversion
940940
- glog
941941
- hermes-engine
@@ -1395,7 +1395,7 @@ SPEC CHECKSUMS:
13951395
React-jsitracing: 233d1a798fe0ff33b8e630b8f00f62c4a8115fbc
13961396
React-logger: 7e7403a2b14c97f847d90763af76b84b152b6fce
13971397
React-Mapbuffer: 11029dcd47c5c9e057a4092ab9c2a8d10a496a33
1398-
react-native-orientation-director: bafe197333f56bc7e25ad17542f159518b1cf3d0
1398+
react-native-orientation-director: 69687eb441507b1c2061dfffe0feb40be19db554
13991399
React-nativeconfig: b0073a590774e8b35192fead188a36d1dca23dec
14001400
React-NativeModulesApple: df46ff3e3de5b842b30b4ca8a6caae6d7c8ab09f
14011401
React-perflogger: 3d31e0d1e8ad891e43a09ac70b7b17a79773003a

example/src/App.tsx

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from 'react';
22

3-
import { Button, StyleSheet, Text, View } from 'react-native';
3+
import { Button, ScrollView, StyleSheet, Text, View } from 'react-native';
44
import RNOrientationDirector, {
55
Orientation,
66
useDeviceOrientation,
@@ -14,7 +14,10 @@ export default function App() {
1414
const isInterfaceOrientationLocked = useIsInterfaceOrientationLocked();
1515

1616
return (
17-
<View style={styles.container}>
17+
<ScrollView
18+
style={styles.container}
19+
contentContainerStyle={styles.contentContainerStyle}
20+
>
1821
<Text style={[styles.text, styles.marginBottom]}>
1922
Current Interface Orientation:
2023
{RNOrientationDirector.convertOrientationToHumanReadableString(
@@ -102,15 +105,25 @@ export default function App() {
102105
RNOrientationDirector.unlock();
103106
}}
104107
/>
105-
</View>
108+
<View style={styles.marginBottom} />
109+
<Button
110+
title={'Reset'}
111+
onPress={() => {
112+
RNOrientationDirector.resetSupportedInterfaceOrientations();
113+
}}
114+
/>
115+
</ScrollView>
106116
);
107117
}
108118

109119
const styles = StyleSheet.create({
110120
container: {
111121
flex: 1,
122+
},
123+
contentContainerStyle: {
112124
alignItems: 'center',
113125
justifyContent: 'center',
126+
flexGrow: 1,
114127
},
115128
text: {
116129
color: 'black',

ios/OrientationDirector.mm

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ - (NSNumber *)isLocked
123123
return @([_director isLocked]);
124124
}
125125

126+
#ifdef RCT_NEW_ARCH_ENABLED
127+
- (void)resetSupportedInterfaceOrientations
128+
#else
129+
RCT_EXPORT_BLOCKING_SYNCHRONOUS_METHOD(resetSupportedInterfaceOrientations)
130+
#endif
131+
{
132+
dispatch_async(dispatch_get_main_queue(), ^{
133+
[_director resetSupportedInterfaceOrientations];
134+
});
135+
}
136+
137+
126138
// Don't compile this code when we build for the old architecture.
127139
#ifdef RCT_NEW_ARCH_ENABLED
128140
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:

ios/implementation/OrientationDirectorImpl.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,26 @@ import UIKit
6666
updateIsLockedTo(value: false)
6767
self.adaptInterfaceTo(deviceOrientation: deviceOrientation)
6868
}
69+
70+
@objc public func resetSupportedInterfaceOrientations() {
71+
self.supportedInterfaceOrientations = self.initialSupportedInterfaceOrientations
72+
self.requestInterfaceUpdateTo(mask: self.supportedInterfaceOrientations)
73+
self.updateIsLockedTo(value: self.initIsLocked())
74+
75+
let lastMask = OrientationDirectorUtils.getMaskFrom(orientation: lastInterfaceOrientation)
76+
let isLastInterfaceOrientationAlreadySupported = self.supportedInterfaceOrientations.contains(lastMask)
77+
if (isLastInterfaceOrientationAlreadySupported) {
78+
return
79+
}
80+
81+
let supportedInterfaceOrientations = OrientationDirectorUtils.readSupportedInterfaceOrientationsFromBundle()
82+
guard let firstSupportedInterfaceOrientation = supportedInterfaceOrientations.first else {
83+
return
84+
}
85+
86+
let orientation = OrientationDirectorUtils.getOrientationFrom(mask: firstSupportedInterfaceOrientation)
87+
self.updateLastInterfaceOrientation(value: orientation)
88+
}
6989

7090
private func initInitialSupportedInterfaceOrientations() -> UIInterfaceOrientationMask {
7191
let supportedInterfaceOrientations = OrientationDirectorUtils.readSupportedInterfaceOrientationsFromBundle()
@@ -139,12 +159,16 @@ import UIKit
139159
return
140160
}
141161

142-
self.eventManager.sendInterfaceOrientationDidChange(orientationValue: deviceOrientation.rawValue)
143-
lastInterfaceOrientation = deviceOrientation
162+
updateLastInterfaceOrientation(value: deviceOrientation)
144163
}
145164

146165
private func updateIsLockedTo(value: Bool) {
147166
eventManager.sendLockDidChange(value: value)
148167
isLocked = value
149168
}
169+
170+
private func updateLastInterfaceOrientation(value: Orientation) {
171+
self.eventManager.sendInterfaceOrientationDidChange(orientationValue: value.rawValue)
172+
lastInterfaceOrientation = value
173+
}
150174
}

ios/implementation/OrientationDirectorUtils.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,23 @@ class OrientationDirectorUtils {
6565

6666
return orientation
6767
}
68+
69+
public static func getOrientationFrom(mask: UIInterfaceOrientationMask) -> Orientation {
70+
var orientation = Orientation.UNKNOWN
71+
72+
switch(mask) {
73+
case UIInterfaceOrientationMask.portraitUpsideDown:
74+
orientation = Orientation.PORTRAIT_UPSIDE_DOWN
75+
case UIInterfaceOrientationMask.landscapeRight:
76+
orientation = Orientation.LANDSCAPE_LEFT
77+
case UIInterfaceOrientationMask.landscapeLeft:
78+
orientation = Orientation.LANDSCAPE_RIGHT
79+
default:
80+
orientation = Orientation.PORTRAIT
81+
}
82+
83+
return orientation
84+
}
6885

6986
/*
7087
Note: .portraitUpsideDown only works for devices with home button

src/NativeOrientationDirector.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export interface Spec extends TurboModule {
88
unlock(): void;
99
isLocked(): boolean;
1010
isAutoRotationEnabled(): boolean;
11+
resetSupportedInterfaceOrientations(): void;
1112

1213
addListener: (eventType: string) => void;
1314
removeListeners: (count: number) => void;

src/RNOrientationDirector.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class RNOrientationDirector {
5555
: AutoRotation.disabled;
5656
}
5757

58+
static resetSupportedInterfaceOrientations(): void {
59+
Module.resetSupportedInterfaceOrientations();
60+
}
61+
5862
static listenForDeviceOrientationChanges(
5963
callback: (orientation: OrientationEvent) => void
6064
) {

0 commit comments

Comments
 (0)