From c2057eb01bec2c7249bb0add763d951cba71de20 Mon Sep 17 00:00:00 2001 From: Nuri Mertcan Guner Date: Thu, 21 Dec 2023 15:11:53 +0100 Subject: [PATCH 1/4] change prop signatures to match rn webview --- .../KlarnaStandaloneWebViewEventSender.java | 81 +++++++----------- .../KlarnaStandaloneWebViewManager.java | 8 +- .../newarch/KlarnaStandaloneWebViewWrapper.mm | 60 +++++++------ .../oldarch/KlarnaStandaloneWebViewWrapper.mm | 85 ++++++++----------- src/KlarnaStandaloneWebView.tsx | 85 +++++++++---------- .../KlarnaStandaloneWebViewNativeComponent.ts | 47 +++++----- 6 files changed, 171 insertions(+), 195 deletions(-) diff --git a/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewEventSender.java b/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewEventSender.java index c09ae24e..97043382 100644 --- a/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewEventSender.java +++ b/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewEventSender.java @@ -3,7 +3,6 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import com.facebook.react.bridge.Arguments; import com.facebook.react.bridge.ReactContext; import com.facebook.react.bridge.ReadableMap; import com.facebook.react.bridge.WritableMap; @@ -17,8 +16,6 @@ import java.util.HashMap; 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. */ @@ -29,14 +26,14 @@ public class KlarnaStandaloneWebViewEventSender { 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_RENDER_PROCESS_GONE_EVENT = "renderProcessGoneEvent"; - 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"; + private static final String PARAM_NAME_CODE = "code"; + private static final String PARAM_NAME_DESCRIPTION = "description"; private static final String PARAM_NAME_TITLE = "title"; private static final String PARAM_NAME_URL = "url"; + private static final String PARAM_NAME_CAN_GO_BACK = "canGoBack"; + private static final String PARAM_NAME_CAN_GO_FORWARD = "canGoForward"; 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 static final String PARAM_NAME_DID_CRASH = "didCrash"; @@ -47,40 +44,48 @@ public class KlarnaStandaloneWebViewEventSender { } public void sendLoadProgressEvent(@Nullable KlarnaStandaloneWebView view, int progress) { - ReadableMap webViewStateMap = (view == null) ? Arguments.createMap() : buildWebViewStateMap(view.getUrl(), view.getTitle(), progress); + WritableMap webViewMap = buildWebViewMap(view, null); + webViewMap.putDouble(PARAM_NAME_PROGRESS, progress); WritableMap params = ArgumentsUtil.createMap(new HashMap<>() {{ - put(PARAM_NAME_PROGRESS_EVENT, webViewStateMap); + put(PARAM_NAME_PROGRESS_EVENT, webViewMap); }}); postEventForView(KlarnaStandaloneWebViewEvent.Event.ON_LOAD_PROGRESS, params, view); } - public void sendErrorEvent(@Nullable KlarnaStandaloneWebView view, String description) { - ReadableMap navigationErrorMap = ArgumentsUtil.createMap(new HashMap<>() {{ - put(PARAM_NAME_ERROR_MESSAGE, description); - }}); + public void sendErrorEvent(@Nullable KlarnaStandaloneWebView view, int code, String description) { + WritableMap webViewMap = buildWebViewMap(view, null); + webViewMap.putDouble(PARAM_NAME_CODE, code); + webViewMap.putString(PARAM_NAME_DESCRIPTION, description); WritableMap params = ArgumentsUtil.createMap(new HashMap<>() {{ - put(PARAM_NAME_NAVIGATION_ERROR, navigationErrorMap); + put(PARAM_NAME_NAVIGATION_ERROR, webViewMap); }}); postEventForView(KlarnaStandaloneWebViewEvent.Event.ON_ERROR, params, view); } - public void sendNavigationEvent(@Nullable KlarnaStandaloneWebView view, KlarnaStandaloneWebViewEvent.Event event) { + public void sendNavigationEvent(@Nullable KlarnaStandaloneWebView view, KlarnaStandaloneWebViewEvent.Event event, String url) { + WritableMap webViewMap = buildWebViewMap(view, url); WritableMap params = ArgumentsUtil.createMap(new HashMap<>() {{ - put(PARAM_NAME_NAVIGATION_EVENT, buildNavigationEventMap(view, event)); + put(PARAM_NAME_NAVIGATION_EVENT, webViewMap); }}); postEventForView(event, params, view); } public void sendKlarnaMessageEvent(@Nullable KlarnaStandaloneWebView view, @NonNull KlarnaProductEvent klarnaProductEvent) { + ReadableMap eventMap = ArgumentsUtil.createMap(new HashMap<>() {{ + put(PARAM_NAME_ACTION, klarnaProductEvent.getAction()); + }}); WritableMap params = ArgumentsUtil.createMap(new HashMap<>() {{ - put(PARAM_NAME_KLARNA_MESSAGE_EVENT, buildKlarnaMessageEventMap(klarnaProductEvent)); + put(PARAM_NAME_KLARNA_MESSAGE_EVENT, eventMap); }}); postEventForView(KlarnaStandaloneWebViewEvent.Event.ON_KLARNA_MESSAGE, params, view); } public void sendRenderProcessGoneEvent(@Nullable KlarnaStandaloneWebView view, boolean didCrash) { + ReadableMap eventMap = ArgumentsUtil.createMap(new HashMap<>() {{ + put(PARAM_NAME_DID_CRASH, didCrash); + }}); WritableMap params = ArgumentsUtil.createMap(new HashMap<>() {{ - put(PARAM_NAME_RENDER_PROCESS_GONE_EVENT, buildRenderProcessGoneEventMap(didCrash)); + put(PARAM_NAME_RENDER_PROCESS_GONE_EVENT, eventMap); }}); postEventForView(KlarnaStandaloneWebViewEvent.Event.ON_RENDER_PROCESS_GONE, params, view); } @@ -117,37 +122,17 @@ private KlarnaStandaloneWebView getKlarnaStandaloneWebView(@Nullable KlarnaStand return null; } - private ReadableMap buildWebViewStateMap(String url, String title, int progress) { - return ArgumentsUtil.createMap(new HashMap<>() {{ - put(PARAM_NAME_URL, url); - put(PARAM_NAME_TITLE, title); - put(PARAM_NAME_PROGRESS, String.valueOf(progress)); - put(PARAM_NAME_IS_LOADING, progress < 100); - }}); - } - - private ReadableMap buildNavigationEventMap(@Nullable KlarnaStandaloneWebView view, KlarnaStandaloneWebViewEvent.Event event) { - if (view == null) { - return Arguments.createMap(); - } else { - return ArgumentsUtil.createMap(new HashMap<>() {{ - // Possible values for 'event' are 'willLoad', 'loadStarted', and '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())); - }}); - } - } - - private ReadableMap buildKlarnaMessageEventMap(KlarnaProductEvent klarnaProductEvent) { - return ArgumentsUtil.createMap(new HashMap<>() {{ - put(PARAM_NAME_ACTION, klarnaProductEvent.getAction()); - }}); - } - - private ReadableMap buildRenderProcessGoneEventMap(boolean didCrash) { + private WritableMap buildWebViewMap(KlarnaStandaloneWebView webView, String url) { return ArgumentsUtil.createMap(new HashMap<>() {{ - put(PARAM_NAME_DID_CRASH, didCrash); + if (webView != null) { + put(PARAM_NAME_URL, url != null ? url : webView.getUrl()); + put(PARAM_NAME_TITLE, webView.getTitle()); + put(PARAM_NAME_IS_LOADING, webView.getProgress() < 100); + put(PARAM_NAME_CAN_GO_BACK, webView.canGoBack()); + put(PARAM_NAME_CAN_GO_FORWARD, webView.canGoForward()); + } else { + put(PARAM_NAME_URL, url); + } }}); } 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 97754e96..9ec24d0d 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 @@ -60,24 +60,24 @@ 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_LOAD_START); + klarnaStandaloneWebViewEventSender.sendNavigationEvent(view, KlarnaStandaloneWebViewEvent.Event.ON_LOAD_START, url); } @Override public void onPageFinished(@Nullable KlarnaStandaloneWebView view, @Nullable String url) { - klarnaStandaloneWebViewEventSender.sendNavigationEvent(view, KlarnaStandaloneWebViewEvent.Event.ON_LOAD); + klarnaStandaloneWebViewEventSender.sendNavigationEvent(view, KlarnaStandaloneWebViewEvent.Event.ON_LOAD, url); } @Override public void onReceivedError(@Nullable KlarnaStandaloneWebView view, @Nullable WebResourceRequest request, @Nullable WebResourceError error) { if (error != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { - klarnaStandaloneWebViewEventSender.sendErrorEvent(view, error.getDescription().toString()); + klarnaStandaloneWebViewEventSender.sendErrorEvent(view, error.getErrorCode(), error.getDescription().toString()); } } @Override public void onReceivedError(@Nullable KlarnaStandaloneWebView view, int errorCode, @Nullable String description, @Nullable String failingUrl) { - klarnaStandaloneWebViewEventSender.sendErrorEvent(view, description); + klarnaStandaloneWebViewEventSender.sendErrorEvent(view, errorCode, description); } @Override diff --git a/ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm b/ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm index f0c5eba8..d52a6116 100644 --- a/ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm +++ b/ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm @@ -60,12 +60,12 @@ - (void)sendProgressChangeEvent:(int)progress { std::dynamic_pointer_cast(_eventEmitter) ->onLoadProgress(RNKlarnaStandaloneWebViewEventEmitter::OnLoadProgress{ .progressEvent = { - .webViewState = { - .url = std::string([url UTF8String]), - .title = std::string([title UTF8String]), - .progress = std::string([[@(progress) stringValue] UTF8String]), - .isLoading = self.klarnaStandaloneWebView.isLoading, - } + .url = std::string([url UTF8String]), + .title = std::string([title UTF8String]), + .loading = self.klarnaStandaloneWebView.isLoading, + .canGoBack = self.klarnaStandaloneWebView.canGoBack, + .canGoForward = self.klarnaStandaloneWebView.canGoForward, + .progress = (double)progress } }); } else { @@ -142,18 +142,14 @@ - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didC 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(_eventEmitter) ->onLoadStart(RNKlarnaStandaloneWebViewEventEmitter::OnLoadStart{ .navigationEvent = { - .event = facebook::react::RNKlarnaStandaloneWebViewEventEmitter::OnLoadStartNavigationEventEvent::LoadStarted, - .newUrl = std::string([webView.url.absoluteString UTF8String]), - .webViewState = { - .url = std::string([webView.url.absoluteString UTF8String]), - .title = std::string([webView.title UTF8String]), - .progress = std::string([[@(progress) stringValue] UTF8String]), - .isLoading = webView.isLoading, - } + .url = std::string([webView.url.absoluteString UTF8String]), + .title = std::string([webView.title UTF8String]), + .loading = self.klarnaStandaloneWebView.isLoading, + .canGoBack = self.klarnaStandaloneWebView.canGoBack, + .canGoForward = self.klarnaStandaloneWebView.canGoForward, } }); } else { @@ -166,18 +162,14 @@ - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didF RCTLogInfo(@"Sending onLoad 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(_eventEmitter) ->onLoad(RNKlarnaStandaloneWebViewEventEmitter::OnLoad{ .navigationEvent = { - .event = facebook::react::RNKlarnaStandaloneWebViewEventEmitter::OnLoadNavigationEventEvent::LoadEnded, - .newUrl = std::string([webView.url.absoluteString UTF8String]), - .webViewState = { - .url = std::string([webView.url.absoluteString UTF8String]), - .title = std::string([webView.title UTF8String]), - .progress = std::string([[@(progress) stringValue] UTF8String]), - .isLoading = webView.isLoading, - } + .url = std::string([webView.url.absoluteString UTF8String]), + .title = std::string([webView.title UTF8String]), + .loading = self.klarnaStandaloneWebView.isLoading, + .canGoBack = self.klarnaStandaloneWebView.canGoBack, + .canGoForward = self.klarnaStandaloneWebView.canGoForward, } }); } else { @@ -190,8 +182,14 @@ - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didF RCTLogInfo(@"Sending onError event"); std::dynamic_pointer_cast(_eventEmitter) ->onError(RNKlarnaStandaloneWebViewEventEmitter::OnError{ - .navigationError = { - .errorMessage = std::string([[error localizedDescription] UTF8String]), + .error = { + .url = std::string([webView.url.absoluteString UTF8String]), + .title = std::string([webView.title UTF8String]), + .loading = self.klarnaStandaloneWebView.isLoading, + .canGoBack = self.klarnaStandaloneWebView.canGoBack, + .canGoForward = self.klarnaStandaloneWebView.canGoForward, + .code = (int)error.code, + .description = std::string([[error localizedDescription] UTF8String]) } }); } else { @@ -204,8 +202,14 @@ - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didF RCTLogInfo(@"Sending onError event"); std::dynamic_pointer_cast(_eventEmitter) ->onError(RNKlarnaStandaloneWebViewEventEmitter::OnError{ - .navigationError = { - .errorMessage = std::string([[error localizedDescription] UTF8String]), + .error = { + .url = std::string([webView.url.absoluteString UTF8String]), + .title = std::string([webView.title UTF8String]), + .loading = self.klarnaStandaloneWebView.isLoading, + .canGoBack = self.klarnaStandaloneWebView.canGoBack, + .canGoForward = self.klarnaStandaloneWebView.canGoForward, + .code = (int)error.code, + .description = std::string([[error localizedDescription] UTF8String]) } }); } else { diff --git a/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm b/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm index b9c14189..45005412 100644 --- a/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm +++ b/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm @@ -35,27 +35,23 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N NSNumber * newProgress = [change objectForKey:NSKeyValueChangeNewKey]; // We need to convert it to an int value in range [0..100] int progress = (int) (newProgress.doubleValue * 100); - [self sendProgressChangeEvent:progress]; + [self sendProgressChangeEvent:self.klarnaStandaloneWebView progress:progress]; } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } -- (void)sendProgressChangeEvent:(int)progress { +- (void)sendProgressChangeEvent:(KlarnaStandaloneWebView * _Nonnull)webView progress:(int)progress { if (!self.onLoadProgress) { RCTLog(@"Missing 'onProgressChange' callback prop."); return; } - self.onLoadProgress(@{ - @"progressEvent": @{ - @"webViewState": @{ - @"url": self.klarnaStandaloneWebView.url == nil ? @"" : self.klarnaStandaloneWebView.url.absoluteString, - @"title": self.klarnaStandaloneWebView.title == nil ? @"" : self.klarnaStandaloneWebView.title, - @"progress": [[NSNumber numberWithInt:progress] stringValue], - @"isLoading": @(self.klarnaStandaloneWebView.isLoading) - } - } - }); + NSMutableDictionary *event = [self webViewDict:webView]; + [event addEntriesFromDictionary:@{ + @"progress": [NSNumber numberWithDouble: progress] // could use self.klarnaStandaloneWebView.estimatedProgress + }]; + + self.onLoadProgress(@{@"progressEvent": event}); } @@ -124,19 +120,8 @@ - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didC RCTLog(@"Missing 'onBeforeLoad' callback prop."); return; } - int progress = (int) (webView.estimatedProgress * 100); - self.onLoadStart(@{ - @"navigationEvent": @{ - @"event": @"loadStarted", - @"newUrl": webView.url.absoluteString, - @"webViewState": @{ - @"url": webView.url.absoluteString, - @"title": webView.title, - @"progress": [[NSNumber numberWithInt:progress] stringValue], - @"isLoading": @(webView.isLoading) - } - } - }); + NSMutableDictionary *event = [self webViewDict:webView]; + self.onLoadStart(@{@"navigationEvent": event}); } - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didFinish:(WKNavigation * _Nonnull)navigation { @@ -144,19 +129,8 @@ - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didF RCTLog(@"Missing 'onLoad' callback prop."); return; } - int progress = (int) (webView.estimatedProgress * 100); - self.onLoad(@{ - @"navigationEvent": @{ - @"event": @"loadEnded", - @"newUrl": webView.url.absoluteString, - @"webViewState": @{ - @"url": webView.url.absoluteString, - @"title": webView.title, - @"progress": [[NSNumber numberWithInt:progress] stringValue], - @"isLoading": @(webView.isLoading) - } - } - }); + NSMutableDictionary *event = [self webViewDict:webView]; + self.onLoad(@{@"navigationEvent": event}); } - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didFailProvisionalNavigation:(WKNavigation * _Nonnull)navigation withError:(NSError * _Nonnull)error { @@ -164,11 +138,12 @@ - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didF RCTLog(@"Missing 'onError' callback prop."); return; } - self.onError(@{ - @"navigationError": @{ - @"errorMessage": error.description, - } - }); + NSMutableDictionary *event = [self webViewDict:webView]; + [event addEntriesFromDictionary:@{ + @"code": @(error.code), + @"description": error.description, + }]; + self.onError(@{@"error": event}); } - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didFail:(WKNavigation * _Nonnull)navigation withError:(NSError * _Nonnull)error { @@ -176,11 +151,12 @@ - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didF RCTLog(@"Missing 'onError' callback prop."); return; } - self.onError(@{ - @"navigationError": @{ - @"errorMessage": error.description, - } - }); + NSMutableDictionary *event = [self webViewDict:webView]; + [event addEntriesFromDictionary:@{ + @"code": @(error.code), + @"description": error.description, + }]; + self.onError(@{@"error": error}); } #pragma mark - KlarnaEventHandler methods @@ -201,6 +177,19 @@ - (void)klarnaComponent:(id _Nonnull)klarnaComponent encounter // Not used as of now } +#pragma mark - Events + +- (NSMutableDictionary *)webViewDict:(KlarnaStandaloneWebView * _Nonnull)webView { + NSDictionary *event = @{ + @"url": webView.url.absoluteString ?: @"", + @"title": webView.title ?: @"", + @"loading" : @(webView.isLoading), + @"canGoBack": @(webView.canGoBack), + @"canGoForward" : @(webView.canGoForward) + }; + return [[NSMutableDictionary alloc] initWithDictionary: event]; +} + @end #endif diff --git a/src/KlarnaStandaloneWebView.tsx b/src/KlarnaStandaloneWebView.tsx index a7a89117..bf13ae8c 100644 --- a/src/KlarnaStandaloneWebView.tsx +++ b/src/KlarnaStandaloneWebView.tsx @@ -8,6 +8,7 @@ import RNKlarnaStandaloneWebView, { RNKlarnaStandaloneWebViewCommands, type RNKlarnaStandaloneWebViewProps, } from './specs/KlarnaStandaloneWebViewNativeComponent'; +import type { Double } from 'react-native/Libraries/Types/CodegenTypes'; export interface KlarnaWebViewProps { style?: ViewStyle; @@ -16,7 +17,7 @@ export interface KlarnaWebViewProps { navigationEvent: KlarnaWebViewNavigationEvent ) => void; readonly onLoad?: (navigationEvent: KlarnaWebViewNavigationEvent) => void; - readonly onError?: (navigationError: KlarnaWebViewNavigationError) => void; + readonly onError?: (navigationError: KlarnaWebViewError) => void; readonly onLoadProgress?: (progressEvent: KlarnaWebViewProgressEvent) => void; readonly onKlarnaMessage?: ( klarnaMessageEvent: KlarnaWebViewKlarnaMessageEvent @@ -51,14 +52,11 @@ export class KlarnaStandaloneWebView extends Component< event: NativeSyntheticEvent< Readonly<{ readonly navigationEvent: Readonly<{ - readonly event: 'willLoad' | 'loadStarted' | 'loadEnded'; - readonly newUrl: string; - readonly webViewState: Readonly<{ - readonly url: string; - readonly title: string; - readonly progress: string; - readonly isLoading: boolean; - }>; + readonly url: string; + readonly loading: boolean; + readonly title: string; + readonly canGoBack: boolean; + readonly canGoForward: boolean; }>; }> > @@ -71,14 +69,11 @@ export class KlarnaStandaloneWebView extends Component< event: NativeSyntheticEvent< Readonly<{ readonly navigationEvent: Readonly<{ - readonly event: 'willLoad' | 'loadStarted' | 'loadEnded'; - readonly newUrl: string; - readonly webViewState: Readonly<{ - readonly url: string; - readonly title: string; - readonly progress: string; - readonly isLoading: boolean; - }>; + readonly url: string; + readonly loading: boolean; + readonly title: string; + readonly canGoBack: boolean; + readonly canGoForward: boolean; }>; }> > @@ -90,26 +85,32 @@ export class KlarnaStandaloneWebView extends Component< onError={( event: NativeSyntheticEvent< Readonly<{ - readonly navigationError: Readonly<{ - readonly errorMessage: string; + readonly error: Readonly<{ + readonly url: string; + readonly loading: boolean; + readonly title: string; + readonly canGoBack: boolean; + readonly canGoForward: boolean; + readonly code: number; + readonly description: string; }>; }> > ) => { if (this.props.onError != null) { - this.props.onError(event.nativeEvent.navigationError); + this.props.onError(event.nativeEvent.error); } }} onLoadProgress={( event: NativeSyntheticEvent< Readonly<{ readonly progressEvent: Readonly<{ - readonly webViewState: Readonly<{ - readonly url: string; - readonly title: string; - readonly progress: string; - readonly isLoading: boolean; - }>; + readonly url: string; + readonly loading: boolean; + readonly title: string; + readonly canGoBack: boolean; + readonly canGoForward: boolean; + readonly progress: Double; }>; }> > @@ -181,28 +182,24 @@ export class KlarnaStandaloneWebView extends Component< }; } -export interface KlarnaWebViewNavigationEvent { - readonly event: 'willLoad' | 'loadStarted' | 'loadEnded'; - readonly newUrl: string; - readonly webViewState: Readonly<{ - readonly url: string; - readonly title: string; - readonly progress: string; - readonly isLoading: boolean; - }>; +export interface KlarnaWebViewNativeEvent { + readonly url: string; + readonly loading: boolean; + readonly title: string; + readonly canGoBack: boolean; + readonly canGoForward: boolean; } -export interface KlarnaWebViewNavigationError { - readonly errorMessage: string; +export interface KlarnaWebViewNavigationEvent + extends KlarnaWebViewNativeEvent {} + +export interface KlarnaWebViewError extends KlarnaWebViewNativeEvent { + readonly code: number; + readonly description: string; } -export interface KlarnaWebViewProgressEvent { - readonly webViewState: Readonly<{ - readonly url: string; - readonly title: string; - readonly progress: string; - readonly isLoading: boolean; - }>; +export interface KlarnaWebViewProgressEvent extends KlarnaWebViewNativeEvent { + readonly progress: number; } export interface KlarnaWebViewKlarnaMessageEvent { diff --git a/src/specs/KlarnaStandaloneWebViewNativeComponent.ts b/src/specs/KlarnaStandaloneWebViewNativeComponent.ts index 7a077fff..b58069e2 100644 --- a/src/specs/KlarnaStandaloneWebViewNativeComponent.ts +++ b/src/specs/KlarnaStandaloneWebViewNativeComponent.ts @@ -4,49 +4,50 @@ import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNati import codegenNativeCommands from 'react-native/Libraries/Utilities/codegenNativeCommands'; import type { DirectEventHandler } from 'react-native/Libraries/Types/CodegenTypes'; import React from 'react'; +import type { Double, Int32 } from 'react-native/Libraries/Types/CodegenTypes'; export interface RNKlarnaStandaloneWebViewProps extends ViewProps { returnUrl: string; onLoadStart: DirectEventHandler; onLoad: DirectEventHandler; - onError: DirectEventHandler; + onError: DirectEventHandler; onLoadProgress: DirectEventHandler; onKlarnaMessage: DirectEventHandler; /* Android only */ onRenderProcessGone: DirectEventHandler; /* End of Android only */ } -type KlarnaWebViewNavigationEvent = Readonly<{ + +export type KlarnaWebViewNavigationEvent = Readonly<{ navigationEvent: Readonly<{ - event: 'willLoad' | 'loadStarted' | 'loadEnded'; - newUrl: string; - webViewState: Readonly<{ - url: string; - title: string; - // Number is not supported for events. So for now we pass 'progress' as a string. - // 'progress' is a number is range [0..100] - progress: string; - isLoading: boolean; - }>; + url: string; + loading: boolean; + title: string; + canGoBack: boolean; + canGoForward: boolean; }>; }>; -// TODO Add the fields when the definition is known. For now we just add an error message -type KlarnaWebViewNavigationError = Readonly<{ - navigationError: Readonly<{ - errorMessage: string; +type KlarnaWebViewError = Readonly<{ + error: Readonly<{ + url: string; + loading: boolean; + title: string; + canGoBack: boolean; + canGoForward: boolean; + code: Int32; + description: string; }>; }>; type KlarnaWebViewProgressEvent = Readonly<{ progressEvent: Readonly<{ - webViewState: Readonly<{ - url: string; - title: string; - // Number is not supported for events - progress: string; - isLoading: boolean; - }>; + url: string; + loading: boolean; + title: string; + canGoBack: boolean; + canGoForward: boolean; + progress: Double; }>; }>; From 40612d4f0a5388caf92ed41ab6ade78acd8265f4 Mon Sep 17 00:00:00 2001 From: Masoud Fallahpourbaee Date: Fri, 22 Dec 2023 10:49:04 +0100 Subject: [PATCH 2/4] Change param name `isLoading` to `loading` --- .../standalonewebview/KlarnaStandaloneWebViewEventSender.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewEventSender.java b/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewEventSender.java index 97043382..82b02225 100644 --- a/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewEventSender.java +++ b/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewEventSender.java @@ -33,7 +33,7 @@ public class KlarnaStandaloneWebViewEventSender { private static final String PARAM_NAME_CAN_GO_BACK = "canGoBack"; private static final String PARAM_NAME_CAN_GO_FORWARD = "canGoForward"; private static final String PARAM_NAME_PROGRESS = "progress"; - private static final String PARAM_NAME_IS_LOADING = "isLoading"; + private static final String PARAM_NAME_LOADING = "loading"; private static final String PARAM_NAME_ACTION = "action"; private static final String PARAM_NAME_DID_CRASH = "didCrash"; @@ -127,7 +127,7 @@ private WritableMap buildWebViewMap(KlarnaStandaloneWebView webView, String url) if (webView != null) { put(PARAM_NAME_URL, url != null ? url : webView.getUrl()); put(PARAM_NAME_TITLE, webView.getTitle()); - put(PARAM_NAME_IS_LOADING, webView.getProgress() < 100); + put(PARAM_NAME_LOADING, webView.getProgress() < 100); put(PARAM_NAME_CAN_GO_BACK, webView.canGoBack()); put(PARAM_NAME_CAN_GO_FORWARD, webView.canGoForward()); } else { From 4b5de26cd866b56b3f9ba4d8f117564977604f53 Mon Sep 17 00:00:00 2001 From: Masoud Fallahpourbaee Date: Fri, 22 Dec 2023 11:03:44 +0100 Subject: [PATCH 3/4] Rename some methods --- ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm | 4 ++-- ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm b/ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm index d52a6116..4c119883 100644 --- a/ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm +++ b/ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm @@ -46,13 +46,13 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N NSNumber * newProgress = [change objectForKey:NSKeyValueChangeNewKey]; // We need to convert it to an int value in range [0..100] int progress = [NSNumber numberWithDouble:(newProgress.doubleValue * 100)].intValue; - [self sendProgressChangeEvent:progress]; + [self sendLoadProgressEvent:progress]; } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } -- (void)sendProgressChangeEvent:(int)progress { +- (void)sendLoadProgressEvent:(int)progress { if (_eventEmitter) { RCTLogInfo(@"Sending onLoadProgress event"); NSString * url = self.klarnaStandaloneWebView.url == nil ? @"" : self.klarnaStandaloneWebView.url.absoluteString; diff --git a/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm b/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm index 45005412..54c66e52 100644 --- a/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm +++ b/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm @@ -35,13 +35,13 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N NSNumber * newProgress = [change objectForKey:NSKeyValueChangeNewKey]; // We need to convert it to an int value in range [0..100] int progress = (int) (newProgress.doubleValue * 100); - [self sendProgressChangeEvent:self.klarnaStandaloneWebView progress:progress]; + [self sendLoadProgressEvent:self.klarnaStandaloneWebView progress:progress]; } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; } } -- (void)sendProgressChangeEvent:(KlarnaStandaloneWebView * _Nonnull)webView progress:(int)progress { +- (void)sendLoadProgressEvent:(KlarnaStandaloneWebView * _Nonnull)webView progress:(int)progress { if (!self.onLoadProgress) { RCTLog(@"Missing 'onProgressChange' callback prop."); return; @@ -117,7 +117,7 @@ - (void)reload { - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didCommit:(WKNavigation * _Nonnull)navigation { if (!self.onLoadStart) { - RCTLog(@"Missing 'onBeforeLoad' callback prop."); + RCTLog(@"Missing 'onLoadStart' callback prop."); return; } NSMutableDictionary *event = [self webViewDict:webView]; From aefac65121b73b2cfddee2394597cbc5cf3100b3 Mon Sep 17 00:00:00 2001 From: Masoud Fallahpourbaee Date: Fri, 22 Dec 2023 11:22:33 +0100 Subject: [PATCH 4/4] Rename event 'onLoad' to 'onLoadEnd' --- TestApp/src/standalonewebview/StandaloneWebViewScreen.tsx | 8 ++++---- .../standalonewebview/KlarnaStandaloneWebViewEvent.java | 2 +- .../standalonewebview/KlarnaStandaloneWebViewManager.java | 4 ++-- ios/Sources/KlarnaStandaloneWebViewManager.mm | 2 +- .../view/newarch/KlarnaStandaloneWebViewWrapper.mm | 4 ++-- ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.h | 2 +- .../view/oldarch/KlarnaStandaloneWebViewWrapper.mm | 6 +++--- src/KlarnaStandaloneWebView.tsx | 8 ++++---- src/specs/KlarnaStandaloneWebViewNativeComponent.ts | 2 +- 9 files changed, 19 insertions(+), 19 deletions(-) diff --git a/TestApp/src/standalonewebview/StandaloneWebViewScreen.tsx b/TestApp/src/standalonewebview/StandaloneWebViewScreen.tsx index 78c73096..9eca2da9 100644 --- a/TestApp/src/standalonewebview/StandaloneWebViewScreen.tsx +++ b/TestApp/src/standalonewebview/StandaloneWebViewScreen.tsx @@ -4,7 +4,7 @@ import React, {useRef, useState} from 'react'; import { KlarnaStandaloneWebView, KlarnaWebViewKlarnaMessageEvent, - KlarnaWebViewNavigationError, + KlarnaWebViewError, KlarnaWebViewNavigationEvent, KlarnaWebViewProgressEvent, KlarnaWebViewRenderProcessGoneEvent, @@ -122,10 +122,10 @@ export default function StandaloneWebViewScreen() { onLoadStart={(event: KlarnaWebViewNavigationEvent) => { onEvent('onLoadStart', JSON.stringify(event)); }} - onLoad={(event: KlarnaWebViewNavigationEvent) => { - onEvent('onLoad', JSON.stringify(event)); + onLoadEnd={(event: KlarnaWebViewNavigationEvent) => { + onEvent('onLoadEnd', JSON.stringify(event)); }} - onError={(event: KlarnaWebViewNavigationError) => { + onError={(event: KlarnaWebViewError) => { onEvent('onError', JSON.stringify(event)); }} onLoadProgress={(event: KlarnaWebViewProgressEvent) => { diff --git a/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewEvent.java b/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewEvent.java index 5139e9b9..a2394f83 100644 --- a/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewEvent.java +++ b/android/src/main/java/com/klarna/mobile/sdk/reactnative/standalonewebview/KlarnaStandaloneWebViewEvent.java @@ -21,7 +21,7 @@ public enum Event { // This event is sent just before loading a URL ON_LOAD_START("onLoadStart"), // This event is sent when loading a URL is done - ON_LOAD("onLoad"), + ON_LOAD_END("onLoadEnd"), // This event is sent when loading a URL encounters an error ON_ERROR("onError"), // This event is sent when the progress of loading a page changes 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 9ec24d0d..2454297e 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 @@ -65,7 +65,7 @@ public void onPageStarted(@Nullable KlarnaStandaloneWebView view, @Nullable Stri @Override public void onPageFinished(@Nullable KlarnaStandaloneWebView view, @Nullable String url) { - klarnaStandaloneWebViewEventSender.sendNavigationEvent(view, KlarnaStandaloneWebViewEvent.Event.ON_LOAD, url); + klarnaStandaloneWebViewEventSender.sendNavigationEvent(view, KlarnaStandaloneWebViewEvent.Event.ON_LOAD_END, url); } @Override @@ -174,7 +174,7 @@ public void reload(KlarnaStandaloneWebView view) { public Map getExportedCustomDirectEventTypeConstants() { MapBuilder.Builder builder = MapBuilder.builder(); 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_END.name, MapBuilder.of("registrationName", KlarnaStandaloneWebViewEvent.Event.ON_LOAD_END.name)); builder.put(KlarnaStandaloneWebViewEvent.Event.ON_ERROR.name, MapBuilder.of("registrationName", KlarnaStandaloneWebViewEvent.Event.ON_ERROR.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)); diff --git a/ios/Sources/KlarnaStandaloneWebViewManager.mm b/ios/Sources/KlarnaStandaloneWebViewManager.mm index 9940cd4b..1695a85f 100644 --- a/ios/Sources/KlarnaStandaloneWebViewManager.mm +++ b/ios/Sources/KlarnaStandaloneWebViewManager.mm @@ -17,7 +17,7 @@ @implementation KlarnaStandaloneWebViewManager RCT_EXPORT_VIEW_PROPERTY(returnUrl, NSString) RCT_EXPORT_VIEW_PROPERTY(onLoadStart, RCTDirectEventBlock) -RCT_EXPORT_VIEW_PROPERTY(onLoad, RCTDirectEventBlock) +RCT_EXPORT_VIEW_PROPERTY(onLoadEnd, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(onError, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(onLoadProgress, RCTDirectEventBlock) RCT_EXPORT_VIEW_PROPERTY(onKlarnaMessage, RCTDirectEventBlock) diff --git a/ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm b/ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm index 4c119883..1d1b0040 100644 --- a/ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm +++ b/ios/Sources/view/newarch/KlarnaStandaloneWebViewWrapper.mm @@ -159,11 +159,11 @@ - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didC - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didFinish:(WKNavigation * _Nonnull)navigation { if (_eventEmitter) { - RCTLogInfo(@"Sending onLoad event"); + RCTLogInfo(@"Sending onLoadEnd event"); // 'estimatedProgress' is a double value in range [0..1]. // We need to convert it to an int value in range [0..100]. std::dynamic_pointer_cast(_eventEmitter) - ->onLoad(RNKlarnaStandaloneWebViewEventEmitter::OnLoad{ + ->onLoadEnd(RNKlarnaStandaloneWebViewEventEmitter::OnLoadEnd{ .navigationEvent = { .url = std::string([webView.url.absoluteString UTF8String]), .title = std::string([webView.title UTF8String]), diff --git a/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.h b/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.h index 7ef04ef1..acefe5ea 100644 --- a/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.h +++ b/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.h @@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN @interface KlarnaStandaloneWebViewWrapper : UIView @property (nonatomic, copy) RCTDirectEventBlock onLoadStart; -@property (nonatomic, copy) RCTDirectEventBlock onLoad; +@property (nonatomic, copy) RCTDirectEventBlock onLoadEnd; @property (nonatomic, copy) RCTDirectEventBlock onError; @property (nonatomic, copy) RCTDirectEventBlock onLoadProgress; @property (nonatomic, copy) RCTDirectEventBlock onKlarnaMessage; diff --git a/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm b/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm index 54c66e52..9c545012 100644 --- a/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm +++ b/ios/Sources/view/oldarch/KlarnaStandaloneWebViewWrapper.mm @@ -125,12 +125,12 @@ - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didC } - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didFinish:(WKNavigation * _Nonnull)navigation { - if (!self.onLoad) { - RCTLog(@"Missing 'onLoad' callback prop."); + if (!self.onLoadEnd) { + RCTLog(@"Missing 'onLoadEnd' callback prop."); return; } NSMutableDictionary *event = [self webViewDict:webView]; - self.onLoad(@{@"navigationEvent": event}); + self.onLoadEnd(@{@"navigationEvent": event}); } - (void)klarnaStandaloneWebView:(KlarnaStandaloneWebView * _Nonnull)webView didFailProvisionalNavigation:(WKNavigation * _Nonnull)navigation withError:(NSError * _Nonnull)error { diff --git a/src/KlarnaStandaloneWebView.tsx b/src/KlarnaStandaloneWebView.tsx index bf13ae8c..aab2ef2d 100644 --- a/src/KlarnaStandaloneWebView.tsx +++ b/src/KlarnaStandaloneWebView.tsx @@ -16,7 +16,7 @@ export interface KlarnaWebViewProps { readonly onLoadStart?: ( navigationEvent: KlarnaWebViewNavigationEvent ) => void; - readonly onLoad?: (navigationEvent: KlarnaWebViewNavigationEvent) => void; + readonly onLoadEnd?: (navigationEvent: KlarnaWebViewNavigationEvent) => void; readonly onError?: (navigationError: KlarnaWebViewError) => void; readonly onLoadProgress?: (progressEvent: KlarnaWebViewProgressEvent) => void; readonly onKlarnaMessage?: ( @@ -65,7 +65,7 @@ export class KlarnaStandaloneWebView extends Component< this.props.onLoadStart(event.nativeEvent.navigationEvent); } }} - onLoad={( + onLoadEnd={( event: NativeSyntheticEvent< Readonly<{ readonly navigationEvent: Readonly<{ @@ -78,8 +78,8 @@ export class KlarnaStandaloneWebView extends Component< }> > ) => { - if (this.props.onLoad != null) { - this.props.onLoad(event.nativeEvent.navigationEvent); + if (this.props.onLoadEnd != null) { + this.props.onLoadEnd(event.nativeEvent.navigationEvent); } }} onError={( diff --git a/src/specs/KlarnaStandaloneWebViewNativeComponent.ts b/src/specs/KlarnaStandaloneWebViewNativeComponent.ts index b58069e2..837b03c2 100644 --- a/src/specs/KlarnaStandaloneWebViewNativeComponent.ts +++ b/src/specs/KlarnaStandaloneWebViewNativeComponent.ts @@ -9,7 +9,7 @@ import type { Double, Int32 } from 'react-native/Libraries/Types/CodegenTypes'; export interface RNKlarnaStandaloneWebViewProps extends ViewProps { returnUrl: string; onLoadStart: DirectEventHandler; - onLoad: DirectEventHandler; + onLoadEnd: DirectEventHandler; onError: DirectEventHandler; onLoadProgress: DirectEventHandler; onKlarnaMessage: DirectEventHandler;