Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add indoorStateChangeEvents() #138

Merged
merged 2 commits into from
Jun 4, 2021
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
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