Skip to content

Commit

Permalink
chore: expose a method to set the torch mode (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
yousrasd authored Mar 11, 2023
1 parent 9a8c6fd commit 6196173
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 0 deletions.
11 changes: 11 additions & 0 deletions android/src/main/java/com/rncamerakit/RNCameraKitModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,15 @@ class RNCameraKitModule(private val reactContext: ReactApplicationContext) : Rea
view.capture(options.toHashMap(), promise)
}
}

@ReactMethod
fun setTorchMode( mode: String, viewTag: Int) {
val context = reactContext
val uiManager = context.getNativeModule(UIManagerModule::class.java)
context.runOnUiQueueThread {
val view = uiManager?.resolveView(viewTag) as CKCamera
view.setTorchMode(mode)
}

}
}
2 changes: 2 additions & 0 deletions ios/ReactNativeCameraKit/CKCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ typedef NS_ENUM(NSInteger, CKCameraZoomMode) {
// api
- (void)snapStillImage:(NSDictionary*)options success:(CaptureBlock)block onError:(void (^)(NSString*))onError;

- (void)setTorchMode:(AVCaptureTorchMode)torchMode;

+ (NSURL*)saveToTmpFolder:(NSData*)data;

@end
11 changes: 11 additions & 0 deletions ios/ReactNativeCameraKit/CKCameraManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ - (UIView *)view {
}];
}

RCT_EXPORT_METHOD(setTorchMode:(NSString*)mode) {
AVCaptureTorchMode torchMode;
if([mode isEqualToString:@"on"]) {
torchMode = AVCaptureTorchModeOn;
} else {
torchMode = AVCaptureTorchModeOff;
}

[self.camera setTorchMode:torchMode ];
}

RCT_EXPORT_METHOD(checkDeviceCameraAuthorizationStatus:(RCTPromiseResolveBlock)resolve
reject:(__unused RCTPromiseRejectBlock)reject) {

Expand Down
3 changes: 3 additions & 0 deletions src/Camera.android.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ const Camera = React.forwardRef((props: any, ref) => {
// we must use the general module and tell it what View it's supposed to be using
return await RNCameraKitModule.capture(options, findNodeHandle(nativeRef.current ?? null));
},
setTorchMode: (mode = "off") => {
RNCameraKitModule.setTorchMode(mode, findNodeHandle(nativeRef.current ?? null));
},
requestDeviceCameraAuthorization: () => {
throw new Error('Not implemented');
},
Expand Down
3 changes: 3 additions & 0 deletions src/Camera.ios.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ const Camera = React.forwardRef((props: any, ref: any) => {
capture: async () => {
return await CKCameraManager.capture({});
},
setTorchMode: (mode = "off") => {
CKCameraManager.setTorchMode(mode);
},
requestDeviceCameraAuthorization: async () => {
return await CKCameraManager.checkDeviceCameraAuthorizationStatus();
},
Expand Down
3 changes: 3 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
type TorchMode = 'on'| 'off';

export type CameraApi = {
capture: () => Promise<{ uri: string }>,
setTorchMode: (mode: TorchMode) => void;
requestDeviceCameraAuthorization: () => Promise<boolean>,
checkDeviceCameraAuthorizationStatus: () => Promise<boolean>,
};

0 comments on commit 6196173

Please sign in to comment.