package com.rtnmapsgl import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.util.Log import android.view.ViewGroup import androidx.fragment.app.Fragment import androidx.compose.ui.platform.ComposeView import androidx.lifecycle.Observer import androidx.lifecycle.ViewModelProvider import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReadableArray import com.facebook.react.bridge.ReadableMap import com.rtnmapsgl.composable.ComposeMain class MapsGLFragment(private var reactContext: ReactApplicationContext, private var reactNativeViewId: Int) : Fragment() { private var viewModel: MainViewModel? = null private var aerisId: String? = null private var aerisSecretKey: String? = null private var mapboxAccessToken: String? = null private var mapLayerState: ReadableMap? = null private var pins: ReadableArray? = null private var selectedPin: ReadableMap? = null private var styleUrl: String? = null private var userLocation: DoubleArray? = null override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { super.onCreateView(inflater, container, savedInstanceState) val composeView = ComposeView(requireNotNull(context)) composeView.setContent { viewModel = ViewModelProvider(requireActivity()).get(MainViewModel::class.java) ComposeMain(viewModel!!, reactContext, reactNativeViewId) } return composeView } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) viewModel?.isWebViewReady?.observe(viewLifecycleOwner, Observer { ready -> if (ready) { updatePropsOnModelInitialization() } }) } private fun updatePropsOnModelInitialization() { Log.d("MapsGLFragment", "CONNOR - updatePropsOnModelInitialization called") aerisId?.let { setAerisId(it) } aerisSecretKey?.let { setAerisSecretKey(it) } mapboxAccessToken?.let { setMapboxAccessToken(it) } mapLayerState?.let { setMapLayerState(it) } pins?.let { updatePins(it) } selectedPin?.let { setSelectedPin(it) } styleUrl?.let { setStyleUrl(it) } userLocation?.let { setUserLocation(it) } } fun setAerisId(aerisId: String) { this.aerisId = aerisId viewModel?.jsBuilder?.setAerisId(aerisId) } fun setAerisSecretKey(aerisSecretKey: String) { this.aerisSecretKey = aerisSecretKey viewModel?.jsBuilder?.setAerisSecret(aerisSecretKey) } fun setMapboxAccessToken(mapboxAccessToken: String) { this.mapboxAccessToken = mapboxAccessToken viewModel?.jsBuilder?.setMapboxAccessToken(mapboxAccessToken) } fun setMapLayerState(mapLayerState: ReadableMap) { this.mapLayerState = mapLayerState viewModel?.jsBuilder?.setMapLayerState(mapLayerState) } fun setSelectedPin(selectedPin: ReadableMap) { this.selectedPin = selectedPin viewModel?.jsBuilder?.setSelectedPin(selectedPin) } fun updatePins(pins: ReadableArray) { this.pins = pins viewModel?.jsBuilder?.updatePins(pins) // Log.d("MapsGLFragment", "CONNOR - updatePins called with: $pins") } fun setStyleUrl(styleUrl: String) { this.styleUrl = styleUrl viewModel?.jsBuilder?.setStyleUrl(styleUrl) } fun setUserLocation(userLocation: DoubleArray?) { this.userLocation = userLocation // Log.d("MapsGLFragment", "CONNOR - setUserLocation called with: ${userLocation?.contentToString()}") if (userLocation != null) { // view.setUserLocation(userLocation) } else { // view.setUserLocation(null) } } override fun onPause() { super.onPause() Log.d("MapsGLFragment", "CONNOR - onPause called") // do any logic that should happen in an `onPause` method // e.g.: customView.onPause(); } override fun onResume() { super.onResume() viewModel?.isWebViewReady?.observe(viewLifecycleOwner, Observer { ready -> Log.d("MapsGLFragment", "CONNOR - isWebViewReady observer called from onResume with: $ready") if (ready) { updatePropsOnModelInitialization() } }) Log.d("MapsGLFragment", "CONNOR - onResume called") // do any logic that should happen in an `onResume` method // e.g.: customView.onResume(); } override fun onDestroy() { super.onDestroy() Log.d("MapsGLFragment", "CONNOR - onDestroy called") // do any logic that should happen in an `onDestroy` method // e.g.: customView.onDestroy(); } }