Skip to content

Commit

Permalink
fix: Adds awaitIsOpen extension functions to the PlacesClient
Browse files Browse the repository at this point in the history
Updates the request builder helper functions to be more idiomatic.
  • Loading branch information
Dale Hawkins committed Feb 8, 2024
1 parent fc6ee96 commit e06c7c5
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class DemoActivity : AppCompatActivity() {
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
it.adapter = DemoAdapter(this, Demo.values())
it.adapter = DemoAdapter(this, Demo.entries.toTypedArray())
it.onItemClickListener = AdapterView.OnItemClickListener { parent, _, position, _ ->
val demo = parent.adapter.getItem(position) as? Demo
demo?.let {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import com.google.android.libraries.places.api.model.AddressComponent
*
* @return the constructed [AddressComponent]
*/
public inline fun addressComponent(
public fun addressComponent(
name: String,
types: List<String>,
noinline actions: (AddressComponent.Builder.() -> Unit)? = null
actions: (AddressComponent.Builder.() -> Unit)? = null
): AddressComponent {
val builder = AddressComponent.builder(name, types)
actions?.let { builder.apply(it) }
return builder.build()
return AddressComponent.builder(name, types).also { builder ->
actions?.let { builder.apply(it) }
}.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import com.google.android.libraries.places.api.model.AutocompletePrediction
*
* @return the constructed [AutocompletePrediction]
*/
public inline fun autocompletePrediction(
public fun autocompletePrediction(
placeId: String,
noinline actions: (AutocompletePrediction.Builder.() -> Unit)? = null
actions: (AutocompletePrediction.Builder.() -> Unit)? = null
): AutocompletePrediction {
val builder = AutocompletePrediction.builder(placeId)
actions?.let { builder.apply(it) }
return builder.build()
return AutocompletePrediction.builder(placeId).also { builder ->
actions?.let { builder.apply(it) }
}.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ import com.google.android.libraries.places.api.model.PhotoMetadata
*
* @return the constructed [PhotoMetadata]
*/
public inline fun photoMetadata(
public fun photoMetadata(
photoReference: String,
noinline actions: (PhotoMetadata.Builder.() -> Unit)? = null
actions: (PhotoMetadata.Builder.() -> Unit)? = null
): PhotoMetadata {
val builder = PhotoMetadata.builder(photoReference)
actions?.let { builder.apply(it) }
return builder.build()
return PhotoMetadata.builder(photoReference).also { builder ->
actions?.let { builder.apply(it) }
}.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import com.google.android.libraries.places.api.net.FetchPhotoRequest
*
* @return the constructed [FetchPhotoRequest]
*/
public inline fun fetchPhotoRequest(
public fun fetchPhotoRequest(
photoMetadata: PhotoMetadata,
noinline actions: (FetchPhotoRequest.Builder.() -> Unit)? = null
actions: (FetchPhotoRequest.Builder.() -> Unit)? = null
): FetchPhotoRequest {
val request = FetchPhotoRequest.builder(photoMetadata)
actions?.let { request.apply(it) }
return request.build()
return FetchPhotoRequest.builder(photoMetadata).also { builder ->
actions?.let { builder.apply(it) }
}.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import com.google.android.libraries.places.api.net.FetchPlaceRequest
*
* @return the constructed [FetchPlaceRequest]
*/
public inline fun fetchPlaceRequest(
public fun fetchPlaceRequest(
placeId: String,
placeFields: List<Place.Field>,
noinline actions: (FetchPlaceRequest.Builder.() -> Unit)? = null
actions: (FetchPlaceRequest.Builder.() -> Unit)? = null
): FetchPlaceRequest {
val request = FetchPlaceRequest.builder(placeId, placeFields)
actions?.let { request.apply(it) }
return request.build()
return FetchPlaceRequest.builder(placeId, placeFields).also { builder ->
actions?.let { builder.apply(it) }
}.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ import com.google.android.libraries.places.api.net.FindCurrentPlaceRequest
*
* @return the constructed [FindCurrentPlaceRequest]
*/
public inline fun findCurrentPlaceRequest(
public fun findCurrentPlaceRequest(
placeFields: List<Place.Field>,
noinline actions: (FindCurrentPlaceRequest.Builder.() -> Unit)? = null
actions: (FindCurrentPlaceRequest.Builder.() -> Unit)? = null
): FindCurrentPlaceRequest {
val request = FindCurrentPlaceRequest.builder(placeFields)
actions?.let { request.apply(it) }
return request.build()
}
return FindCurrentPlaceRequest.builder(placeFields).also { builder ->
actions?.let { builder.apply(it) }
}.build()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.android.libraries.places.ktx.api.net

import com.google.android.libraries.places.api.model.Place
import com.google.android.libraries.places.api.net.IsOpenRequest

/**
* Builds a new [IsOpenRequest].
*
* @param placeId The [Place.id] of the place for which isOpen is to be determined.
* @param utcTimeMillis The milliseconds from 1970-01-01T00:00:00Z.
* @param actions the actions to apply to the [IsOpenRequest.Builder]
* @return the constructed [IsOpenRequest]
*/
public fun isOpenRequest(
placeId: String,
utcTimeMillis: Long? = null,
actions: (IsOpenRequest.Builder.() -> Unit)? = null,
): IsOpenRequest {
return if (utcTimeMillis == null) {
IsOpenRequest.builder(placeId)
} else {
IsOpenRequest.builder(placeId, utcTimeMillis)
}.also { request ->
actions?.let { request.apply(it) }
}.build()
}

/**
* Builds a new [IsOpenRequest].
*
* @param place The [Place] for which isOpen is to be determined.
* @param utcTimeMillis The milliseconds from 1970-01-01T00:00:00Z.
* @param actions the actions to apply to the [IsOpenRequest.Builder]
* @return the constructed [IsOpenRequest]
* @throws IllegalArgumentException if [Place] does not have a [Place.id] associated with it.
*/
public fun isOpenRequest(
place: Place,
utcTimeMillis: Long? = null,
actions: (IsOpenRequest.Builder.() -> Unit)? = null,
): IsOpenRequest {
return if (utcTimeMillis == null) {
IsOpenRequest.builder(place)
} else {
IsOpenRequest.builder(place, utcTimeMillis)
}.also { request ->
actions?.let { request.apply(it) }
}.build()
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.google.android.libraries.places.api.net.FindAutocompletePredictionsRe
import com.google.android.libraries.places.api.net.FindAutocompletePredictionsResponse
import com.google.android.libraries.places.api.net.FindCurrentPlaceRequest
import com.google.android.libraries.places.api.net.FindCurrentPlaceResponse
import com.google.android.libraries.places.api.net.IsOpenResponse
import com.google.android.libraries.places.api.net.PlacesClient
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.tasks.await
Expand Down Expand Up @@ -103,3 +104,35 @@ public suspend fun PlacesClient.awaitFindCurrentPlace(
.build()
return this.findCurrentPlace(request).await(cancellationTokenSource)
}

/**
* Wraps [PlacesClient.isOpen] in a suspending function with the given [Place] object.
*
* Returns whether or not a place is open. If an error occurred, an [ApiException] will be thrown.
*/
@ExperimentalCoroutinesApi
public suspend fun PlacesClient.awaitIsOpen(
place: Place, utcTimeMillis: Long? = null
): IsOpenResponse {
val cancellationTokenSource = CancellationTokenSource()
val request = isOpenRequest(place, utcTimeMillis) {
cancellationToken = cancellationTokenSource.token
}
return this.isOpen(request).await(cancellationTokenSource)
}

/**
* Wraps [PlacesClient.isOpen] in a suspending function with the given placeId.
*
* Returns whether or not a place is open. If an error occurred, an [ApiException] will be thrown.
*/
@ExperimentalCoroutinesApi
public suspend fun PlacesClient.awaitIsOpen(
placeId: String,
utcTimeMillis: Long? = null,
): IsOpenResponse {
val cancellationTokenSource = CancellationTokenSource()
val request =
isOpenRequest(placeId, utcTimeMillis) { cancellationToken = cancellationTokenSource.token }
return this.isOpen(request).await(cancellationTokenSource)
}

0 comments on commit e06c7c5

Please sign in to comment.