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

Fix SDK dialogs for standalone webview #261

Merged
merged 1 commit into from
Jul 4, 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
6 changes: 5 additions & 1 deletion TestApp/src/standalonewebview/StandaloneWebViewScreen.tsx
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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<KlarnaStandaloneWebView>(null);
const [url, setUrl] = useState('');
const [eventState, setEventState] = useState<string>();

const logEvent = (...params: Array<string>) => {
console.log('onEvent', params);
setEventState(params.join(', '));
};

const renderUrlTextInput = () => {
Expand Down Expand Up @@ -113,6 +116,7 @@ export default function StandaloneWebViewScreen() {
{renderGoBackButton()}
{renderGoForwardButton()}
</View>
<Text {...testProps('state_events')}>{eventState}</Text>
<KlarnaStandaloneWebView
ref={klarnaStandaloneWebViewRef}
/* eslint-disable-next-line react-native/no-inline-styles */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ private void postEventForView(String eventName, WritableMap additionalParams, Kl
private void postEventForView(String eventName, WritableMap additionalParams, PaymentViewWrapper wrapper) {
if (wrapper != null) {
KlarnaPaymentEvent event = new KlarnaPaymentEvent(wrapper.getId(), eventName, additionalParams);
EventDispatcher eventDispatcher = UIManagerHelper.getEventDispatcherForReactTag((ReactContext) wrapper.getContext(), wrapper.getId());
EventDispatcher eventDispatcher = UIManagerHelper.getEventDispatcherForReactTag(reactAppContext, wrapper.getId());
if (eventDispatcher != null) {
eventDispatcher.dispatchEvent(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ public class KlarnaStandaloneWebViewEventSender {
private static final String PARAM_NAME_DID_CRASH = "didCrash";

private final Gson gson = new Gson();
private final WeakReference<ReactContext> reactContextWeakReference;
private final Map<WeakReference<KlarnaStandaloneWebView>, EventDispatcher> viewToDispatcher;

KlarnaStandaloneWebViewEventSender(@NonNull final Map<WeakReference<KlarnaStandaloneWebView>, EventDispatcher> viewToDispatcher) {
KlarnaStandaloneWebViewEventSender(@NonNull ReactContext reactContext, @NonNull final Map<WeakReference<KlarnaStandaloneWebView>, EventDispatcher> viewToDispatcher) {
this.reactContextWeakReference = new WeakReference<>(reactContext);
this.viewToDispatcher = viewToDispatcher;
}

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -224,7 +224,7 @@ public Map<String, Object> 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,
Expand All @@ -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;
}
Expand Down
Loading