2 years ago
#53868

Zar E Ahmer
Paging 3.0 handle Api and Network error using clean architecture
I am following uncle bob clean architecture to some extent and for that I have to implement UseCase Interactor and Repository pattern for Implementing Paging 3.0.
I haven't find any example that handle network response code
and Api Error
with Paging.
I am building a Gallery app that can fetch images from multiple sources like Unsplash or Pixabay and map PagingData to my PagingData.
I want to know where can I handle ApiResponse and Network Error
FetchPhotoUseCase.kt
In order to handle ApiResponse, I have created below class
sealed class ApiResponse<out R> {
data class Success<out T>(val data: T) : ApiResponse<T>()
data class Error(val errorMessage: String) : ApiResponse<Nothing>()
object Empty : ApiResponse<Nothing>()
}
PhotoRepository.kt
interface PhotoRepository {
fun searchPhotos(queryString: String): Flow<PagingData<PhotoBO>>
}
PhotoRepositoryImpl.kt
class PhotoRepositoryImpl @Inject constructor(
private val unsplashApi: UnsplashApi
) :PhotoRepository{
override fun searchPhotos(queryString: String): Flow<PagingData<PhotoBO>> {
return Pager(
config = PagingConfig(
pageSize = 20,
maxSize = 100,
enablePlaceholders = false
),
pagingSourceFactory = { UnsplashPagingSource(unsplashApi, queryString) }
).flow.mapLatest { pagingData ->
pagingData.map {
it.toPhotoBO()
}
}
}
}
FetchPhotoUseCase.kt
class FetchPhotoUseCase @Inject constructor(
private val photoRepository: PhotoRepository
) : UseCaseWithParams<String,Flow<PagingData<PhotoBO>>>()
{
override suspend fun buildUseCase(params: String): Flow<PagingData<PhotoBO>> {
return photoRepository.searchPhotos(params)
}
}
What if there isn't any network connected or api fail due to any reason. I want to know how and at which layer I have to handle those errors
At which level ApiResponse is handle and how to implement it in clean architecture..
android
kotlin
clean-architecture
kotlin-flow
android-paging-3
0 Answers
Your Answer