https://kotlinlang.org logo
Title
k

Koya Sivaji

06/15/2020, 9:27 PM
I am new to kotlin flows and trying to add Flows to one of my project and facing an issue while converting list of objects of one type to another as shown below. Basically, am not sure which operator to be used for this.

//Repository - method return type is  Flow<List<HistoryEvent>>
fun getHistoryEvents(): Flow<List<HistoryEvent>>

//ViewModel - calling repository method and trying to convert data to List<AccessoryEvent>

val accessoryEvents : LiveData<List<AccessoryEvent>>=  liveData{
                                                      repository.getHistoryEvents()

                                                  // what operator to be used to convert from List<HistoryEvent> to List<AccessoryEvent>

                                                        .collect()
                                                      }
I tried like below
val eventHistory: LiveData<List<AccessoryEventType>> = 
        liveData {
            deviceRepository.getDeviceEventHistoryFor("abcd")
                .map { historyEvents ->
                    emit((historyEvents.map { AccessoryEventType.getEventType(it) }))
                }
                .collect()
        }

after this, all elements in the returned list are same and equals to the last emitted element. Am not sure what I am doing wrong
p

Peter

06/15/2020, 9:31 PM
don’t know anything about android but why are you
emit
in your map ?
doesn’t
getDeviceEventHistoryFor
already return a flow ?
k

Koya Sivaji

06/15/2020, 9:32 PM
to convert
Flow<xx>
to
Livedata<xx>
p

Peter

06/15/2020, 9:32 PM
also
Flow<List<HistoryEvent>>
seems like a smell
ah ok - no idea what
LiveData
is sorry
k

Koya Sivaji

06/15/2020, 9:35 PM
ok..basically how to convert List of objects of one type to another. In my case, I have to convert
List<HistoryEvents>
to
List<AccessoryEventType>
getDeviceEventHistoryFor
returns
Flow<List<HistoryEvent>>
p

Peter

06/15/2020, 9:36 PM
should be a very simple then:
List<HE>.map { convertHEtoAET(it) }
ie. no
emit
emit
is used when creating a reactive publisher (a new flow)
k

Koya Sivaji

06/15/2020, 9:40 PM
ok. lemme see how to convert to
Livedata<xx>
after doing
map
without
emit