From 7a6d1c392727b6cd6abe291841b4074dc16481ba Mon Sep 17 00:00:00 2001 From: Nuri Mertcan Guner Date: Thu, 4 Jul 2024 10:28:10 +0200 Subject: [PATCH] add activity as context for standalone webview --- TestApp/src/standalonewebview/StandaloneWebViewScreen.tsx | 6 +++++- .../sdk/reactnative/payments/KlarnaPaymentViewManager.java | 2 +- .../KlarnaStandaloneWebViewEventSender.java | 6 ++++-- .../standalonewebview/KlarnaStandaloneWebViewManager.java | 6 +++--- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/TestApp/src/standalonewebview/StandaloneWebViewScreen.tsx b/TestApp/src/standalonewebview/StandaloneWebViewScreen.tsx index 1077b714..2538ec81 100644 --- a/TestApp/src/standalonewebview/StandaloneWebViewScreen.tsx +++ b/TestApp/src/standalonewebview/StandaloneWebViewScreen.tsx @@ -1,4 +1,4 @@ -import {Keyboard, TextInput, View} from 'react-native'; +import {Keyboard, Text, TextInput, View} from 'react-native'; import styles from '../common/ui/Styles'; import React, {useRef, useState} from 'react'; import { @@ -10,13 +10,16 @@ import { KlarnaWebViewRenderProcessGoneEvent, } from 'react-native-klarna-inapp-sdk'; import Button from '../common/ui/view/Button'; +import testProps from '../common/util/TestProps.tsx'; export default function StandaloneWebViewScreen() { const klarnaStandaloneWebViewRef = useRef(null); const [url, setUrl] = useState(''); + const [eventState, setEventState] = useState(); const logEvent = (...params: Array) => { console.log('onEvent', params); + setEventState(params.join(', ')); }; const renderUrlTextInput = () => { @@ -113,6 +116,7 @@ export default function StandaloneWebViewScreen() { {renderGoBackButton()} {renderGoForwardButton()} + {eventState} reactContextWeakReference; private final Map, EventDispatcher> viewToDispatcher; - KlarnaStandaloneWebViewEventSender(@NonNull final Map, EventDispatcher> viewToDispatcher) { + KlarnaStandaloneWebViewEventSender(@NonNull ReactContext reactContext, @NonNull final Map, EventDispatcher> viewToDispatcher) { + this.reactContextWeakReference = new WeakReference<>(reactContext); this.viewToDispatcher = viewToDispatcher; } @@ -110,7 +112,7 @@ public void sendRenderProcessGoneEvent(@Nullable KlarnaStandaloneWebView view, b private void postEventForView(KlarnaStandaloneWebViewEvent.Event event, WritableMap additionalParams, @Nullable KlarnaStandaloneWebView view) { KlarnaStandaloneWebView klarnaStandaloneWebView = getKlarnaStandaloneWebView(view); if (klarnaStandaloneWebView != null) { - EventDispatcher eventDispatcher = UIManagerHelper.getEventDispatcherForReactTag((ReactContext) klarnaStandaloneWebView.getContext(), klarnaStandaloneWebView.getId()); + EventDispatcher eventDispatcher = UIManagerHelper.getEventDispatcherForReactTag(reactContextWeakReference.get(), klarnaStandaloneWebView.getId()); if (eventDispatcher != null) { KlarnaStandaloneWebViewEvent e = new KlarnaStandaloneWebViewEvent(klarnaStandaloneWebView.getId(), event.name, additionalParams); eventDispatcher.dispatchEvent(e); diff --git a/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewManager.java b/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewManager.java index ffed3a9a..afc99138 100644 --- a/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewManager.java +++ b/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewManager.java @@ -105,7 +105,7 @@ public KlarnaStandaloneWebViewManager(ReactApplicationContext reactContext) { super(); this.reactAppContext = reactContext; viewToDispatcher = new HashMap<>(); - klarnaStandaloneWebViewEventSender = new KlarnaStandaloneWebViewEventSender(viewToDispatcher); + klarnaStandaloneWebViewEventSender = new KlarnaStandaloneWebViewEventSender(reactContext, viewToDispatcher); } @ReactProp(name = "returnUrl") @@ -224,7 +224,7 @@ public Map getExportedCustomDirectEventTypeConstants() { @Override protected KlarnaStandaloneWebView createViewInstance(@NonNull ThemedReactContext themedReactContext) { KlarnaStandaloneWebView klarnaStandaloneWebView = new KlarnaStandaloneWebView( - /* context */ reactAppContext, + /* context */ reactAppContext.getCurrentActivity(), // needs to be activity for SDK dialogs to work /* attrs */ null, /* defStyleAttr */ 0, /* webViewClient */ klarnaStandaloneWebViewClient, @@ -235,7 +235,7 @@ protected KlarnaStandaloneWebView createViewInstance(@NonNull ThemedReactContext /* resourceEndpoint */ null, /* returnURL */ null); // Each view has its own event dispatcher. - EventDispatcher dispatcher = UIManagerHelper.getEventDispatcherForReactTag((ReactContext) klarnaStandaloneWebView.getContext(), klarnaStandaloneWebView.getId()); + EventDispatcher dispatcher = UIManagerHelper.getEventDispatcherForReactTag(reactAppContext, klarnaStandaloneWebView.getId()); viewToDispatcher.put(new WeakReference<>(klarnaStandaloneWebView), dispatcher); return klarnaStandaloneWebView; }