Permalink
Browse files

add constant and JS cleanup

  • Loading branch information...
ajwhite committed Nov 17, 2017
1 parent f494ae4 commit 48a9321cf8966d1cd3fdb54304dbc5d30a71c96f
Showing with 12 additions and 15 deletions.
  1. +5 −10 index.js
  2. +0 −2 ios/DeviceBattery/DeviceBattery.h
  3. +7 −3 ios/DeviceBattery/DeviceBattery.m
View
@@ -1,15 +1,10 @@
var { NativeModules, NativeEventEmitter } = require('react-native');
var { DeviceBattery } = NativeModules;
var {NativeModules, NativeEventEmitter} = require('react-native');
var {DeviceBattery} = NativeModules;
const batteryEventEmitter = new NativeEventEmitter(DeviceBattery);
export default {
isCharging: DeviceBattery.isCharging,
getBatteryLevel: DeviceBattery.getBatteryLevel,
addListener: (callback) => {
const deviceBatteryEvt = new NativeEventEmitter(NativeModules.DeviceBattery);
return deviceBatteryEvt.addListener('batteryChange', callback);
},
removeListener: (listener) => {
// FIXME: missing from original implementation
// DeviceEventEmitter.removeListener('batteryChange', listener);
}
addListener: callback => batteryEventEmitter.addListener(DeviceBattery.BATTERY_CHANGE_EVENT, callback)
};
@@ -13,11 +13,9 @@
#if __has_include(<React/RCTAssert.h>)
#import <React/RCTBridgeModule.h>
#import <React/RCTBridge.h>
#import <React/RCTEventEmitter.h>
#else // backward compat for rn pre 0.40
#import "RCTBridgeModule.h"
#import "RCTBridge.h"
#import "RCTEventEmitter.h"
#endif
@@ -13,10 +13,14 @@
@implementation DeviceBattery
static const NSString *BATTERY_CHANGE_EVENT = @"batteryChanged";
- (instancetype)init
{
if((self = [super init])) {
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
dispatch_async(dispatch_get_main_queue(), ^{
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
});
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(batteryLevelChanged:)
@@ -52,7 +56,7 @@ - (instancetype)init
}
- (NSArray<NSString *> *)supportedEvents {
return @[@"batteryChange"];
return @[BATTERY_CHANGE_EVENT];
}
-(void)batteryLevelChanged:(NSNotification*)notification {
@@ -64,7 +68,7 @@ -(void)batteryLevelChanged:(NSNotification*)notification {
[payload setObject:[NSNumber numberWithBool:isCharging] forKey:@"charging"];
[payload setObject:[NSNumber numberWithFloat:batteryLevel] forKey:@"level"];
[self sendEventWithName:@"batteryChange" body:payload];
[self sendEventWithName:BATTERY_CHANGE_EVENT body:payload];
}
- (void)dealloc

0 comments on commit 48a9321

Please sign in to comment.