Skip to content

Instantly share code, notes, and snippets.

@chiragthummar
Created December 23, 2023 10:10
Show Gist options
  • Select an option

  • Save chiragthummar/66103a26770708ef33e1d583c2caf8a8 to your computer and use it in GitHub Desktop.

Select an option

Save chiragthummar/66103a26770708ef33e1d583c2caf8a8 to your computer and use it in GitHub Desktop.
@Singleton
class ImageRepositoryImpl @Inject constructor(
private val imageApi: ImageApi
) : ImageRepository {
override fun getImages(text: String): Flow<Resources<List<Image>>> {
return flow {
emit(Resources.Loading(true))
val remoteList = try {
imageApi.getInfiniteApiImages(text)
} catch (e: IOException) {
e.printStackTrace()
emit(Resources.Error("Could not load data"))
null
} catch (e: HttpException) {
e.printStackTrace()
emit(Resources.Error("Could not load data"))
null
}
remoteList.let { listing ->
emit(Resources.Success(data = listing?.images?.map { it.toImage() }))
emit(Resources.Loading(false))
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment