Skip to content

Commit

Permalink
feat: Add indoorStateChangeEvents() (#138)
Browse files Browse the repository at this point in the history
* feat: Add indoorStateChangeEvents()

* Update demo to use local maps-ktx
  • Loading branch information
arriolac committed Jun 4, 2021
1 parent 83446e8 commit 135c124
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ dependencies {
implementation deps.androidx.coreKtx
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.3.1'
implementation 'com.google.android.gms:play-services-maps:17.0.1'
implementation 'com.google.maps.android:maps-ktx:3.0.1'
// implementation 'com.google.maps.android:maps-ktx:3.0.0'
implementation 'com.google.maps.android:maps-utils-ktx:3.0.1'
// implementation project(':maps-ktx')
// implementation project(':maps-utils-ktx')
implementation project(':maps-ktx')
// implementation project(':maps-utils-ktx')
}

secrets {
Expand All @@ -64,4 +64,4 @@ secrets {
// MAPS_API_KEY=YOUR_API_KEY
propertiesFileName 'secure.properties'
defaultPropertiesFileName 'secure.defaults.properties'
}
}
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 135c124

Please sign in to comment.