Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.sw.hc.surge.adapter.fitness
- import android.arch.paging.PagedListAdapter
- import android.content.Context
- import android.support.v7.util.DiffUtil
- import android.support.v7.widget.RecyclerView
- import android.view.LayoutInflater
- import android.view.View
- import android.view.ViewGroup
- import com.sw.hc.surge.R
- import com.sw.hc.surge.model.wellbeing.Resource
- import kotlinx.android.synthetic.main.row_wellbeing_resource_item.view.*
- class ResourcesAdapter(private val context: Context) : PagedListAdapter<Resource, ResourcesAdapter.ViewHolder>(DIFF_CALLBACK) {
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
- val itemView = LayoutInflater.from(parent.context)
- .inflate(R.layout.row_wellbeing_resource_item, parent, false)
- return ViewHolder(itemView)
- }
- override fun onBindViewHolder(holder: ViewHolder, position: Int) {
- holder.bind(position)
- }
- inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
- fun bind(position: Int) {
- val resource = getItem(position)
- with(itemView) {
- tv_type.text = resource?.title
- }
- }
- }
- companion object {
- private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Resource>() {
- override fun areItemsTheSame(oldItem: Resource, newItem: Resource): Boolean {
- return oldItem.postId == newItem.postId // todo: make three =
- }
- override fun areContentsTheSame(oldItem: Resource, newItem: Resource): Boolean {
- return oldItem == newItem
- }
- }
- }
- }
- public class ItemViewModel extends ViewModel {
- public LiveData<PagedList<Resource>> itemPagedList;
- public LiveData<PageKeyedDataSource<Integer, Resource>> liveDataSource;
- public ItemViewModel() {
- ItemDataSourceFactory itemDataSourceFactory = new ItemDataSourceFactory();
- liveDataSource = itemDataSourceFactory.getItemLiveDataSource();
- PagedList.Config config =
- (new PagedList.Config.Builder())
- .setEnablePlaceholders(false)
- .setPageSize(ItemDataSource.Companion.getPAGE_SIZE())
- .build();
- itemPagedList = (new LivePagedListBuilder(itemDataSourceFactory, config)).build();
- }
- }
- public class ItemDataSourceFactory extends DataSource.Factory {
- private MutableLiveData<PageKeyedDataSource<Integer, Resource>> itemLiveDataSource = new MutableLiveData<>();
- @Override
- public DataSource create() {
- ItemDataSource itemDataSource = new ItemDataSource();
- itemLiveDataSource.postValue(itemDataSource);
- return itemDataSource;
- }
- public MutableLiveData<PageKeyedDataSource<Integer, Resource>> getItemLiveDataSource() {
- return itemLiveDataSource;
- }
- }
- public class GetAllResourceListAPI {
- private static String LOG_TAG = "GetAllResourceListAPI";
- public interface ThisCallback {
- void onSuccess(GetResourceList getResourceList);
- void onFailure(String failureMessage);
- void onError(String errorMessage);
- }
- /* POST */
- public static void postData(JsonObject jo, final ThisCallback callback) {
- Call<JsonObject> call = Service.getService().get_all_resource_list(jo);
- call.enqueue(new Callback<JsonObject>() {
- @Override
- public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
- try {
- if (response.body().get("success").getAsBoolean()) {
- Log.e(LOG_TAG, "success");
- Gson gson = new GsonBuilder().setPrettyPrinting().create();
- GetResourceList getResourceList = gson.fromJson(response.body(), GetResourceList.class);
- callback.onSuccess(getResourceList);
- } else {
- Log.e(LOG_TAG, "else");
- String error = response.body().get("err").getAsString();
- callback.onError(error);
- }
- } catch (Exception e) {
- Log.e(LOG_TAG, "exception" + e.getLocalizedMessage());
- callback.onFailure(e.getMessage());
- }
- }
- @Override
- public void onFailure(Call<JsonObject> call, Throwable t) {
- Log.e(LOG_TAG, "onFailure" + t.getMessage());
- callback.onFailure(t.getMessage());
- }
- });
- }
- }
Add Comment
Please, Sign In to add comment