Did anyone implement google autocomplete suggestion text field or fragment in a jetpack compose project? If so kindly guide or share code snippets as I'm having difficulty in implementing it.
Update
Here is the intent that I'm triggering to open full-screen dialog, but when I start typing within it gets closed, and also I'm unable to figure out what the issue is and need a clue about handling on activity result for reading the result of the predictions within this compose function.
Places.initialize(context, "sa")
val fields = listOf(Place.Field.ID, Place.Field.NAME)
val intent = Autocomplete.IntentBuilder(
AutocompleteActivityMode.FULLSCREEN,fields).build(context)
startActivityForResult(context as MainActivity,intent, AUTOCOMPLETE_REQUEST_CODE, Bundle.EMPTY)
I am using the MVVM architecture and this is how I implemented it:
GooglePlacesApi
I've created an api for reaching google api named GooglePlacesApiThe @Query("types") field is for specifiying what are you looking for in the query, you can look for establishments etc. Types can be found here
Models
So I created 3 models for this implementation:GooglePredictionsResponse
The way the response looks if you are doing a GET request with postman is:Google Prediction Response
You can see that we have an object with "predictions" key so this is our first model.
GooglePredictionTerm
GooglePrediction
I only needed that information, if you need anything else, feel free to modify the models or create your own.
GooglePlacesRepository
And finally we create the repository to get the information (I'm using hilt to inject my dependencies, you can ignore those annotations if not using it)Here I've used an extra class I've created to handle the response, called Resource
View Model
Again I'm using hilt so ignore annotations if not using it.If you need any further help let me know