|
| 1 | +package fr.free.nrw.commons.profile.leaderboard |
| 2 | + |
| 3 | +import android.accounts.Account |
| 4 | +import androidx.lifecycle.MutableLiveData |
| 5 | +import androidx.paging.PageKeyedDataSource |
| 6 | +import fr.free.nrw.commons.auth.SessionManager |
| 7 | +import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient |
| 8 | +import fr.free.nrw.commons.profile.leaderboard.LeaderboardConstants.LoadingStatus.LOADING |
| 9 | +import fr.free.nrw.commons.profile.leaderboard.LeaderboardConstants.LoadingStatus.LOADED |
| 10 | +import io.reactivex.disposables.CompositeDisposable |
| 11 | +import io.reactivex.disposables.Disposable |
| 12 | +import timber.log.Timber |
| 13 | +import java.util.Objects |
| 14 | + |
| 15 | +/** |
| 16 | + * This class will call the leaderboard API to get new list when the pagination is performed |
| 17 | + */ |
| 18 | +class DataSourceClass( |
| 19 | + private val okHttpJsonApiClient: OkHttpJsonApiClient, |
| 20 | + private val sessionManager: SessionManager, |
| 21 | + private val duration: String?, |
| 22 | + private val category: String?, |
| 23 | + private val limit: Int, |
| 24 | + private val offset: Int |
| 25 | +) : PageKeyedDataSource<Int, LeaderboardList>() { |
| 26 | + val progressLiveStatus: MutableLiveData<LeaderboardConstants.LoadingStatus> = MutableLiveData() |
| 27 | + private val compositeDisposable = CompositeDisposable() |
| 28 | + |
| 29 | + |
| 30 | + override fun loadInitial( |
| 31 | + params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, LeaderboardList?> |
| 32 | + ) { |
| 33 | + compositeDisposable.add(okHttpJsonApiClient.getLeaderboard( |
| 34 | + sessionManager.currentAccount?.name, |
| 35 | + duration, |
| 36 | + category, |
| 37 | + limit.toString(), |
| 38 | + offset.toString() |
| 39 | + ).doOnSubscribe { disposable: Disposable? -> |
| 40 | + compositeDisposable.add(disposable!!) |
| 41 | + progressLiveStatus.postValue(LOADING) |
| 42 | + }.subscribe({ response: LeaderboardResponse? -> |
| 43 | + if (response != null && response.status == 200) { |
| 44 | + progressLiveStatus.postValue(LOADED) |
| 45 | + callback.onResult(response.leaderboardList!!, null, response.limit) |
| 46 | + } |
| 47 | + }, { t: Throwable? -> |
| 48 | + Timber.e(t, "Fetching leaderboard statistics failed") |
| 49 | + progressLiveStatus.postValue(LOADING) |
| 50 | + })) |
| 51 | + } |
| 52 | + |
| 53 | + override fun loadBefore( |
| 54 | + params: LoadParams<Int>, callback: LoadCallback<Int, LeaderboardList?> |
| 55 | + ) = Unit |
| 56 | + |
| 57 | + override fun loadAfter( |
| 58 | + params: LoadParams<Int>, callback: LoadCallback<Int, LeaderboardList?> |
| 59 | + ) { |
| 60 | + compositeDisposable.add(okHttpJsonApiClient.getLeaderboard( |
| 61 | + Objects.requireNonNull<Account?>(sessionManager.currentAccount).name, |
| 62 | + duration, |
| 63 | + category, |
| 64 | + limit.toString(), |
| 65 | + params.key.toString() |
| 66 | + ).doOnSubscribe { disposable: Disposable? -> |
| 67 | + compositeDisposable.add(disposable!!) |
| 68 | + progressLiveStatus.postValue(LOADING) |
| 69 | + }.subscribe({ response: LeaderboardResponse? -> |
| 70 | + if (response != null && response.status == 200) { |
| 71 | + progressLiveStatus.postValue(LOADED) |
| 72 | + callback.onResult(response.leaderboardList!!, params.key + limit) |
| 73 | + } |
| 74 | + }, { t: Throwable? -> |
| 75 | + Timber.e(t, "Fetching leaderboard statistics failed") |
| 76 | + progressLiveStatus.postValue(LOADING) |
| 77 | + })) |
| 78 | + } |
| 79 | +} |
0 commit comments