Skip to content

[0.74] Add support for Fabric & Bridgeless Mode #516

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ def safeExtGet(prop, fallback) {
rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
}

def isNewArchitectureEnabled() {
return project.hasProperty("newArchEnabled") && project.newArchEnabled == "true"
}

apply plugin: 'com.android.library'
if (isNewArchitectureEnabled()) {
apply plugin: 'com.facebook.react'
}


android {
Expand All @@ -23,6 +30,11 @@ android {
targetSdkVersion safeExtGet('targetSdkVersion', DEFAULT_TARGET_SDK_VERSION)
versionCode 1
versionName "1.0"
buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString())
}

buildFeatures {
buildConfig true
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.UIManager;
import com.facebook.react.fabric.FabricUIManager;
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
import com.facebook.react.uimanager.UIManagerHelper;
import com.facebook.react.uimanager.UIManagerModule;
import com.facebook.react.uimanager.common.UIManagerType;

import java.io.File;
import java.io.FilenameFilter;
Expand All @@ -29,7 +34,7 @@
import fr.greweb.reactnativeviewshot.ViewShot.Formats;
import fr.greweb.reactnativeviewshot.ViewShot.Results;

public class RNViewShotModule extends ReactContextBaseJavaModule {
public class RNViewShotModule extends ReactContextBaseJavaModule implements TurboModule {

public static final String RNVIEW_SHOT = "RNViewShot";

Expand Down Expand Up @@ -71,7 +76,8 @@ public void releaseCapture(String uri) {
}

@ReactMethod
public void captureRef(int tag, ReadableMap options, Promise promise) {
public void captureRef(double tagFromJs, ReadableMap options, Promise promise) {
int tag = (int) tagFromJs;
final ReactApplicationContext context = getReactApplicationContext();
final DisplayMetrics dm = context.getResources().getDisplayMetrics();

Expand Down Expand Up @@ -99,13 +105,19 @@ public void captureRef(int tag, ReadableMap options, Promise promise) {
}

final Activity activity = getCurrentActivity();
final UIManagerModule uiManager = this.reactContext.getNativeModule(UIManagerModule.class);

uiManager.addUIBlock(new ViewShot(
ViewShot uiBlock = new ViewShot(
tag, extension, imageFormat, quality,
scaleWidth, scaleHeight, outputFile, resultStreamFormat,
snapshotContentContainer, reactContext, activity, handleGLSurfaceView, promise, executor)
snapshotContentContainer, reactContext, activity, handleGLSurfaceView, promise, executor
);

if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
UIManager uiManager = UIManagerHelper.getUIManager(context, UIManagerType.FABRIC);
((FabricUIManager)uiManager).addUIBlock(uiBlock);
} else {
final UIManagerModule uiManager = this.reactContext.getNativeModule(UIManagerModule.class);
uiManager.addUIBlock(uiBlock);
}
} catch (final Throwable ex) {
Log.e(RNVIEW_SHOT, "Failed to snapshot view tag " + tag, ex);
promise.reject(ViewShot.ERROR_UNABLE_TO_SNAPSHOT, "Failed to snapshot view tag " + tag);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,44 @@

package fr.greweb.reactnativeviewshot;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.react.ReactPackage;
import com.facebook.react.TurboReactPackage;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import com.facebook.react.bridge.JavaScriptModule;
public class RNViewShotPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(new RNViewShotModule(reactContext));
}
import com.facebook.react.module.model.ReactModuleInfo;
import com.facebook.react.module.model.ReactModuleInfoProvider;

// Deprecated RN 0.47
// @Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
import java.util.HashMap;
import java.util.Map;

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
public class RNViewShotPackage extends TurboReactPackage {
@Nullable
@Override
public NativeModule getModule(@NonNull String name, @NonNull ReactApplicationContext reactApplicationContext) {
if (name.equals(RNViewShotModule.RNVIEW_SHOT)) {
return new RNViewShotModule(reactApplicationContext);
} else {
return null;
}
}

@Override
public ReactModuleInfoProvider getReactModuleInfoProvider() {
return () -> {
final Map<String, ReactModuleInfo> moduleInfos = new HashMap<>();
moduleInfos.put(
RNViewShotModule.RNVIEW_SHOT,
new ReactModuleInfo(
RNViewShotModule.RNVIEW_SHOT,
RNViewShotModule.RNVIEW_SHOT,
false, // canOverrideExistingModule
false, // needsEagerInit
false, // isCxxModule
true // isTurboModule
));
return moduleInfos;
};
}
}
18 changes: 15 additions & 3 deletions android/src/main/java/fr/greweb/reactnativeviewshot/ViewShot.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.fabric.interop.UIBlockViewResolver;
import com.facebook.react.uimanager.NativeViewHierarchyManager;
import com.facebook.react.uimanager.UIBlock;

Expand Down Expand Up @@ -57,7 +58,7 @@
/**
* Snapshot utility class allow to screenshot a view.
*/
public class ViewShot implements UIBlock {
public class ViewShot implements UIBlock, com.facebook.react.fabric.interop.UIBlock {
//region Constants
/**
* Tag fort Class logs.
Expand Down Expand Up @@ -183,6 +184,17 @@ public ViewShot(
//region Overrides
@Override
public void execute(final NativeViewHierarchyManager nativeViewHierarchyManager) {
executeImpl(nativeViewHierarchyManager, null);
}

@Override
public void execute(@NonNull UIBlockViewResolver uiBlockViewResolver) {
executeImpl(null, uiBlockViewResolver);
}
//endregion

//region Implementation
private void executeImpl(final NativeViewHierarchyManager nativeViewHierarchyManager, final UIBlockViewResolver uiBlockViewResolver) {
executor.execute(new Runnable () {
@Override
public void run() {
Expand All @@ -191,6 +203,8 @@ public void run() {

if (tag == -1) {
view = currentActivity.getWindow().getDecorView().findViewById(android.R.id.content);
} else if (uiBlockViewResolver != null) {
view = uiBlockViewResolver.resolveView(tag);
} else {
view = nativeViewHierarchyManager.resolveView(tag);
}
Expand Down Expand Up @@ -221,9 +235,7 @@ public void run() {
}
});
}
//endregion

//region Implementation
private void saveToTempFileOnDevice(@NonNull final View view) throws IOException {
final FileOutputStream fos = new FileOutputStream(output);
captureView(view, fos);
Expand Down
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,13 @@
"flow-bin": "^0.170.0",
"html-webpack-plugin": "^5.5.1",
"react-native-windows": "^0.63.16"
},
"codegenConfig": {
"name": "RNViewShot",
"type": "all",
"jsSrcsDir": "./src/specs",
"android": {
"javaPackageName": "fr.greweb.reactnativeviewshot"
}
}
}
4 changes: 2 additions & 2 deletions src/RNViewShot.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
//@flow
import { NativeModules } from "react-native";
export default NativeModules.RNViewShot
import { RNViewShot } from './specs/NativeRNViewShot'
export default RNViewShot
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @flow
import React, { Component } from "react";
import { View, Platform, findNodeHandle, StyleProp } from "react-native";
import RNViewShot from "./RNViewShot";
import RNViewShot from "./specs/NativeRNViewShot";
import type { ViewStyleProp } from "react-native/Libraries/StyleSheet/StyleSheet";
import type { LayoutEvent } from "react-native/Libraries/Types/CoreEventTypes";

Expand All @@ -19,7 +19,7 @@ type Options = {

if (!RNViewShot) {
console.warn(
"react-native-view-shot: NativeModules.RNViewShot is undefined. Make sure the library is linked on the native side."
"react-native-view-shot: RNViewShot is undefined. Make sure the library is linked on the native side."
);
}

Expand Down
10 changes: 10 additions & 0 deletions src/specs/NativeRNViewShot.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';

export interface Spec extends TurboModule {
releaseCapture: () => string;
captureRef: (tag: number, options: Object) => Promise<string>
captureScreen: (options: Object) => Promise<string>;
}

export default TurboModuleRegistry.getEnforcing<Spec>('RNViewShot');