From d3f3746db3b25afbc8774ed46d6980c2907cc7e7 Mon Sep 17 00:00:00 2001 From: "enes.zor" Date: Thu, 10 Oct 2024 09:51:30 +0300 Subject: [PATCH] Convert map object to nullable to prevent uninitial property crashes --- .../maplibrary/GoogleMapsOperations.kt | 57 +++++++++++-------- .../maplibrary/HuaweiMapsOperations.kt | 57 +++++++++++-------- 2 files changed, 67 insertions(+), 47 deletions(-) diff --git a/mapskit/src/main/java/com/trendyol/mapskit/maplibrary/GoogleMapsOperations.kt b/mapskit/src/main/java/com/trendyol/mapskit/maplibrary/GoogleMapsOperations.kt index 5df3ca6..c6cb836 100644 --- a/mapskit/src/main/java/com/trendyol/mapskit/maplibrary/GoogleMapsOperations.kt +++ b/mapskit/src/main/java/com/trendyol/mapskit/maplibrary/GoogleMapsOperations.kt @@ -11,6 +11,7 @@ import com.google.android.gms.maps.MapView import com.google.android.gms.maps.OnMapReadyCallback import com.trendyol.mapskit.maplibrary.listeners.* import com.trendyol.mapskit.maplibrary.model.CameraPosition +import com.trendyol.mapskit.maplibrary.model.LatLng import com.trendyol.mapskit.maplibrary.model.Marker import com.trendyol.mapskit.maplibrary.model.MarkerOptions @@ -19,9 +20,9 @@ class GoogleMapsOperations(context: Context) : OnMapReadyCallback, MapsLifeCycle { - private lateinit var googleMap: GoogleMap + private var googleMap: GoogleMap? = null private var mapView: MapView? = null - private lateinit var onMapReadyListener: IOnMapReadyCallback + private var onMapReadyListener: IOnMapReadyCallback? = null private val cameraUpdateProvider = GoogleCameraUpdateProvider() private var isLiteModeEnabled: Boolean? = null @@ -32,7 +33,7 @@ class GoogleMapsOperations(context: Context) : override fun onMapReady(map: GoogleMap) { googleMap = map isLiteModeEnabled?.let { setLiteMode(it) } - onMapReadyListener.onMapReady(this) + onMapReadyListener?.onMapReady(this) } override fun getMapView(): View? { @@ -49,98 +50,102 @@ class GoogleMapsOperations(context: Context) : } override fun setCompassEnabled(isCompassEnabled: Boolean) { - googleMap.uiSettings?.isCompassEnabled = isCompassEnabled + googleMap?.uiSettings?.isCompassEnabled = isCompassEnabled } override fun setAllGesturesEnabled(allGesturesEnabled: Boolean) { - googleMap.uiSettings?.setAllGesturesEnabled(allGesturesEnabled) + googleMap?.uiSettings?.setAllGesturesEnabled(allGesturesEnabled) } override fun setMyLocationButtonEnabled(isMyLocationButtonEnabled: Boolean) { - googleMap.uiSettings?.isMyLocationButtonEnabled = isMyLocationButtonEnabled + googleMap?.uiSettings?.isMyLocationButtonEnabled = isMyLocationButtonEnabled } @RequiresPermission(ACCESS_COARSE_LOCATION) override fun setMyLocationEnabled(isMyLocationEnabled: Boolean) { - googleMap.isMyLocationEnabled = isMyLocationEnabled + googleMap?.isMyLocationEnabled = isMyLocationEnabled } override fun setMinZoomPreference(zoomLevel: Float) { - googleMap.setMinZoomPreference(zoomLevel) + googleMap?.setMinZoomPreference(zoomLevel) } override fun getCameraPosition(): CameraPosition { return CameraPosition( - target = googleMap.cameraPosition.target.toMapsKitLatLng(), - zoom = googleMap.cameraPosition.zoom + target = googleMap?.cameraPosition?.target?.toMapsKitLatLng() ?: LatLng( + DEFAULT_LATITUDE, + DEFAULT_LONGITUDE + ), + zoom = googleMap?.cameraPosition?.zoom ?: ZOOM_LEVEL_STREET ) } override fun setOnMapClickListener(onMapClickListener: IOnMapClickListener) { - googleMap.setOnMapClickListener { latLng -> onMapClickListener.onMapClick(latLng.toMapsKitLatLng()) } + googleMap?.setOnMapClickListener { latLng -> onMapClickListener.onMapClick(latLng.toMapsKitLatLng()) } } override fun animateCamera(cameraUpdate: CameraUpdate, duration: Int?) { val googleCameraUpdate = cameraUpdateProvider.provide(cameraUpdate) if (duration == null) { - googleMap.animateCamera(googleCameraUpdate) + googleMap?.animateCamera(googleCameraUpdate) } else { - googleMap.animateCamera(googleCameraUpdate, duration, null) + googleMap?.animateCamera(googleCameraUpdate, duration, null) } } override fun moveCamera(cameraUpdate: CameraUpdate) { val googleCameraUpdate = cameraUpdateProvider.provide(cameraUpdate) - googleMap.moveCamera(googleCameraUpdate) + googleMap?.moveCamera(googleCameraUpdate) } override fun setOnMarkerClickListener(onMarkerClickListener: IOnMarkerClickListener) { - googleMap.setOnMarkerClickListener { + googleMap?.setOnMarkerClickListener { onMarkerClickListener.onMarkerClick(it.toMapsKitMarker()) } } override fun setOnMapLoadedCallback(onMapLoadedListener: IOnMapLoadedCallback) { - googleMap.setOnMapLoadedCallback { + googleMap?.setOnMapLoadedCallback { onMapLoadedListener.onMapLoaded() } } override fun setOnCameraIdleListener(onCameraIdleListener: IOnCameraIdleListener) { - googleMap.setOnCameraIdleListener { + googleMap?.setOnCameraIdleListener { onCameraIdleListener.onCameraIdle() } } override fun setOnCameraMoveStartedListener(onCameraMoveStartedListener: IOnCameraMoveStartedListener) { - googleMap.setOnCameraMoveStartedListener { + googleMap?.setOnCameraMoveStartedListener { val reason = MapCameraReason.of(it) onCameraMoveStartedListener.onCameraMoveStarted(reason) } } override fun addMarker(markerOptions: MarkerOptions, tag: Any?): Marker? { - val googleMarker = googleMap.addMarker(markerOptions.toGoogleMarkerOptions()) ?: return null + val googleMarker = + googleMap?.addMarker(markerOptions.toGoogleMarkerOptions()) ?: return null googleMarker.tag = tag googleMarker.title = markerOptions.title return googleMarker.toMapsKitMarker() } override fun setLiteMode(isLiteModeEnabled: Boolean) { - if (::googleMap.isInitialized) { + if (googleMap != null) { val options = GoogleMapOptions().liteMode(isLiteModeEnabled) - googleMap.mapType = options.mapType + googleMap?.mapType = options.mapType } else { this.isLiteModeEnabled = isLiteModeEnabled } } override fun setOnZoomControlsListener(isZoomControlsEnabled: Boolean) { - googleMap.uiSettings.isZoomControlsEnabled = isZoomControlsEnabled + googleMap?.uiSettings?.isZoomControlsEnabled = isZoomControlsEnabled } override fun clear() { - googleMap.clear() + googleMap?.clear() } override fun onSaveInstanceState(bundle: Bundle) { @@ -170,4 +175,10 @@ class GoogleMapsOperations(context: Context) : override fun onLowMemory() { mapView?.onLowMemory() } + + companion object { + const val ZOOM_LEVEL_STREET = 18F + const val DEFAULT_LATITUDE = 41.046555 + const val DEFAULT_LONGITUDE = 29.033402 + } } diff --git a/mapskit/src/main/java/com/trendyol/mapskit/maplibrary/HuaweiMapsOperations.kt b/mapskit/src/main/java/com/trendyol/mapskit/maplibrary/HuaweiMapsOperations.kt index cff5671..01f5b72 100644 --- a/mapskit/src/main/java/com/trendyol/mapskit/maplibrary/HuaweiMapsOperations.kt +++ b/mapskit/src/main/java/com/trendyol/mapskit/maplibrary/HuaweiMapsOperations.kt @@ -27,9 +27,9 @@ class HuaweiMapsOperations(context: Context) : OnMapReadyCallback, MapsLifeCycle { - private lateinit var huaweiMap: HuaweiMap + private var huaweiMap: HuaweiMap? = null private var mapView: MapView? = null - private lateinit var onMapReadyListener: IOnMapReadyCallback + private var onMapReadyListener: IOnMapReadyCallback? = null private val cameraUpdateProvider = HuaweiCameraUpdateProvider() private var isLiteModeEnabled: Boolean? = null @@ -41,7 +41,7 @@ class HuaweiMapsOperations(context: Context) : override fun onMapReady(map: HuaweiMap) { huaweiMap = map isLiteModeEnabled?.let { setLiteMode(it) } - onMapReadyListener.onMapReady(this) + onMapReadyListener?.onMapReady(this) } override fun getMapView(): View? { @@ -58,99 +58,103 @@ class HuaweiMapsOperations(context: Context) : } override fun setCompassEnabled(isCompassEnabled: Boolean) { - huaweiMap.uiSettings?.isCompassEnabled = isCompassEnabled + huaweiMap?.uiSettings?.isCompassEnabled = isCompassEnabled } override fun setAllGesturesEnabled(allGesturesEnabled: Boolean) { - huaweiMap.uiSettings?.setAllGesturesEnabled(allGesturesEnabled) + huaweiMap?.uiSettings?.setAllGesturesEnabled(allGesturesEnabled) } override fun setMyLocationButtonEnabled(isMyLocationButtonEnabled: Boolean) { - huaweiMap.uiSettings?.isMyLocationButtonEnabled = isMyLocationButtonEnabled + huaweiMap?.uiSettings?.isMyLocationButtonEnabled = isMyLocationButtonEnabled } @RequiresPermission(Manifest.permission.ACCESS_COARSE_LOCATION) override fun setMyLocationEnabled(isMyLocationEnabled: Boolean) { - huaweiMap.isMyLocationEnabled = isMyLocationEnabled + huaweiMap?.isMyLocationEnabled = isMyLocationEnabled } override fun setMinZoomPreference(zoomLevel: Float) { - huaweiMap.setMinZoomPreference(zoomLevel) + huaweiMap?.setMinZoomPreference(zoomLevel) } override fun setOnMapClickListener(onMapClickListener: IOnMapClickListener) { - huaweiMap.setOnMapClickListener { latLng -> onMapClickListener.onMapClick(latLng.toMapsKitLatLng()) } + huaweiMap?.setOnMapClickListener { latLng -> onMapClickListener.onMapClick(latLng.toMapsKitLatLng()) } } override fun animateCamera(cameraUpdate: CameraUpdate, duration: Int?) { val huaweiCameraUpdate = cameraUpdateProvider.provide(cameraUpdate) if (duration == null) { - huaweiMap.animateCamera(huaweiCameraUpdate) + huaweiMap?.animateCamera(huaweiCameraUpdate) } else { - huaweiMap.animateCamera(huaweiCameraUpdate, duration, null) + huaweiMap?.animateCamera(huaweiCameraUpdate, duration, null) } } override fun moveCamera(cameraUpdate: CameraUpdate) { val huaweiCameraUpdate = cameraUpdateProvider.provide(cameraUpdate) - huaweiMap.moveCamera(huaweiCameraUpdate) + huaweiMap?.moveCamera(huaweiCameraUpdate) } override fun getCameraPosition(): CameraPosition { - val cameraPosition = huaweiMap.cameraPosition + val cameraPosition = huaweiMap?.cameraPosition return CameraPosition( - LatLng(cameraPosition.target.latitude, cameraPosition.target.longitude), - cameraPosition.zoom + target = LatLng( + latitude = cameraPosition?.target?.latitude ?: DEFAULT_LATITUDE, + longitude = cameraPosition?.target?.longitude ?: DEFAULT_LONGITUDE + ), + zoom = cameraPosition?.zoom ?: ZOOM_LEVEL_STREET ) } override fun setOnMarkerClickListener(onMarkerClickListener: IOnMarkerClickListener) { - huaweiMap.setOnMarkerClickListener { + huaweiMap?.setOnMarkerClickListener { onMarkerClickListener.onMarkerClick(it.toMapsKitMarker()) } } override fun setOnMapLoadedCallback(onMapLoadedListener: IOnMapLoadedCallback) { - huaweiMap.setOnMapLoadedCallback { + huaweiMap?.setOnMapLoadedCallback { onMapLoadedListener.onMapLoaded() } } override fun setOnCameraIdleListener(onCameraIdleListener: IOnCameraIdleListener) { - huaweiMap.setOnCameraIdleListener { + huaweiMap?.setOnCameraIdleListener { onCameraIdleListener.onCameraIdle() } } override fun setOnCameraMoveStartedListener(onCameraMoveStartedListener: IOnCameraMoveStartedListener) { - huaweiMap.setOnCameraMoveStartedListener { + huaweiMap?.setOnCameraMoveStartedListener { val reason = MapCameraReason.of(it) onCameraMoveStartedListener.onCameraMoveStarted(reason) } } override fun setOnZoomControlsListener(isZoomControlsEnabled: Boolean) { - huaweiMap.uiSettings.isZoomControlsEnabled = isZoomControlsEnabled + huaweiMap?.uiSettings?.isZoomControlsEnabled = isZoomControlsEnabled } override fun addMarker(markerOptions: MarkerOptions, tag: Any?): Marker? { - val huaweiMarker = huaweiMap.addMarker(markerOptions.toHuaweiMarkerOptions()) ?: return null + val huaweiMarker = + huaweiMap?.addMarker(markerOptions.toHuaweiMarkerOptions()) ?: return null huaweiMarker.tag = tag huaweiMarker.title = markerOptions.title return huaweiMarker.toMapsKitMarker() } override fun setLiteMode(isLiteModeEnabled: Boolean) { - if (::huaweiMap.isInitialized) { + if (huaweiMap != null) { val options = HuaweiMapOptions().liteMode(isLiteModeEnabled) - huaweiMap.mapType = options.mapType + huaweiMap?.mapType = options.mapType } else { this.isLiteModeEnabled = isLiteModeEnabled } } override fun clear() { - huaweiMap.clear() + huaweiMap?.clear() } override fun onSaveInstanceState(bundle: Bundle) { @@ -181,4 +185,9 @@ class HuaweiMapsOperations(context: Context) : mapView?.onLowMemory() } + companion object { + const val ZOOM_LEVEL_STREET = 18F + const val DEFAULT_LATITUDE = 41.046555 + const val DEFAULT_LONGITUDE = 29.033402 + } } \ No newline at end of file