Skip to content

Commit

Permalink
Add the full implementation of onKlarnaMessage prop to KlarnaStandalo…
Browse files Browse the repository at this point in the history
…neWebView component
  • Loading branch information
MasoudFallahpourbaee committed Oct 15, 2023
1 parent 5d7d5f7 commit 75a97ae
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.uimanager.UIManagerHelper;
import com.facebook.react.uimanager.events.EventDispatcher;
import com.klarna.mobile.sdk.api.KlarnaProductEvent;
import com.klarna.mobile.sdk.api.standalonewebview.KlarnaStandaloneWebView;
import com.klarna.mobile.sdk.reactnative.common.ArgumentsUtil;

Expand All @@ -17,6 +18,7 @@
import java.util.Map;

// TODO: Double-check that we're sending the correct values for parameters of the events.

/**
* This class is responsible for sending some KlarnaStandaloneWebView-related events to the React Native side.
*/
Expand All @@ -25,6 +27,7 @@ public class KlarnaStandaloneWebViewEventSender {
private static final String PARAM_NAME_NAVIGATION_EVENT = "navigationEvent";
private static final String PARAM_NAME_NAVIGATION_ERROR = "navigationError";
private static final String PARAM_NAME_PROGRESS_EVENT = "progressEvent";
private static final String PARAM_NAME_KLARNA_MESSAGE_EVENT = "klarnaMessageEvent";
private static final String PARAM_NAME_ERROR_MESSAGE = "errorMessage";
private static final String PARAM_NAME_EVENT = "event";
private static final String PARAM_NAME_NEW_URL = "newUrl";
Expand All @@ -33,6 +36,7 @@ public class KlarnaStandaloneWebViewEventSender {
private static final String PARAM_NAME_PROGRESS = "progress";
private static final String PARAM_NAME_IS_LOADING = "isLoading";
private static final String PARAM_NAME_WEB_VIEW_STATE = "webViewState";
private static final String PARAM_NAME_ACTION = "action";

private final Map<WeakReference<KlarnaStandaloneWebView>, EventDispatcher> viewToDispatcher;

Expand Down Expand Up @@ -65,6 +69,13 @@ public void sendNavigationEvent(KlarnaStandaloneWebView view, KlarnaStandaloneWe
postEventForView(event, params, view);
}

public void sendKlarnaMessageEvent(@NonNull KlarnaStandaloneWebView view, @NonNull KlarnaProductEvent klarnaProductEvent) {
WritableMap params = ArgumentsUtil.createMap(new HashMap<>() {{
put(PARAM_NAME_KLARNA_MESSAGE_EVENT, buildKlarnaMessageEventMap(klarnaProductEvent));
}});
postEventForView(KlarnaStandaloneWebViewEvent.Event.ON_KLARNA_MESSAGE, params, view);
}

/**
* Creates an event from event name and a map of params. Sends it via the right dispatcher.
*
Expand Down Expand Up @@ -116,4 +127,10 @@ private ReadableMap buildNavigationEventMap(@Nullable KlarnaStandaloneWebView vi
}
}

private ReadableMap buildKlarnaMessageEventMap(KlarnaProductEvent klarnaProductEvent) {
return ArgumentsUtil.createMap(new HashMap<>() {{
put(PARAM_NAME_ACTION, klarnaProductEvent.getAction());
}});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.Map;
import java.util.Objects;

// TODO Add support for sending onKlarnaMessage events.
public class KlarnaStandaloneWebViewManager extends RNKlarnaStandaloneWebViewSpec<KlarnaStandaloneWebView> {

// Commands that can be triggered from React Native side
Expand All @@ -47,12 +46,14 @@ public class KlarnaStandaloneWebViewManager extends RNKlarnaStandaloneWebViewSpe
private final KlarnaEventHandler klarnaEventHandler = new KlarnaEventHandler() {
@Override
public void onEvent(@NonNull KlarnaComponent klarnaComponent, @NonNull KlarnaProductEvent klarnaProductEvent) {
// Not used as of now
if (klarnaComponent instanceof KlarnaStandaloneWebView) {
klarnaStandaloneWebViewEventSender.sendKlarnaMessageEvent((KlarnaStandaloneWebView) klarnaComponent, klarnaProductEvent);
}
}

@Override
public void onError(@NonNull KlarnaComponent klarnaComponent, @NonNull KlarnaMobileSDKError klarnaMobileSDKError) {
// No used as of now
// Not used as of now
}
};
private final KlarnaStandaloneWebViewClient klarnaStandaloneWebViewClient = new KlarnaStandaloneWebViewClient() {
Expand Down
23 changes: 22 additions & 1 deletion ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

using namespace facebook::react;

@interface KlarnaStandaloneWebViewWrapper () <KlarnaStandaloneWebViewDelegate, RCTRNKlarnaStandaloneWebViewViewProtocol>
@interface KlarnaStandaloneWebViewWrapper () <KlarnaStandaloneWebViewDelegate, KlarnaEventHandler, RCTRNKlarnaStandaloneWebViewViewProtocol>

@property (nonatomic, strong) KlarnaStandaloneWebView* klarnaStandaloneWebView;

Expand All @@ -33,6 +33,7 @@ - (id)init {
// TODO: What should we pass here for 'returnUrl'?
[self initializeKlarnaStandaloneWebView: @"returnUrl://"];
self.klarnaStandaloneWebView.delegate = self;
self.klarnaStandaloneWebView.eventHandler = self;
// TODO: Where is the proper place to call removeObserver?
[self.klarnaStandaloneWebView addObserver:self forKeyPath:PROPERTY_NAME_ESTIMATED_PROGRESS options:NSKeyValueObservingOptionNew context:nil];
return self;
Expand Down Expand Up @@ -213,6 +214,26 @@ - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didF

#pragma mark - RCTRNKlarnaStandaloneWebViewViewProtocol methods

- (void)klarnaComponent:(id <KlarnaComponent> _Nonnull)klarnaComponent dispatchedEvent:(KlarnaProductEvent * _Nonnull)event {
if (_eventEmitter) {
RCTLogInfo(@"Sending onKlarnaMessage event");
std::dynamic_pointer_cast<const RNKlarnaStandaloneWebViewEventEmitter>(_eventEmitter)
->onKlarnaMessage(RNKlarnaStandaloneWebViewEventEmitter::OnKlarnaMessage{
.klarnaMessageEvent = {
.action = std::string([[event action] UTF8String]),
}
});
} else {
RCTLogInfo(@"_eventEmitter is nil!");
}
}

- (void)klarnaComponent:(id <KlarnaComponent> _Nonnull)klarnaComponent encounteredError:(KlarnaError * _Nonnull)error {
// Not used as of now
}

#pragma mark - RCTRNKlarnaStandaloneWebViewViewProtocol methods

- (void)load:(nonnull NSString *)url {
[self.klarnaStandaloneWebView loadURL:[NSURL URLWithString:url]];
}
Expand Down
23 changes: 21 additions & 2 deletions ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
#import <KlarnaMobileSDK/KlarnaMobileSDK-Swift.h>
#import <React/RCTLog.h>

@interface KlarnaStandaloneWebViewWrapper () <KlarnaStandaloneWebViewDelegate>
@interface KlarnaStandaloneWebViewWrapper () <KlarnaStandaloneWebViewDelegate, KlarnaEventHandler>

@property (nonatomic, strong) KlarnaStandaloneWebView* klarnaStandaloneWebView;

@end

// TODO: Add support for sending onKlarnaMessage event.
// TODO: Double-check that we're sending the correct values for parameters of the events.
@implementation KlarnaStandaloneWebViewWrapper

Expand All @@ -22,6 +21,8 @@ - (id)init {
self = [super init];
[self initializeKlarnaStandaloneWebView];
self.klarnaStandaloneWebView.delegate = self;
self.klarnaStandaloneWebView.eventHandler = self;
// TODO: Where is the proper place to call removeObserver?
[self.klarnaStandaloneWebView addObserver:self forKeyPath:PROPERTY_NAME_ESTIMATED_PROGRESS options:NSKeyValueObservingOptionNew context:nil];
return self;
}
Expand Down Expand Up @@ -175,6 +176,24 @@ - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didF
});
}

#pragma mark - KlarnaEventHandler methods

- (void)klarnaComponent:(id <KlarnaComponent> _Nonnull)klarnaComponent dispatchedEvent:(KlarnaProductEvent * _Nonnull)event {
if (!self.onKlarnaMessage) {
RCTLog(@"Missing 'onKlarnaMessage' callback prop.");
return;
}
self.onKlarnaMessage(@{
@"klarnaMessageEvent": @{
@"action": event.action
}
});
}

- (void)klarnaComponent:(id <KlarnaComponent> _Nonnull)klarnaComponent encounteredError:(KlarnaError * _Nonnull)error {
// Not used as of now
}

@end

#endif
10 changes: 8 additions & 2 deletions src/KlarnaStandaloneWebView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ export class KlarnaStandaloneWebView extends Component<
event: NativeSyntheticEvent<
Readonly<{
readonly klarnaMessageEvent: Readonly<{
readonly message: string;
action: string;
// Dictionary is not support for events
// readonly params: { [key: string]: any };
}>;
}>
>
Expand Down Expand Up @@ -188,7 +190,11 @@ export interface KlarnaWebViewProgressEvent {
}

export interface KlarnaWebViewKlarnaMessageEvent {
readonly message: string;
readonly action: string;
// Dictionary is not support for events
// readonly params: { [key: string]: any };
// TODO What is a KlarnaWebViewComponent?
// readonly component: KlarnaWebViewComponent;
}

export default KlarnaStandaloneWebView;
7 changes: 5 additions & 2 deletions src/specs/KlarnaStandaloneWebViewNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,13 @@ type KlarnaWebViewProgressEvent = Readonly<{
}>;
}>;

// TODO add the fields when the definition is known. For now we just add a message
type KlarnaWebViewKlarnaMessageEvent = Readonly<{
readonly klarnaMessageEvent: Readonly<{
readonly message: string;
readonly action: string;
// Dictionary is not support for events
// readonly params: { [key: string]: any };
// TODO What is a KlarnaWebViewComponent?
// readonly component: KlarnaWebViewComponent;
}>;
}>;

Expand Down

0 comments on commit 75a97ae

Please sign in to comment.