Map Flow of PagingData to another model
Asked Answered
A

1

1

I have created a gallery app for learning. Images are fetched from different source like Pixabay, Unsplash, Imgur etc. I want to map each paging result to my own data called PhotoBO

I am using Clean architecture, Suggest me better approach to handle

I want to map all paging respone PagingData<UnsplashPhoto> to my PagingData<PhotoBo >

class Resource<T> private constructor(val status: Status, val data: T?, val message: String?) {
    enum class Status {
        SUCCESS, ERROR, LOADING
    }
    companion object {
        fun <T> success(data: T?): Resource<T> {
            return Resource(
                Status.SUCCESS,
                data,
                null
            )
        }
        fun <T> error(message: String): Resource<T> {
            return Resource(
                Status.ERROR,
                null,
                message
            )
        }
        fun <T> loading(): Resource<T> {
            return Resource(
                Status.LOADING,
                null,
                null
            )
        }
    }
}

UnSplashPhotoUseCase.kt

class UnSplashPhotoUseCase @Inject constructor(
    private val repository: UnSplashRepository
) : UseCaseWithParams<String,Resource<Flow<PagingData<UnsplashPhoto>>>>(){
    override suspend fun buildUseCase(params: String): Resource<Flow<PagingData<UnsplashPhoto>>> {
        return repository.getSearchResult(query = params)
    }
}

UnSplashRepositoryImpl.kt

class UnSplashRepositoryImpl @Inject constructor(
    private val networkManager: NetworkManager,
    private val unsplashApi: UnsplashApi,
    private val unSplashMapper: UnSplashMapper
) : UnSplashRepository{
    override suspend fun getSearchResult(query: String): Resource<Flow<PagingData<UnsplashPhoto>>> {
        return if(networkManager.isNetworkAvailable()){
            try {
                val pagingDataFlow = Pager(
                    config = PagingConfig(
                        pageSize = 20,
                        maxSize = 100,
                        enablePlaceholders = false
                    ),
                    pagingSourceFactory = { UnsplashPagingSource(unsplashApi, query) }
                ).flow
                Resource.success(pagingDataFlow)
            } catch (exception:Exception){
                Resource.error("")
            }
        } else  Resource.error("Internet Not Available")
    }
}

Or is there a better way to handle Network and other exception and map data to my type

In UnSplashRepositoryImpl.kt I want to return Resource<Flow<PagingData<PhotoBO>>>

Apospory answered 14/1, 2022 at 7:47 Comment(0)
B
-2

Yeah, you can just map the Flow<PagingData<UnsplashPhoto>>, there shouldn't be no problem here, you just need two maps:

Resource.success(pagingDataFlow.map { pagingData ->
   pagingData.map { unsplashPhoto -> PhotoBO(unsplashPhoto) }
})
Bibliopegy answered 1/2, 2022 at 21:20 Comment(1)
can you please look this issueUnpracticed

© 2022 - 2024 — McMap. All rights reserved.