Having an application with `Room` and `Compose`, I...
# compose
p
Having an application with
Room
and
Compose
, I'm on a composable that needs to retrieve data from the database for painting it. It is a list of favorite flights, and favorite only haves the IDs (iata) of the departure and destination airports. My composable haves the favorite but needs to do selects from airport table to recover all the data for each airport and be able to paint it. Is a list, so it need to do it for a lot of items. Which is the correct approach? I thought was creating a function on the
viewmodel
that returns the result of the query but then I noticed that this whould return a
Flow
and not a object instance, also, the composable doesn't have the
viewmodel
, so I can't call functions of the viewmodel. Which is the best way to solve this?
The database entities:
Copy code
@Entity
data class Airport(
    @PrimaryKey
    val id: Int,
    @ColumnInfo(name = "iata_code")
    val iataCode: String,
    val name: String,
    val passengers: Int
)

@Entity
data class Favorite(
    @PrimaryKey(autoGenerate = true)
    val id: Int = 0,
    @ColumnInfo(name = "departure_code")
    val departureCode: String,
    @ColumnInfo(name = "destination_code")
    val destinationCode: String
)
The composable:
Copy code
LazyColumn(
        modifier = Modifier.padding(8.dp)
    ) {
        items(uiState.favorites) { favorite ->
            //TODO here i need to get two airports using the ids (iata), so I need to query room, once I have them, I can use them to paint the details of both airports of the favorite flight.
        }
    }
The viewmodel function to recover the airport by iata:
Copy code
fun getAirportByIata(iata: String): Flow<Airport> {
        return flightRepository.getAirport(iata)
}
m
//TODO here i need to get two airports using the ids
No. There, you need the data about the two airports, so you can render that data. That data needs to be part of
favorite
or something that replaces or wraps
favorite
as the viewstate that you are rendering.