Skip to content

Commit

Permalink
Convert map object to nullable to prevent uninitial property crashes
Browse files Browse the repository at this point in the history
  • Loading branch information
enes.zor committed Oct 10, 2024
1 parent c5dd17c commit d3f3746
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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? {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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? {
Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
}

0 comments on commit d3f3746

Please sign in to comment.