Skip to content

Commit

Permalink
feat: add inline in-app support
Browse files Browse the repository at this point in the history
refactor: remove unnecessary logging

MV-332
  • Loading branch information
biancalui-emarsys committed Dec 12, 2023
1 parent 2834e26 commit f3793af
Show file tree
Hide file tree
Showing 10 changed files with 212 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.emarsys.rnwrapper;

import android.util.Log;

import com.emarsys.Emarsys;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.LifecycleEventListener;
Expand Down Expand Up @@ -72,7 +70,6 @@ private void sendEvents() {
}

public void provideReactContext(ReactContext reactContext) {
Log.d(this.getClass().getSimpleName(), "Registered for events.");
this.reactContext = reactContext;
eventEmitter = reactContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class);
reactContext.addLifecycleEventListener(this);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.emarsys.rnwrapper;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.emarsys.inapp.ui.InlineInAppView;
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableArray;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.common.MapBuilder;
import com.facebook.react.uimanager.SimpleViewManager;
import com.facebook.react.uimanager.ThemedReactContext;
import com.facebook.react.uimanager.events.RCTEventEmitter;

import java.util.Map;

public class RNEmarsysInlineInAppViewManager extends SimpleViewManager<InlineInAppView> {

private static final String TAG = "RNEmarsysInlineInAppView";
private final int COMMAND_LOAD_IN_APP = 1;

private final ReactApplicationContext reactContext;

public RNEmarsysInlineInAppViewManager(ReactApplicationContext reactContext) {
this.reactContext = reactContext;
}

@Override
public String getName() { return TAG; }

@Override
public InlineInAppView createViewInstance(ThemedReactContext context) {
InlineInAppView view = new InlineInAppView(reactContext);
view.setOnAppEventListener((property, json) -> {
WritableMap event = Arguments.createMap();
event.putString("eventName", property);
event.putMap("payload", MapUtil.jsonToWritableMap(json).getMap("payload"));
reactContext.getJSModule(RCTEventEmitter.class)
.receiveEvent(view.getId(), "onAppEvent", event);
return null;
});
view.setOnCompletionListener((error) -> {
WritableMap event = Arguments.createMap();
event.putString("error", error == null ? null : error.getLocalizedMessage());
reactContext.getJSModule(RCTEventEmitter.class)
.receiveEvent(view.getId(), "onCompleted", event);
});
view.setOnCloseListener(() -> {
reactContext.getJSModule(RCTEventEmitter.class)
.receiveEvent(view.getId(), "onClose", null);
return null;
});
return view;
}

public Map getExportedCustomBubblingEventTypeConstants() {
return MapBuilder.builder().put(
"onAppEvent",
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of("bubbled", "onAppEvent")
)
).put(
"onCompleted",
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of("bubbled", "onCompleted")
)
).put(
"onClose",
MapBuilder.of(
"phasedRegistrationNames",
MapBuilder.of("bubbled", "onClose")
)
).build();
}

@Override
public Map<String, Integer> getCommandsMap() {
return MapBuilder.of("loadInApp", COMMAND_LOAD_IN_APP);
}

@Override
public void receiveCommand(
@NonNull InlineInAppView root,
String commandId,
@Nullable ReadableArray args
) {
super.receiveCommand(root, commandId, args);

int commandIdInt = Integer.parseInt(commandId);
switch (commandIdInt) {
case COMMAND_LOAD_IN_APP:
String viewId = args.getString(0);
root.loadInApp(viewId);
break;
default: { }
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,6 @@ private Product convertMapToProduct(ReadableMap map) {
ReadableType type = map.getType(key);
switch (type) {
case Null:
Log.d("Logs", "Received null in map");
break;
case Boolean:
available = map.getBoolean(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class RNEmarsysWrapperPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Arrays.<NativeModule>asList(
return Arrays.asList(
new RNEmarsysWrapperModule(reactContext),
new RNEmarsysPushWrapperModule(reactContext),
new RNEmarsysInAppWrapperModule(reactContext),
Expand All @@ -23,13 +23,9 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactConte
new RNEmarsysPredictWrapperModule(reactContext));
}

// Deprecated from RN 0.47
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}

@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
return Arrays.asList(
new RNEmarsysInlineInAppViewManager(reactContext));
}
}
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NativeModules, NativeEventEmitter } from 'react-native';
const { RNEmarsysWrapper } = NativeModules;

import Push from './src/push';
import InApp from './src/inapp';
import InApp, { InlineInAppView } from './src/inapp';
import Inbox from './src/inbox';
import Predict from './src/predict';
import Geofence from './src/geofence';
Expand Down Expand Up @@ -124,6 +124,7 @@ const Emarsys = {

push: Push,
inApp: InApp,
InlineInAppView,
inbox: Inbox,
predict: Predict,
geofence: Geofence,
Expand Down
1 change: 0 additions & 1 deletion ios/MapUtil.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ + (NSString*) toJsonString:(NSDictionary *)dict WithPrettyPrint:(BOOL) prettyPri
error:&error];

if (! jsonData) {
NSLog(@"error");
return @"{}";
} else {
return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
Expand Down
17 changes: 17 additions & 0 deletions ios/RNEmarsysInlineInAppViewManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#import <React/RCTViewManager.h>
#import <React/RCTUIManager.h>

#import "EMSInlineInAppView.h"

@interface RNEmarsysInlineInAppView: EMSInlineInAppView

@property (nonatomic, copy) RCTBubblingEventBlock onAppEvent;
@property (nonatomic, copy) RCTBubblingEventBlock onCompleted;
@property (nonatomic, copy) RCTBubblingEventBlock onClose;

@end


@interface RNEmarsysInlineInAppViewManager : RCTViewManager

@end
42 changes: 42 additions & 0 deletions ios/RNEmarsysInlineInAppViewManager.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#import "RNEmarsysInlineInAppViewManager.h"

@implementation RNEmarsysInlineInAppView

@end


@implementation RNEmarsysInlineInAppViewManager

RCT_EXPORT_MODULE()

- (UIView *)view {
RNEmarsysInlineInAppView *view = [[RNEmarsysInlineInAppView alloc] init];
__weak RNEmarsysInlineInAppView *weakView = view;
view.eventHandler = ^(NSString *eventName, NSDictionary<NSString *, NSObject *> *payload) {
if (!weakView.onAppEvent) { return; }
weakView.onAppEvent(@{ @"eventName": eventName, @"payload": payload });
};
view.completionBlock = ^(NSError *error) {
if (!weakView.onCompleted) { return; }
weakView.onCompleted(@{ @"error": error ? error.localizedDescription : [NSNull null] });
};
view.closeBlock = ^ {
if (!weakView.onClose) { return; }
weakView.onClose(@{ });
};
return view;
}

RCT_EXPORT_VIEW_PROPERTY(onAppEvent, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onCompleted, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onClose, RCTBubblingEventBlock)

RCT_EXPORT_METHOD(loadInApp:(NSNumber * _Nonnull)viewTag withViewId:(NSString * _Nonnull)viewId) {
[self.bridge.uiManager addUIBlock:^(RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
UIView *view = viewRegistry[viewTag];
if (!view || ![view isKindOfClass:[EMSInlineInAppView class]]) { return; }
[(EMSInlineInAppView *)view loadInAppWithViewId:viewId];
}];
}

@end
3 changes: 1 addition & 2 deletions ios/RNEmarsysWrapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,11 @@ - (dispatch_queue_t)methodQueue {
activity.webpageURL = activityURL;

[Emarsys trackDeepLinkWithUserActivity:activity sourceHandler:^(NSString *source) {
NSLog(@"trackDeepLink. Source url: %@", source);
resolve([NSNumber numberWithBool:YES]);
}];
}
@catch (NSException *ex) {
NSLog(@"RNEmarsysWrapper trackDeepLink: %@ %@", ex.name, ex.reason);
reject(ex.name, ex.reason, nil);
}
}
}
Expand Down
46 changes: 45 additions & 1 deletion src/inapp.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { NativeModules } from 'react-native';
import React from 'react';
import { NativeModules, requireNativeComponent, UIManager, findNodeHandle } from 'react-native';
import PropTypes from 'prop-types';

const { RNEmarsysInAppWrapper } = NativeModules;

Expand All @@ -22,4 +24,46 @@ const InApp = {

};

const RNEmarsysInlineInAppView = requireNativeComponent('RNEmarsysInlineInAppView');

class InlineInAppView extends React.Component {
loadInApp(viewId) {
let commandID = UIManager.RNEmarsysInlineInAppView.Commands.loadInApp
UIManager.dispatchViewManagerCommand(
findNodeHandle(this),
Platform.OS === 'ios' ? commandID : commandID.toString(),
[viewId],
);
}

_onAppEvent = event => {
if (!this.props.onAppEvent) { return; }
this.props.onAppEvent(event.nativeEvent.eventName, event.nativeEvent.payload);
};

_onCompleted = event => {
if (!this.props.onCompleted) { return; }
this.props.onCompleted(event.nativeEvent.error);
};

_onClose = event => {
if (!this.props.onClose) { return; }
this.props.onClose();
};

render() {
return <RNEmarsysInlineInAppView {...this.props}
onAppEvent={this._onAppEvent}
onCompleted={this._onCompleted}
onClose={this._onClose} />;
}
}

InlineInAppView.propTypes = {
onAppEvent: PropTypes.func,
onCompleted: PropTypes.func,
onClose: PropTypes.func,
};

export default InApp;
export { InlineInAppView };

0 comments on commit f3793af

Please sign in to comment.