Skip to content

Commit 04c9971

Browse files
authored
Merge pull request #418 from Xerus19573/main
Update plugin to support modern Flutter plugin API and AGP
2 parents dfd2d8e + 0ff7f94 commit 04c9971

File tree

2 files changed

+30
-28
lines changed

2 files changed

+30
-28
lines changed

android/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ apply plugin: "com.android.library"
2525
apply plugin: "kotlin-android"
2626

2727
android {
28+
namespace "id.oddbit.flutter.facebook_app_events"
2829
// Conditional for compatibility with AGP < 4.2
2930
if (project.android.hasProperty("namespace")) {
3031
namespace "id.oddbit.flutter.facebook_app_events"

android/src/main/kotlin/id/oddbit/flutter/facebook_app_events/FacebookAppEventsPlugin.kt

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class FacebookAppEventsPlugin: FlutterPlugin, MethodCallHandler {
6565
}
6666

6767
private fun handleSetUserData(call: MethodCall, result: Result) {
68-
val parameters = call.argument("parameters") as? Map<String, Object>
68+
val parameters = call.argument<Map<String, Any>>("parameters") ?: mapOf()
6969
val parameterBundle = createBundleFromMap(parameters)
7070

7171
AppEventsLogger.setUserData(
@@ -115,9 +115,13 @@ class FacebookAppEventsPlugin: FlutterPlugin, MethodCallHandler {
115115
}
116116

117117
private fun handleLogEvent(call: MethodCall, result: Result) {
118-
val eventName = call.argument("name") as? String
119-
val parameters = call.argument("parameters") as? Map<String, Object>
120-
val valueToSum = call.argument("_valueToSum") as? Double
118+
val eventName = call.argument<String>("name")
119+
if (eventName == null) {
120+
result.error("INVALID_ARGUMENT", "Event name is required and cannot be null.", null)
121+
return
122+
}
123+
val parameters = call.argument<Map<String, Any>>("parameters")
124+
val valueToSum = call.argument<Double>("_valueToSum")
121125

122126
if (valueToSum != null && parameters != null) {
123127
val parameterBundle = createBundleFromMap(parameters)
@@ -135,8 +139,8 @@ class FacebookAppEventsPlugin: FlutterPlugin, MethodCallHandler {
135139
}
136140

137141
private fun handlePushNotificationOpen(call: MethodCall, result: Result) {
138-
val action = call.argument("action") as? String
139-
val payload = call.argument("payload") as? Map<String, Object>
142+
val action = call.argument<String>("action")
143+
val payload = call.argument<Map<String, Any>>("payload")
140144
val payloadBundle = createBundleFromMap(payload)!!
141145

142146
if (action != null) {
@@ -163,21 +167,18 @@ class FacebookAppEventsPlugin: FlutterPlugin, MethodCallHandler {
163167
for (jsonParam in parameterMap.entries) {
164168
val value = jsonParam.value
165169
val key = jsonParam.key
166-
if (value is String) {
167-
bundle.putString(key, value as String)
168-
} else if (value is Int) {
169-
bundle.putInt(key, value as Int)
170-
} else if (value is Long) {
171-
bundle.putLong(key, value as Long)
172-
} else if (value is Double) {
173-
bundle.putDouble(key, value as Double)
174-
} else if (value is Boolean) {
175-
bundle.putBoolean(key, value as Boolean)
176-
} else if (value is Map<*, *>) {
177-
val nestedBundle = createBundleFromMap(value as Map<String, Any>)
178-
bundle.putBundle(key, nestedBundle as Bundle)
179-
} else {
180-
throw IllegalArgumentException(
170+
when (value) {
171+
is String -> bundle.putString(key, value)
172+
is Int -> bundle.putInt(key, value)
173+
is Long -> bundle.putLong(key, value)
174+
is Double -> bundle.putDouble(key, value)
175+
is Boolean -> bundle.putBoolean(key, value)
176+
is Map<*, *> -> {
177+
@Suppress("UNCHECKED_CAST")
178+
val nestedBundle = createBundleFromMap(value as Map<String, Any>)
179+
bundle.putBundle(key, nestedBundle)
180+
}
181+
else -> throw IllegalArgumentException(
181182
"Unsupported value type: " + value.javaClass.kotlin)
182183
}
183184
}
@@ -191,21 +192,21 @@ class FacebookAppEventsPlugin: FlutterPlugin, MethodCallHandler {
191192
}
192193

193194
private fun handleSetDataProcessingOptions(call: MethodCall, result: Result) {
194-
val options = call.argument("options") as? ArrayList<String> ?: arrayListOf()
195-
val country = call.argument("country") as? Int ?: 0
196-
val state = call.argument("state") as? Int ?: 0
195+
val options = call.argument<ArrayList<String>>("options") ?: arrayListOf()
196+
val country = call.argument<Int>("country") ?: 0
197+
val state = call.argument<Int>("state") ?: 0
197198

198199
FacebookSdk.setDataProcessingOptions(options.toTypedArray(), country, state)
199200
result.success(null)
200201
}
201202

202203
private fun handlePurchased(call: MethodCall, result: Result) {
203-
var amount = (call.argument("amount") as? Double)?.toBigDecimal()
204-
var currency = Currency.getInstance(call.argument("currency") as? String)
205-
val parameters = call.argument("parameters") as? Map<String, Object>
204+
val amount = (call.argument<Double>("amount"))?.toBigDecimal()
205+
val currency = Currency.getInstance(call.argument<String>("currency"))
206+
val parameters = call.argument<Map<String, Any>>("parameters")
206207
val parameterBundle = createBundleFromMap(parameters) ?: Bundle()
207208

208209
appEventsLogger.logPurchase(amount, currency, parameterBundle)
209210
result.success(null)
210211
}
211-
}
212+
}

0 commit comments

Comments
 (0)