mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-29 22:03:55 +01:00
Convert LeaderboardListViewModel to kotlin
This commit is contained in:
parent
5b8d74a6f1
commit
051dfde1f0
5 changed files with 88 additions and 225 deletions
|
|
@ -1,110 +0,0 @@
|
|||
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;
|
||||
|
||||
/**
|
||||
* This class will create a new instance of the data source class on pagination
|
||||
*/
|
||||
public class DataSourceFactory extends DataSource.Factory<Integer, LeaderboardList> {
|
||||
|
||||
private MutableLiveData<DataSourceClass> liveData;
|
||||
private OkHttpJsonApiClient okHttpJsonApiClient;
|
||||
private CompositeDisposable compositeDisposable;
|
||||
private SessionManager sessionManager;
|
||||
private String duration;
|
||||
private String category;
|
||||
private int limit;
|
||||
private int offset;
|
||||
|
||||
/**
|
||||
* Gets the current set leaderboard list duration
|
||||
*/
|
||||
public String getDuration() {
|
||||
return duration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the current set leaderboard duration with the new duration
|
||||
*/
|
||||
public void setDuration(final String duration) {
|
||||
this.duration = duration;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current set leaderboard list category
|
||||
*/
|
||||
public String getCategory() {
|
||||
return category;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the current set leaderboard category with the new category
|
||||
*/
|
||||
public void setCategory(final String category) {
|
||||
this.category = category;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current set leaderboard list limit
|
||||
*/
|
||||
public int getLimit() {
|
||||
return limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the current set leaderboard limit with the new limit
|
||||
*/
|
||||
public void setLimit(final int limit) {
|
||||
this.limit = limit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the current set leaderboard list offset
|
||||
*/
|
||||
public int getOffset() {
|
||||
return offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the current set leaderboard offset with the new offset
|
||||
*/
|
||||
public void setOffset(final int offset) {
|
||||
this.offset = offset;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor for DataSourceFactory class
|
||||
* @param okHttpJsonApiClient client for OKhttp
|
||||
* @param compositeDisposable composite disposable
|
||||
* @param sessionManager sessionManager
|
||||
*/
|
||||
public DataSourceFactory(OkHttpJsonApiClient okHttpJsonApiClient, CompositeDisposable compositeDisposable,
|
||||
SessionManager sessionManager) {
|
||||
this.okHttpJsonApiClient = okHttpJsonApiClient;
|
||||
this.compositeDisposable = compositeDisposable;
|
||||
this.sessionManager = sessionManager;
|
||||
liveData = new MutableLiveData<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the live data
|
||||
*/
|
||||
public MutableLiveData<DataSourceClass> getMutableLiveData() {
|
||||
return liveData;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates the new instance of data source class
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public DataSource<Integer, LeaderboardList> create() {
|
||||
DataSourceClass dataSourceClass = new DataSourceClass(okHttpJsonApiClient, sessionManager, duration, category, limit, offset);
|
||||
liveData.postValue(dataSourceClass);
|
||||
return dataSourceClass;
|
||||
}
|
||||
}
|
||||
|
|
@ -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) }
|
||||
}
|
||||
|
|
@ -20,10 +20,10 @@ import fr.free.nrw.commons.profile.leaderboard.LeaderboardListAdapter.ListViewHo
|
|||
*/
|
||||
class LeaderboardListAdapter : PagedListAdapter<LeaderboardList, ListViewHolder>(DIFF_CALLBACK) {
|
||||
inner class ListViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
var rank: TextView = itemView.findViewById(R.id.user_rank)
|
||||
var avatar: SimpleDraweeView = itemView.findViewById(R.id.user_avatar)
|
||||
var username: TextView = itemView.findViewById(R.id.user_name)
|
||||
var count: TextView = itemView.findViewById(R.id.user_count)
|
||||
var rank: TextView? = itemView.findViewById(R.id.user_rank)
|
||||
var avatar: SimpleDraweeView? = itemView.findViewById(R.id.user_avatar)
|
||||
var username: TextView? = itemView.findViewById(R.id.user_name)
|
||||
var count: TextView? = itemView.findViewById(R.id.user_count)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -46,10 +46,10 @@ class LeaderboardListAdapter : PagedListAdapter<LeaderboardList, ListViewHolder>
|
|||
override fun onBindViewHolder(holder: ListViewHolder, position: Int) = with (holder) {
|
||||
val item = getItem(position)!!
|
||||
|
||||
rank.text = item.rank.toString()
|
||||
avatar.setImageURI(Uri.parse(item.avatar))
|
||||
username.text = item.username
|
||||
count.text = item.categoryCount.toString()
|
||||
rank?.text = item.rank.toString()
|
||||
avatar?.setImageURI(Uri.parse(item.avatar))
|
||||
username?.text = item.username
|
||||
count?.text = item.categoryCount.toString()
|
||||
|
||||
/*
|
||||
Now that we have our in app profile-section, lets take the user there
|
||||
|
|
|
|||
|
|
@ -1,107 +0,0 @@
|
|||
package fr.free.nrw.commons.profile.leaderboard;
|
||||
|
||||
import static fr.free.nrw.commons.profile.leaderboard.LeaderboardConstants.PAGE_SIZE;
|
||||
|
||||
import androidx.lifecycle.LiveData;
|
||||
import androidx.lifecycle.MutableLiveData;
|
||||
import androidx.lifecycle.Transformations;
|
||||
import androidx.lifecycle.ViewModel;
|
||||
import androidx.paging.LivePagedListBuilder;
|
||||
import androidx.paging.PagedList;
|
||||
import fr.free.nrw.commons.auth.SessionManager;
|
||||
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
|
||||
import io.reactivex.disposables.CompositeDisposable;
|
||||
|
||||
/**
|
||||
* Extends the ViewModel class and creates the LeaderboardList View Model
|
||||
*/
|
||||
public class LeaderboardListViewModel extends ViewModel {
|
||||
|
||||
private DataSourceFactory dataSourceFactory;
|
||||
private LiveData<PagedList<LeaderboardList>> listLiveData;
|
||||
private CompositeDisposable compositeDisposable = new CompositeDisposable();
|
||||
private LiveData<String> progressLoadStatus = new MutableLiveData<>();
|
||||
|
||||
/**
|
||||
* Constructor for a new LeaderboardListViewModel
|
||||
* @param okHttpJsonApiClient
|
||||
* @param sessionManager
|
||||
*/
|
||||
public LeaderboardListViewModel(OkHttpJsonApiClient okHttpJsonApiClient, SessionManager
|
||||
sessionManager) {
|
||||
|
||||
dataSourceFactory = new DataSourceFactory(okHttpJsonApiClient,
|
||||
compositeDisposable, sessionManager);
|
||||
initializePaging();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Initialises the paging
|
||||
*/
|
||||
private void initializePaging() {
|
||||
|
||||
PagedList.Config pagedListConfig =
|
||||
new PagedList.Config.Builder()
|
||||
.setEnablePlaceholders(false)
|
||||
.setInitialLoadSizeHint(PAGE_SIZE)
|
||||
.setPageSize(PAGE_SIZE).build();
|
||||
|
||||
listLiveData = new LivePagedListBuilder<>(dataSourceFactory, pagedListConfig)
|
||||
.build();
|
||||
|
||||
progressLoadStatus = Transformations
|
||||
.switchMap(dataSourceFactory.getMutableLiveData(), DataSourceClass::getProgressLiveStatus);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Refreshes the paged list with the new params and starts the loading of new data
|
||||
* @param duration
|
||||
* @param category
|
||||
* @param limit
|
||||
* @param offset
|
||||
*/
|
||||
public void refresh(String duration, String category, int limit, int offset) {
|
||||
dataSourceFactory.setDuration(duration);
|
||||
dataSourceFactory.setCategory(category);
|
||||
dataSourceFactory.setLimit(limit);
|
||||
dataSourceFactory.setOffset(offset);
|
||||
dataSourceFactory.getMutableLiveData().getValue().invalidate();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the new params for the paged list API calls
|
||||
* @param duration
|
||||
* @param category
|
||||
* @param limit
|
||||
* @param offset
|
||||
*/
|
||||
public void setParams(String duration, String category, int limit, int offset) {
|
||||
dataSourceFactory.setDuration(duration);
|
||||
dataSourceFactory.setCategory(category);
|
||||
dataSourceFactory.setLimit(limit);
|
||||
dataSourceFactory.setOffset(offset);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the loading status of paged list
|
||||
*/
|
||||
public LiveData<String> getProgressLoadStatus() {
|
||||
return progressLoadStatus;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the paged list with live data
|
||||
*/
|
||||
public LiveData<PagedList<LeaderboardList>> getListLiveData() {
|
||||
return listLiveData;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onCleared() {
|
||||
super.onCleared();
|
||||
compositeDisposable.clear();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
package fr.free.nrw.commons.profile.leaderboard
|
||||
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.switchMap
|
||||
import androidx.paging.LivePagedListBuilder
|
||||
import androidx.paging.PagedList
|
||||
import fr.free.nrw.commons.auth.SessionManager
|
||||
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient
|
||||
import fr.free.nrw.commons.profile.leaderboard.LeaderboardConstants.PAGE_SIZE
|
||||
|
||||
/**
|
||||
* Extends the ViewModel class and creates the LeaderboardList View Model
|
||||
*/
|
||||
class LeaderboardListViewModel(
|
||||
okHttpJsonApiClient: OkHttpJsonApiClient,
|
||||
sessionManager: SessionManager
|
||||
) : ViewModel() {
|
||||
private val dataSourceFactory = DataSourceFactory(okHttpJsonApiClient, sessionManager)
|
||||
|
||||
val listLiveData: LiveData<PagedList<LeaderboardList>> = LivePagedListBuilder(
|
||||
dataSourceFactory,
|
||||
PagedList.Config.Builder()
|
||||
.setEnablePlaceholders(false)
|
||||
.setInitialLoadSizeHint(PAGE_SIZE)
|
||||
.setPageSize(PAGE_SIZE).build()
|
||||
).build()
|
||||
|
||||
val progressLoadStatus: LiveData<String> = dataSourceFactory.mutableLiveData.switchMap {
|
||||
it.progressLiveStatus
|
||||
}
|
||||
|
||||
/**
|
||||
* Refreshes the paged list with the new params and starts the loading of new data
|
||||
*/
|
||||
fun refresh(duration: String?, category: String?, limit: Int, offset: Int) {
|
||||
dataSourceFactory.duration = duration
|
||||
dataSourceFactory.category = category
|
||||
dataSourceFactory.limit = limit
|
||||
dataSourceFactory.offset = offset
|
||||
dataSourceFactory.mutableLiveData.value!!.invalidate()
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the new params for the paged list API calls
|
||||
*/
|
||||
fun setParams(duration: String?, category: String?, limit: Int, offset: Int) {
|
||||
dataSourceFactory.duration = duration
|
||||
dataSourceFactory.category = category
|
||||
dataSourceFactory.limit = limit
|
||||
dataSourceFactory.offset = offset
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue