diff --git a/ios/OrientationDirector.mm b/ios/OrientationDirector.mm index 8dd30db..5d02375 100644 --- a/ios/OrientationDirector.mm +++ b/ios/OrientationDirector.mm @@ -10,11 +10,12 @@ #import "OrientationDirector/OrientationDirector-Swift.h" #endif +#include + static OrientationDirectorImpl *_director = SharedOrientationDirectorImpl.shared; /////////////////////////////////////////////////////////////////////////////////////// /// EVENT EMITTER SETUP -///https://github.com/react-native-community/RNNewArchitectureLibraries/tree/feat/swift-event-emitter @interface OrientationDirector() @end /// @@ -43,17 +44,31 @@ + (BOOL)requiresMainQueueSetup /////////////////////////////////////////////////////////////////////////////////////// /// EVENT EMITTER SETUP /// --(void)emitOnDeviceOrientationDidChangeWithParams:(NSDictionary*)params { - [self emitOnDeviceOrientationChanged:params]; + +-(void)emitDeviceOrientationChangedWithParams:(NSDictionary*)params { + try { + [self emitOnDeviceOrientationChanged:params]; + } catch (std::exception &e) { + // Ignore if no listeners + } } --(void)emitOnInterfaceOrientationDidChangeWithParams:(NSDictionary*)params { - [self emitOnInterfaceOrientationChanged:params]; +-(void)emitInterfaceOrientationChangedWithParams:(NSDictionary*)params { + try { + [self emitOnInterfaceOrientationChanged:params]; + } catch (std::exception &e) { + // Ignore if no listeners + } } -(void)emitOnLockChangedWithParams:(NSDictionary*)params { - [self emitOnLockChanged:params]; + try { + [self emitOnLockChanged:params]; + } catch (std::exception &e) { + // Ignore if no listeners + } } + /// /////////////////////////////////////////////////////////////////////////////////////// diff --git a/ios/implementation/EventManager.swift b/ios/implementation/EventManager.swift index 0d32361..486712a 100644 --- a/ios/implementation/EventManager.swift +++ b/ios/implementation/EventManager.swift @@ -14,9 +14,9 @@ public class EventManager: NSObject { guard let delegate = delegate else { return } - + let params = Dictionary(dictionaryLiteral: ("orientation", value)) - delegate.emitOnDeviceOrientationDidChange(params: params as NSDictionary) + delegate.emitDeviceOrientationChanged(params: params as NSDictionary) } func sendInterfaceOrientationDidChange(value: Int) { @@ -25,7 +25,7 @@ public class EventManager: NSObject { } let params = Dictionary(dictionaryLiteral: ("orientation", value)) - delegate.emitOnInterfaceOrientationDidChange(params: params as NSDictionary) + delegate.emitInterfaceOrientationChanged(params: params as NSDictionary) } func sendLockDidChange(value: Bool) { @@ -40,6 +40,6 @@ public class EventManager: NSObject { @objc public protocol OrientationEventEmitterDelegate { func emitOnLockChanged(params: NSDictionary) - func emitOnDeviceOrientationDidChange(params: NSDictionary) - func emitOnInterfaceOrientationDidChange(params: NSDictionary) + func emitDeviceOrientationChanged(params: NSDictionary) + func emitInterfaceOrientationChanged(params: NSDictionary) } diff --git a/src/EventEmitter.ts b/src/EventEmitter.ts index f20e05e..a827218 100644 --- a/src/EventEmitter.ts +++ b/src/EventEmitter.ts @@ -1,5 +1,5 @@ -import { Platform, type EventSubscription } from 'react-native'; -import Module from './module'; +import { type EventSubscription, Platform } from 'react-native'; +import NativeOrientationDirector from './NativeOrientationDirector'; import type { OrientationEvent } from './types/OrientationEvent.interface'; import type { LockedEvent } from './types/LockedEvent.interface'; @@ -9,7 +9,8 @@ class EventEmitter { static addDeviceOrientationDidChangeListener( callback: (orientation: OrientationEvent) => void ) { - let listener = Module.onDeviceOrientationChanged(callback); + let listener = + NativeOrientationDirector.onDeviceOrientationChanged(callback); if (Platform.OS !== 'android') { return listener; @@ -17,7 +18,7 @@ class EventEmitter { EventEmitter.androidListenerCount++; if (EventEmitter.androidListenerCount === 1) { - Module.enableOrientationSensors(); + NativeOrientationDirector.enableOrientationSensors(); } return EventEmitter.createDeviceOrientationListenerProxy(listener); @@ -26,11 +27,11 @@ class EventEmitter { static addInterfaceOrientationDidChangeListener( callback: (orientation: OrientationEvent) => void ) { - return Module.onInterfaceOrientationChanged(callback); + return NativeOrientationDirector.onInterfaceOrientationChanged(callback); } static addLockDidChangeListener(callback: (event: LockedEvent) => void) { - return Module.onLockChanged(callback); + return NativeOrientationDirector.onLockChanged(callback); } private static createDeviceOrientationListenerProxy( @@ -50,7 +51,7 @@ class EventEmitter { function disableOrientationSensorsIfLastListener() { if (EventEmitter.androidListenerCount === 1) { EventEmitter.androidListenerCount = 0; - Module.disableOrientationSensors(); + NativeOrientationDirector.disableOrientationSensors(); return; } diff --git a/src/RNOrientationDirector.ts b/src/RNOrientationDirector.ts index ee0143c..26a8148 100644 --- a/src/RNOrientationDirector.ts +++ b/src/RNOrientationDirector.ts @@ -1,5 +1,5 @@ import { Platform } from 'react-native'; -import Module from './module'; +import NativeOrientationDirector from './NativeOrientationDirector'; import type { HumanReadableOrientationsResource } from './types/HumanReadableOrientationsResource.type'; import { Orientation } from './types/Orientation.enum'; import { AutoRotation } from './types/AutoRotation.enum'; @@ -39,11 +39,11 @@ class RNOrientationDirector { } static getInterfaceOrientation(): Promise { - return Module.getInterfaceOrientation(); + return NativeOrientationDirector.getInterfaceOrientation(); } static getDeviceOrientation(): Promise { - return Module.getDeviceOrientation(); + return NativeOrientationDirector.getDeviceOrientation(); } /** @@ -69,42 +69,42 @@ class RNOrientationDirector { orientationType: OrientationType = OrientationType.interface ) { if (orientationType === OrientationType.interface) { - Module.lockTo(orientation); + NativeOrientationDirector.lockTo(orientation); return; } if (orientation === Orientation.landscapeLeft) { - Module.lockTo(Orientation.landscapeRight); + NativeOrientationDirector.lockTo(Orientation.landscapeRight); return; } if (orientation === Orientation.landscapeRight) { - Module.lockTo(Orientation.landscapeLeft); + NativeOrientationDirector.lockTo(Orientation.landscapeLeft); return; } - Module.lockTo(orientation); + NativeOrientationDirector.lockTo(orientation); } static unlock() { - Module.unlock(); + NativeOrientationDirector.unlock(); } static isLocked() { - return Module.isLocked(); + return NativeOrientationDirector.isLocked(); } static isAutoRotationEnabled() { if (Platform.OS !== 'android') { return AutoRotation.unknown; } - return Module.isAutoRotationEnabled() + return NativeOrientationDirector.isAutoRotationEnabled() ? AutoRotation.enabled : AutoRotation.disabled; } static resetSupportedInterfaceOrientations() { - Module.resetSupportedInterfaceOrientations(); + NativeOrientationDirector.resetSupportedInterfaceOrientations(); } static listenForDeviceOrientationChanges( diff --git a/src/module.ts b/src/module.ts deleted file mode 100644 index 4bed768..0000000 --- a/src/module.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { NativeModules, Platform } from 'react-native'; -import type { Spec } from './NativeOrientationDirector'; - -const LINKING_ERROR = - `The package 'react-native-orientation-director' doesn't seem to be linked. Make sure: \n\n` + - Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) + - '- You rebuilt the app after installing the package\n' + - '- You are not using Expo Go\n'; - -// @ts-expect-error -const isTurboModuleEnabled = global.__turboModuleProxy != null; - -const Module = isTurboModuleEnabled - ? require('./NativeOrientationDirector').default - : NativeModules.OrientationDirector; - -const OrientationDirectorModule = Module - ? Module - : new Proxy( - {}, - { - get() { - throw new Error(LINKING_ERROR); - }, - } - ); - -export default OrientationDirectorModule as Spec;