I am working on an Android application in which I would like to filter both parent and child list.
To make it more clear: Let us consider that there are two data classes and one list.
data class User(val id:String, val name: String, val devices: List<Devices>)
data class Device(val deviceId:String, val manufacturer: String, val status:Int)
val list: List<User> = <Initialisation here>
The UI hierarchy will be something like below.
(Parent)User 1
-> (Child) Device 1 (Status -> 0)
-> (Child) Device 2 (Status -> 0)
-> (Child) Device 3 (Status -> 1)
(Parent)User 2
-> (Child) Device 4 (Status -> 1)
-> (Child) Device 5 (Status -> 0)
-> (Child) Device 6 (Status -> 1)
(Parent)User 3
-> (Child) Device 4 (Status -> 0)
-> (Child) Device 5 (Status -> 0)
-> (Child) Device 6 (Status -> 0)
And, I want to show the header and child only if includes the status "1".
(Parent)User 1
-> (Child) Device 3 (Status -> 1)
(Parent)User 2
-> (Child) Device 4 (Status -> 1)
-> (Child) Device 6 (Status -> 1)
What I have tried ?
list?.filter { user -> user.devices.any { device -> device.status == 1}}
The above snippet ignores "User 3" but still fetches all the devices(with status 1 & 0) of "User 1" and "User 2".
Please help me to achieve this.