|
| 1 | +package com.mparticle.mparticle_flutter_sdk |
| 2 | + |
| 3 | +import android.app.Activity |
| 4 | +import android.os.Build |
| 5 | +import androidx.annotation.RequiresApi |
| 6 | +import androidx.lifecycle.Lifecycle |
| 7 | +import androidx.lifecycle.LifecycleOwner |
| 8 | +import androidx.lifecycle.lifecycleScope |
| 9 | +import androidx.lifecycle.repeatOnLifecycle |
| 10 | +import com.mparticle.RoktEvent |
| 11 | +import io.flutter.plugin.common.BinaryMessenger |
| 12 | +import io.flutter.plugin.common.EventChannel |
| 13 | +import kotlinx.coroutines.Job |
| 14 | +import kotlinx.coroutines.flow.Flow |
| 15 | +import kotlinx.coroutines.launch |
| 16 | +import java.util.concurrent.ConcurrentHashMap |
| 17 | +import java.util.concurrent.ConcurrentLinkedDeque |
| 18 | + |
| 19 | +@RequiresApi(Build.VERSION_CODES.LOLLIPOP) |
| 20 | +class RoktEventHandler(private val messenger: BinaryMessenger) { |
| 21 | + |
| 22 | + private val eventListeners = ConcurrentHashMap<Any?, ConcurrentLinkedDeque<EventChannel.EventSink>>() |
| 23 | + private val eventSubscriptions = mutableMapOf<String, Job?>() |
| 24 | + |
| 25 | + init { |
| 26 | + setupEventChannel() |
| 27 | + } |
| 28 | + |
| 29 | + fun subscribeToEvents(events: Flow<RoktEvent>, identifier: String? = null, activity: Activity) { |
| 30 | + val activeJob = eventSubscriptions[identifier.orEmpty()]?.takeIf { it.isActive } |
| 31 | + if (activeJob != null) { |
| 32 | + return |
| 33 | + } |
| 34 | + val owner = activity as? LifecycleOwner ?: return |
| 35 | + |
| 36 | + val job = owner.lifecycleScope.launch { |
| 37 | + owner.lifecycle.repeatOnLifecycle(Lifecycle.State.CREATED) { |
| 38 | + events.collect { event -> |
| 39 | + val params = mutableMapOf<String, String>() |
| 40 | + |
| 41 | + params["event"] = event::class.simpleName ?: "RoktEvent" |
| 42 | + event.placementId?.let { params["placementId"] = it } |
| 43 | + |
| 44 | + when (event) { |
| 45 | + is RoktEvent.InitComplete -> { |
| 46 | + params["status"] = event.success.toString() |
| 47 | + } |
| 48 | + is RoktEvent.OpenUrl -> { |
| 49 | + params["url"] = event.url |
| 50 | + } |
| 51 | + is RoktEvent.CartItemInstantPurchase -> { |
| 52 | + params["cartItemId"] = event.cartItemId |
| 53 | + params["catalogItemId"] = event.catalogItemId |
| 54 | + params["currency"] = event.currency |
| 55 | + params["description"] = event.description |
| 56 | + params["linkedProductId"] = event.linkedProductId |
| 57 | + params["totalPrice"] = event.totalPrice.toString() |
| 58 | + params["quantity"] = event.quantity.toString() |
| 59 | + params["unitPrice"] = event.unitPrice.toString() |
| 60 | + } |
| 61 | + else -> { |
| 62 | + // No custom parameters needed for other events |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + identifier?.let { params["identifier"] = it } |
| 67 | + eventListeners.values.flatten().forEach { listener -> listener.success(params) } |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + eventSubscriptions[identifier.orEmpty()] = job |
| 72 | + } |
| 73 | + |
| 74 | + private fun setupEventChannel() { |
| 75 | + EventChannel(messenger, EVENT_CHANNEL_NAME).setStreamHandler( |
| 76 | + object : EventChannel.StreamHandler { |
| 77 | + override fun onListen( |
| 78 | + arguments: Any?, |
| 79 | + sink: EventChannel.EventSink?, |
| 80 | + ) { |
| 81 | + sink?.let { |
| 82 | + val sinks = eventListeners.getOrPut(arguments ?: "") { ConcurrentLinkedDeque() } |
| 83 | + sinks.addLast(it) |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + override fun onCancel(arguments: Any?) { |
| 88 | + val sinks = eventListeners[arguments ?: ""] |
| 89 | + if (sinks?.isNotEmpty() == true) { |
| 90 | + sinks.removeLast() |
| 91 | + } |
| 92 | + if (sinks?.isEmpty() == true) { |
| 93 | + eventListeners.remove(arguments ?: "") |
| 94 | + } |
| 95 | + } |
| 96 | + }, |
| 97 | + ) |
| 98 | + } |
| 99 | + |
| 100 | + private val RoktEvent.placementId: String? |
| 101 | + get() = when (this) { |
| 102 | + is RoktEvent.FirstPositiveEngagement -> placementId |
| 103 | + is RoktEvent.OfferEngagement -> placementId |
| 104 | + is RoktEvent.PlacementClosed -> placementId |
| 105 | + is RoktEvent.PlacementCompleted -> placementId |
| 106 | + is RoktEvent.PlacementFailure -> placementId |
| 107 | + is RoktEvent.PlacementInteractive -> placementId |
| 108 | + is RoktEvent.PlacementReady -> placementId |
| 109 | + is RoktEvent.PositiveEngagement -> placementId |
| 110 | + is RoktEvent.OpenUrl -> placementId |
| 111 | + is RoktEvent.CartItemInstantPurchase -> placementId |
| 112 | + else -> null |
| 113 | + } |
| 114 | + |
| 115 | + companion object { |
| 116 | + private const val EVENT_CHANNEL_NAME = "MPRoktEvents" |
| 117 | + } |
| 118 | +} |
0 commit comments