Solomon Tolu Samuel
11/04/2022, 7:54 PM//Repository
data class PostResponse(
val body: String,
val title: String,
val id: Int,
val userId: Int
)
class NewsRepository @Inject constructor(private val newsRemoteDataSource: NewsRemoteDataSource ) {
val fetchLatestNews : Flow<List<PostResponse>> = newsRemoteDataSource.fatchLatestNews
//ViewModel
data class UiPostResponse(
val post: PostResponse,
val message: String = "message"
)
class UiViewModel @Inject constructor(private val newsRepository: NewsRepository): ViewModel() {
private val data : Flow<UiPostResponse> = newsRepository.fetchLatestNews.map { post->
UiPostResponse(
//
)
}
Please what would be the best way to map a list of flow data to useful UI data in the viewmodel?Pablichjenkov
11/04/2022, 8:08 PMUiPostResponse1(val postResponse: PostResponse) : IPostResponsePresenter
UiPostResponse2(val postResponse: PostResponse) : IPostResponsePresenter
and have as many UiPostResponse presenters as you UI needs. It is up to your App specs.
If you want to fully decouple you UI/Data domain from your API domain then you can do direct mapping. eg: UiPostResponse1.title = PostResponse.title
and so onSolomon Tolu Samuel
11/04/2022, 8:15 PMDaniel
11/04/2022, 10:43 PMPablichjenkov
11/04/2022, 10:49 PMprivate val uiMappedData : Flow<List<UiPostResponse>> = newsRepository.fetchLatestNews.map { postList->
postList.map { postResponse ->// This map refers to the List<T>.map and not Flow<T>.map
// Do direct mapping here
UiPostResponse().apply {title = postResponse.title}
}
}
As Ezike mentioned, map only what you need, is cleaner.Solomon Tolu Samuel
11/05/2022, 2:45 AM