Skip to content

Commit

Permalink
add macOS implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
tido64 committed Sep 21, 2023
1 parent 2193ddb commit f65d01d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,12 @@ Pod::Spec.new do |s|
# Include both package and repository relative paths to allow the podspec to
# be consumed from both a local path, and as a podspec outside a spec
# repository.
s.source_files = 'ios/*.{h,m}', # :path
"#{repository['directory']}/ios/*.{h,m}" # :podspec
s.public_header_files = 'ios/*.h', # :path
"#{repository['directory']}/ios/*.h" # :podspec
s.ios.source_files = 'ios/*.{h,m}', # :path
"#{repository['directory']}/ios/*.{h,m}" # :podspec
s.ios.public_header_files = 'ios/*.h', # :path
"#{repository['directory']}/ios/*.h" # :podspec
s.osx.source_files = 'macos/*.{h,m}', # :path
"#{repository['directory']}/macos/*.{h,m}" # :podspec
s.osx.public_header_files = 'macos/*.h', # :path
"#{repository['directory']}/macos/*.h" # :podspec
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#import <Foundation/Foundation.h>

#import <React/RCTBridgeModule.h>

NS_ASSUME_NONNULL_BEGIN

@interface RNWBatteryStatus : NSObject <RCTBridgeModule>
@end

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#import "RNWBatteryStatus.h"

#import <IOKit/ps/IOPowerSources.h>

@implementation RNWBatteryStatus

RCT_EXPORT_MODULE(RNWBatteryStatus)

+ (BOOL)requiresMainQueueSetup
{
return NO;
}

// clang-format off
RCT_EXPORT_METHOD(getStatus:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
// clang-format on
{
NSMutableDictionary *status = [NSMutableDictionary dictionaryWithCapacity:4];

CFTypeRef psInfo = IOPSCopyPowerSourcesInfo();
if (psInfo != nil) {
CFArrayRef psListRef = IOPSCopyPowerSourcesList(psInfo);
if (CFArrayGetCount(psListRef) > 0) {
CFTypeRef ps = CFArrayGetValueAtIndex(psListRef, 0);
CFDictionaryRef desc = IOPSGetPowerSourceDescription(psInfo, ps);

CFTypeRef state = CFDictionaryGetValue(desc, CFSTR(kIOPSPowerSourceStateKey));
BOOL isPluggedIn = CFStringCompare(state, CFSTR(kIOPSACPowerValue), 0) == kCFCompareEqualTo;

CFTypeRef charging = CFDictionaryGetValue(desc, CFSTR(kIOPSIsChargingKey));
status[@"charging"] = [NSNumber numberWithBool:CFBooleanGetValue(charging)];

CFTypeRef chargingTime = nil;
if (isPluggedIn && CFDictionaryGetValueIfPresent(desc, CFSTR(kIOPSTimeToFullChargeKey), &chargingTime)) {
int minutes = 0;
CFNumberGetValue(chargingTime, kCFNumberIntType, &minutes);
status[@"chargingTime"] = [NSNumber numberWithInt:minutes < 0 ? minutes : minutes * 60];
} else {
status[@"chargingTime"] = @-1;
}

CFTypeRef dischargingTime = nil;
if (!isPluggedIn && CFDictionaryGetValueIfPresent(desc, CFSTR(kIOPSTimeToEmptyKey), &dischargingTime)) {
int minutes = 0;
CFNumberGetValue(dischargingTime, kCFNumberIntType, &minutes);
status[@"dischargingTime"] = [NSNumber numberWithInt:minutes < 0 ? minutes : minutes * 60];
} else {
status[@"dischargingTime"] = @-1;
}

CFTypeRef level = CFDictionaryGetValue(desc, CFSTR(kIOPSCurrentCapacityKey));
int capacity = 0;
CFNumberGetValue(level, kCFNumberIntType, &capacity);
status[@"level"] = [NSNumber numberWithFloat:capacity / 100.0];
}

CFRelease(psListRef);
CFRelease(psInfo);
}

resolve(status);
}

@end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/* Dummy file so @react-native-community/cli recognizes this as an macOS package */

0 comments on commit f65d01d

Please sign in to comment.