Last active
June 10, 2020 16:11
-
-
Save maskaravivek/e94e9e7cc827453d31f325fed5690b5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UserBoundaryCallback constructor(val userDao: UserDao?) : | |
PagedList.BoundaryCallback<User?>() { | |
private val compositeDisposable: CompositeDisposable = CompositeDisposable() | |
override fun onZeroItemsLoaded() { | |
fetchUsers() | |
} | |
override fun onItemAtFrontLoaded(itemAtFront: User) { | |
fetchUsers() | |
} | |
override fun onItemAtEndLoaded(itemAtEnd: User) { | |
fetchUsers() | |
} | |
/** | |
* Fetches contributions using the MediaWiki API | |
*/ | |
fun fetchUsers() { | |
compositeDisposable.add( | |
mockGetUsers() | |
.subscribeOn(Schedulers.io()) | |
.subscribe( | |
::saveUsersToDb | |
) { error: Throwable -> | |
// do nothing | |
} | |
) | |
} | |
fun mockGetUsers(): Observable<MutableList<User>> { | |
val users = mutableListOf<User>() | |
for (i in 0..9) { | |
users.add(i, getUser()) | |
} | |
return Observable.just(users) | |
} | |
fun getUser(): User { | |
return User(0, getRandomString(5), getRandomString(8)) | |
} | |
fun getRandomString(length: Int): String { | |
val allowedChars = ('A'..'Z') + ('a'..'z') | |
return (1..length) | |
.map { allowedChars.random() } | |
.joinToString("") | |
} | |
private fun saveUsersToDb(contributions: List<User>) { | |
compositeDisposable.add( | |
userDao!!.save(contributions) | |
.subscribeOn(Schedulers.io()) | |
.subscribe { longs: List<Long?>? -> | |
//do nothing | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment