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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds PlacesClient.awaitIsOpen extension functions #233

Merged
merged 2 commits into from
Feb 9, 2024
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
10 changes: 5 additions & 5 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,17 @@ android {
dependencies {
implementation "androidx.appcompat:appcompat:1.6.1"
implementation "androidx.core:core-ktx:1.12.0"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.9.21"
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.9.22"
implementation 'androidx.multidex:multidex:2.0.1'
implementation "com.google.android.gms:play-services-maps:18.2.0"
implementation "androidx.fragment:fragment-ktx:1.6.2"
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.2'
implementation 'com.google.android.material:material:1.10.0'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.7.0'
implementation 'com.google.android.material:material:1.11.0'
implementation 'com.android.volley:volley:1.2.1'

// Hilt
implementation "com.google.dagger:hilt-android:2.48.1"
kapt "com.google.dagger:hilt-android-compiler:2.48.1"
implementation "com.google.dagger:hilt-android:2.50"
kapt "com.google.dagger:hilt-android-compiler:2.50"

implementation project(':places-ktx')
}
Expand Down
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
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ buildscript {
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath 'com.android.tools.build:gradle:8.1.4'
classpath 'com.android.tools.build:gradle:8.2.2'
classpath 'org.jetbrains.dokka:dokka-gradle-plugin:1.9.10'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.10"
classpath "com.google.dagger:hilt-android-gradle-plugin:2.48"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.22"
classpath "com.google.dagger:hilt-android-gradle-plugin:2.50"
classpath "com.google.android.libraries.mapsplatform.secrets-gradle-plugin:secrets-gradle-plugin:2.0.1"
classpath 'com.mxalbert.gradle:jacoco-android:0.2.1'
}
Expand Down Expand Up @@ -168,4 +168,4 @@ subprojects { project ->

tasks.register('clean', Delete) {
delete rootProject.buildDir
}
}
2 changes: 1 addition & 1 deletion places-ktx/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ android {
dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib:1.9.22"
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.2"
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.7.0"
implementation "com.android.volley:volley:1.2.1"
implementation 'androidx.multidex:multidex:2.0.1'
api "org.jetbrains.kotlinx:kotlinx-coroutines-play-services:1.7.3"
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)
}
Loading