Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class DictionaryAllWordsDataSource(//...) :
- PositionalDataSource<Meaning>() {
- //...
- override fun loadRange(params: LoadRangeParams, callback: LoadRangeCallback<Meaning>) {
- //...
- val startPage = (params.startPosition / PAGE_SIZE) + 1
- if (totalPages >= startPage) {
- val words = getWordsFromPage(startPage, startPage)
- if (words.isEmpty()) throw NoDataException()
- callback.onResult(words)
- }
- //...
- }
- override fun loadInitial(params: LoadInitialParams, callback: LoadInitialCallback<Meaning>) {
- //...
- val total = getTotalPages()
- val endPage = min((params.requestedLoadSize / PAGE_SIZE), 3)
- val startPage = 1
- val words = getWordsFromPage(startPage, endPage)
- if (words.isEmpty()) throw NoDataException()
- callback.onResult(words, 0, (total.end * PAGE_SIZE))
- totalPages = total.end
- //...
- }
- fun getWordsFromPage(startPage: Int, endPage: Int): MutableList<Meaning> {
- val words = mutableListOf<Meaning>()
- for (index in startPage..endPage) {
- val dictionary = getWordsFromPage(index)
- dictionary.words.forEach { words.add(it) }
- }
- //...
- return words
- }
- private fun getWordsFromPage(pageNo: Int): Dictionary {
- //...
- return repository.getPageInDictionary(pageNo)
- }
- private fun getTotalPages(): Page {
- //...
- return repository.getNumberOfPagesInDictionary()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement