Skip to content

Commit

Permalink
feat: Add camera specific flow events and deprecate cameraEvents(). (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
arriolac authored Apr 7, 2021
1 parent 1cd5586 commit ce77946
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 9 deletions.
21 changes: 12 additions & 9 deletions app/src/main/java/com/google/maps/android/ktx/demo/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ import com.google.maps.android.ktx.awaitMap
import com.google.maps.android.ktx.awaitMapLoad
import com.google.maps.android.ktx.awaitSnapshot
import com.google.maps.android.ktx.cameraEvents
import com.google.maps.android.ktx.awaitMap
import com.google.maps.android.ktx.cameraIdleEvents
import com.google.maps.android.ktx.cameraMoveStartedEvents
import com.google.maps.android.ktx.demo.io.MyItemReader
import com.google.maps.android.ktx.demo.model.MyItem
import com.google.maps.android.ktx.model.cameraPosition
Expand All @@ -54,6 +57,7 @@ import com.google.maps.android.ktx.utils.geojson.geoJsonLayer
import com.google.maps.android.ktx.utils.kml.kmlLayer
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch
import org.json.JSONException

/**
Expand Down Expand Up @@ -94,15 +98,14 @@ class MainActivity : AppCompatActivity() {
}
showMapLayers(googleMap)
addButtonClickListener(googleMap)
googleMap.cameraEvents().collect { event ->
when (event) {
is CameraIdleEvent -> Log.d(TAG, "Camera is idle.")
is CameraMoveCanceledEvent -> Log.d(TAG, "Camera move canceled")
is CameraMoveEvent -> Log.d(TAG, "Camera moved")
is CameraMoveStartedEvent -> Log.d(
TAG,
"Camera moved started. Reason: ${event.reason}"
)
launch {
googleMap.cameraMoveStartedEvents().collect {
Log.d(TAG, "Camera moved.")
}
}
launch {
googleMap.cameraIdleEvents().collect {
Log.d(TAG, "Camera is idle.")
}
}
}
Expand Down
65 changes: 65 additions & 0 deletions maps-ktx/src/main/java/com/google/maps/android/ktx/GoogleMap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ private fun <E> SendChannel<E>.offerCatching(element: E): Boolean {
* [GoogleMap.setOnCameraMoveListener] and [GoogleMap.setOnCameraMoveStartedListener].
*/
@ExperimentalCoroutinesApi
@Deprecated(
message = "Use cameraIdleEvents(), cameraMoveCanceledEvents(), cameraMoveEvents() or cameraMoveStartedEvents",
)
public fun GoogleMap.cameraEvents(): Flow<CameraEvent> =
callbackFlow {
setOnCameraIdleListener {
Expand All @@ -100,6 +103,7 @@ public fun GoogleMap.cameraEvents(): Flow<CameraEvent> =
}

/**
<<<<<<< HEAD
* A suspending function that awaits the completion of the [cameraUpdate] animation.
*
* @param cameraUpdate the [CameraUpdate] to apply on the map
Expand Down Expand Up @@ -132,6 +136,52 @@ public suspend inline fun GoogleMap.awaitMapLoad(): Unit =
}
}

/**
* Returns a flow that emits when the camera is idle. Using this to observe camera idle events will
* override an existing listener (if any) to [GoogleMap.setOnCameraIdleListener].
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.cameraIdleEvents(): Flow<Unit> =
callbackFlow {
setOnCameraIdleListener {
offerCatching(Unit)
}
awaitClose{
setOnCameraIdleListener(null)
}
}

/**
* Returns a flow that emits when a camera move is canceled. Using this to observe camera move
* cancel events will override an existing listener (if any) to
* [GoogleMap.setOnCameraMoveCanceledListener].
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.cameraMoveCanceledEvents(): Flow<Unit> =
callbackFlow {
setOnCameraMoveCanceledListener {
offerCatching(Unit)
}
awaitClose{
setOnCameraMoveCanceledListener(null)
}
}

/**
* Returns a flow that emits when the camera moves. Using this to observe camera move events will
* override an existing listener (if any) to [GoogleMap.setOnCameraMoveListener].
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.cameraMoveEvents(): Flow<Unit> =
callbackFlow {
setOnCameraMoveListener {
offerCatching(Unit)
}
awaitClose{
setOnCameraMoveListener(null)
}
}

/**
* A suspending function that returns a bitmap snapshot of the current view of the map. Uses
* [GoogleMap.snapshot].
Expand All @@ -144,6 +194,21 @@ public suspend inline fun GoogleMap.awaitSnapshot(bitmap: Bitmap? = null): Bitma
snapshot({ continuation.resume(it) }, bitmap)
}

/**
* Returns a flow that emits when a camera move started. Using this to observe camera move start
* events will override an existing listener (if any) to [GoogleMap.setOnCameraMoveStartedListener].
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.cameraMoveStartedEvents(): Flow<Unit> =
callbackFlow {
setOnCameraMoveStartedListener {
offerCatching(Unit)
}
awaitClose{
setOnCameraMoveStartedListener(null)
}
}

/**
* Builds a new [GoogleMapOptions] using the provided [optionsActions].
*
Expand Down

0 comments on commit ce77946

Please sign in to comment.