Skip to content

Commit 7a3c848

Browse files
authored
1.0.0 (#25)
1 parent d1328b6 commit 7a3c848

File tree

29 files changed

+2344
-2678
lines changed

29 files changed

+2344
-2678
lines changed

README.md

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,26 @@ import { Button, SafeAreaView } from "react-native";
3030
import parseUrl from "url-parse";
3131

3232
const App = () => {
33-
const handleOnPress = useCallback(() => {
34-
openBrowser("https://swan.io", {
35-
onClose: (url) => {
36-
if (url) {
37-
const { protocol, host, query } = parseUrl(url, true);
38-
const origin = `${protocol}//${host}`;
39-
40-
if (origin === "com.company.myapp://close") {
41-
console.log(JSON.stringify(query, null, 2));
42-
}
33+
useEffect(() => {
34+
const subscription = Linking.addListener(
35+
"url",
36+
({ url }: { url: string }) => {
37+
const { protocol, host, query } = parseUrl(url, true);
38+
const origin = `${protocol}//${host}`;
39+
40+
if (origin === "com.company.myapp://close") {
41+
console.log(JSON.stringify(query, null, 2));
4342
}
4443
},
45-
}).catch((error) => {
46-
console.error(error);
47-
});
44+
);
45+
46+
return () => {
47+
subscription.remove();
48+
};
49+
}, []);
50+
51+
const handleOnPress = useCallback(() => {
52+
openBrowser("https://swan.io").catch((error) => console.error(error));
4853
}, []);
4954

5055
return (
@@ -57,7 +62,7 @@ const App = () => {
5762

5863
## API
5964

60-
### openBrowser(url: string, options: Options)
65+
### openBrowser(url: string, options?: Options)
6166

6267
```tsx
6368
import { openBrowser } from "@swan-io/react-native-browser";
@@ -67,14 +72,6 @@ openBrowser("https://swan.io", {
6772
dismissButtonStyle: "close", // "cancel" | "close" | "done" (default to "close")
6873
barTintColor: "#FFF", // in-app browser UI background color
6974
controlTintColor: "#000", // in-app browser buttons color
70-
onOpen: () => {
71-
// fired on browser opened
72-
// useful to switch the StatusBar color, for example
73-
},
74-
onClose: (url) => {
75-
// fired on browser closed
76-
// url will be defined if the browser has been closed via deeplink
77-
},
7875
}).catch((error) => {
7976
console.error(error);
8077
});
@@ -85,7 +82,7 @@ openBrowser("https://swan.io", {
8582
8683
## Handle deeplinks
8784

88-
In order to receive deeplink on browser close event, you have to setup them first. We **highly** recommand defining a custom schema + url for this specific task. For example, `com.company.myapp://close`.
85+
In order to receive deeplink on browser close, you have to setup them first. We **highly** recommand defining a custom schema + url for this specific task. For example, `com.company.myapp://close`.
8986

9087
### On iOS
9188

RNSwanBrowser.podspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Pod::Spec.new do |s|
1111
s.author = package["author"]
1212
s.homepage = package["homepage"]
1313

14-
s.platforms = { :ios => "12.4", :tvos => "12.4" }
14+
s.platforms = { :ios => "13.4", :tvos => "13.4" }
1515
s.requires_arc = true
1616

1717
s.source = { :git => package["repository"]["url"], :tag => s.version }

android/build.gradle

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ buildscript {
77
gradlePluginPortal()
88
}
99
dependencies {
10-
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${safeExtGet("kotlinVersion", "1.8.0")}")
11-
classpath("com.android.tools.build:gradle:7.3.1")
10+
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:${safeExtGet("kotlinVersion", "1.9.22")}")
11+
classpath("com.android.tools.build:gradle:8.2.1")
1212
}
1313
}
1414

@@ -24,20 +24,13 @@ if (isNewArchitectureEnabled()) {
2424
}
2525

2626
android {
27+
namespace "io.swan.rnbrowser"
28+
2729
buildToolsVersion safeExtGet("buildToolsVersion", "34.0.0")
2830
compileSdkVersion safeExtGet("compileSdkVersion", 34)
2931

30-
if (project.android.hasProperty("namespace")) {
31-
namespace "io.swan.rnbrowser"
32-
33-
buildFeatures {
34-
buildConfig true
35-
}
36-
sourceSets {
37-
main {
38-
manifest.srcFile "src/main/AndroidManifestNew.xml"
39-
}
40-
}
32+
buildFeatures {
33+
buildConfig true
4134
}
4235
defaultConfig {
4336
buildConfigField("boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString())

android/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="io.swan.rnbrowser">
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
33
<queries>
44
<intent>
55
<action android:name="android.support.customtabs.action.CustomTabsService" />

android/src/main/AndroidManifestNew.xml

Lines changed: 0 additions & 8 deletions
This file was deleted.

android/src/main/java/io/swan/rnbrowser/RNSwanBrowserModuleImpl.kt

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,35 @@
11
package io.swan.rnbrowser
22

33
import android.content.Intent
4-
import android.net.Uri
54

65
import androidx.annotation.ColorInt
76
import androidx.browser.customtabs.CustomTabColorSchemeParams
87
import androidx.browser.customtabs.CustomTabsIntent
98
import androidx.core.content.ContextCompat
109
import androidx.core.graphics.ColorUtils
10+
import androidx.core.net.toUri
1111

1212
import com.facebook.react.bridge.Promise
1313
import com.facebook.react.bridge.ReactApplicationContext
1414
import com.facebook.react.bridge.ReadableMap
15-
import com.facebook.react.modules.core.DeviceEventManagerModule.RCTDeviceEventEmitter
1615

1716
import io.swan.rnbrowser.helpers.CustomTabActivityHelper
1817

1918
object RNSwanBrowserModuleImpl {
2019
const val NAME = "RNSwanBrowser"
21-
private var browserVisible = false
22-
23-
internal fun onHostResume(reactContext: ReactApplicationContext) {
24-
if (browserVisible && reactContext.hasActiveReactInstance()) {
25-
browserVisible = false
26-
27-
reactContext
28-
.getJSModule(RCTDeviceEventEmitter::class.java)
29-
.emit("swanBrowserDidClose", null)
30-
}
31-
}
3220

3321
internal fun open(
3422
reactContext: ReactApplicationContext,
3523
url: String,
3624
options: ReadableMap,
3725
promise: Promise
3826
) {
39-
if (browserVisible) {
40-
return promise.reject(
41-
"swan_browser_visible",
42-
"An instance of the swan browser is already visible"
43-
)
44-
}
45-
4627
val activity = reactContext.currentActivity
4728
?: return promise.reject(
4829
"no_current_activity",
4930
"Couldn't call open() when the app is in background"
5031
)
5132

52-
browserVisible = true
53-
5433
val intentBuilder = CustomTabsIntent.Builder().apply {
5534
setBookmarksButtonEnabled(false)
5635
setDownloadButtonEnabled(false)
@@ -95,7 +74,7 @@ object RNSwanBrowserModuleImpl {
9574
}
9675

9776
CustomTabActivityHelper.openCustomTab(
98-
activity, customTabsIntent, Uri.parse(url)
77+
activity, customTabsIntent, url.toUri()
9978
) { currentActivity, uri ->
10079
currentActivity.startActivity(Intent(Intent.ACTION_VIEW, uri))
10180
}
Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,23 @@
11
package io.swan.rnbrowser
22

3-
import com.facebook.react.bridge.LifecycleEventListener
43
import com.facebook.react.bridge.Promise
54
import com.facebook.react.bridge.ReactApplicationContext
65
import com.facebook.react.bridge.ReadableMap
76
import com.facebook.react.module.annotations.ReactModule
87

98
@ReactModule(name = RNSwanBrowserModuleImpl.NAME)
109
class RNSwanBrowserModule(reactContext: ReactApplicationContext) :
11-
NativeRNSwanBrowserSpec(reactContext),
12-
LifecycleEventListener {
13-
14-
init {
15-
reactApplicationContext.addLifecycleEventListener(this)
16-
}
17-
18-
override fun invalidate() {
19-
reactApplicationContext.removeLifecycleEventListener(this)
20-
}
10+
NativeRNSwanBrowserSpec(reactContext) {
2111

2212
override fun getName(): String {
2313
return RNSwanBrowserModuleImpl.NAME
2414
}
2515

26-
override fun onHostResume() {
27-
RNSwanBrowserModuleImpl.onHostResume(reactApplicationContext)
28-
}
29-
30-
override fun onHostPause() {}
31-
32-
override fun onHostDestroy() {}
33-
3416
override fun open(url: String, options: ReadableMap, promise: Promise) {
3517
RNSwanBrowserModuleImpl.open(reactApplicationContext, url, options, promise)
3618
}
3719

3820
override fun close() {
3921
// noop on Android since the modal is closed by deep-link
4022
}
41-
42-
override fun addListener(eventName: String) {
43-
// iOS only
44-
}
45-
46-
override fun removeListeners(count: Double) {
47-
// iOS only
48-
}
4923
}
Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.swan.rnbrowser
22

3-
import com.facebook.react.bridge.LifecycleEventListener
43
import com.facebook.react.bridge.Promise
54
import com.facebook.react.bridge.ReactApplicationContext
65
import com.facebook.react.bridge.ReactContextBaseJavaModule
@@ -10,29 +9,12 @@ import com.facebook.react.module.annotations.ReactModule
109

1110
@ReactModule(name = RNSwanBrowserModuleImpl.NAME)
1211
class RNSwanBrowserModule(reactContext: ReactApplicationContext) :
13-
ReactContextBaseJavaModule(reactContext),
14-
LifecycleEventListener {
15-
16-
init {
17-
reactApplicationContext.addLifecycleEventListener(this)
18-
}
19-
20-
override fun invalidate() {
21-
reactApplicationContext.removeLifecycleEventListener(this)
22-
}
12+
ReactContextBaseJavaModule(reactContext) {
2313

2414
override fun getName(): String {
2515
return RNSwanBrowserModuleImpl.NAME
2616
}
2717

28-
override fun onHostResume() {
29-
RNSwanBrowserModuleImpl.onHostResume(reactApplicationContext)
30-
}
31-
32-
override fun onHostPause() {}
33-
34-
override fun onHostDestroy() {}
35-
3618
@ReactMethod
3719
fun open(url: String, options: ReadableMap, promise: Promise) {
3820
RNSwanBrowserModuleImpl.open(reactApplicationContext, url, options, promise)
@@ -42,14 +24,4 @@ class RNSwanBrowserModule(reactContext: ReactApplicationContext) :
4224
fun close() {
4325
// noop on Android since the modal is closed by deep-link
4426
}
45-
46-
@ReactMethod
47-
fun addListener(eventName: String) {
48-
// iOS only
49-
}
50-
51-
@ReactMethod
52-
fun removeListeners(count: Double) {
53-
// iOS only
54-
}
5527
}

example/Gemfile.lock

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ GEM
2323
httpclient (~> 2.8, >= 2.8.3)
2424
json (>= 1.5.1)
2525
atomos (0.1.3)
26-
base64 (0.2.0)
27-
benchmark (0.4.0)
28-
bigdecimal (3.1.9)
26+
base64 (0.3.0)
27+
benchmark (0.4.1)
28+
bigdecimal (3.2.2)
2929
claide (1.1.0)
3030
cocoapods (1.15.2)
3131
addressable (~> 2.8)
@@ -67,7 +67,7 @@ GEM
6767
colored2 (3.1.2)
6868
concurrent-ruby (1.3.3)
6969
connection_pool (2.5.3)
70-
drb (2.2.1)
70+
drb (2.2.3)
7171
escape (0.0.4)
7272
ethon (0.16.0)
7373
ffi (>= 1.15.0)
@@ -79,7 +79,7 @@ GEM
7979
mutex_m
8080
i18n (1.14.7)
8181
concurrent-ruby (~> 1.0)
82-
json (2.11.3)
82+
json (2.13.2)
8383
logger (1.7.0)
8484
minitest (5.25.5)
8585
molinillo (0.8.0)
@@ -121,4 +121,4 @@ RUBY VERSION
121121
ruby 3.3.4p94
122122

123123
BUNDLED WITH
124-
2.6.8
124+
2.6.9

example/android/app/src/main/java/com/rnswanbrowserexample/MainApplication.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import android.app.Application
44
import com.facebook.react.PackageList
55
import com.facebook.react.ReactApplication
66
import com.facebook.react.ReactHost
7+
import com.facebook.react.ReactNativeApplicationEntryPoint.loadReactNative
78
import com.facebook.react.ReactNativeHost
89
import com.facebook.react.ReactPackage
9-
import com.facebook.react.defaults.DefaultNewArchitectureEntryPoint.load
1010
import com.facebook.react.defaults.DefaultReactHost.getDefaultReactHost
1111
import com.facebook.react.defaults.DefaultReactNativeHost
12-
import com.facebook.react.soloader.OpenSourceMergedSoMapping
13-
import com.facebook.soloader.SoLoader
1412

1513
class MainApplication : Application(), ReactApplication {
1614

@@ -35,10 +33,6 @@ class MainApplication : Application(), ReactApplication {
3533

3634
override fun onCreate() {
3735
super.onCreate()
38-
SoLoader.init(this, OpenSourceMergedSoMapping)
39-
if (BuildConfig.IS_NEW_ARCHITECTURE_ENABLED) {
40-
// If you opted-in for the New Architecture, we load the native entry point for this app.
41-
load()
42-
}
36+
loadReactNative(this)
4337
}
4438
}

0 commit comments

Comments
 (0)