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>>>