-
Notifications
You must be signed in to change notification settings - Fork 629
/
PlacesClientProxy.kt
185 lines (173 loc) · 6.52 KB
/
PlacesClientProxy.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.stripe.android.ui.core.elements.autocomplete
import android.content.Context
import android.graphics.Typeface
import android.text.style.StyleSpan
import androidx.annotation.RestrictTo
import com.google.android.libraries.places.api.Places
import com.google.android.libraries.places.api.model.AutocompleteSessionToken
import com.google.android.libraries.places.api.model.TypeFilter
import com.google.android.libraries.places.api.net.FetchPlaceRequest
import com.google.android.libraries.places.api.net.FindAutocompletePredictionsRequest
import com.google.android.libraries.places.api.net.PlacesClient
import com.stripe.android.BuildConfig
import com.stripe.android.ui.core.R
import com.stripe.android.ui.core.elements.autocomplete.model.AddressComponent
import com.stripe.android.ui.core.elements.autocomplete.model.AutocompletePrediction
import com.stripe.android.ui.core.elements.autocomplete.model.FetchPlaceResponse
import com.stripe.android.ui.core.elements.autocomplete.model.FindAutocompletePredictionsResponse
import com.stripe.android.ui.core.elements.autocomplete.model.Place
import kotlinx.coroutines.tasks.await
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
interface PlacesClientProxy {
suspend fun findAutocompletePredictions(
query: String?,
country: String,
limit: Int
): Result<FindAutocompletePredictionsResponse>
suspend fun fetchPlace(
placeId: String
): Result<FetchPlaceResponse>
companion object {
fun create(
context: Context,
googlePlacesApiKey: String,
isPlacesAvailable: IsPlacesAvailable = DefaultIsPlacesAvailable(),
clientFactory: (Context) -> PlacesClient = { Places.createClient(context) },
initializer: () -> Unit = { Places.initialize(context, googlePlacesApiKey) }
): PlacesClientProxy {
return if (isPlacesAvailable()) {
initializer()
DefaultPlacesClientProxy(
clientFactory(context)
)
} else {
UnsupportedPlacesClientProxy()
}
}
fun getPlacesPoweredByGoogleDrawable(
isSystemDarkTheme: Boolean,
isPlacesAvailable: IsPlacesAvailable = DefaultIsPlacesAvailable()
): Int? {
return if (isPlacesAvailable()) {
if (isSystemDarkTheme) {
R.drawable.places_powered_by_google_dark
} else {
R.drawable.places_powered_by_google_light
}
} else {
if (BuildConfig.DEBUG) {
throw IllegalStateException(
"Missing Google Places dependency, please add it to your apps build.gradle"
)
}
null
}
}
}
}
internal class DefaultPlacesClientProxy(
private val client: PlacesClient
) : PlacesClientProxy {
private val token = AutocompleteSessionToken.newInstance()
override suspend fun findAutocompletePredictions(
query: String?,
country: String,
limit: Int
): Result<FindAutocompletePredictionsResponse> {
return try {
val response = client.findAutocompletePredictions(
FindAutocompletePredictionsRequest
.builder()
.setSessionToken(token)
.setQuery(query)
.setCountry(country)
.setTypeFilter(TypeFilter.ADDRESS)
.build()
).await()
Result.success(
FindAutocompletePredictionsResponse(
autocompletePredictions = response.autocompletePredictions.map {
AutocompletePrediction(
primaryText = it.getPrimaryText(StyleSpan(Typeface.BOLD)),
secondaryText = it.getSecondaryText(StyleSpan(Typeface.BOLD)),
placeId = it.placeId
)
}.take(limit ?: response.autocompletePredictions.size)
)
)
} catch (e: Exception) {
Result.failure(
Exception("Could not find autocomplete predictions: ${e.message}")
)
}
}
override suspend fun fetchPlace(
placeId: String
): Result<FetchPlaceResponse> {
return try {
val response = client.fetchPlace(
FetchPlaceRequest.newInstance(
placeId,
listOf(
com.google.android.libraries.places.api.model.Place.Field.ADDRESS_COMPONENTS
)
)
).await()
Result.success(
FetchPlaceResponse(
Place(
response.place.addressComponents?.asList()?.map {
AddressComponent(
shortName = it.shortName,
longName = it.name,
types = it.types
)
}
)
)
)
} catch (e: Exception) {
Result.failure(
Exception("Could not fetch place: ${e.message}")
)
}
}
}
internal class UnsupportedPlacesClientProxy : PlacesClientProxy {
override suspend fun findAutocompletePredictions(
query: String?,
country: String,
limit: Int
): Result<FindAutocompletePredictionsResponse> {
val exception = IllegalStateException(
"Missing Google Places dependency, please add it to your apps build.gradle"
)
if (BuildConfig.DEBUG) {
throw exception
}
return Result.failure(exception)
}
override suspend fun fetchPlace(placeId: String): Result<FetchPlaceResponse> {
val exception = IllegalStateException(
"Missing Google Places dependency, please add it to your apps build.gradle"
)
if (BuildConfig.DEBUG) {
throw exception
}
return Result.failure(exception)
}
}
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
interface IsPlacesAvailable {
operator fun invoke(): Boolean
}
internal class DefaultIsPlacesAvailable : IsPlacesAvailable {
override fun invoke(): Boolean {
return try {
Class.forName("com.google.android.libraries.places.api.Places")
true
} catch (_: Exception) {
false
}
}
}