-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adding Flow support for getting place selection events.
Change-Id: I51108f10b4576a016dd258d2101e8f64a0af02bd
- Loading branch information
Showing
13 changed files
with
227 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
app/src/main/java/com/google/places/android/ktx/demo/AutocompleteDemoActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.google.places.android.ktx.demo | ||
|
||
import android.os.Bundle | ||
import android.widget.Toast | ||
import androidx.appcompat.app.AppCompatActivity | ||
import androidx.lifecycle.lifecycleScope | ||
import com.google.android.libraries.places.api.model.Place | ||
import com.google.android.libraries.places.ktx.widget.PlaceSelectionError | ||
import com.google.android.libraries.places.ktx.widget.PlaceSelectionSuccess | ||
import com.google.android.libraries.places.ktx.widget.placeSelectionEvents | ||
import com.google.android.libraries.places.widget.AutocompleteSupportFragment | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.collect | ||
|
||
class AutocompleteDemoActivity : AppCompatActivity() { | ||
|
||
@OptIn(ExperimentalCoroutinesApi::class) | ||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
setContentView(R.layout.activity_autocomplete) | ||
|
||
val autocompleteFragment = | ||
supportFragmentManager.findFragmentById(R.id.autocomplete_fragment) | ||
as AutocompleteSupportFragment | ||
|
||
// Specify the types of place data to return. | ||
autocompleteFragment.setPlaceFields(listOf(Place.Field.ID, Place.Field.NAME)) | ||
|
||
// Listen to place selection events | ||
lifecycleScope.launchWhenCreated { | ||
autocompleteFragment.placeSelectionEvents().collect { event -> | ||
when (event) { | ||
is PlaceSelectionSuccess -> Toast.makeText( | ||
this@AutocompleteDemoActivity, | ||
"Got place '${event.place.name}'", | ||
Toast.LENGTH_SHORT | ||
).show() | ||
is PlaceSelectionError -> Toast.makeText( | ||
this@AutocompleteDemoActivity, | ||
"Failed to get place '${event.status.statusMessage}'", | ||
Toast.LENGTH_SHORT | ||
).show() | ||
} | ||
} | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
app/src/main/java/com/google/places/android/ktx/demo/Demo.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.google.places.android.ktx.demo | ||
|
||
import android.app.Activity | ||
import androidx.annotation.StringRes | ||
|
||
enum class Demo( | ||
@StringRes val title: Int, | ||
@StringRes val description: Int, | ||
val activity: Class<out Activity> | ||
) { | ||
AUTOCOMPLETE_FRAGMENT_DEMO( | ||
R.string.autocomplete_fragment_demo_title, | ||
R.string.autocomplete_fragment_demo_description, | ||
AutocompleteDemoActivity::class.java | ||
), | ||
PLACES_SEARCH_DEMO( | ||
R.string.places_demo_title, | ||
R.string.places_demo_description, | ||
PlacesSearchDemoActivity::class.java | ||
) | ||
} |
61 changes: 61 additions & 0 deletions
61
app/src/main/java/com/google/places/android/ktx/demo/DemoActivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.google.places.android.ktx.demo | ||
|
||
import android.content.Context | ||
import android.content.Intent | ||
import android.os.Bundle | ||
import android.view.LayoutInflater | ||
import android.view.View | ||
import android.view.ViewGroup | ||
import android.widget.AdapterView | ||
import android.widget.ArrayAdapter | ||
import android.widget.LinearLayout | ||
import android.widget.ListView | ||
import android.widget.TextView | ||
import androidx.appcompat.app.AppCompatActivity | ||
|
||
class DemoActivity : AppCompatActivity() { | ||
|
||
override fun onCreate(savedInstanceState: Bundle?) { | ||
super.onCreate(savedInstanceState) | ||
|
||
val listView = ListView(this).also { | ||
it.layoutParams = ViewGroup.LayoutParams( | ||
ViewGroup.LayoutParams.MATCH_PARENT, | ||
ViewGroup.LayoutParams.MATCH_PARENT | ||
) | ||
it.adapter = DemoAdapter(this, Demo.values()) | ||
it.onItemClickListener = AdapterView.OnItemClickListener { parent, _, position, _ -> | ||
val demo = parent.adapter.getItem(position) as? Demo | ||
demo?.let { | ||
startActivity(Intent(this, demo.activity)) | ||
} | ||
} | ||
} | ||
setContentView(listView) | ||
} | ||
|
||
private class DemoAdapter(context: Context, demos: Array<Demo>) : | ||
ArrayAdapter<Demo>(context, R.layout.item_demo, demos) { | ||
|
||
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View { | ||
val demoView = (convertView as? DemoItemView) ?: DemoItemView(context) | ||
return demoView.also { | ||
val demo = getItem(position) | ||
it.title.setText(demo?.title ?: 0) | ||
it.description.setText(demo?.description ?: 0) | ||
} | ||
} | ||
} | ||
|
||
private class DemoItemView(context: Context) : LinearLayout(context) { | ||
|
||
val title: TextView by lazy { findViewById<TextView>(R.id.textViewTitle) } | ||
|
||
val description: TextView by lazy { findViewById<TextView>(R.id.textViewDescription) } | ||
|
||
init { | ||
LayoutInflater.from(context) | ||
.inflate(R.layout.item_demo, this) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
android:orientation="vertical" | ||
android:background="@color/colorGray" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent"> | ||
|
||
<androidx.fragment.app.FragmentContainerView | ||
android:id="@+id/autocomplete_fragment" | ||
android:background="@android:color/white" | ||
android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" /> | ||
|
||
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...c/main/java/com/google/android/libraries/places/ktx/widget/AutocompleteSupportFragment.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.google.android.libraries.places.ktx.widget | ||
|
||
import com.google.android.gms.common.api.Status | ||
import com.google.android.libraries.places.api.model.Place | ||
import com.google.android.libraries.places.widget.AutocompleteSupportFragment | ||
import com.google.android.libraries.places.widget.listener.PlaceSelectionListener | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.callbackFlow | ||
|
||
sealed class PlaceSelectionResult | ||
|
||
data class PlaceSelectionSuccess(val place: Place) : PlaceSelectionResult() | ||
|
||
data class PlaceSelectionError(val status: Status) : PlaceSelectionResult() | ||
|
||
@ExperimentalCoroutinesApi | ||
fun AutocompleteSupportFragment.placeSelectionEvents() : Flow<PlaceSelectionResult> = | ||
callbackFlow { | ||
this@placeSelectionEvents.setOnPlaceSelectedListener(object : PlaceSelectionListener { | ||
override fun onPlaceSelected(place: Place) { | ||
offer(PlaceSelectionSuccess(place)) | ||
} | ||
|
||
override fun onError(status: Status) { | ||
offer(PlaceSelectionError(status)) | ||
} | ||
}) | ||
} |