11package com.qonversion.flutter.sdk.qonversion_flutter_sdk
22
3+ import android.app.Activity
34import android.app.Application
45import androidx.annotation.NonNull;
5- import com.android.billingclient.api.BillingClient
6- import com.android.billingclient.api.BillingResult
7- import io.flutter.embedding.engine.plugins.FlutterPlugin
86import io.flutter.plugin.common.MethodCall
97import io.flutter.plugin.common.MethodChannel
108import io.flutter.plugin.common.MethodChannel.MethodCallHandler
@@ -14,99 +12,106 @@ import io.flutter.plugin.common.PluginRegistry.Registrar
1412import com.qonversion.android.sdk.Qonversion;
1513import com.qonversion.android.sdk.QonversionBillingBuilder
1614import com.qonversion.android.sdk.QonversionCallback
17- import io.flutter.embedding.engine.plugins.activity.ActivityAware
18- import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
1915
2016/* * QonversionFlutterSdkPlugin */
21- class QonversionFlutterSdkPlugin : FlutterPlugin , MethodCallHandler , ActivityAware {
22- private lateinit var channel : MethodChannel
23- private var application: Application ? = null
24-
25-
26- override fun onAttachedToEngine (@NonNull flutterPluginBinding : FlutterPlugin .FlutterPluginBinding ) {
27- channel = MethodChannel (flutterPluginBinding.getFlutterEngine().getDartExecutor(), " qonversion_flutter_sdk" )
28- channel.setMethodCallHandler(this );
29- }
30-
31- override fun onDetachedFromEngine (@NonNull binding : FlutterPlugin .FlutterPluginBinding ) {
32- channel.setMethodCallHandler(null )
33- }
34-
35- override fun onAttachedToActivity (binding : ActivityPluginBinding ) {
36- this .application = binding.activity.application
37- }
38-
39- override fun onDetachedFromActivityForConfigChanges () {
40- this .application = null
41- }
42-
43- override fun onReattachedToActivityForConfigChanges (binding : ActivityPluginBinding ) {
44- this .application = binding.activity.application
45- }
46-
47- override fun onDetachedFromActivity () {
48- this .application = null
49- }
50-
51- companion object {
52- @JvmStatic
53- fun registerWith (registrar : Registrar ) {
54- val channel = MethodChannel (registrar.messenger(), " qonversion_flutter_sdk" )
55- channel.setMethodCallHandler(QonversionFlutterSdkPlugin ())
56- }
57- }
58-
59- override fun onMethodCall (@NonNull call : MethodCall , @NonNull result : Result ) {
60- val args = call.arguments as ? Map <String , Any > ? : return result.error(" 1" ,
61- " Please provide arguments" ,
62- " There was no arguments in method call" )
63-
64- val key = args[" key" ] as ? String
65- val internalUserId = args[" userID" ] as ? String
66-
67- when (call.method) {
68- " launchWithKeyCompletion" -> launchWith(key, internalUserId, result)
69- " launchWithKeyUserId" -> launchWith(key, internalUserId, result)
70- " launchWithKeyAutoTrackPurchasesCompletion" -> launchWith(key, internalUserId, result)
71- " addAttributionData" -> result.notImplemented() // since there is no such method in Android SDK
72- else -> result.notImplemented()
17+ class QonversionFlutterSdkPlugin internal constructor(registrar : Registrar ): MethodCallHandler {
18+ private val activity: Activity = registrar.activity()
19+ private val application: Application = activity.application
20+
21+ companion object {
22+ @JvmStatic
23+ fun registerWith (registrar : Registrar ) {
24+ val channel = MethodChannel (registrar.messenger(), " qonversion_flutter_sdk" )
25+ channel.setMethodCallHandler(QonversionFlutterSdkPlugin (registrar))
26+ }
7327 }
74- }
7528
76- private fun launchWith (key : String? , internalUserId : String? , result : Result ) {
77- if (key == null ) {
78- return result.error(" 1" , " Could not find API key" , " Please provide valid API key" )
29+ override fun onMethodCall (@NonNull call : MethodCall , @NonNull result : Result ) {
30+ val args = call.arguments<Map <String , Any >>()
31+
32+ if (args == null || args.isEmpty()) {
33+ result.noArgsError()
34+ return
35+ }
36+
37+ val apiKey = call.argument<String >(" key" )
38+ if (apiKey == null ) {
39+ result.noApiKeyError()
40+ return
41+ }
42+
43+ val internalUserId = args[" userID" ] as ? String ? : " "
44+ val autoTrackPurchases = args[" autoTrackPurchases" ] as ? Boolean ? : true
45+
46+ when (call.method) {
47+ " launch" -> launch(apiKey, args, result)
48+
49+ // TODO remove when old methods get removed on Dart side
50+ " launchWithKeyCompletion" ,
51+ " launchWithKeyUserId" ,
52+ " launchWithKeyAutoTrackPurchasesCompletion" -> launchWith(apiKey, internalUserId, autoTrackPurchases, result)
53+ " addAttributionData" -> result.notImplemented() // since there is no such method in Android SDK
54+ else -> result.notImplemented()
55+ }
7956 }
8057
81- val billingBuilder = buildBilling()
82-
83- val callback = object : QonversionCallback {
84- override fun onSuccess (uid : String ) {
85- result.success(uid)
86- }
87-
88- override fun onError (t : Throwable ) {
89- result.error(" 1" , t.localizedMessage, t.cause.toString())
90- }
58+ private fun launch (apiKey : String , args : Map <String , Any >, result : Result ) {
59+ val userId = args[" userID" ] as ? String ? : " "
60+
61+ val billingBuilder = QonversionBillingBuilder ()
62+ .enablePendingPurchases()
63+ .setListener { _, _ -> }
64+
65+ val callback = object : QonversionCallback {
66+ override fun onSuccess (uid : String ) {
67+ result.success(uid)
68+ }
69+
70+ override fun onError (t : Throwable ) {
71+ result.qonversionError(t.localizedMessage, t.cause.toString())
72+ }
73+ }
74+
75+ Qonversion .initialize(
76+ application,
77+ apiKey,
78+ userId,
79+ billingBuilder,
80+ true ,
81+ callback
82+ )
9183 }
9284
93- val app = application ? : return
94-
95- Qonversion .initialize(
96- app,
97- key,
98- internalUserId ? : " " ,
99- billingBuilder,
100- true ,
101- callback
102- )
103- }
104-
105- private fun buildBilling (): QonversionBillingBuilder {
106- return QonversionBillingBuilder ()
107- .enablePendingPurchases()
108- .setChildDirected(BillingClient .ChildDirected .CHILD_DIRECTED )
109- .setUnderAgeOfConsent(BillingClient .UnderAgeOfConsent .UNSPECIFIED )
110- .setListener { _, _ -> }
111- }
85+ // TODO remove when old methods get removed on Dart side
86+ private fun launchWith (key : String? ,
87+ internalUserId : String = "",
88+ autoTrackPurchases : Boolean = true,
89+ result : Result ) {
90+ if (key == null ) {
91+ return result.error(" 1" , " Could not find API key" , " Please provide valid API key" )
92+ }
93+
94+ val billingBuilder = QonversionBillingBuilder ()
95+ .enablePendingPurchases()
96+ .setListener { _, _ -> }
97+
98+ val callback = object : QonversionCallback {
99+ override fun onSuccess (uid : String ) {
100+ result.success(uid)
101+ }
102+
103+ override fun onError (t : Throwable ) {
104+ result.qonversionError(t.localizedMessage, t.cause.toString())
105+ }
106+ }
107+
108+ Qonversion .initialize(
109+ application,
110+ key,
111+ internalUserId,
112+ billingBuilder,
113+ autoTrackPurchases,
114+ callback
115+ )
116+ }
112117}
0 commit comments