Doru N.
09/11/2018, 8:43 AMdata class DataSourceResponse(val response: Response)
data class Response(val notifications: List<Notification>)
data class Notification(id: Int)
I have a function which takes a DataSourceResponse as a param and a callback, which should return a modified DataSourceResponse, filtering out some of the notifications from it.
How should I implement this filtering and copy, considering the given model classes above. (immutable notifications list).
The closest I got, was doing something like this:
callback.onDataLoaded(
paramDataSourceResponse.copy (
response = paramDataSourceResponse.response.copy (
notifications = paramDataSourceResponse.response.notifications.filter { check(it) }
)
)
)
but it looks kinda ugly to me. Also, not having every class made data class, would make it even uglier. Are there any other solutions for this?hho
09/11/2018, 8:51 AMhho
09/11/2018, 8:57 AMResponse
be a copy of the paramDataSourceResponse
as well?Pavlo Liapota
09/11/2018, 9:04 AMDataSourceResponse(Response(paramDataSourceResponse.response.notifications.filter { check(it) }))
Doru N.
09/11/2018, 9:08 AMDoru N.
09/11/2018, 9:09 AMPavlo Liapota
09/11/2018, 9:11 AMparamDataSourceResponse.response.copy (
instead of
paramDataSourceResponse.copy (
in your second copy()
callhho
09/11/2018, 9:12 AM.response
in your inner copy.hho
09/11/2018, 9:13 AMval filteredNotifications = paramDataSourceResponse.response.notifications.filter { check(it) }
val filteredResponse = paramDataSourceResponse.response.copy(notifications = filteredNotifications)
val filteredDataSourceResponse = paramDataSourceResponse.copy(response = filteredResponse)
callback.onDataLoaded(filteredDataSourceResponse)
Pavlo Liapota
09/11/2018, 9:24 AMrun
or let
functions:
paramDataSourceResponse.run {
copy(response = response.run {
copy(notifications = notifications.filter { check(it) })
})
}
Doru N.
09/11/2018, 9:48 AMDoru N.
09/11/2018, 9:49 AMcopy
function call inside