Skip to content

Commit

Permalink
Properly initialize FilamentModule
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed Feb 21, 2024
1 parent bd9908f commit 0c53c79
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 45 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.margelo.filament;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

public class FilamentModule extends ReactContextBaseJavaModule {
static {
System.loadLibrary("RNFilament");
}

private @Nullable FilamentProxy proxy = null;
private final ReactApplicationContext context;

public FilamentModule(ReactApplicationContext reactContext) {
context = reactContext;
}

@NonNull
@Override
public String getName() {
return "FilamentModule";
}


@ReactMethod(isBlockingSynchronousMethod = true)
public boolean install() {
try {
proxy = new FilamentProxy(context);
FilamentInstaller.install(proxy);
return true;
} catch (Throwable cause) {
throw new RuntimeException("Failed to initialize react-native-filament! Reason: " + cause.getMessage(), cause);
}
}

@Override
public void onCatalystInstanceDestroy() {
super.onCatalystInstanceDestroy();
proxy = null;
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,16 @@
package com.margelo.filament;

import android.graphics.Color;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.annotations.ReactProp;

@ReactModule(name = FilamentViewManager.NAME)
public class FilamentViewManager extends FilamentViewManagerSpec<FilamentView> {
static {
try {
System.loadLibrary("RNFilament");
} catch (Throwable throwable) {
throw new RuntimeException("Failed to load the C++ react-native-filament library! " +
"Reason: " + throwable.getMessage(), throwable);
}
System.loadLibrary("RNFilament");
}

public static final String NAME = "FilamentView";
private final ReactApplicationContext context;
private @Nullable FilamentProxy proxy = null;

FilamentViewManager(ReactApplicationContext context) {
this.context = context;
}

@Override
public @NonNull String getName() {
Expand All @@ -39,26 +21,4 @@ public class FilamentViewManager extends FilamentViewManagerSpec<FilamentView> {
public @NonNull FilamentView createViewInstance(@NonNull ThemedReactContext context) {
return new FilamentView(context);
}

@Override
@ReactProp(name = "color")
public void setColor(FilamentView view, @Nullable String color) {
view.setBackgroundColor(Color.parseColor(color));
}

@ReactMethod(isBlockingSynchronousMethod = true)
public void install() {
try {
proxy = new FilamentProxy(context);
FilamentInstaller.install(proxy);
} catch (Throwable cause) {
throw new RuntimeException("Failed to initialize react-native-filament! Reason: " + cause.getMessage(), cause);
}
}

@Override
public void onCatalystInstanceDestroy() {
super.onCatalystInstanceDestroy();
proxy = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@
import com.facebook.react.uimanager.SimpleViewManager;

public abstract class FilamentViewManagerSpec<T extends View> extends SimpleViewManager<T> {
public abstract void setColor(T view, @Nullable String value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ public class FilamentViewPackage implements ReactPackage {
@Override
public @NonNull List<ViewManager> createViewManagers(@NonNull ReactApplicationContext reactContext) {
List<ViewManager> viewManagers = new ArrayList<>();
viewManagers.add(new FilamentViewManager(reactContext));
viewManagers.add(new FilamentViewManager());
return viewManagers;
}

@Override
public @NonNull List<NativeModule> createNativeModules(@NonNull ReactApplicationContext reactContext) {
return Collections.emptyList();
List<NativeModule> modules = new ArrayList<>();
modules.add(new FilamentModule(reactContext));
return modules;
}
}
4 changes: 3 additions & 1 deletion package/src/FilamentProxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ if (global.nativeCallSyncHook == null) {
// Maybe we need to create a separate FilamentModule as well (in addition to the FilamentView).
const successful = FilamentNativeModule.install()
if (!successful) {
throw new Error('Failed to initialize react-native-filament! Check the native logs (adb logcat or Xcode logs) for more information.')
throw new Error(
'Failed to initialize react-native-filament! The install() method returned false - check the native logs (adb logcat or Xcode logs) for more information.'
)
}

// @ts-expect-error global is untyped, it's a C++ host-object
Expand Down

0 comments on commit 0c53c79

Please sign in to comment.