Guest User

Untitled

a guest
Dec 18th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.55 KB | None | 0 0
  1. package com.sw.hc.surge.adapter.fitness
  2.  
  3.  
  4. import android.arch.paging.PagedListAdapter
  5. import android.content.Context
  6. import android.support.v7.util.DiffUtil
  7. import android.support.v7.widget.RecyclerView
  8. import android.view.LayoutInflater
  9. import android.view.View
  10. import android.view.ViewGroup
  11. import com.sw.hc.surge.R
  12. import com.sw.hc.surge.model.wellbeing.Resource
  13. import kotlinx.android.synthetic.main.row_wellbeing_resource_item.view.*
  14.  
  15. class ResourcesAdapter(private val context: Context) : PagedListAdapter<Resource, ResourcesAdapter.ViewHolder>(DIFF_CALLBACK) {
  16.  
  17. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
  18. val itemView = LayoutInflater.from(parent.context)
  19. .inflate(R.layout.row_wellbeing_resource_item, parent, false)
  20.  
  21. return ViewHolder(itemView)
  22. }
  23.  
  24. override fun onBindViewHolder(holder: ViewHolder, position: Int) {
  25.  
  26. holder.bind(position)
  27. }
  28.  
  29. inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
  30.  
  31. fun bind(position: Int) {
  32.  
  33. val resource = getItem(position)
  34.  
  35. with(itemView) {
  36.  
  37. tv_type.text = resource?.title
  38. }
  39. }
  40. }
  41.  
  42. companion object {
  43.  
  44. private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Resource>() {
  45. override fun areItemsTheSame(oldItem: Resource, newItem: Resource): Boolean {
  46. return oldItem.postId == newItem.postId // todo: make three =
  47. }
  48.  
  49. override fun areContentsTheSame(oldItem: Resource, newItem: Resource): Boolean {
  50. return oldItem == newItem
  51. }
  52. }
  53. }
  54. }
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61. public class ItemViewModel extends ViewModel {
  62.  
  63. public LiveData<PagedList<Resource>> itemPagedList;
  64. public LiveData<PageKeyedDataSource<Integer, Resource>> liveDataSource;
  65.  
  66. public ItemViewModel() {
  67.  
  68. ItemDataSourceFactory itemDataSourceFactory = new ItemDataSourceFactory();
  69. liveDataSource = itemDataSourceFactory.getItemLiveDataSource();
  70.  
  71. PagedList.Config config =
  72. (new PagedList.Config.Builder())
  73. .setEnablePlaceholders(false)
  74. .setPageSize(ItemDataSource.Companion.getPAGE_SIZE())
  75. .build();
  76.  
  77. itemPagedList = (new LivePagedListBuilder(itemDataSourceFactory, config)).build();
  78.  
  79. }
  80. }
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87. public class ItemDataSourceFactory extends DataSource.Factory {
  88.  
  89. private MutableLiveData<PageKeyedDataSource<Integer, Resource>> itemLiveDataSource = new MutableLiveData<>();
  90.  
  91.  
  92. @Override
  93. public DataSource create() {
  94. ItemDataSource itemDataSource = new ItemDataSource();
  95. itemLiveDataSource.postValue(itemDataSource);
  96. return itemDataSource;
  97. }
  98.  
  99. public MutableLiveData<PageKeyedDataSource<Integer, Resource>> getItemLiveDataSource() {
  100. return itemLiveDataSource;
  101. }
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108. public class GetAllResourceListAPI {
  109.  
  110. private static String LOG_TAG = "GetAllResourceListAPI";
  111.  
  112. public interface ThisCallback {
  113.  
  114. void onSuccess(GetResourceList getResourceList);
  115.  
  116. void onFailure(String failureMessage);
  117.  
  118. void onError(String errorMessage);
  119. }
  120.  
  121.  
  122. /* POST */
  123. public static void postData(JsonObject jo, final ThisCallback callback) {
  124. Call<JsonObject> call = Service.getService().get_all_resource_list(jo);
  125. call.enqueue(new Callback<JsonObject>() {
  126.  
  127. @Override
  128. public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
  129.  
  130. try {
  131. if (response.body().get("success").getAsBoolean()) {
  132. Log.e(LOG_TAG, "success");
  133.  
  134. Gson gson = new GsonBuilder().setPrettyPrinting().create();
  135. GetResourceList getResourceList = gson.fromJson(response.body(), GetResourceList.class);
  136.  
  137. callback.onSuccess(getResourceList);
  138.  
  139. } else {
  140. Log.e(LOG_TAG, "else");
  141.  
  142. String error = response.body().get("err").getAsString();
  143.  
  144. callback.onError(error);
  145. }
  146.  
  147. } catch (Exception e) {
  148. Log.e(LOG_TAG, "exception" + e.getLocalizedMessage());
  149.  
  150. callback.onFailure(e.getMessage());
  151. }
  152. }
  153.  
  154. @Override
  155. public void onFailure(Call<JsonObject> call, Throwable t) {
  156. Log.e(LOG_TAG, "onFailure" + t.getMessage());
  157.  
  158. callback.onFailure(t.getMessage());
  159. }
  160. });
  161. }
  162. }
Add Comment
Please, Sign In to add comment