Skip to content
This repository has been archived by the owner on Jul 21, 2021. It is now read-only.

Fix: iOS 13 and React Native 0.6.x #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions RNProximity/RNProximity/EventEmitter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// EventEmitter.h
// AFNetworking
//
// Created by Stephan on 11.05.20.
//

#import <Foundation/Foundation.h>
#import <React/RCTEventEmitter.h>


NS_ASSUME_NONNULL_BEGIN

@interface EventEmitter: RCTEventEmitter


@end

NS_ASSUME_NONNULL_END
17 changes: 17 additions & 0 deletions RNProximity/RNProximity/EventEmitter.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//
// EventEmitter.m
// AFNetworking
//
// Created by Stephan on 11.05.20.
//

#import "EventEmitter.h"

@implementation EventEmitter

- (NSArray<NSString *> *)supportedEvents {
return @[@"proximityStateDidChange"];
}

@end

4 changes: 3 additions & 1 deletion RNProximity/RNProximity/RNProximity.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
//

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>

@interface RNProximity : NSObject <RCTBridgeModule>

@property (retain) EventEmitter * emitter;

@end
20 changes: 15 additions & 5 deletions RNProximity/RNProximity/RNProximity.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@

#import <React/RCTBridge.h>
#import <React/RCTEventDispatcher.h>
#import "EventEmitter.h"
#import "RNProximity.h"


@implementation RNProximity

@synthesize bridge = _bridge;
@synthesize emitter;

RCT_EXPORT_MODULE();

- (instancetype)init
{
if ((self = [super init])) {
[[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sensorStateChange:) name:@"UIDeviceProximityStateDidChangeNotification" object:nil];
emitter = [[EventEmitter alloc] init];
}
return self;
}
Expand All @@ -26,18 +32,22 @@ - (void)dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

+ (BOOL) requiresMainQueueSetup {
return YES;
}

- (void)sensorStateChange:(NSNotificationCenter *)notification
{
BOOL proximityState = [[UIDevice currentDevice] proximityState];
[_bridge.eventDispatcher sendDeviceEventWithName:@"proximityStateDidChange"
body:@{@"proximity": @(proximityState)}];
emitter.bridge = self.bridge;
[emitter sendEventWithName:@"proximityStateDidChange"
body:@{@"proximity": @(proximityState)}];
}

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(proximityEnabled:(BOOL)enabled) {
[[UIDevice currentDevice] setProximityMonitoringEnabled:enabled];
dispatch_async(dispatch_get_main_queue(), ^{
[[UIDevice currentDevice] setProximityMonitoringEnabled:enabled];
});
}

@end
3 changes: 2 additions & 1 deletion android/src/main/java/com/RNProximity/RNProximityModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.annotation.Nullable;
import android.util.Log;

import androidx.annotation.Nullable;

import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
Expand Down
8 changes: 3 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
'use strict';

import {
DeviceEventEmitter,
NativeModules,
Platform,
} from 'react-native';

const nativeModule = NativeModules.RNProximity;
const { RNProximity } = NativeModules;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to also change the Android if to use RNProximity instead of nativeModule


let addListener = null;
let removeListener = null;

if (Platform.OS === 'ios') {
addListener = function(callback) {
NativeModules.RNProximity.proximityEnabled(true);
RNProximity.proximityEnabled(true);
return DeviceEventEmitter.addListener(
'proximityStateDidChange', callback
);
},
removeListener = function(listener) {
NativeModules.RNProximity.proximityEnabled(false);
RNProximity.proximityEnabled(false);
DeviceEventEmitter.removeAllListeners(
'proximityStateDidChange', listener
);
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
{
"name": "react-native-proximity",
"version": "3.0.0",
"version": "3.0.1",
"description": "Detect proximity on iOS and Android",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/williambout/react-native-proximity.git"
"url": "https://github.com/williambout/react-native-proximity"
},
"keywords": [
"react-native",
Expand Down