react-native-sdk-pianoio 0.3.1 → 0.3.4

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.
Files changed (37) hide show
  1. package/README.md +176 -186
  2. package/android/build.gradle +29 -42
  3. package/android/gradle.properties +33 -14
  4. package/android/src/main/java/com/sdkpianoio/ComposerPianoImpl.kt +366 -0
  5. package/android/src/main/java/com/sdkpianoio/SdkPianoioModule.kt +281 -507
  6. package/android/src/main/java/com/sdkpianoio/TokenService.kt +139 -0
  7. package/android/test.sh +494 -0
  8. package/ios/ComposerPianoImpl.swift +128 -225
  9. package/ios/MyComposerDelegate.swift +142 -109
  10. package/ios/SdkPianoio.swift +69 -143
  11. package/ios/SdkPianoioBridge.m +18 -46
  12. package/ios/TokenService.swift +219 -0
  13. package/lib/commonjs/NativeSdkPianoio.ts +35 -22
  14. package/lib/commonjs/PianoComposer.js +89 -132
  15. package/lib/commonjs/PianoComposer.js.map +1 -1
  16. package/lib/commonjs/index.js.map +1 -1
  17. package/lib/module/NativeSdkPianoio.ts +35 -22
  18. package/lib/module/PianoComposer.js +90 -132
  19. package/lib/module/PianoComposer.js.map +1 -1
  20. package/lib/module/index.js +0 -14
  21. package/lib/module/index.js.map +1 -1
  22. package/lib/typescript/commonjs/src/NativeSdkPianoio.d.ts +28 -21
  23. package/lib/typescript/commonjs/src/NativeSdkPianoio.d.ts.map +1 -1
  24. package/lib/typescript/commonjs/src/PianoComposer.d.ts +55 -31
  25. package/lib/typescript/commonjs/src/PianoComposer.d.ts.map +1 -1
  26. package/lib/typescript/commonjs/src/index.d.ts.map +1 -1
  27. package/lib/typescript/module/src/NativeSdkPianoio.d.ts +28 -21
  28. package/lib/typescript/module/src/NativeSdkPianoio.d.ts.map +1 -1
  29. package/lib/typescript/module/src/PianoComposer.d.ts +55 -31
  30. package/lib/typescript/module/src/PianoComposer.d.ts.map +1 -1
  31. package/lib/typescript/module/src/index.d.ts.map +1 -1
  32. package/package.json +5 -2
  33. package/src/NativeSdkPianoio.ts +35 -22
  34. package/src/PianoComposer.tsx +78 -141
  35. package/src/index.tsx +0 -14
  36. package/android/src/main/AndroidManifestNew.xml +0 -2
  37. package/ios/services/TokenService.swift +0 -70
@@ -0,0 +1,366 @@
1
+ package com.sdkpianoio
2
+
3
+ import android.content.Context
4
+ import com.facebook.react.bridge.Promise
5
+ import io.piano.android.composer.Composer
6
+ import io.piano.android.composer.listeners.EventTypeListener
7
+ import io.piano.android.composer.listeners.MeterListener
8
+ import io.piano.android.composer.listeners.NonSiteListener
9
+ import io.piano.android.composer.listeners.ShowTemplateListener
10
+ import io.piano.android.composer.listeners.UserSegmentListener
11
+ import io.piano.android.composer.model.CustomParameters
12
+ import io.piano.android.composer.model.Event
13
+ import io.piano.android.composer.model.ExperienceRequest
14
+ import io.piano.android.composer.model.events.ShowTemplate
15
+
16
+ /**
17
+ * ComposerPianoImpl - Main implementation class for the Piano SDK on Android
18
+ *
19
+ * This class is responsible for:
20
+ * - Managing the Piano SDK's Composer instance
21
+ * - Configuring execution parameters
22
+ * - Executing Piano experiences
23
+ * - Managing React Native callbacks and promises
24
+ */
25
+ class ComposerPianoImpl {
26
+
27
+ // MARK: - Properties
28
+
29
+ /** The Piano SDK's Composer instance */
30
+ private var composer: Composer? = null
31
+
32
+ /** The Piano ID Token Service instance */
33
+ internal var tokenService: TokenService? = null
34
+
35
+ /** The Piano Application ID */
36
+ companion object {
37
+ var aid: String = ""
38
+ }
39
+
40
+ // MARK: - Configuration Properties
41
+
42
+ /** Configured tags for the composer */
43
+ private val tags = mutableListOf<String>()
44
+
45
+ /** Configured Zone ID */
46
+ private var zoneId: String? = null
47
+
48
+ /** Configured referrer */
49
+ private var referrer: String? = null
50
+
51
+ /** Configured URL */
52
+ private var url: String? = null
53
+
54
+ /** Configured user token */
55
+ private var userToken: String? = null
56
+
57
+ /** Configured custom variables */
58
+ private val customVariables = mutableMapOf<String, String>()
59
+
60
+ // MARK: - Initialization
61
+
62
+ /** Public class constructor Initializes the instance with default values */
63
+ constructor() {
64
+ println("ComposerPianoImpl: Constructor initialized")
65
+ }
66
+
67
+ /**
68
+ * Initializes the Composer with the provided Application ID.
69
+ *
70
+ * @param context The Android application context.
71
+ * @param aid The Piano Application ID.
72
+ */
73
+ fun initializeComposer(context: Context, aid: String) {
74
+ println("ComposerPianoImpl: Initializing composer with AID: $aid")
75
+
76
+ // Validate input parameter
77
+ if (aid.isBlank()) {
78
+ throw IllegalArgumentException("AID cannot be empty")
79
+ }
80
+
81
+ // Configure static AID for the whole class
82
+ ComposerPianoImpl.aid = aid
83
+ println("ComposerPianoImpl: Static AID configured: ${ComposerPianoImpl.aid}")
84
+
85
+ try {
86
+ // 1. Initialize the Composer SDK statically using the official API
87
+ Composer.init(context, aid, Composer.Endpoint.PRODUCTION)
88
+ println("ComposerPianoImpl: Composer SDK initialized for PRODUCTION")
89
+
90
+ // 2. Get the singleton instance and store it
91
+ this.composer = Composer.getInstance()
92
+ println("ComposerPianoImpl: Composer instance retrieved")
93
+
94
+ // 3. Initialize the TokenService and store it as a property
95
+ this.tokenService = TokenService()
96
+ this.tokenService?.initialize(aid)
97
+ println("ComposerPianoImpl: TokenService initialized")
98
+
99
+ println("ComposerPianoImpl: Initialization completed successfully")
100
+ } catch (e: Exception) {
101
+ println("ComposerPianoImpl: Error during initialization: ${e.message}")
102
+ throw e
103
+ }
104
+ }
105
+
106
+ // MARK: - Configuration Methods
107
+
108
+ /**
109
+ * Adds a tag to the composer
110
+ *
111
+ * @param tag The tag to be added
112
+ */
113
+ fun addTag(tag: String) {
114
+ if (!this.tags.contains(tag)) {
115
+ this.tags.add(tag)
116
+ }
117
+ }
118
+
119
+ /**
120
+ * Adds multiple tags to the composer
121
+ *
122
+ * @param tags The list of tags to be added
123
+ */
124
+ fun addTags(tags: List<String>) {
125
+ for (tag in tags) {
126
+ if (!this.tags.contains(tag)) {
127
+ this.tags.add(tag)
128
+ }
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Sets the Zone ID for the composer
134
+ *
135
+ * @param zoneId The Zone ID to be configured
136
+ */
137
+ fun setZoneId(zoneId: String) {
138
+ this.zoneId = zoneId
139
+ }
140
+
141
+ /**
142
+ * Sets the referrer for the composer
143
+ *
144
+ * @param referrer The referrer to be configured
145
+ */
146
+ fun setReferrer(referrer: String) {
147
+ this.referrer = referrer
148
+ }
149
+
150
+ /**
151
+ * Sets the URL for the composer
152
+ *
153
+ * @param url The URL to be configured
154
+ */
155
+ fun setUrl(url: String) {
156
+ this.url = url
157
+ }
158
+
159
+ /**
160
+ * Sets the user token on the Composer instance.
161
+ *
162
+ * @param token The user's access token.
163
+ */
164
+ fun setUserToken(token: String) {
165
+ this.userToken = token
166
+ // Call the official SDK method to set the token
167
+ composer?.userToken(token)
168
+ }
169
+
170
+ /**
171
+ * Adds a custom variable.
172
+ *
173
+ * @param key The variable's key
174
+ * @param value The variable's value
175
+ */
176
+ fun addCustomVariable(key: String, value: String) {
177
+ this.customVariables[key] = value
178
+ }
179
+
180
+ // MARK: - Advanced Configuration Methods
181
+
182
+ /**
183
+ * Sets multiple custom variables at once.
184
+ *
185
+ * @param variables A map of custom variables.
186
+ */
187
+ fun setCustomVariables(variables: Map<String, String>) {
188
+ this.customVariables.clear()
189
+ this.customVariables.putAll(variables)
190
+ }
191
+
192
+ /** Removes all configured tags. */
193
+ fun clearTags() {
194
+ this.tags.clear()
195
+ }
196
+
197
+ /** Removes all custom variables. */
198
+ fun clearCustomVariables() {
199
+ this.customVariables.clear()
200
+ }
201
+
202
+ /**
203
+ * Removes a specific tag.
204
+ *
205
+ * @param tag The tag to be removed.
206
+ */
207
+ fun removeTag(tag: String) {
208
+ this.tags.remove(tag)
209
+ }
210
+
211
+ /**
212
+ * Removes a specific custom variable.
213
+ *
214
+ * @param key The key of the variable to be removed.
215
+ */
216
+ fun removeCustomVariable(key: String) {
217
+ this.customVariables.remove(key)
218
+ }
219
+
220
+ // MARK: - State Methods (Getters)
221
+
222
+ /**
223
+ * Gets the Application ID.
224
+ *
225
+ * @return The current Application ID.
226
+ */
227
+ fun getAid(): String {
228
+ return ComposerPianoImpl.aid
229
+ }
230
+
231
+ /**
232
+ * Gets the configured tags.
233
+ *
234
+ * @return A list of configured tags.
235
+ */
236
+ fun getTags(): List<String> {
237
+ return this.tags.toList()
238
+ }
239
+
240
+ /**
241
+ * Gets the configured Zone ID.
242
+ *
243
+ * @return The current Zone ID.
244
+ */
245
+ fun getZoneId(): String {
246
+ return this.zoneId ?: ""
247
+ }
248
+
249
+ /**
250
+ * Gets the configured referrer.
251
+ *
252
+ * @return The current referrer.
253
+ */
254
+ fun getReferrer(): String {
255
+ return referrer ?: ""
256
+ }
257
+
258
+ /**
259
+ * Gets the configured URL.
260
+ *
261
+ * @return The current URL.
262
+ */
263
+ fun getUrl(): String {
264
+ return url ?: ""
265
+ }
266
+
267
+ /**
268
+ * Gets the user token.
269
+ *
270
+ * @return The current user token.
271
+ */
272
+ fun getUserToken(): String {
273
+ return userToken ?: ""
274
+ }
275
+
276
+ /**
277
+ * Gets the composer instance.
278
+ *
279
+ * @return The Composer instance, or null if not initialized.
280
+ */
281
+ fun getComposer(): Composer? {
282
+ return composer
283
+ }
284
+
285
+ /**
286
+ * Gets the configured custom variables.
287
+ *
288
+ * @return A map of the custom variables.
289
+ */
290
+ fun getCustomVariables(): Map<String, String> {
291
+ return customVariables.toMap()
292
+ }
293
+
294
+ // MARK: - Execution Methods
295
+
296
+ /**
297
+ * Executes the composer with the current configuration.
298
+ *
299
+ * @param promise The promise that will be resolved with the execution result.
300
+ * @throws IllegalStateException if the composer has not been initialized.
301
+ */
302
+ fun executeComposer(promise: Promise) {
303
+ println("ComposerPianoImpl: Starting composer execution")
304
+
305
+ // 1. Validate that the composer has been initialized
306
+ val composerInstance = composer
307
+ if (composerInstance == null) {
308
+ val error =
309
+ IllegalStateException(
310
+ "Composer not initialized. Call initializeComposer() first."
311
+ )
312
+ println("ComposerPianoImpl: Error - Composer not initialized")
313
+ promise.reject("INIT_ERROR", error.message, error)
314
+ return
315
+ }
316
+
317
+ try {
318
+ val customParams = CustomParameters()
319
+ this.customVariables.forEach { (key, value) -> customParams.request(key, value) }
320
+
321
+ // 2. Build the ExperienceRequest with all stored configurations
322
+ val request =
323
+ ExperienceRequest.Builder()
324
+ .tags(this.tags)
325
+ .zone(this.zoneId)
326
+ .referer(this.referrer)
327
+ .url(this.url ?: "")
328
+ .customParams(customParams)
329
+ .debug(false)
330
+ .build()
331
+ println("ComposerPianoImpl: ExperienceRequest created successfully")
332
+
333
+ // 3. Define the listeners to handle events from the SDK
334
+ val listeners: List<EventTypeListener<*>> =
335
+ listOf(
336
+ UserSegmentListener { event ->
337
+ println("UserSegmentListener event: ${event.eventData}")
338
+ // You can process user segment data here if needed
339
+ },
340
+ MeterListener { event ->
341
+ println("MeterListener event: ${event.eventData}")
342
+ promise.resolve(event.eventData.toString())
343
+ },
344
+ ShowTemplateListener { event: Event<ShowTemplate> ->
345
+ println("ShowTemplateListener event: ${event.eventData}")
346
+ promise.resolve(event.eventData.toString())
347
+ },
348
+ NonSiteListener { event ->
349
+ println("NonSiteListener event: ${event.eventData}")
350
+ }
351
+ )
352
+
353
+ println("ComposerPianoImpl: Event listeners created")
354
+
355
+ // 4. Execute the experience request using the real Piano SDK method
356
+ composerInstance.getExperience(request, listeners) { exception ->
357
+ println("ComposerPianoImpl: SDK returned an exception: ${exception.message}")
358
+ promise.reject("EXECUTION_ERROR", exception.message, exception)
359
+ }
360
+ println("ComposerPianoImpl: getExperience() called successfully")
361
+ } catch (e: Exception) {
362
+ println("ComposerPianoImpl: Error during execution setup: ${e.message}")
363
+ promise.reject("SETUP_ERROR", e.message, e)
364
+ }
365
+ }
366
+ }