Ritwik Raj Srivastava
07/09/2023, 6:41 PMRitwik Raj Srivastava
07/10/2023, 5:40 AM@Keep
@Parcelize
@Immutable
d.ata class Country(
@SerializedName("abbreviation")
val abbreviation: List<String?>? = null,
@SerializedName("city")
val city: List<String?> = emptyList(),
@SerializedName("countryCode")
val countryCode: String = "",
@SerializedName("countryName")
val countryName: String = ""
) : Parcelable
Now when there is no query I only need to display list of countryName but as the user types , the list should update to a list of "city, countryName"
Example - Idle state (USA), Search state (Boston, USA), (Dallas, USA), (Nyc, USA) ....
My Composable will only know the title I am providing
@Keep
@Parcelize
@Immutable
data class IRSearch(
val title: String,
val country: Country
) : Parcelable
I want to write concise and elegant Kotlin for the mapping part of cities with country Name which I am unable to do so.
Any help, advice, follow up is appreciated
LaunchedEffect(key1 = searchText) {
snapshotFlow { searchText }
.onEach {
searchingInProgress = it.isNotEmpty()
}
.distinctUntilChanged()
.combine(viewModel.getCountryList()) { text, list ->
(list as? ApiResponse.Success)?.let {
if (text.isEmpty()) {
it.data?.countryList?.filterNotNull()?.map {
country -> IRSearch(
title = country.countryName,
country = country
)
}
} else
it.data?.countryList?.filterNotNull()?.map {
country -> IRSearch(
title = , // <---- Need help here mapping list of city with country
country = country
)
}
} ?: emptyList()
}
.flowOn(Dispatchers.Default)
.collectLatest {
}
Ritwik Raj Srivastava
07/10/2023, 5:41 AM