Skip to content

Commit 33548fa

Browse files
pshnicolas-raoul
andauthored
Convert profile package to kotlin (commons-app#5979)
* Convert ViewModelFactory to kotlin * Convert UpdateAvatarResponse and related test to Kotlin * Convert LeaderboardResponse and related test to kotlin * Convert LeaderboardListAdapter to kotlin * Convert UserDetailAdapter to kotlin * Convert LeaderboardListViewModel to kotlin * Convert DataSourceClass to kotlin * Convert the LeaderboardFragment to kotlin * Converted AchievementsFragment to kotlin * Revert "Converted AchievementsFragment to kotlin" This reverts commit 4fcbb81. --------- Co-authored-by: Nicolas Raoul <nicolas.raoul@gmail.com>
1 parent 8265cc6 commit 33548fa

26 files changed

+1042
-1694
lines changed

app/src/main/java/fr/free/nrw/commons/profile/leaderboard/DataSourceClass.java

-125
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
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+
}

app/src/main/java/fr/free/nrw/commons/profile/leaderboard/DataSourceFactory.java

-110
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package fr.free.nrw.commons.profile.leaderboard
2+
3+
import androidx.lifecycle.MutableLiveData
4+
import androidx.paging.DataSource
5+
import fr.free.nrw.commons.auth.SessionManager
6+
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient
7+
8+
/**
9+
* This class will create a new instance of the data source class on pagination
10+
*/
11+
class DataSourceFactory(
12+
private val okHttpJsonApiClient: OkHttpJsonApiClient,
13+
private val sessionManager: SessionManager
14+
) : DataSource.Factory<Int, LeaderboardList>() {
15+
val mutableLiveData: MutableLiveData<DataSourceClass> = MutableLiveData()
16+
var duration: String? = null
17+
var category: String? = null
18+
var limit: Int = 0
19+
var offset: Int = 0
20+
21+
/**
22+
* Creates the new instance of data source class
23+
*/
24+
override fun create(): DataSource<Int, LeaderboardList> = DataSourceClass(
25+
okHttpJsonApiClient, sessionManager, duration, category, limit, offset
26+
).also { mutableLiveData.postValue(it) }
27+
}

app/src/main/java/fr/free/nrw/commons/profile/leaderboard/LeaderboardConstants.java

-45
This file was deleted.

0 commit comments

Comments
 (0)