|
| 1 | +package io.customer.customer_io.location |
| 2 | + |
| 3 | +import io.customer.customer_io.bridge.NativeModuleBridge |
| 4 | +import io.customer.customer_io.bridge.nativeMapArgs |
| 5 | +import io.customer.customer_io.bridge.nativeNoArgs |
| 6 | +import io.customer.customer_io.utils.getAs |
| 7 | +import io.customer.location.LocationModuleConfig |
| 8 | +import io.customer.location.LocationTrackingMode |
| 9 | +import io.customer.location.ModuleLocation |
| 10 | +import io.customer.sdk.CustomerIOBuilder |
| 11 | +import io.customer.sdk.core.di.SDKComponent |
| 12 | +import io.customer.sdk.core.util.Logger |
| 13 | +import io.flutter.embedding.engine.plugins.FlutterPlugin |
| 14 | +import io.flutter.plugin.common.MethodCall |
| 15 | +import io.flutter.plugin.common.MethodChannel |
| 16 | + |
| 17 | +/** |
| 18 | + * Flutter module implementation for location module in native SDKs. All functionality |
| 19 | + * linked with the module should be placed here. |
| 20 | + */ |
| 21 | +internal class CustomerIOLocation( |
| 22 | + pluginBinding: FlutterPlugin.FlutterPluginBinding, |
| 23 | +) : NativeModuleBridge, MethodChannel.MethodCallHandler { |
| 24 | + override val moduleName: String = "Location" |
| 25 | + override val flutterCommunicationChannel: MethodChannel = |
| 26 | + MethodChannel(pluginBinding.binaryMessenger, "customer_io_location") |
| 27 | + private val logger: Logger = SDKComponent.logger |
| 28 | + |
| 29 | + private fun getLocationServices() = runCatching { |
| 30 | + ModuleLocation.instance().locationServices |
| 31 | + }.onFailure { |
| 32 | + logger.error("Location module is not initialized. Ensure location config is provided during SDK initialization.") |
| 33 | + }.getOrNull() |
| 34 | + |
| 35 | + override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { |
| 36 | + when (call.method) { |
| 37 | + "setLastKnownLocation" -> call.nativeMapArgs(result, ::setLastKnownLocation) |
| 38 | + "requestLocationUpdate" -> call.nativeNoArgs(result, ::requestLocationUpdate) |
| 39 | + else -> super.onMethodCall(call, result) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + private fun setLastKnownLocation(params: Map<String, Any>) { |
| 44 | + val latitude = params.getAs<Double>("latitude") |
| 45 | + val longitude = params.getAs<Double>("longitude") |
| 46 | + |
| 47 | + if (latitude == null || longitude == null) { |
| 48 | + logger.error("Latitude and longitude are required for setLastKnownLocation") |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + getLocationServices()?.setLastKnownLocation(latitude, longitude) |
| 53 | + } |
| 54 | + |
| 55 | + private fun requestLocationUpdate() { |
| 56 | + getLocationServices()?.requestLocationUpdate() |
| 57 | + } |
| 58 | + |
| 59 | + override fun configureModule( |
| 60 | + builder: CustomerIOBuilder, |
| 61 | + config: Map<String, Any> |
| 62 | + ) { |
| 63 | + val trackingModeValue = config.getAs<String>("trackingMode") |
| 64 | + val trackingMode = trackingModeValue?.let { value -> |
| 65 | + runCatching { enumValueOf<LocationTrackingMode>(value) }.getOrNull() |
| 66 | + } ?: LocationTrackingMode.MANUAL |
| 67 | + |
| 68 | + val module = ModuleLocation( |
| 69 | + LocationModuleConfig.Builder() |
| 70 | + .setLocationTrackingMode(trackingMode) |
| 71 | + .build() |
| 72 | + ) |
| 73 | + builder.addCustomerIOModule(module) |
| 74 | + } |
| 75 | +} |
0 commit comments