Skip to content

Commit

Permalink
feat: Add indoorStateChangeEvents()
Browse files Browse the repository at this point in the history
  • Loading branch information
arriolac committed Jun 4, 2021
1 parent 92c34aa commit cf8707c
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 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 @@ -26,6 +26,7 @@ import com.google.android.gms.maps.model.Circle
import com.google.android.gms.maps.model.CircleOptions
import com.google.android.gms.maps.model.GroundOverlay
import com.google.android.gms.maps.model.GroundOverlayOptions
import com.google.android.gms.maps.model.IndoorBuilding
import com.google.android.gms.maps.model.Marker
import com.google.android.gms.maps.model.MarkerOptions
import com.google.android.gms.maps.model.Polygon
Expand Down Expand Up @@ -63,6 +64,23 @@ public object CameraMoveCanceledEvent : CameraEvent()
public object CameraMoveEvent : CameraEvent()
public data class CameraMoveStartedEvent(@MoveStartedReason val reason: Int) : CameraEvent()

/**
* Change event when the indoor state changes. See [GoogleMap.OnIndoorStateChangeListener]
*/
public sealed class IndoorChangeEvent

/**
* Change event when an indoor building is focused.
* See [GoogleMap.OnIndoorStateChangeListener.onIndoorBuildingFocused]
*/
public object IndoorBuildingFocusedEvent : IndoorChangeEvent()

/**
* Change event when an indoor level is activated.
* See [GoogleMap.OnIndoorStateChangeListener.onIndoorLevelActivated]
*/
public data class IndoorLevelActivatedEvent(val building: IndoorBuilding) : IndoorChangeEvent()

// Since offer() can throw when the channel is closed (channel can close before the
// block within awaitClose), wrap `offer` calls inside `runCatching`.
// See: https://github.com/Kotlin/kotlinx.coroutines/issues/974
Expand Down Expand Up @@ -239,6 +257,28 @@ public fun GoogleMap.groundOverlayClicks(): Flow<GroundOverlay> =
}
}

/**
* Returns a flow that emits when the indoor state changes. Using this to observe indoor state
* change events will override an existing listener (if any) to
* [GoogleMap.setOnIndoorStateChangeListener]
*/
@ExperimentalCoroutinesApi
public fun GoogleMap.indoorStateChangeEvents(): Flow<IndoorChangeEvent> =
callbackFlow {
setOnIndoorStateChangeListener(object : GoogleMap.OnIndoorStateChangeListener {
override fun onIndoorBuildingFocused() {
offerCatching(IndoorBuildingFocusedEvent)
}

override fun onIndoorLevelActivated(indoorBuilding: IndoorBuilding) {
offerCatching(IndoorLevelActivatedEvent(building = indoorBuilding))
}
})
awaitClose {
setOnIndoorStateChangeListener(null)
}
}

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

0 comments on commit cf8707c

Please sign in to comment.