Skip to content

Commit

Permalink
Rename event 'onBeforeLoad' to 'onLoadStart' and event 'onProgressCha…
Browse files Browse the repository at this point in the history
…nge' to 'onLoadProgress'
  • Loading branch information
MasoudFallahpourbaee committed Dec 20, 2023
1 parent 0b46b9f commit b355b6e
Show file tree
Hide file tree
Showing 10 changed files with 36 additions and 38 deletions.
8 changes: 4 additions & 4 deletions TestApp/src/standalonewebview/StandaloneWebViewScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,17 +119,17 @@ export default function StandaloneWebViewScreen() {
flex: 1,
}}
returnUrl={'returnUrl://'}
onBeforeLoad={(event: KlarnaWebViewNavigationEvent) => {
onEvent('onBeforeLoad', JSON.stringify(event));
onLoadStart={(event: KlarnaWebViewNavigationEvent) => {
onEvent('onLoadStart', JSON.stringify(event));
}}
onLoad={(event: KlarnaWebViewNavigationEvent) => {
onEvent('onLoad', JSON.stringify(event));
}}
onLoadError={(event: KlarnaWebViewNavigationError) => {
onEvent('onLoadError', JSON.stringify(event));
}}
onProgressChange={(event: KlarnaWebViewProgressEvent) => {
onEvent('onProgressChange', JSON.stringify(event));
onLoadProgress={(event: KlarnaWebViewProgressEvent) => {
onEvent('onLoadProgress', JSON.stringify(event));
}}
onKlarnaMessage={(event: KlarnaWebViewKlarnaMessageEvent) => {
onEvent('onKlarnaMessage', JSON.stringify(event));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public class KlarnaStandaloneWebViewEvent extends Event<KlarnaStandaloneWebViewE
// List of possible events that can be sent to the React Native side.
public enum Event {
// This event is sent just before loading a URL
ON_BEFORE_LOAD("onBeforeLoad"),
ON_LOAD_START("onLoadStart"),
// This event is sent when loading a URL is done
ON_LOAD("onLoad"),
// This event is sent when loading a URL encounters an error
ON_LOAD_ERROR("onLoadError"),
// This event is sent when the progress of loading a page changes
ON_PROGRESS_CHANGE("onProgressChange"),
ON_LOAD_PROGRESS("onLoadProgress"),
// This event is sent when the Mobile SDK wants to send an event to the Web View
ON_KLARNA_MESSAGE("onKlarnaMessage"),
// This event is sent when the WebView's render process has exited
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void sendProgressChangeEvent(@Nullable KlarnaStandaloneWebView view, int
WritableMap params = ArgumentsUtil.createMap(new HashMap<>() {{
put(PARAM_NAME_PROGRESS_EVENT, webViewStateMap);
}});
postEventForView(KlarnaStandaloneWebViewEvent.Event.ON_PROGRESS_CHANGE, params, view);
postEventForView(KlarnaStandaloneWebViewEvent.Event.ON_LOAD_PROGRESS, params, view);
}

public void sendLoadErrorEvent(@Nullable KlarnaStandaloneWebView view, String description) {
Expand Down Expand Up @@ -132,7 +132,7 @@ private ReadableMap buildNavigationEventMap(@Nullable KlarnaStandaloneWebView vi
} else {
return ArgumentsUtil.createMap(new HashMap<>() {{
// Possible values for 'event' are 'willLoad', 'loadStarted', and 'loadEnded'
put(PARAM_NAME_EVENT, event == KlarnaStandaloneWebViewEvent.Event.ON_BEFORE_LOAD ? "loadStarted" : "loadEnded");
put(PARAM_NAME_EVENT, event == KlarnaStandaloneWebViewEvent.Event.ON_LOAD_START ? "loadStarted" : "loadEnded");
put(PARAM_NAME_NEW_URL, view.getUrl());
put(PARAM_NAME_WEB_VIEW_STATE, buildWebViewStateMap(view.getUrl(), view.getTitle(), view.getProgress()));
}});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void onError(@NonNull KlarnaComponent klarnaComponent, @NonNull KlarnaMob
private final KlarnaStandaloneWebViewClient klarnaStandaloneWebViewClient = new KlarnaStandaloneWebViewClient() {
@Override
public void onPageStarted(@Nullable KlarnaStandaloneWebView view, @Nullable String url, @Nullable Bitmap favicon) {
klarnaStandaloneWebViewEventSender.sendNavigationEvent(view, KlarnaStandaloneWebViewEvent.Event.ON_BEFORE_LOAD);
klarnaStandaloneWebViewEventSender.sendNavigationEvent(view, KlarnaStandaloneWebViewEvent.Event.ON_LOAD_START);
}

@Override
Expand Down Expand Up @@ -173,10 +173,10 @@ public void reload(KlarnaStandaloneWebView view) {
@Override
public Map<String, Object> getExportedCustomDirectEventTypeConstants() {
MapBuilder.Builder<String, Object> builder = MapBuilder.builder();
builder.put(KlarnaStandaloneWebViewEvent.Event.ON_BEFORE_LOAD.name, MapBuilder.of("registrationName", KlarnaStandaloneWebViewEvent.Event.ON_BEFORE_LOAD.name));
builder.put(KlarnaStandaloneWebViewEvent.Event.ON_LOAD_START.name, MapBuilder.of("registrationName", KlarnaStandaloneWebViewEvent.Event.ON_LOAD_START.name));
builder.put(KlarnaStandaloneWebViewEvent.Event.ON_LOAD.name, MapBuilder.of("registrationName", KlarnaStandaloneWebViewEvent.Event.ON_LOAD.name));
builder.put(KlarnaStandaloneWebViewEvent.Event.ON_LOAD_ERROR.name, MapBuilder.of("registrationName", KlarnaStandaloneWebViewEvent.Event.ON_LOAD_ERROR.name));
builder.put(KlarnaStandaloneWebViewEvent.Event.ON_PROGRESS_CHANGE.name, MapBuilder.of("registrationName", KlarnaStandaloneWebViewEvent.Event.ON_PROGRESS_CHANGE.name));
builder.put(KlarnaStandaloneWebViewEvent.Event.ON_LOAD_PROGRESS.name, MapBuilder.of("registrationName", KlarnaStandaloneWebViewEvent.Event.ON_LOAD_PROGRESS.name));
builder.put(KlarnaStandaloneWebViewEvent.Event.ON_KLARNA_MESSAGE.name, MapBuilder.of("registrationName", KlarnaStandaloneWebViewEvent.Event.ON_KLARNA_MESSAGE.name));
builder.put(KlarnaStandaloneWebViewEvent.Event.ON_RENDER_PROCESS_GONE.name, MapBuilder.of("registrationName", KlarnaStandaloneWebViewEvent.Event.ON_RENDER_PROCESS_GONE.name));
return builder.build();
Expand Down
4 changes: 2 additions & 2 deletions ios/Sources/KlarnaStandaloneWebViewManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ @implementation KlarnaStandaloneWebViewManager
#pragma mark - View

RCT_EXPORT_VIEW_PROPERTY(returnUrl, NSString)
RCT_EXPORT_VIEW_PROPERTY(onBeforeLoad, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onLoadStart, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onLoad, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onLoadError, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onProgressChange, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onLoadProgress, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onKlarnaMessage, RCTDirectEventBlock)

- (UIView *)view {
Expand Down
10 changes: 5 additions & 5 deletions ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N

- (void)sendProgressChangeEvent:(int)progress {
if (_eventEmitter) {
RCTLogInfo(@"Sending onProgressChange event");
RCTLogInfo(@"Sending onLoadProgress event");
NSString * url = self.klarnaStandaloneWebView.url == nil ? @"" : self.klarnaStandaloneWebView.url.absoluteString;
NSString * title = self.klarnaStandaloneWebView.title == nil ? @"" : self.klarnaStandaloneWebView.title;
std::dynamic_pointer_cast<const RNKlarnaStandaloneWebViewEventEmitter>(_eventEmitter)
->onProgressChange(RNKlarnaStandaloneWebViewEventEmitter::OnProgressChange{
->onLoadProgress(RNKlarnaStandaloneWebViewEventEmitter::OnLoadProgress{
.progressEvent = {
.webViewState = {
.url = std::string([url UTF8String]),
Expand Down Expand Up @@ -139,14 +139,14 @@ - (void)handleCommand:(const NSString *)commandName args:(const NSArray *)args {

- (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didCommit:(WKNavigation * _Nonnull)navigation {
if (_eventEmitter) {
RCTLogInfo(@"Sending onBeforeLoad event");
RCTLogInfo(@"Sending onLoadStart event");
// 'estimatedProgress' is a double value in range [0..1].
// We need to convert it to an int value in range [0..100].
int progress = (int) (self.klarnaStandaloneWebView.estimatedProgress * 100);
std::dynamic_pointer_cast<const RNKlarnaStandaloneWebViewEventEmitter>(_eventEmitter)
->onBeforeLoad(RNKlarnaStandaloneWebViewEventEmitter::OnBeforeLoad{
->onLoadStart(RNKlarnaStandaloneWebViewEventEmitter::OnLoadStart{
.navigationEvent = {
.event = facebook::react::RNKlarnaStandaloneWebViewEventEmitter::OnBeforeLoadNavigationEventEvent::LoadStarted,
.event = facebook::react::RNKlarnaStandaloneWebViewEventEmitter::OnLoadStartNavigationEventEvent::LoadStarted,
.newUrl = std::string([webView.url.absoluteString UTF8String]),
.webViewState = {
.url = std::string([webView.url.absoluteString UTF8String]),
Expand Down
4 changes: 2 additions & 2 deletions ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ NS_ASSUME_NONNULL_BEGIN

@interface KlarnaStandaloneWebViewWrapper : UIView

@property (nonatomic, copy) RCTDirectEventBlock onBeforeLoad;
@property (nonatomic, copy) RCTDirectEventBlock onLoadStart;
@property (nonatomic, copy) RCTDirectEventBlock onLoad;
@property (nonatomic, copy) RCTDirectEventBlock onLoadError;
@property (nonatomic, copy) RCTDirectEventBlock onProgressChange;
@property (nonatomic, copy) RCTDirectEventBlock onLoadProgress;
@property (nonatomic, copy) RCTDirectEventBlock onKlarnaMessage;

@property (nonatomic, strong) NSString* returnUrl;
Expand Down
8 changes: 4 additions & 4 deletions ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
}

- (void)sendProgressChangeEvent:(int)progress {
if (!self.onProgressChange) {
if (!self.onLoadProgress) {
RCTLog(@"Missing 'onProgressChange' callback prop.");
return;
}
self.onProgressChange(@{
self.onLoadProgress(@{
@"progressEvent": @{
@"webViewState": @{
@"url": self.klarnaStandaloneWebView.url == nil ? @"" : self.klarnaStandaloneWebView.url.absoluteString,
Expand Down Expand Up @@ -120,12 +120,12 @@ - (void)reload {
#pragma mark - KlarnaStandaloneWebViewDelegate methods

- (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didCommit:(WKNavigation * _Nonnull)navigation {
if (!self.onBeforeLoad) {
if (!self.onLoadStart) {
RCTLog(@"Missing 'onBeforeLoad' callback prop.");
return;
}
int progress = (int) (webView.estimatedProgress * 100);
self.onBeforeLoad(@{
self.onLoadStart(@{
@"navigationEvent": @{
@"event": @"loadStarted",
@"newUrl": webView.url.absoluteString,
Expand Down
20 changes: 9 additions & 11 deletions src/KlarnaStandaloneWebView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,14 @@ import RNKlarnaStandaloneWebView, {
export interface KlarnaWebViewProps {
style?: ViewStyle;
readonly returnUrl: string;
readonly onBeforeLoad?: (
readonly onLoadStart?: (
navigationEvent: KlarnaWebViewNavigationEvent
) => void;
readonly onLoad?: (navigationEvent: KlarnaWebViewNavigationEvent) => void;
readonly onLoadError?: (
navigationError: KlarnaWebViewNavigationError
) => void;
readonly onProgressChange?: (
progressEvent: KlarnaWebViewProgressEvent
) => void;
readonly onLoadProgress?: (progressEvent: KlarnaWebViewProgressEvent) => void;
readonly onKlarnaMessage?: (
klarnaMessageEvent: KlarnaWebViewKlarnaMessageEvent
) => void;
Expand Down Expand Up @@ -51,7 +49,7 @@ export class KlarnaStandaloneWebView extends Component<
ref={this.standaloneWebViewRef}
style={this.props.style}
returnUrl={this.props.returnUrl || ''}
onBeforeLoad={(
onLoadStart={(
event: NativeSyntheticEvent<
Readonly<{
readonly navigationEvent: Readonly<{
Expand All @@ -67,8 +65,8 @@ export class KlarnaStandaloneWebView extends Component<
}>
>
) => {
if (this.props.onBeforeLoad != null) {
this.props.onBeforeLoad(event.nativeEvent.navigationEvent);
if (this.props.onLoadStart != null) {
this.props.onLoadStart(event.nativeEvent.navigationEvent);
}
}}
onLoad={(
Expand Down Expand Up @@ -104,7 +102,7 @@ export class KlarnaStandaloneWebView extends Component<
this.props.onLoadError(event.nativeEvent.navigationError);
}
}}
onProgressChange={(
onLoadProgress={(
event: NativeSyntheticEvent<
Readonly<{
readonly progressEvent: Readonly<{
Expand All @@ -118,8 +116,8 @@ export class KlarnaStandaloneWebView extends Component<
}>
>
) => {
if (this.props.onProgressChange != null) {
this.props.onProgressChange(event.nativeEvent.progressEvent);
if (this.props.onLoadProgress != null) {
this.props.onLoadProgress(event.nativeEvent.progressEvent);
}
}}
onKlarnaMessage={(
Expand Down Expand Up @@ -211,7 +209,7 @@ export interface KlarnaWebViewProgressEvent {

export interface KlarnaWebViewKlarnaMessageEvent {
readonly action: string;
// Dictionary is not support for events
// Dictionary is not supported for events
// readonly params: { [key: string]: any };
// TODO What is a KlarnaWebViewComponent?
// readonly component: KlarnaWebViewComponent;
Expand Down
6 changes: 3 additions & 3 deletions src/specs/KlarnaStandaloneWebViewNativeComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import React from 'react';

export interface RNKlarnaStandaloneWebViewProps extends ViewProps {
returnUrl: string;
onBeforeLoad: DirectEventHandler<KlarnaWebViewNavigationEvent>;
onLoadStart: DirectEventHandler<KlarnaWebViewNavigationEvent>;
onLoad: DirectEventHandler<KlarnaWebViewNavigationEvent>;
onLoadError: DirectEventHandler<KlarnaWebViewNavigationError>;
onProgressChange: DirectEventHandler<KlarnaWebViewProgressEvent>;
onLoadProgress: DirectEventHandler<KlarnaWebViewProgressEvent>;
onKlarnaMessage: DirectEventHandler<KlarnaWebViewKlarnaMessageEvent>;
/* Android only */
onRenderProcessGone: DirectEventHandler<KlarnaWebViewRenderProcessGoneEvent>;
Expand Down Expand Up @@ -53,7 +53,7 @@ type KlarnaWebViewProgressEvent = Readonly<{
type KlarnaWebViewKlarnaMessageEvent = Readonly<{
klarnaMessageEvent: Readonly<{
action: string;
// Dictionary is not support for events
// Dictionary is not supported for events
// params: { [key: string]: any };
// TODO What is a KlarnaWebViewComponent?
// component: KlarnaWebViewComponent;
Expand Down

0 comments on commit b355b6e

Please sign in to comment.