Skip to content

Commit

Permalink
Add webViewDebuggingEnabled property to RNVisitableView
Browse files Browse the repository at this point in the history
  • Loading branch information
pklatka committed Apr 26, 2024
1 parent 5b7fe1c commit 3084415
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Int> = RNVisitableViewCommand.values()
.associate {
it.jsCallbackName to it.ordinal
Expand Down
6 changes: 0 additions & 6 deletions packages/turbo/ios/RNSession.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}()
Expand Down
30 changes: 20 additions & 10 deletions packages/turbo/ios/RNVisitableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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){
Expand Down
1 change: 1 addition & 0 deletions packages/turbo/ios/RNVisitableViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions packages/turbo/src/RNVisitableView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export interface RNVisitableViewProps {
scrollEnabled: boolean;
contentInset: ContentInsetObject;
progressViewOffset?: ProgressViewOffsetObject;
webViewDebuggingEnabled: boolean;
onLoad?: (e: NativeSyntheticEvent<LoadEvent>) => void;
onMessage?: (e: NativeSyntheticEvent<MessageEvent>) => void;
onError?: (e: NativeSyntheticEvent<ErrorEvent>) => void;
Expand Down
3 changes: 3 additions & 0 deletions packages/turbo/src/VisitableView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export interface Props {
scrollEnabled?: boolean;
contentInset?: ContentInsetObject;
progressViewOffset?: ProgressViewOffsetObject;
webViewDebuggingEnabled?: boolean;
renderLoading?: RenderLoading;
renderError?: RenderError;
onVisitProposal: (proposal: VisitProposal) => void;
Expand Down Expand Up @@ -86,6 +87,7 @@ const VisitableView = React.forwardRef<RefObject, React.PropsWithRef<Props>>(
scrollEnabled = true,
contentInset = { top: 0, left: 0, right: 0, bottom: 0 },
progressViewOffset,
webViewDebuggingEnabled = false,
renderLoading,
renderError,
onLoad,
Expand Down Expand Up @@ -225,6 +227,7 @@ const VisitableView = React.forwardRef<RefObject, React.PropsWithRef<Props>>(
scrollEnabled={scrollEnabled}
contentInset={contentInset}
progressViewOffset={progressViewOffset}
webViewDebuggingEnabled={webViewDebuggingEnabled}
onError={onErrorCombinedHandlers}
onVisitProposal={handleVisitProposal}
onMessage={handleOnMessage}
Expand Down

0 comments on commit 3084415

Please sign in to comment.