-
-
Notifications
You must be signed in to change notification settings - Fork 846
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(android): remove ts and use NativeModule instead of setNativeProp…
…s to update camera with imperative method (#3376)
- Loading branch information
Showing
15 changed files
with
362 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
android/src/main/java/com/rnmapbox/rnmbx/components/camera/RNMBXCameraModule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.rnmapbox.rnmbx.components.camera | ||
|
||
import com.facebook.react.bridge.Callback | ||
import com.facebook.react.bridge.Promise | ||
import com.facebook.react.bridge.ReactApplicationContext | ||
import com.facebook.react.bridge.ReadableArray | ||
import com.facebook.react.bridge.ReadableMap | ||
import com.facebook.react.bridge.WritableMap | ||
import com.facebook.react.bridge.WritableNativeMap | ||
import com.rnmapbox.rnmbx.NativeRNMBXCameraModuleSpec | ||
import com.rnmapbox.rnmbx.components.mapview.CommandResponse | ||
import com.rnmapbox.rnmbx.utils.ViewTagResolver | ||
|
||
|
||
class RNMBXCameraModule(context: ReactApplicationContext, val viewTagResolver: ViewTagResolver) : NativeRNMBXCameraModuleSpec(context) { | ||
private fun withViewportOnUIThread( | ||
viewRef: Double?, | ||
reject: Promise, | ||
fn: (RNMBXCamera) -> Unit | ||
) { | ||
if (viewRef == null) { | ||
reject.reject(Exception("viewRef is null")) | ||
} else { | ||
viewTagResolver.withViewResolved(viewRef.toInt(), reject, fn) | ||
} | ||
} | ||
|
||
private fun createCommandResponse(promise: Promise): CommandResponse = object : CommandResponse { | ||
override fun success(builder: (WritableMap) -> Unit) { | ||
val payload: WritableMap = WritableNativeMap() | ||
builder(payload) | ||
|
||
promise.resolve(payload) | ||
} | ||
|
||
override fun error(message: String) { | ||
promise.reject(Exception(message)) | ||
} | ||
} | ||
|
||
companion object { | ||
const val NAME = "RNMBXCameraModule" | ||
} | ||
|
||
override fun updateCameraStop(viewRef: Double?, stop: ReadableMap, promise: Promise) { | ||
withViewportOnUIThread(viewRef, promise) { | ||
it.updateCameraStop(stop) | ||
promise.resolve(null) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
android/src/main/old-arch/com/rnmapbox/rnmbx/NativeRNMBXCameraModuleSpec.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
/** | ||
* This code was generated by [react-native-codegen](https://www.npmjs.com/package/react-native-codegen). | ||
* | ||
* Do not edit this file as changes may cause incorrect behavior and will be lost | ||
* once the code is regenerated. | ||
* | ||
* @generated by codegen project: GenerateModuleJavaSpec.js | ||
* | ||
* @nolint | ||
*/ | ||
|
||
package com.rnmapbox.rnmbx; | ||
|
||
import com.facebook.proguard.annotations.DoNotStrip; | ||
import com.facebook.react.bridge.Promise; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReactContextBaseJavaModule; | ||
import com.facebook.react.bridge.ReactMethod; | ||
import com.facebook.react.bridge.ReactModuleWithSpec; | ||
import com.facebook.react.bridge.ReadableMap; | ||
import com.facebook.react.turbomodule.core.interfaces.TurboModule; | ||
import javax.annotation.Nonnull; | ||
import javax.annotation.Nullable; | ||
|
||
public abstract class NativeRNMBXCameraModuleSpec extends ReactContextBaseJavaModule implements ReactModuleWithSpec, TurboModule { | ||
public static final String NAME = "RNMBXCameraModule"; | ||
|
||
public NativeRNMBXCameraModuleSpec(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
} | ||
|
||
@Override | ||
public @Nonnull String getName() { | ||
return NAME; | ||
} | ||
|
||
@ReactMethod | ||
@DoNotStrip | ||
public abstract void updateCameraStop(@Nullable Double viewRef, ReadableMap stops, Promise promise); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#import <Foundation/Foundation.h> | ||
#import <UIKit/UIKit.h> | ||
|
||
#ifdef RCT_NEW_ARCH_ENABLED | ||
#import "rnmapbox_maps_specs.h" | ||
#else | ||
#import <React/RCTBridge.h> | ||
#endif | ||
|
||
@interface RNMBXCameraModule : NSObject | ||
#ifdef RCT_NEW_ARCH_ENABLED | ||
<NativeRNMBXCameraModuleSpec> | ||
#else | ||
<RCTBridgeModule> | ||
#endif | ||
|
||
@end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#import <React/RCTBridge.h> | ||
#import <React/RCTUIManager.h> | ||
#import <React/RCTUIManagerUtils.h> | ||
|
||
#import "RNMBXCameraModule.h" | ||
#ifdef RCT_NEW_ARCH_ENABLED | ||
#import "RNMBXCameraComponentView.h" | ||
#endif // RCT_NEW_ARCH_ENABLED | ||
|
||
#import "rnmapbox_maps-Swift.pre.h" | ||
|
||
@implementation RNMBXCameraModule | ||
|
||
RCT_EXPORT_MODULE(); | ||
|
||
#ifdef RCT_NEW_ARCH_ENABLED | ||
@synthesize viewRegistry_DEPRECATED = _viewRegistry_DEPRECATED; | ||
#endif // RCT_NEW_ARCH_ENABLED | ||
@synthesize bridge = _bridge; | ||
|
||
- (dispatch_queue_t)methodQueue | ||
{ | ||
// It seems that due to how UIBlocks work with uiManager, we need to call the methods there | ||
// for the blocks to be dispatched before the batch is completed | ||
return RCTGetUIManagerQueue(); | ||
} | ||
|
||
// Thanks to this guard, we won't compile this code when we build for the old architecture. | ||
#ifdef RCT_NEW_ARCH_ENABLED | ||
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule: | ||
(const facebook::react::ObjCTurboModule::InitParams &)params | ||
{ | ||
return std::make_shared<facebook::react::NativeRNMBXCameraModuleSpecJSI>(params); | ||
} | ||
#endif // RCT_NEW_ARCH_ENABLED | ||
|
||
- (void)withCamera:(nonnull NSNumber*)viewRef block:(void (^)(RNMBXCamera *))block reject:(RCTPromiseRejectBlock)reject methodName:(NSString *)methodName | ||
{ | ||
#ifdef RCT_NEW_ARCH_ENABLED | ||
[self.viewRegistry_DEPRECATED addUIBlock:^(RCTViewRegistry *viewRegistry) { | ||
RNMBXCameraComponentView *componentView = [self.viewRegistry_DEPRECATED viewForReactTag:viewRef]; | ||
RNMBXCamera *view = componentView.contentView; | ||
|
||
#else | ||
[self.bridge.uiManager | ||
addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) { | ||
RNMBXCamera *view = [uiManager viewForReactTag:viewRef]; | ||
#endif // RCT_NEW_ARCH_ENABLED | ||
if (view != nil) { | ||
block(view); | ||
} else { | ||
reject(methodName, [NSString stringWithFormat:@"Unknown reactTag: %@", viewRef], nil); | ||
} | ||
}]; | ||
} | ||
|
||
RCT_EXPORT_METHOD(updateCameraStop:(nonnull NSNumber *)viewRef | ||
stop: (NSDictionary*)stop | ||
resolve:(RCTPromiseResolveBlock)resolve | ||
reject:(RCTPromiseRejectBlock)reject) | ||
{ | ||
[self withCamera:viewRef block:^(RNMBXCamera *view) { | ||
[view updateCameraStop: stop]; | ||
} reject:reject methodName:@"someMethod"]; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.