@screeb/react-native 1.0.0-rc.2 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -123,6 +123,6 @@ def kotlin_version = getExtOrDefault('kotlinVersion')
123
123
  dependencies {
124
124
  // noinspection GradleDynamicVersion
125
125
  api 'com.facebook.react:react-native:+'
126
- implementation "app.screeb.sdk:survey:2.0.0-rc.2"
127
- implementation "org.jetbrains.kotlin:kotlin-stdlib:1.7.0"
126
+ implementation "app.screeb.sdk:survey:2.0.2"
127
+ implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.8.22"
128
128
  }
@@ -1,5 +1,5 @@
1
1
  distributionBase=GRADLE_USER_HOME
2
2
  distributionPath=wrapper/dists
3
- distributionUrl=https\://services.gradle.org/distributions/gradle-7.6-bin.zip
4
3
  zipStoreBase=GRADLE_USER_HOME
5
4
  zipStorePath=wrapper/dists
5
+ distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.2-all.zip
@@ -5,6 +5,10 @@ import com.facebook.react.bridge.ReactContextBaseJavaModule
5
5
  import com.facebook.react.bridge.ReactMethod
6
6
  import com.facebook.react.bridge.Promise
7
7
  import com.facebook.react.bridge.ReadableMap
8
+ import com.facebook.react.bridge.ReadableArray
9
+ import com.facebook.react.bridge.WritableNativeMap
10
+ import com.facebook.react.bridge.Arguments
11
+ import com.facebook.react.modules.core.DeviceEventManagerModule
8
12
  import java.util.HashMap
9
13
  import androidx.annotation.NonNull
10
14
  import android.content.Context
@@ -21,14 +25,31 @@ class ScreebModuleModule(reactContext: ReactApplicationContext) :
21
25
  }
22
26
 
23
27
  @ReactMethod
24
- fun initSdk(channelId: String, userId: String?, properties: ReadableMap?) {
28
+ fun initSdk(channelId: String, userId: String?, properties: ReadableMap?, hooks: ReadableMap?) {
25
29
  Log.d("ScreebModule", "Called initSdk : $userId")
26
30
  var map: HashMap<String, Any?>? = null
27
31
  if (properties != null) {
28
32
  map = properties.toHashMap()
29
33
  }
34
+ var mapHooks:HashMap<String, Any>? = null
35
+ if (hooks != null) {
36
+ mapHooks = hashMapOf<String, Any>()
37
+ val hooksKeys = hooks.keySetIterator()
38
+ while (hooksKeys.hasNextKey()) {
39
+ val key = hooksKeys.nextKey()
40
+ val value = hooks.getString(key)
41
+ if (key == "version"){
42
+ mapHooks[key] = value!!
43
+ } else {
44
+ mapHooks[key] = { payload:Any -> this.reactApplicationContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java).emit("ScreebEvent", Arguments.createMap().apply {
45
+ putString("hookId", value!!)
46
+ putString("payload", payload.toString())
47
+ }) }
48
+ }
49
+ }
50
+ }
30
51
  Handler(Looper.getMainLooper()).post {
31
- Screeb.pluginInit(channelId, userId, map)
52
+ Screeb.pluginInit(channelId, userId, map, mapHooks)
32
53
  }
33
54
  }
34
55
 
@@ -104,14 +125,31 @@ class ScreebModuleModule(reactContext: ReactApplicationContext) :
104
125
  }
105
126
 
106
127
  @ReactMethod
107
- fun startSurvey(surveyId: String, allowMultipleResponses: Boolean? = true, hiddenFields: ReadableMap? = null, ignoreSurveyStatus: Boolean? = true) {
128
+ fun startSurvey(surveyId: String, allowMultipleResponses: Boolean? = true, hiddenFields: ReadableMap? = null, ignoreSurveyStatus: Boolean? = true, hooks: ReadableMap? = null) {
108
129
  Log.e("ScreebModule", "Called startSurvey : $surveyId")
109
130
  var map: HashMap<String, Any>? = null
110
131
  if (hiddenFields != null) {
111
132
  map = hiddenFields.toHashMap()
112
133
  }
134
+ var mapHooks:HashMap<String, Any>? = null
135
+ if (hooks != null) {
136
+ mapHooks = hashMapOf<String, Any>()
137
+ val hooksKeys = hooks.keySetIterator()
138
+ while (hooksKeys.hasNextKey()) {
139
+ val key = hooksKeys.nextKey()
140
+ val value = hooks.getString(key)
141
+ if (key == "version"){
142
+ mapHooks[key] = value!!
143
+ } else {
144
+ mapHooks[key] = { payload:Any -> this.reactApplicationContext.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java).emit("ScreebEvent", Arguments.createMap().apply {
145
+ putString("hookId", value!!)
146
+ putString("payload", payload.toString())
147
+ }) }
148
+ }
149
+ }
150
+ }
113
151
  Handler(Looper.getMainLooper()).post {
114
- Screeb.startSurvey(surveyId, allowMultipleResponses ?: true, map, ignoreSurveyStatus ?: true)
152
+ Screeb.startSurvey(surveyId, allowMultipleResponses ?: true, map, ignoreSurveyStatus ?: true, mapHooks)
115
153
  }
116
154
  }
117
155
 
@@ -1,15 +1,16 @@
1
1
  #import <React/RCTBridgeModule.h>
2
+ #import <React/RCTEventEmitter.h>
2
3
 
3
- @interface RCT_EXTERN_MODULE(ScreebModule, NSObject)
4
+ @interface RCT_EXTERN_MODULE(ScreebModule, RCTEventEmitter)
4
5
 
5
- RCT_EXTERN_METHOD(initSdk:(NSString *)channelId userId:(NSString *)userId_ properties:(NSDictionary<NSString *, id> *)properties_)
6
+ RCT_EXTERN_METHOD(initSdk:(NSString *)channelId userId:(NSString *)userId_ properties:(NSDictionary<NSString *, id> *)properties_ hooks:(NSDictionary<NSString *, id> *)hooks_)
6
7
  RCT_EXTERN_METHOD(setIdentity:(NSString *)userId properties:(NSDictionary<NSString *, id> *)properties_)
7
8
  RCT_EXTERN_METHOD(setProperties:(NSDictionary<NSString *, id> *)properties)
8
9
  RCT_EXTERN_METHOD(assignGroup:(NSString *)type name:(NSString *)name_ properties:(NSDictionary<NSString *, id> *)properties_)
9
10
  RCT_EXTERN_METHOD(unassignGroup:(NSString *)type name:(NSString *)name_ properties:(NSDictionary<NSString *, id> *)properties_)
10
11
  RCT_EXTERN_METHOD(trackEvent:(NSString *)eventId properties:(NSDictionary<NSString *, id> *)properties_)
11
12
  RCT_EXTERN_METHOD(trackScreen:(NSString *)screen properties:(NSDictionary<NSString *, id> *)properties_)
12
- RCT_EXTERN_METHOD(startSurvey:(NSString *)surveyId allowMultipleResponses:(BOOL)allowMultipleResponses_ hiddenFields:(NSDictionary<NSString *, id> *)hiddenFields_ ignoreSurveyStatus:(BOOL)ignoreSurveyStatus_)
13
+ RCT_EXTERN_METHOD(startSurvey:(NSString *)surveyId allowMultipleResponses:(BOOL)allowMultipleResponses_ hiddenFields:(NSDictionary<NSString *, id> *)hiddenFields_ ignoreSurveyStatus:(BOOL)ignoreSurveyStatus_ hooks:(NSDictionary<NSString *, id> *)hooks_)
13
14
  RCT_EXTERN_METHOD(debug)
14
15
  RCT_EXTERN_METHOD(debugTargeting)
15
16
  RCT_EXTERN_METHOD(closeSdk)
@@ -18,4 +19,4 @@ RCT_EXTERN_METHOD(resetIdentity)
18
19
  {
19
20
  return YES;
20
21
  }
21
- @end
22
+ @end
@@ -3,20 +3,32 @@ import UIKit
3
3
  import Foundation
4
4
 
5
5
  @objc(ScreebModule)
6
- class ScreebModule: NSObject {
6
+ class ScreebModule: RCTEventEmitter {
7
7
 
8
- @objc(initSdk:userId:properties:)
8
+ @objc(initSdk:userId:properties:hooks:)
9
9
  func initSdk(
10
10
  _ channelId: String,
11
11
  userId userId_: String?,
12
- properties properties_: [String: Any]?) {
12
+ properties properties_: [String: Any]?,
13
+ hooks hooks_: [String: Any]?) {
13
14
  var map: [String: AnyEncodable?] = [:]
14
15
  if (properties_ != nil) {
15
16
  map = self.mapToAnyEncodable(map: properties_!)
16
17
  }
18
+ var mapHooks: [String: Any?]? = nil
19
+ if (hooks_ != nil) {
20
+ mapHooks = [:]
21
+ hooks_?.forEach{ hook in
22
+ if(hook.key == "version"){
23
+ mapHooks![hook.key] = hook.value as? String
24
+ } else {
25
+ mapHooks![hook.key] = {(payload:Any) -> () in self.sendEvent(withName: "ScreebEvent", body: ["hookId":hook.value,"payload":String(describing: payload)]) }
26
+ }
27
+ }
28
+ }
17
29
  if let controller = UIApplication.shared.keyWindow?.rootViewController {
18
30
  DispatchQueue.main.async {
19
- Screeb.initSdk(context: controller, channelId: channelId, identity: userId_, visitorProperty: map)
31
+ Screeb.initSdk(context: controller, channelId: channelId, identity: userId_, visitorProperty: map, hooks: mapHooks)
20
32
  }
21
33
  } else {
22
34
  print("Screeb : error init, could not find rootViewController")
@@ -61,13 +73,24 @@ class ScreebModule: NSObject {
61
73
  }
62
74
  }
63
75
 
64
- @objc func startSurvey(_ surveyId: String, allowMultipleResponses allowMultipleResponses_: Bool, hiddenFields hiddenFields_: [String: Any]?,ignoreSurveyStatus ignoreSurveyStatus_: Bool) {
76
+ @objc func startSurvey(_ surveyId: String, allowMultipleResponses allowMultipleResponses_: Bool, hiddenFields hiddenFields_: [String: Any]?,ignoreSurveyStatus ignoreSurveyStatus_: Bool, hooks hooks_: [String: Any]?) {
65
77
  var map: [String: AnyEncodable] = [:]
66
78
  if (hiddenFields_ != nil) {
67
79
  map = self.mapToAnyEncodable(map: hiddenFields_!).filter({ $0.value != nil }).mapValues({ $0! })
68
80
  }
81
+ var mapHooks: [String: Any?]? = nil
82
+ if (hooks_ != nil) {
83
+ mapHooks = [:]
84
+ hooks_?.forEach{ hook in
85
+ if(hook.key == "version"){
86
+ mapHooks![hook.key] = hook.value as? String
87
+ } else {
88
+ mapHooks![hook.key] = {(payload:Any) -> () in self.sendEvent(withName: "ScreebEvent", body: ["hookId":hook.value,"payload":String(describing: payload)]) }
89
+ }
90
+ }
91
+ }
69
92
  DispatchQueue.main.async {
70
- Screeb.startSurvey(surveyId: surveyId, allowMultipleResponses: allowMultipleResponses_, hiddenFields: map, ignoreSurveyStatus: ignoreSurveyStatus_)
93
+ Screeb.startSurvey(surveyId: surveyId, allowMultipleResponses: allowMultipleResponses_, hiddenFields: map, ignoreSurveyStatus: ignoreSurveyStatus_, hooks: mapHooks)
71
94
  }
72
95
  }
73
96
 
@@ -132,4 +155,8 @@ class ScreebModule: NSObject {
132
155
  return nil
133
156
  }
134
157
  }
158
+
159
+ override func supportedEvents() -> [String]! {
160
+ return ["ScreebEvent"]
161
+ }
135
162
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@screeb/react-native",
3
- "version": "1.0.0-rc.2",
3
+ "version": "2.0.1",
4
4
  "description": "Continuous Product Discovery",
5
5
  "scripts": {
6
6
  "clean": "rm -rf lib/",
@@ -16,5 +16,5 @@ Pod::Spec.new do |s|
16
16
  s.source_files = "ios/**/*.{h,m,mm,swift}"
17
17
 
18
18
  s.dependency "React-Core"
19
- s.dependency "Screeb", '~> 2.0.0-rc.3'
19
+ s.dependency "Screeb", '~> 2.0.0'
20
20
  end
package/src/index.tsx CHANGED
@@ -1,31 +1,64 @@
1
- import { NativeModules, Platform } from 'react-native';
1
+ import {
2
+ DeviceEventEmitter,
3
+ NativeEventEmitter,
4
+ NativeModules,
5
+ Platform,
6
+ } from "react-native";
2
7
 
3
8
  const LINKING_ERROR =
4
9
  `The package '@screeb/react-native' doesn't seem to be linked. Make sure: \n\n` +
5
- Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
6
- '- You rebuilt the app after installing the package\n' +
7
- '- You are not using Expo managed workflow\n';
10
+ Platform.select({ ios: "- You have run 'pod install'\n", default: "" }) +
11
+ "- You rebuilt the app after installing the package\n" +
12
+ "- You are not using Expo managed workflow\n";
8
13
 
9
14
  const ScreebModule = NativeModules.ScreebModule
10
15
  ? NativeModules.ScreebModule
11
16
  : new Proxy(
12
- {},
13
- {
14
- get() {
15
- throw new Error(LINKING_ERROR);
16
- },
17
- }
18
- );
17
+ {},
18
+ {
19
+ get() {
20
+ throw new Error(LINKING_ERROR);
21
+ },
22
+ }
23
+ );
24
+
25
+ let hooksRegistry = new Map<string, any>();
19
26
 
20
27
  export function initSdk(
21
28
  androidChannelId: string,
22
29
  iosChannelId: string,
23
30
  userId?: string,
24
- properties?: Map<string, any>) {
25
- if (Platform.OS === 'ios') {
26
- return ScreebModule.initSdk(iosChannelId, userId, properties);
31
+ properties?: Map<string, any>,
32
+ hooks?: any
33
+ ) {
34
+ const emitter =
35
+ Platform.OS === "ios"
36
+ ? new NativeEventEmitter(NativeModules.ScreebModule)
37
+ : DeviceEventEmitter;
38
+ emitter.addListener("ScreebEvent", handleEvent);
39
+ let mapHooksId: any = undefined;
40
+ if (hooks != null) {
41
+ mapHooksId = new Object();
42
+ Object.keys(hooks).map((key) => {
43
+ if (key == "version") {
44
+ mapHooksId = { ...mapHooksId, version: hooks[key] };
45
+ } else {
46
+ // Random id
47
+ let uuid = Date.now().toString() + Math.random().toString();
48
+ hooksRegistry.set(uuid, hooks[key]);
49
+ mapHooksId = { ...mapHooksId, [key]: uuid };
50
+ }
51
+ });
52
+ }
53
+ if (Platform.OS === "ios") {
54
+ return ScreebModule.initSdk(iosChannelId, userId, properties, mapHooksId);
27
55
  } else {
28
- return ScreebModule.initSdk(androidChannelId, userId, properties);
56
+ return ScreebModule.initSdk(
57
+ androidChannelId,
58
+ userId,
59
+ properties,
60
+ mapHooksId
61
+ );
29
62
  }
30
63
  }
31
64
  export function setIdentity(userId: string, properties?: Map<string, any>) {
@@ -34,10 +67,18 @@ export function setIdentity(userId: string, properties?: Map<string, any>) {
34
67
  export function setProperties(properties?: Map<string, any>) {
35
68
  return ScreebModule.setProperties(properties);
36
69
  }
37
- export function assignGroup(type: string | null, name: string, properties?: Map<string, any>) {
70
+ export function assignGroup(
71
+ type: string | null,
72
+ name: string,
73
+ properties?: Map<string, any>
74
+ ) {
38
75
  return ScreebModule.assignGroup(type, name, properties);
39
76
  }
40
- export function unassignGroup(type: string | null, name: string, properties?: Map<string, any>) {
77
+ export function unassignGroup(
78
+ type: string | null,
79
+ name: string,
80
+ properties?: Map<string, any>
81
+ ) {
41
82
  return ScreebModule.unassignGroup(type, name, properties);
42
83
  }
43
84
  export function trackEvent(name: string, properties?: Map<string, any>) {
@@ -46,18 +87,53 @@ export function trackEvent(name: string, properties?: Map<string, any>) {
46
87
  export function trackScreen(name: string, properties?: Map<string, any>) {
47
88
  return ScreebModule.trackScreen(name, properties);
48
89
  }
49
- export function startSurvey(surveyId: string, allowMultipleResponses?: boolean, hiddenFields?: Map<string, any>, ignoreSurveyStatus?: boolean) {
50
- return ScreebModule.startSurvey(surveyId, allowMultipleResponses ?? true, hiddenFields, ignoreSurveyStatus ?? true);
90
+ export function startSurvey(
91
+ surveyId: string,
92
+ allowMultipleResponses?: boolean,
93
+ hiddenFields?: Map<string, any>,
94
+ ignoreSurveyStatus?: boolean,
95
+ hooks?: any
96
+ ) {
97
+ let mapHooksId: any = undefined;
98
+ if (hooks != undefined) {
99
+ mapHooksId = new Object();
100
+ Object.keys(hooks).map((key) => {
101
+ if (key == "version") {
102
+ mapHooksId = { ...mapHooksId, version: hooks[key] };
103
+ } else {
104
+ // Random id
105
+ let uuid = Date.now().toString() + Math.random().toString();
106
+ hooksRegistry.set(uuid, hooks[key]);
107
+ mapHooksId = { ...mapHooksId, [key]: uuid };
108
+ }
109
+ });
110
+ }
111
+ return ScreebModule.startSurvey(
112
+ surveyId,
113
+ allowMultipleResponses ?? true,
114
+ hiddenFields,
115
+ ignoreSurveyStatus ?? true,
116
+ mapHooksId
117
+ );
51
118
  }
52
- export function debug(){
119
+ export function debug() {
53
120
  return ScreebModule.debug();
54
121
  }
55
- export function debugTargeting(){
122
+ export function debugTargeting() {
56
123
  return ScreebModule.debugTargeting();
57
124
  }
58
- export function resetIdentity(){
125
+ export function resetIdentity() {
59
126
  return ScreebModule.resetIdentity();
60
127
  }
61
- export function closeSdk(){
128
+ export function closeSdk() {
62
129
  return ScreebModule.closeSdk();
63
- }
130
+ }
131
+
132
+ function handleEvent(event: any) {
133
+ if (event?.hookId != null) {
134
+ let hook = hooksRegistry.get(event.hookId);
135
+ if (hook != null) {
136
+ hook(event.payload);
137
+ }
138
+ }
139
+ }