23
23
* This class contains information about the current battery status.
24
24
* @constructor
25
25
*/
26
- var exec = require ( '@remobile/react-native-cordova' ) . exec ;
26
+ const exec = require ( '@remobile/react-native-cordova' ) . exec ;
27
+ const { NativeEventEmitter, DeviceEventEmitter, Platform, NativeModules } = require ( 'react-native' ) ;
28
+ const EventEmitter = Platform . OS === 'android' ? DeviceEventEmitter : new NativeEventEmitter ( NativeModules . FileTransfer ) ;
27
29
28
- var STATUS_CRITICAL = 5 ;
29
- var STATUS_LOW = 20 ;
30
- var subscription = null ;
30
+ const STATUS_CRITICAL = 5 ;
31
+ const STATUS_LOW = 20 ;
32
+ let subscription = null ;
31
33
32
- exports . register = function ( options ) {
33
- var _level = null ;
34
- var _isPlugged = null ;
35
- const { onBatteryStatus, onBatteryLow, onBatteryCritical} = options ;
36
- if ( onBatteryStatus || onBatteryLow || onBatteryCritical ) {
37
- subscription = EventEmitter . addListener ( 'BATTERY_STATUS_EVENT' , ( info ) => {
34
+ exports . register = function ( options ) {
35
+ let _level = null ;
36
+ let _isPlugged = null ;
37
+ const { onBatteryStatus, onBatteryLow, onBatteryCritical } = options ;
38
+ if ( onBatteryStatus || onBatteryLow || onBatteryCritical ) {
39
+ subscription = EventEmitter . addListener ( 'BATTERY_STATUS_EVENT' , ( info ) => {
38
40
if ( info ) {
39
41
if ( _level !== info . level || _isPlugged !== info . isPlugged ) {
40
-
41
- if ( info . level === null && _level !== null ) {
42
+ if ( info . level === null && _level !== null ) {
42
43
return ; // special case where callback is called because we stopped listening to the native side.
43
44
}
44
45
@@ -51,8 +52,7 @@ exports.register = function(options) {
51
52
if ( _level > STATUS_CRITICAL && info . level <= STATUS_CRITICAL ) {
52
53
// Fire critical battery event
53
54
onBatteryCritical && onBatteryCritical ( info ) ;
54
- }
55
- else if ( _level > STATUS_LOW && info . level <= STATUS_LOW ) {
55
+ } else if ( _level > STATUS_LOW && info . level <= STATUS_LOW ) {
56
56
// Fire low battery event
57
57
onBatteryCritical && onBatteryCritical ( info ) ;
58
58
}
@@ -63,10 +63,10 @@ exports.register = function(options) {
63
63
}
64
64
} ) ;
65
65
}
66
- exec ( null , null , " BatteryStatus" , " start" , [ ] ) ;
66
+ exec ( null , null , ' BatteryStatus' , ' start' , [ ] ) ;
67
67
} ;
68
68
69
- exports . unregister = function ( opt ) {
69
+ exports . unregister = function ( opt ) {
70
70
subscription && subscription . remove ( ) ;
71
- exec ( null , null , " BatteryStatus" , " stop" , [ ] ) ;
71
+ exec ( null , null , ' BatteryStatus' , ' stop' , [ ] ) ;
72
72
} ;
0 commit comments