Skip to content

Commit

Permalink
Convert profile package to kotlin (commons-app#5979)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
psh and nicolas-raoul authored Dec 3, 2024
1 parent 8265cc6 commit 33548fa
Show file tree
Hide file tree
Showing 26 changed files with 1,042 additions and 1,694 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package fr.free.nrw.commons.profile.leaderboard

import android.accounts.Account
import androidx.lifecycle.MutableLiveData
import androidx.paging.PageKeyedDataSource
import fr.free.nrw.commons.auth.SessionManager
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient
import fr.free.nrw.commons.profile.leaderboard.LeaderboardConstants.LoadingStatus.LOADING
import fr.free.nrw.commons.profile.leaderboard.LeaderboardConstants.LoadingStatus.LOADED
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.disposables.Disposable
import timber.log.Timber
import java.util.Objects

/**
* This class will call the leaderboard API to get new list when the pagination is performed
*/
class DataSourceClass(
private val okHttpJsonApiClient: OkHttpJsonApiClient,
private val sessionManager: SessionManager,
private val duration: String?,
private val category: String?,
private val limit: Int,
private val offset: Int
) : PageKeyedDataSource<Int, LeaderboardList>() {
val progressLiveStatus: MutableLiveData<LeaderboardConstants.LoadingStatus> = MutableLiveData()
private val compositeDisposable = CompositeDisposable()


override fun loadInitial(
params: LoadInitialParams<Int>, callback: LoadInitialCallback<Int, LeaderboardList?>
) {
compositeDisposable.add(okHttpJsonApiClient.getLeaderboard(
sessionManager.currentAccount?.name,
duration,
category,
limit.toString(),
offset.toString()
).doOnSubscribe { disposable: Disposable? ->
compositeDisposable.add(disposable!!)
progressLiveStatus.postValue(LOADING)
}.subscribe({ response: LeaderboardResponse? ->
if (response != null && response.status == 200) {
progressLiveStatus.postValue(LOADED)
callback.onResult(response.leaderboardList!!, null, response.limit)
}
}, { t: Throwable? ->
Timber.e(t, "Fetching leaderboard statistics failed")
progressLiveStatus.postValue(LOADING)
}))
}

override fun loadBefore(
params: LoadParams<Int>, callback: LoadCallback<Int, LeaderboardList?>
) = Unit

override fun loadAfter(
params: LoadParams<Int>, callback: LoadCallback<Int, LeaderboardList?>
) {
compositeDisposable.add(okHttpJsonApiClient.getLeaderboard(
Objects.requireNonNull<Account?>(sessionManager.currentAccount).name,
duration,
category,
limit.toString(),
params.key.toString()
).doOnSubscribe { disposable: Disposable? ->
compositeDisposable.add(disposable!!)
progressLiveStatus.postValue(LOADING)
}.subscribe({ response: LeaderboardResponse? ->
if (response != null && response.status == 200) {
progressLiveStatus.postValue(LOADED)
callback.onResult(response.leaderboardList!!, params.key + limit)
}
}, { t: Throwable? ->
Timber.e(t, "Fetching leaderboard statistics failed")
progressLiveStatus.postValue(LOADING)
}))
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
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

/**
* This class will create a new instance of the data source class on pagination
*/
class DataSourceFactory(
private val okHttpJsonApiClient: OkHttpJsonApiClient,
private val sessionManager: SessionManager
) : DataSource.Factory<Int, LeaderboardList>() {
val mutableLiveData: MutableLiveData<DataSourceClass> = MutableLiveData()
var duration: String? = null
var category: String? = null
var limit: Int = 0
var offset: Int = 0

/**
* Creates the new instance of data source class
*/
override fun create(): DataSource<Int, LeaderboardList> = DataSourceClass(
okHttpJsonApiClient, sessionManager, duration, category, limit, offset
).also { mutableLiveData.postValue(it) }
}

This file was deleted.

Loading

0 comments on commit 33548fa

Please sign in to comment.