From 032ef197fba7c3018bf5952d52ca4c7505b1195d Mon Sep 17 00:00:00 2001 From: Chris Arriola Date: Wed, 17 Feb 2021 08:05:01 -0800 Subject: [PATCH] feat: Add camera specific flow events and deprecate cameraEvents(). --- .../maps/android/ktx/demo/MainActivity.kt | 21 +++--- .../com/google/maps/android/ktx/GoogleMap.kt | 65 +++++++++++++++++++ 2 files changed, 77 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/google/maps/android/ktx/demo/MainActivity.kt b/app/src/main/java/com/google/maps/android/ktx/demo/MainActivity.kt index f91d3b80..f6258360 100644 --- a/app/src/main/java/com/google/maps/android/ktx/demo/MainActivity.kt +++ b/app/src/main/java/com/google/maps/android/ktx/demo/MainActivity.kt @@ -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 @@ -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 /** @@ -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.") } } } diff --git a/maps-ktx/src/main/java/com/google/maps/android/ktx/GoogleMap.kt b/maps-ktx/src/main/java/com/google/maps/android/ktx/GoogleMap.kt index dafd8e9b..bc5cff0e 100644 --- a/maps-ktx/src/main/java/com/google/maps/android/ktx/GoogleMap.kt +++ b/maps-ktx/src/main/java/com/google/maps/android/ktx/GoogleMap.kt @@ -77,6 +77,9 @@ private fun SendChannel.offerCatching(element: E): Boolean { * [GoogleMap.setOnCameraMoveListener] and [GoogleMap.setOnCameraMoveStartedListener]. */ @ExperimentalCoroutinesApi +@Deprecated( + message = "Use cameraIdleEvents(), cameraMoveCanceledEvents(), cameraMoveEvents() or cameraMoveStartedEvents", +) public fun GoogleMap.cameraEvents(): Flow = callbackFlow { setOnCameraIdleListener { @@ -100,6 +103,7 @@ public fun GoogleMap.cameraEvents(): Flow = } /** +<<<<<<< HEAD * A suspending function that awaits the completion of the [cameraUpdate] animation. * * @param cameraUpdate the [CameraUpdate] to apply on the map @@ -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 = + 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 = + 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 = + 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]. @@ -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 = + callbackFlow { + setOnCameraMoveStartedListener { + offerCatching(Unit) + } + awaitClose{ + setOnCameraMoveStartedListener(null) + } + } + /** * Builds a new [GoogleMapOptions] using the provided [optionsActions]. *