Skip to content
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

Keep Options struct #6

Merged
merged 3 commits into from
Apr 18, 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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter;

import io.swan.rnbrowser.helpers.CustomTabActivityHelper;
Expand All @@ -29,7 +30,7 @@ protected static void onHostResume(final ReactApplicationContext reactContext) {

protected static void open(final Activity activity,
final String url,
final Double barTintColor,
final ReadableMap options,
final Promise promise) {
CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
intentBuilder.setShowTitle(false);
Expand All @@ -44,13 +45,13 @@ protected static void open(final Activity activity,
CustomTabColorSchemeParams.Builder paramsBuilder = new CustomTabColorSchemeParams.Builder();
paramsBuilder.setNavigationBarColor(blackColor);

if (barTintColor != null) {
@ColorInt int intValue = barTintColor.intValue();
if (options.hasKey("barTintColor")) {
@ColorInt int barTintColor = options.getInt("barTintColor");

paramsBuilder.setToolbarColor(intValue);
paramsBuilder.setSecondaryToolbarColor(intValue);
paramsBuilder.setToolbarColor(barTintColor);
paramsBuilder.setSecondaryToolbarColor(barTintColor);

intentBuilder.setColorScheme(ColorUtils.calculateLuminance(intValue) > 0.5
intentBuilder.setColorScheme(ColorUtils.calculateLuminance(barTintColor) > 0.5
? CustomTabsIntent.COLOR_SCHEME_LIGHT
: CustomTabsIntent.COLOR_SCHEME_DARK);
}
Expand Down
10 changes: 3 additions & 7 deletions android/src/newarch/io/swan/rnbrowser/RNSwanBrowserModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import android.app.Activity;

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

import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.module.annotations.ReactModule;

@ReactModule(name = RNSwanBrowserModuleImpl.NAME)
Expand Down Expand Up @@ -41,11 +41,7 @@ public void onHostPause() {}
public void onHostDestroy() {}

@Override
public void open(String url,
@Nullable String dismissButtonStyle,
@Nullable Double barTintColor,
@Nullable Double controlTintColor,
Promise promise) {
public void open(String url, ReadableMap options, Promise promise) {
if (mBrowserVisible) {
promise.reject("swan_browser_visible",
"An instance of the swan browser is already visible");
Expand All @@ -61,7 +57,7 @@ public void open(String url,
}

mBrowserVisible = true;
RNSwanBrowserModuleImpl.open(activity, url, barTintColor, promise);
RNSwanBrowserModuleImpl.open(activity, url, options, promise);
}

@Override
Expand Down
10 changes: 3 additions & 7 deletions android/src/oldarch/io/swan/rnbrowser/RNSwanBrowserModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import android.app.Activity;

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

import com.facebook.react.bridge.LifecycleEventListener;
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.ReadableMap;
import com.facebook.react.module.annotations.ReactModule;

@ReactModule(name = RNSwanBrowserModuleImpl.NAME)
Expand Down Expand Up @@ -43,11 +43,7 @@ public void onHostPause() {}
public void onHostDestroy() {}

@ReactMethod
public void open(String url,
@Nullable String dismissButtonStyle,
@Nullable Double barTintColor,
@Nullable Double controlTintColor,
Promise promise) {
public void open(String url, ReadableMap options, Promise promise) {
if (mBrowserVisible) {
promise.reject("swan_browser_visible",
"An instance of the swan browser is already visible");
Expand All @@ -63,7 +59,7 @@ public void open(String url,
}

mBrowserVisible = true;
RNSwanBrowserModuleImpl.open(activity, url, barTintColor, promise);
RNSwanBrowserModuleImpl.open(activity, url, options, promise);
}

@ReactMethod
Expand Down
4 changes: 2 additions & 2 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1163,7 +1163,7 @@ PODS:
- React-logger (= 0.74.0-rc.8)
- React-perflogger (= 0.74.0-rc.8)
- React-utils (= 0.74.0-rc.8)
- RNSwanBrowser (0.2.0):
- RNSwanBrowser (0.2.1):
- React-Core
- SocketRocket (0.7.0)
- Yoga (0.0.0)
Expand Down Expand Up @@ -1398,7 +1398,7 @@ SPEC CHECKSUMS:
React-runtimescheduler: 91c3069839ca10d2dd1d18dbe60a478f2bb77405
React-utils: b3ef4d6a2c13c343b9150e3070fa1984a1c1ee27
ReactCommon: a2081027a321e7dd4825855111d57954a01fb02a
RNSwanBrowser: 82802945bfe1142eb5aad8c9f1276df17864cbaa
RNSwanBrowser: 57d20670f4461b8bc3842665189937250560f18e
SocketRocket: abac6f5de4d4d62d24e11868d7a2f427e0ef940d
Yoga: 553006c695f727afa3d30fdac7542f7ea6e2a4f9

Expand Down
22 changes: 14 additions & 8 deletions ios/RNSwanBrowser.mm
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,24 @@ - (void)safariViewControllerDidFinish:(SFSafariViewController *)controller {
}

- (void)open:(NSString *)url
dismissButtonStyle:(NSString *)dismissButtonStyle
barTintColor:(NSNumber *)barTintColor
controlTintColor:(NSNumber *)controlTintColor
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject {
options:(JS::NativeRNSwanBrowser::Options &)options
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject {

NSString *dismissButtonStyle = options.dismissButtonStyle();
NSNumber *barTintColor = options.barTintColor().has_value() ? [NSNumber numberWithDouble:options.barTintColor().value()] : nil;
NSNumber *controlTintColor = options.controlTintColor().has_value() ? [NSNumber numberWithDouble:options.controlTintColor().value()] : nil;

#else
RCT_EXPORT_METHOD(open:(NSString *)url
dismissButtonStyle:(NSString *)dismissButtonStyle
barTintColor:(NSNumber *)barTintColor
controlTintColor:(NSNumber *)controlTintColor
options:(NSDictionary * _Nonnull)options
resolve:(RCTPromiseResolveBlock)resolve
reject:(RCTPromiseRejectBlock)reject) {

NSString *dismissButtonStyle = [options valueForKey:@"dismissButtonStyle"];
NSNumber *barTintColor = [options valueForKey:@"barTintColor"];
NSNumber *controlTintColor = [options valueForKey:@"controlTintColor"];

#endif
if (_safariVC != nil) {
return reject(@"swan_browser_visible", @"An instance of the swan browser is already visible", nil);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@swan-io/react-native-browser",
"version": "0.2.0",
"version": "0.2.1",
"license": "MIT",
"description": "An easy-to-use in-app browser module for React Native.",
"author": "Mathieu Acthernoene <mathieu.acthernoene@swan.io>",
Expand Down
15 changes: 7 additions & 8 deletions src/NativeRNSwanBrowser.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import type { TurboModule } from "react-native";
import { TurboModuleRegistry } from "react-native";

export interface Spec extends TurboModule {
open(
url: string,
dismissButtonStyle?: string,
barTintColor?: number,
controlTintColor?: number,
): Promise<null>;
type Options = {
dismissButtonStyle?: string;
barTintColor?: number;
controlTintColor?: number;
};

export interface Spec extends TurboModule {
open(url: string, options: Options): Promise<null>;
close(): void;

// Events
addListener: (eventName: string) => void;
removeListeners: (count: number) => void;
Expand Down
23 changes: 9 additions & 14 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,15 @@ const convertColorToNumber = (
};

export const openBrowser = (url: string, options: Options): Promise<void> => {
const {
dismissButtonStyle,
barTintColor,
controlTintColor,
onOpen,
onClose,
} = options;

return NativeModule.open(
url,
dismissButtonStyle,
convertColorToNumber(barTintColor),
convertColorToNumber(controlTintColor),
).then(() => {
const { dismissButtonStyle, onOpen, onClose } = options;
const barTintColor = convertColorToNumber(options.barTintColor);
const controlTintColor = convertColorToNumber(options.controlTintColor);

return NativeModule.open(url, {
...(dismissButtonStyle != null && { dismissButtonStyle }),
...(barTintColor != null && { barTintColor }),
...(controlTintColor != null && { controlTintColor }),
}).then(() => {
let deeplink: string | undefined;

onOpen?.();
Expand Down