Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. class DictionaryAllWordsDataSource(//...) :
  2. PositionalDataSource<Meaning>() {
  3. //...
  4. override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<Meaning>) {
  5. //...
  6. val startPage = (params.startPosition / PAGE_SIZE) + 1
  7. if (totalPages >= startPage) {
  8. val words = getWordsFromPage(startPage, startPage)
  9. if (words.isEmpty()) throw NoDataException()
  10. callback.onResult(words)
  11. }
  12. //...
  13. }
  14.  
  15. override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<Meaning>) {
  16. //...
  17. val total = getTotalPages()
  18. val endPage = min((params.requestedLoadSize / PAGE_SIZE), 3)
  19. val startPage = 1
  20. val words = getWordsFromPage(startPage, endPage)
  21. if (words.isEmpty()) throw NoDataException()
  22. callback.onResult(words, 0, (total.end * PAGE_SIZE))
  23. totalPages = total.end
  24. //...
  25. }
  26.  
  27. fun getWordsFromPage(startPage: Int, endPage: Int): MutableList<Meaning> {
  28. val words = mutableListOf<Meaning>()
  29. for (index in startPage..endPage) {
  30. val dictionary = getWordsFromPage(index)
  31. dictionary.words.forEach { words.add(it) }
  32. }
  33. //...
  34. return words
  35. }
  36.  
  37. private fun getWordsFromPage(pageNo: Int): Dictionary {
  38. //...
  39. return repository.getPageInDictionary(pageNo)
  40. }
  41.  
  42. private fun getTotalPages(): Page {
  43. //...
  44. return repository.getNumberOfPagesInDictionary()
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement