Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.05 KB | None | 0 0
  1. /**
  2. * This guy extends Repository class so the retrofit variable will be available to use as it's instantiated in the init!
  3. */
  4. class CatsRepository(
  5. baseUrl: String,
  6. isDebugEnabled: Boolean,
  7. apiKey: String
  8. ) : Repository(baseUrl, isDebugEnabled, apiKey) {
  9.  
  10. private val catsDataSource: CatsDataSource = CatsDataSource(retrofit)
  11.  
  12. // a class to wrap around the response to make things easier later
  13. inner class Result(val netCats: List<NetCat>? = null, val errorMessage: String? = null) {
  14.  
  15. fun hasCats(): Boolean {
  16. return netCats != null && !netCats.isEmpty()
  17. }
  18.  
  19. fun hasError(): Boolean {
  20. return errorMessage != null
  21. }
  22. }
  23.  
  24. // the method that's gonna be called by our activity
  25. fun getNumberOfRandomCats(limit: Int, category_ids: Int?): Single<Result> {
  26.  
  27. return catsDataSource.getNumberOfRandomCats(limit, category_ids)
  28. .map { netCats: List<NetCat> -> Result(netCats = netCats) }
  29. .onErrorReturn { t: Throwable -> Result(errorMessage = t.message) }
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement