11package com.qonversion.flutter.sdk.qonversion_flutter_sdk
22
33import com.google.gson.Gson
4+ import io.flutter.embedding.engine.plugins.FlutterPlugin
45import io.flutter.plugin.common.BinaryMessenger
6+ import io.flutter.plugin.common.MethodCall
7+ import io.flutter.plugin.common.MethodChannel
8+ import io.flutter.plugin.common.PluginRegistry
59import io.qonversion.sandwich.AutomationsEventListener
610import io.qonversion.sandwich.AutomationsSandwich
711import io.qonversion.sandwich.BridgeData
812
9- class AutomationsPlugin (messenger : BinaryMessenger ) : AutomationsEventListener {
13+ class AutomationsPlugin (private val messenger : BinaryMessenger ) : AutomationsEventListener {
1014 private var shownScreensStreamHandler: BaseEventStreamHandler ? = null
1115 private var startedActionsStreamHandler: BaseEventStreamHandler ? = null
1216 private var failedActionsStreamHandler: BaseEventStreamHandler ? = null
@@ -17,6 +21,10 @@ class AutomationsPlugin(messenger: BinaryMessenger) : AutomationsEventListener {
1721 AutomationsSandwich ()
1822 }
1923
24+ init {
25+ setup()
26+ }
27+
2028 companion object {
2129 private const val EVENT_CHANNEL_SHOWN_SCREENS = " shown_screens"
2230 private const val EVENT_CHANNEL_STARTED_ACTIONS = " started_actions"
@@ -25,7 +33,60 @@ class AutomationsPlugin(messenger: BinaryMessenger) : AutomationsEventListener {
2533 private const val EVENT_CHANNEL_FINISHED_AUTOMATIONS = " finished_automations"
2634 }
2735
28- init {
36+ override fun onAutomationEvent (event : AutomationsEventListener .Event , payload : BridgeData ? ) {
37+ val (data, stream) = when (event) {
38+ AutomationsEventListener .Event .ScreenShown -> Pair (Gson ().toJson(payload), shownScreensStreamHandler)
39+ AutomationsEventListener .Event .ActionStarted -> Pair (Gson ().toJson(payload), startedActionsStreamHandler)
40+ AutomationsEventListener .Event .ActionFinished -> Pair (Gson ().toJson(payload), finishedActionsStreamHandler)
41+ AutomationsEventListener .Event .ActionFailed -> Pair (Gson ().toJson(payload), failedActionsStreamHandler)
42+ AutomationsEventListener .Event .AutomationsFinished -> Pair (payload, finishedAutomationsStreamHandler)
43+ }
44+
45+ stream?.eventSink?.success(data)
46+ }
47+
48+ fun subscribe () {
49+ automationSandwich.setDelegate(this )
50+ }
51+
52+ fun setNotificationsToken (token : String? , result : MethodChannel .Result ) {
53+ token?.let {
54+ automationSandwich.setNotificationToken(it)
55+ result.success(null )
56+ } ? : result.noNecessaryDataError()
57+ }
58+
59+ fun handleNotification (args : Map <String , Any >, result : MethodChannel .Result ) {
60+ @Suppress(" UNCHECKED_CAST" )
61+ val data = args[" notificationData" ] as ? Map <String , Any > ? : return result.noNecessaryDataError()
62+
63+ if (data.isEmpty()) {
64+ return result.noNecessaryDataError()
65+ }
66+
67+ val isQonversionNotification = automationSandwich.handleNotification(data)
68+ result.success(isQonversionNotification)
69+ }
70+
71+ fun getNotificationCustomPayload (args : Map <String , Any >, result : MethodChannel .Result ) {
72+ @Suppress(" UNCHECKED_CAST" )
73+ val data = args[" notificationData" ] as ? Map <String , Any > ? : return result.noNecessaryDataError()
74+
75+ if (data.isEmpty()) {
76+ return result.noNecessaryDataError()
77+ }
78+
79+ val payload = automationSandwich.getNotificationCustomPayload(data)
80+ val payloadJson = Gson ().toJson(payload)
81+ result.success(payloadJson)
82+ }
83+
84+ fun showScreen (screenId : String? , result : MethodChannel .Result ) {
85+ screenId ? : return result.noNecessaryDataError()
86+ automationSandwich.showScreen(screenId, result.toResultListener())
87+ }
88+
89+ private fun setup () {
2990 val shownScreensListener = BaseListenerWrapper (messenger, EVENT_CHANNEL_SHOWN_SCREENS )
3091 shownScreensListener.register()
3192 shownScreensStreamHandler = shownScreensListener.eventStreamHandler
@@ -46,20 +107,4 @@ class AutomationsPlugin(messenger: BinaryMessenger) : AutomationsEventListener {
46107 finishedAutomationsListener.register()
47108 finishedAutomationsStreamHandler = finishedAutomationsListener.eventStreamHandler
48109 }
49-
50- fun subscribe () {
51- automationSandwich.subscribe(this )
52- }
53-
54- override fun onAutomationEvent (event : AutomationsEventListener .Event , payload : BridgeData ? ) {
55- val (data, stream) = when (event) {
56- AutomationsEventListener .Event .ScreenShown -> Pair (Gson ().toJson(payload), shownScreensStreamHandler)
57- AutomationsEventListener .Event .ActionStarted -> Pair (Gson ().toJson(payload), startedActionsStreamHandler)
58- AutomationsEventListener .Event .ActionFinished -> Pair (Gson ().toJson(payload), finishedActionsStreamHandler)
59- AutomationsEventListener .Event .ActionFailed -> Pair (Gson ().toJson(payload), failedActionsStreamHandler)
60- AutomationsEventListener .Event .AutomationsFinished -> Pair (payload, finishedAutomationsStreamHandler)
61- }
62-
63- stream?.eventSink?.success(data)
64- }
65110}
0 commit comments