diff --git a/flutter/android/src/main/kotlin/io/sentry/flutter/SentryFlutterPlugin.kt b/flutter/android/src/main/kotlin/io/sentry/flutter/SentryFlutterPlugin.kt index e869834171..00819cc1ed 100644 --- a/flutter/android/src/main/kotlin/io/sentry/flutter/SentryFlutterPlugin.kt +++ b/flutter/android/src/main/kotlin/io/sentry/flutter/SentryFlutterPlugin.kt @@ -287,86 +287,22 @@ class SentryFlutterPlugin : FlutterPlugin, MethodCallHandler, ActivityAware { } private fun setUser(user: Map?, result: Result) { - if (user == null) { + if (user != null) { + val options = HubAdapter.getInstance().options + val userInstance = User.fromMap(user, options) + Sentry.setUser(userInstance) + } else { Sentry.setUser(null) - result.success("") - return } - - val userInstance = User() - val userData = mutableMapOf() - val unknown = mutableMapOf() - - (user["email"] as? String)?.let { userInstance.email = it } - (user["id"] as? String)?.let { userInstance.id = it } - (user["username"] as? String)?.let { userInstance.username = it } - (user["ip_address"] as? String)?.let { userInstance.ipAddress = it } - (user["segment"] as? String)?.let { userInstance.segment = it } - (user["name"] as? String)?.let { userInstance.name = it } - (user["geo"] as? Map)?.let { - val geo = Geo() - geo.city = it["city"] as? String - geo.countryCode = it["country_code"] as? String - geo.region = it["region"] as? String - userInstance.geo = geo - } - - (user["extras"] as? Map)?.let { extras -> - for ((key, value) in extras.entries) { - if (value != null) { - userData[key] = value.toString() - } - } - } - (user["data"] as? Map)?.let { data -> - for ((key, value) in data.entries) { - if (value != null) { - // data has precedence over extras - userData[key] = value.toString() - } - } - } - - if (userData.isNotEmpty()) { - userInstance.data = userData - } - if (unknown.isNotEmpty()) { - userInstance.unknown = unknown - } - - Sentry.setUser(userInstance) - result.success("") } private fun addBreadcrumb(breadcrumb: Map?, result: Result) { - if (breadcrumb == null) { - result.success("") - return + if (breadcrumb != null) { + val options = HubAdapter.getInstance().options + val breadcrumbInstance = Breadcrumb.fromMap(breadcrumb, options) + Sentry.addBreadcrumb(breadcrumbInstance) } - val breadcrumbInstance = Breadcrumb() - - (breadcrumb["message"] as? String)?.let { breadcrumbInstance.message = it } - (breadcrumb["type"] as? String)?.let { breadcrumbInstance.type = it } - (breadcrumb["category"] as? String)?.let { breadcrumbInstance.category = it } - (breadcrumb["level"] as? String)?.let { - breadcrumbInstance.level = when (it) { - "fatal" -> SentryLevel.FATAL - "warning" -> SentryLevel.WARNING - "info" -> SentryLevel.INFO - "debug" -> SentryLevel.DEBUG - "error" -> SentryLevel.ERROR - else -> SentryLevel.INFO - } - } - (breadcrumb["data"] as? Map)?.let { data -> - for ((key, value) in data.entries) { - breadcrumbInstance.data[key] = value - } - } - - Sentry.addBreadcrumb(breadcrumbInstance) - result.success("") } diff --git a/flutter/ios/Classes/SentryFlutterPluginApple.swift b/flutter/ios/Classes/SentryFlutterPluginApple.swift index 0dd162e3f9..d5baa5fd0f 100644 --- a/flutter/ios/Classes/SentryFlutterPluginApple.swift +++ b/flutter/ios/Classes/SentryFlutterPluginApple.swift @@ -566,47 +566,9 @@ public class SentryFlutterPluginApple: NSObject, FlutterPlugin { } } - // swiftlint:disable:next cyclomatic_complexity private func setUser(user: [String: Any?]?, result: @escaping FlutterResult) { if let user = user { - let userInstance = User() - - if let email = user["email"] as? String { - userInstance.email = email - } - if let id = user["id"] as? String { - userInstance.userId = id - } - if let username = user["username"] as? String { - userInstance.username = username - } - if let ipAddress = user["ip_address"] as? String { - userInstance.ipAddress = ipAddress - } - if let segment = user["segment"] as? String { - userInstance.segment = segment - } - if let extras = user["extras"] as? [String: Any] { - userInstance.data = extras - } - if let data = user["data"] as? [String: Any] { - if let oldData = userInstance.data { - userInstance.data = oldData.reduce(into: data) { (first, second) in first[second.0] = second.1 } - } else { - userInstance.data = data - } - } - if let name = user["name"] as? String { - userInstance.name = name - } - if let geoData = user["geo"] as? [String: Any] { - let geo = Geo() - geo.city = geoData["city"] as? String - geo.countryCode = geoData["country_code"] as? String - geo.region = geoData["region"] as? String - userInstance.geo = geo - } - + let userInstance = PrivateSentrySDKOnly.user(with: user) SentrySDK.setUser(userInstance) } else { SentrySDK.setUser(nil) @@ -614,51 +576,11 @@ public class SentryFlutterPluginApple: NSObject, FlutterPlugin { result("") } - // swiftlint:disable:next cyclomatic_complexity private func addBreadcrumb(breadcrumb: [String: Any?]?, result: @escaping FlutterResult) { - guard let breadcrumb = breadcrumb else { - result("") - return + if let breadcrumb = breadcrumb { + let breadcrumbInstance = PrivateSentrySDKOnly.breadcrumb(with: breadcrumb) + SentrySDK.addBreadcrumb(breadcrumbInstance) } - - let breadcrumbInstance = Breadcrumb() - - if let message = breadcrumb["message"] as? String { - breadcrumbInstance.message = message - } - if let type = breadcrumb["type"] as? String { - breadcrumbInstance.type = type - } - if let category = breadcrumb["category"] as? String { - breadcrumbInstance.category = category - } - if let level = breadcrumb["level"] as? String { - switch level { - case "fatal": - breadcrumbInstance.level = SentryLevel.fatal - case "warning": - breadcrumbInstance.level = SentryLevel.warning - case "info": - breadcrumbInstance.level = SentryLevel.info - case "debug": - breadcrumbInstance.level = SentryLevel.debug - case "error": - breadcrumbInstance.level = SentryLevel.error - default: - breadcrumbInstance.level = SentryLevel.error - } - } - if let data = breadcrumb["data"] as? [String: Any] { - breadcrumbInstance.data = data - } - - if let timestampValue = breadcrumb["timestamp"] as? String, - let timestamp = dateFrom(iso8601String: timestampValue) { - breadcrumbInstance.timestamp = timestamp - } - - SentrySDK.addBreadcrumb(breadcrumbInstance) - result("") }