From 3084415336580c50e5b1caf6a4607270f12f5ff9 Mon Sep 17 00:00:00 2001 From: Patryk Klatka Date: Sat, 27 Apr 2024 00:02:09 +0200 Subject: [PATCH] Add webViewDebuggingEnabled property to RNVisitableView --- .../com/reactnativeturbowebview/RNSession.kt | 3 -- .../RNVisitableView.kt | 7 ++++- .../RNVisitableViewManager.kt | 5 ++++ packages/turbo/ios/RNSession.swift | 6 ---- packages/turbo/ios/RNVisitableView.swift | 30 ++++++++++++------- packages/turbo/ios/RNVisitableViewManager.m | 1 + packages/turbo/src/RNVisitableView.ts | 1 + packages/turbo/src/VisitableView.tsx | 3 ++ 8 files changed, 36 insertions(+), 20 deletions(-) diff --git a/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNSession.kt b/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNSession.kt index 6c289cc1..f4802235 100644 --- a/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNSession.kt +++ b/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNSession.kt @@ -2,13 +2,11 @@ package com.reactnativeturbowebview import android.webkit.JavascriptInterface import android.webkit.WebSettings -import android.webkit.WebView import androidx.appcompat.app.AppCompatActivity import androidx.lifecycle.Lifecycle import androidx.lifecycle.LifecycleOwner import androidx.lifecycle.lifecycleScope import androidx.lifecycle.whenStateAtLeast -import com.facebook.react.BuildConfig import com.facebook.react.bridge.ReactApplicationContext import dev.hotwire.turbo.errors.TurboVisitError import dev.hotwire.turbo.session.TurboSession @@ -34,7 +32,6 @@ class RNSession( val webView = TurboWebView(activity, null) val session = TurboSession(sessionHandle, activity, webView) - WebView.setWebContentsDebuggingEnabled(BuildConfig.DEBUG) webView.settings.setJavaScriptEnabled(true) webView.addJavascriptInterface(JavaScriptInterface(), "AndroidInterface") setUserAgentString(webView, applicationNameForUserAgent) diff --git a/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNVisitableView.kt b/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNVisitableView.kt index 567c9e79..41b0d1ba 100644 --- a/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNVisitableView.kt +++ b/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNVisitableView.kt @@ -5,7 +5,7 @@ import android.graphics.Bitmap import android.view.MotionEvent import android.view.ViewGroup import android.webkit.CookieManager -import android.webkit.WebSettings +import android.webkit.WebView import android.widget.LinearLayout import androidx.appcompat.widget.AppCompatImageView import androidx.core.view.isVisible @@ -65,6 +65,11 @@ class RNVisitableView(context: Context) : LinearLayout(context), SessionSubscrib ) } } + var webViewDebuggingEnabled: Boolean = false + set(value) { + field = value + WebView.setWebContentsDebuggingEnabled(value) + } // Session private var _session: RNSession? = null diff --git a/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNVisitableViewManager.kt b/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNVisitableViewManager.kt index 04c3c6ee..1e24ef10 100644 --- a/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNVisitableViewManager.kt +++ b/packages/turbo/android/src/main/java/com/reactnativeturbowebview/RNVisitableViewManager.kt @@ -68,6 +68,11 @@ class RNVisitableViewManager( view.progressViewOffset = progressViewOffset } + @ReactProp(name = "webViewDebuggingEnabled") + fun setWebViewDebuggingEnabled(view: RNVisitableView, webViewDebuggingEnabled: Boolean) { + view.webViewDebuggingEnabled = webViewDebuggingEnabled + } + override fun getCommandsMap(): MutableMap = RNVisitableViewCommand.values() .associate { it.jsCallbackName to it.ordinal diff --git a/packages/turbo/ios/RNSession.swift b/packages/turbo/ios/RNSession.swift index 5f4a59a9..54d8544d 100644 --- a/packages/turbo/ios/RNSession.swift +++ b/packages/turbo/ios/RNSession.swift @@ -50,12 +50,6 @@ class RNSession: NSObject { session.webView.allowsLinkPreview = false session.webView.scrollView.contentInsetAdjustmentBehavior = .never session.webView.uiDelegate = self.wkUiDelegate - - #if DEBUG - if #available(iOS 16.4, *) { - session.webView.isInspectable = true - } - #endif return session }() diff --git a/packages/turbo/ios/RNVisitableView.swift b/packages/turbo/ios/RNVisitableView.swift index b841e96b..927fbcba 100644 --- a/packages/turbo/ios/RNVisitableView.swift +++ b/packages/turbo/ios/RNVisitableView.swift @@ -14,13 +14,7 @@ let REFRESH_SCRIPT = "typeof Turbo.session.refresh === 'function'" + class RNVisitableView: UIView, RNSessionSubscriber { var id: UUID = UUID() @objc var sessionHandle: NSString? = nil - @objc var url: NSString = "" { - didSet { - if(url != oldValue) { - visit() - } - } - } + @objc var url: NSString = "" @objc var applicationNameForUserAgent: NSString? = nil { didSet { webViewConfiguration.applicationNameForUserAgent = applicationNameForUserAgent as? String @@ -41,6 +35,11 @@ class RNVisitableView: UIView, RNSessionSubscriber { configureWebView() } } + @objc var webViewDebuggingEnabled: Bool = false { + didSet { + configureWebView() + } + } @objc var onMessage: RCTDirectEventBlock? @objc var onVisitProposal: RCTDirectEventBlock? @objc var onOpenExternalUrl: RCTDirectEventBlock? @@ -83,7 +82,11 @@ class RNVisitableView: UIView, RNSessionSubscriber { if (webView == nil) { return } - + + if #available(iOS 16.4, *) { + webView!.isInspectable = webViewDebuggingEnabled + } + webView!.scrollView.isScrollEnabled = scrollEnabled webView!.scrollView.contentInset = UIEdgeInsets(top: contentInset["top"] ?? 0, left: contentInset["left"] ?? 0, @@ -122,6 +125,13 @@ class RNVisitableView: UIView, RNSessionSubscriber { controller!.endAppearanceTransition() } } + + override func didSetProps(_ changedProps: [String]!) { + super.didSetProps(changedProps) + + // When all properties of RNVisitableView are initialized, the visit function can be safely called. + visit() + } override func removeFromSuperview() { super.removeFromSuperview() @@ -159,11 +169,11 @@ class RNVisitableView: UIView, RNSessionSubscriber { } private func visit() { - if (controller?.visitableURL?.absoluteString == url as String) { + if (controller?.visitableURL?.absoluteString == url as String || session == nil) { return } controller!.visitableURL = URL(string: String(url)) - session?.visit(controller!) + session!.visit(controller!) } public func didProposeVisit(proposal: VisitProposal){ diff --git a/packages/turbo/ios/RNVisitableViewManager.m b/packages/turbo/ios/RNVisitableViewManager.m index d9042cb7..2ffa7418 100644 --- a/packages/turbo/ios/RNVisitableViewManager.m +++ b/packages/turbo/ios/RNVisitableViewManager.m @@ -19,6 +19,7 @@ @interface RCT_EXTERN_MODULE(RNVisitableViewManager, NSObject) RCT_EXPORT_VIEW_PROPERTY(pullToRefreshEnabled, BOOL) RCT_EXPORT_VIEW_PROPERTY(scrollEnabled, BOOL) RCT_EXPORT_VIEW_PROPERTY(contentInset, NSDictionary) + RCT_EXPORT_VIEW_PROPERTY(webViewDebuggingEnabled, BOOL) RCT_EXPORT_VIEW_PROPERTY(onVisitProposal, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(onOpenExternalUrl, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(onMessage, RCTDirectEventBlock) diff --git a/packages/turbo/src/RNVisitableView.ts b/packages/turbo/src/RNVisitableView.ts index f7953236..9402dc24 100644 --- a/packages/turbo/src/RNVisitableView.ts +++ b/packages/turbo/src/RNVisitableView.ts @@ -32,6 +32,7 @@ export interface RNVisitableViewProps { scrollEnabled: boolean; contentInset: ContentInsetObject; progressViewOffset?: ProgressViewOffsetObject; + webViewDebuggingEnabled: boolean; onLoad?: (e: NativeSyntheticEvent) => void; onMessage?: (e: NativeSyntheticEvent) => void; onError?: (e: NativeSyntheticEvent) => void; diff --git a/packages/turbo/src/VisitableView.tsx b/packages/turbo/src/VisitableView.tsx index 21a9ff8b..287d65fe 100644 --- a/packages/turbo/src/VisitableView.tsx +++ b/packages/turbo/src/VisitableView.tsx @@ -54,6 +54,7 @@ export interface Props { scrollEnabled?: boolean; contentInset?: ContentInsetObject; progressViewOffset?: ProgressViewOffsetObject; + webViewDebuggingEnabled?: boolean; renderLoading?: RenderLoading; renderError?: RenderError; onVisitProposal: (proposal: VisitProposal) => void; @@ -86,6 +87,7 @@ const VisitableView = React.forwardRef>( scrollEnabled = true, contentInset = { top: 0, left: 0, right: 0, bottom: 0 }, progressViewOffset, + webViewDebuggingEnabled = false, renderLoading, renderError, onLoad, @@ -225,6 +227,7 @@ const VisitableView = React.forwardRef>( scrollEnabled={scrollEnabled} contentInset={contentInset} progressViewOffset={progressViewOffset} + webViewDebuggingEnabled={webViewDebuggingEnabled} onError={onErrorCombinedHandlers} onVisitProposal={handleVisitProposal} onMessage={handleOnMessage}