-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[GSoC] Added Pagination to Leaderboard #3881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
8b15863
0104a76
089c153
ce30c8f
3fca9e9
4b2b7ad
52bdb0a
dbd7908
4e78b2d
f5aa5a0
370462c
68682a9
5f4c589
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package fr.free.nrw.commons.profile.leaderboard; | ||
|
||
import static fr.free.nrw.commons.profile.leaderboard.LeaderboardConstants.LOADED; | ||
import static fr.free.nrw.commons.profile.leaderboard.LeaderboardConstants.LOADING; | ||
import static fr.free.nrw.commons.profile.leaderboard.LeaderboardConstants.PAGE_SIZE; | ||
import static fr.free.nrw.commons.profile.leaderboard.LeaderboardConstants.START_OFFSET; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.lifecycle.MutableLiveData; | ||
import androidx.paging.PageKeyedDataSource; | ||
import fr.free.nrw.commons.auth.SessionManager; | ||
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient; | ||
import io.reactivex.disposables.CompositeDisposable; | ||
import java.util.Objects; | ||
import timber.log.Timber; | ||
|
||
public class DataSourceClass extends PageKeyedDataSource<Integer, LeaderboardList> { | ||
|
||
private OkHttpJsonApiClient okHttpJsonApiClient; | ||
private SessionManager sessionManager; | ||
private MutableLiveData<String> progressLiveStatus; | ||
private CompositeDisposable compositeDisposable = new CompositeDisposable(); | ||
|
||
public DataSourceClass(OkHttpJsonApiClient okHttpJsonApiClient,SessionManager sessionManager) { | ||
this.okHttpJsonApiClient = okHttpJsonApiClient; | ||
this.sessionManager = sessionManager; | ||
progressLiveStatus = new MutableLiveData<>(); | ||
} | ||
|
||
|
||
public MutableLiveData<String> getProgressLiveStatus() { | ||
return progressLiveStatus; | ||
} | ||
|
||
@Override | ||
public void loadInitial(@NonNull LoadInitialParams<Integer> params, | ||
@NonNull LoadInitialCallback<Integer, LeaderboardList> callback) { | ||
|
||
compositeDisposable.add(okHttpJsonApiClient | ||
.getLeaderboard(Objects.requireNonNull(sessionManager.getCurrentAccount()).name, | ||
"all_time", "upload", String.valueOf(PAGE_SIZE), String.valueOf(START_OFFSET)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Define these as constants. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I plan to move these as constants when I implement the filters, will it be okay if we leave them as hardcoded for now? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's OK for now if you don't forget to fix them soon :-) |
||
.doOnSubscribe(disposable -> { | ||
compositeDisposable.add(disposable); | ||
progressLiveStatus.postValue(LOADING); | ||
}).subscribe( | ||
response -> { | ||
if (response != null && response.getStatus() == 200) { | ||
progressLiveStatus.postValue(LOADED); | ||
callback.onResult(response.getLeaderboardList(), null, response.getLimit()); | ||
} | ||
}, | ||
t -> { | ||
Timber.e(t, "Fetching leaderboard statistics failed"); | ||
progressLiveStatus.postValue(LOADING); | ||
} | ||
)); | ||
|
||
} | ||
|
||
@Override | ||
public void loadBefore(@NonNull LoadParams<Integer> params, | ||
@NonNull LoadCallback<Integer, LeaderboardList> callback) { | ||
|
||
} | ||
|
||
@Override | ||
public void loadAfter(@NonNull LoadParams<Integer> params, | ||
@NonNull LoadCallback<Integer, LeaderboardList> callback) { | ||
compositeDisposable.add(okHttpJsonApiClient | ||
.getLeaderboard(Objects.requireNonNull(sessionManager.getCurrentAccount()).name, | ||
"all_time", "upload", String.valueOf(PAGE_SIZE), String.valueOf(params.key)) | ||
.doOnSubscribe(disposable -> { | ||
compositeDisposable.add(disposable); | ||
progressLiveStatus.postValue(LOADING); | ||
}).subscribe( | ||
response -> { | ||
if (response != null && response.getStatus() == 200) { | ||
progressLiveStatus.postValue(LOADED); | ||
callback.onResult(response.getLeaderboardList(), params.key + PAGE_SIZE); | ||
} | ||
}, | ||
t -> { | ||
Timber.e(t, "Fetching leaderboard statistics failed"); | ||
progressLiveStatus.postValue(LOADING); | ||
} | ||
)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package fr.free.nrw.commons.profile.leaderboard; | ||
|
||
import androidx.lifecycle.MutableLiveData; | ||
import androidx.paging.DataSource; | ||
import fr.free.nrw.commons.auth.SessionManager; | ||
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient; | ||
import io.reactivex.disposables.CompositeDisposable; | ||
|
||
public class DataSourceFactory extends DataSource.Factory<Integer, LeaderboardList> { | ||
|
||
private MutableLiveData<DataSourceClass> liveData; | ||
private OkHttpJsonApiClient okHttpJsonApiClient; | ||
private CompositeDisposable compositeDisposable; | ||
private SessionManager sessionManager; | ||
|
||
public DataSourceFactory(OkHttpJsonApiClient okHttpJsonApiClient, CompositeDisposable compositeDisposable, | ||
SessionManager sessionManager) { | ||
this.okHttpJsonApiClient = okHttpJsonApiClient; | ||
this.compositeDisposable = compositeDisposable; | ||
this.sessionManager = sessionManager; | ||
liveData = new MutableLiveData<>(); | ||
} | ||
|
||
public MutableLiveData<DataSourceClass> getMutableLiveData() { | ||
return liveData; | ||
} | ||
|
||
@Override | ||
public DataSource<Integer, LeaderboardList> create() { | ||
DataSourceClass dataSourceClass = new DataSourceClass(okHttpJsonApiClient, sessionManager); | ||
liveData.postValue(dataSourceClass); | ||
return dataSourceClass; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package fr.free.nrw.commons.profile.leaderboard; | ||
|
||
public class LeaderboardConstants { | ||
|
||
public static final int PAGE_SIZE = 10; | ||
|
||
public static final int START_OFFSET = 0; | ||
|
||
public static final String AVATAR_SOURCE_URL = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0a/%s/1024px-%s.png"; | ||
|
||
public final static String LOADING = "Loading"; | ||
|
||
public final static String LOADED = "Loaded"; | ||
|
||
} |
Uh oh!
There was an error while loading. Please reload this page.