Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ fun W3WMapComponent(
mapConfig = mapConfig,
mapProvider = mapManager.mapProvider,
content = content,
locationSource = locationSource,
mapState = mapState,
onMapTypeClicked = {
mapManager.setMapType(it)
Expand All @@ -66,6 +65,13 @@ fun W3WMapComponent(
onCameraUpdated = {
mapManager.updateCameraState(it)
},
onMyLocationClicked = {
fetchCurrentLocation(
locationSource = locationSource,
mapManager = mapManager,
onError = onError
)
},
onError = onError
)
}
Expand All @@ -79,7 +85,6 @@ fun W3WMapComponent(
* @param layoutConfig [W3WMapDefaults.LayoutConfig] Configuration for the map's layout.
* @param mapConfig [W3WMapDefaults.MapConfig] Configuration for the map's appearance.
* @param mapState The [W3WMapState] object that holds the mapState of the map.
* @param locationSource An optional [W3WLocationSource] used to fetch the user's location.
* @param mapProvider An instance of enum [MapProvider] to define map provide: GoogleMap, MapBox.
* @param content Optional composable content to be displayed on the map.
* @param onMapTypeClicked Callback invoked when the map type is clicked.
Expand All @@ -93,10 +98,10 @@ fun W3WMapComponent(
layoutConfig: W3WMapDefaults.LayoutConfig = W3WMapDefaults.defaultLayoutConfig(),
mapConfig: W3WMapDefaults.MapConfig = W3WMapDefaults.defaultMapConfig(),
mapState: W3WMapState,
locationSource: W3WLocationSource? = null,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are exposing the mapState, the client already has control over the composable. Therefore, the locationSource parameter is unnecessary. Instead, we provide a callback to notify when the MyLocation button is clicked.

mapProvider: MapProvider,
content: (@Composable () -> Unit)? = null,
onMapTypeClicked: ((W3WMapType) -> Unit)? = null,
onMyLocationClicked: (() -> Unit)? = null,
onMapClicked: ((W3WCoordinates) -> Unit)? = null,
onCameraUpdated: (W3WCameraState<*>) -> Unit,
onError: ((W3WError) -> Unit)? = null,
Expand All @@ -108,13 +113,15 @@ fun W3WMapComponent(
mapProvider = mapProvider,
content = content,
mapState = mapState,
locationSource = locationSource,
onMapClicked = {
onMapClicked?.invoke(it)
},
onMapTypeClicked = {
onMapTypeClicked?.invoke(it)
},
onMyLocationClicked = {
onMyLocationClicked?.invoke()
},
onCameraUpdated = {
onCameraUpdated.invoke(it)
},
Expand All @@ -133,7 +140,6 @@ fun W3WMapComponent(
* @param mapConfig [W3WMapDefaults.MapConfig] Configuration for the map's appearance.
* @param mapState The [W3WMapState] object that holds the mapState of the map.
* @param mapProvider An instance of enum [MapProvider] to define map provide: GoogleMap, MapBox.
* @param locationSource An optional [W3WLocationSource] used to fetch the user's location.
* @param content Optional composable content to be displayed on the map.
* @param onMapTypeClicked Callback invoked when the user clicks on the map type button.
* @param onMapClicked Callback invoked when the user clicks on the map.
Expand All @@ -147,9 +153,9 @@ internal fun W3WMapContent(
mapConfig: W3WMapDefaults.MapConfig = W3WMapDefaults.defaultMapConfig(),
mapState: W3WMapState,
mapProvider: MapProvider,
locationSource: W3WLocationSource? = null,
content: (@Composable () -> Unit)? = null,
onMapTypeClicked: ((W3WMapType) -> Unit),
onMyLocationClicked: () -> Unit,
onMapClicked: (W3WCoordinates) -> Unit,
onCameraUpdated: (W3WCameraState<*>) -> Unit,
onError: ((W3WError) -> Unit)? = null,
Expand All @@ -160,11 +166,7 @@ internal fun W3WMapContent(
// Fetch current location when launch
LaunchedEffect(Unit) {
if (mapState.isMyLocationEnabled) {
fetchCurrentLocation(
locationSource = locationSource,
mapState = mapState,
onError = onError
)
onMyLocationClicked.invoke()
}
}

Expand All @@ -186,13 +188,7 @@ internal fun W3WMapContent(
modifier = Modifier
.align(Alignment.BottomEnd)
.padding(layoutConfig.contentPadding),
onMyLocationClicked = {
fetchCurrentLocation(
locationSource = locationSource,
mapState = mapState,
onError = onError
)
},
onMyLocationClicked = onMyLocationClicked,
onMapTypeClicked = onMapTypeClicked,
)
}
Expand Down Expand Up @@ -307,23 +303,22 @@ internal fun W3WMapView(
* This function is responsible for update camera position and button state based on current location
*
* @param locationSource An optional [W3WLocationSource] used to fetch the user's location.
* @param mapState The [W3WMapState] object that holds the mapState of the map.
* @param mapManager The [W3WMapManager] instance that manages the map's mapState and interactions.
* @param onError Callback invoked when an error occurs during map initialization or interaction.
*/
fun fetchCurrentLocation(
private fun fetchCurrentLocation(
locationSource: W3WLocationSource?,
mapState: W3WMapState,
mapManager: W3WMapManager,
onError: ((W3WError) -> Unit)? = null
) {
// Fetch location
locationSource?.fetchLocation(
onLocationFetched = { location ->
// Update camera state
mapState.cameraState?.moveToPosition(
mapManager.moveToPosition(
coordinates = W3WCoordinates(location.latitude, location.longitude),
animate = true
)

//TODO: Update button state
},
onError = { error ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,17 @@ class W3WMapManager(
mapState.value.cameraState?.orientCamera()
}

fun moveToPosition(coordinates: W3WCoordinates) {
mapState.value.cameraState?.moveToPosition(coordinates, true)
fun moveToPosition(coordinates: W3WCoordinates, animate: Boolean) {
mapState.value.cameraState?.moveToPosition(coordinates, animate)
}

fun updateCameraState(newCameraState: W3WCameraState<*>) {
CoroutineScope(Dispatchers.IO).launch {
val newGridLine = calculateGridPolylines(newCameraState)
_mapState.update {
it.copy(
cameraState = newCameraState,
gridLines = calculateGridPolylines(newCameraState)
gridLines = newGridLine
)
}
}
Expand Down Expand Up @@ -304,6 +305,7 @@ class W3WMapManager(
companion object {
val CAMERA_POSITION_DEFAULT =
W3WCameraPosition(W3WCoordinates(51.521251, -0.203586), 19f, 0f, 0f)
val MAKER_COLOR_DEFAULT = W3WMarkerColor(background = Color.Red, slashesColor = Color.Yellow)
val MAKER_COLOR_DEFAULT =
W3WMarkerColor(background = Color.Red, slashesColor = Color.Yellow)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ data class W3WMapState(
val listMakers: Map<String, List<W3WMarker>> = emptyMap(),

// Control camera position of map
val cameraState: W3WCameraState<*>? = null,
internal val cameraState: W3WCameraState<*>? = null,

// data class handling draw grid lines on map
internal val gridLines: W3WGridLines = W3WGridLines(),
Expand Down