Guest User

Past Fragment

a guest
Dec 31st, 2016
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. import android.content.Context;
  2. import android.content.Intent;
  3. import android.content.IntentFilter;
  4. import android.os.Bundle;
  5. import android.support.annotation.Nullable;
  6. import android.support.v4.app.Fragment;
  7. import android.support.v4.content.LocalBroadcastManager;
  8. import android.support.v7.widget.DefaultItemAnimator;
  9. import android.support.v7.widget.LinearLayoutManager;
  10. import android.support.v7.widget.RecyclerView;
  11. import android.view.LayoutInflater;
  12. import android.view.View;
  13. import android.view.ViewGroup;
  14. import android.widget.Button;
  15. import android.widget.TextView;
  16.  
  17. import java.util.ArrayList;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Locale;
  21. import java.util.Map;
  22.  
  23. import controllers.listeners.ProcessedResult;
  24. import retrofit2.Response;
  25.  
  26. public class Past extends Fragment implements ProcessedResult,View.OnClickListener
  27. {
  28. private int skip = 0, totalSize = -1;
  29. private List<MBooking> orderData;
  30. private StatesRecyclerViewAdapter statesRecyclerViewAdapter;
  31.  
  32. private Context context;
  33.  
  34. @Override
  35. public void onAttach(Context context) {
  36. super.onAttach(context);
  37. this.context=context;
  38. }
  39.  
  40. @Nullable
  41. @Override
  42. public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  43. super.onCreateView(inflater, container, savedInstanceState);
  44. return inflater.inflate(R.layout.recyclerview,container,false);
  45. }
  46.  
  47. @Override
  48. public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
  49. super.onViewCreated(view, savedInstanceState);
  50.  
  51. RecyclerView mRecyclerView = GeneralFunction.findViewByIdAndCast(view, R.id.common_recyclerView);
  52. mRecyclerView.setLayoutManager(new LinearLayoutManager(context));
  53. mRecyclerView.addItemDecoration(new SimpleDividerDecoration(context,R.dimen.dimen_drawable_padding,R.color.color_tranparent));
  54. mRecyclerView.setItemAnimator(new DefaultItemAnimator());
  55.  
  56. View loadingView = LayoutInflater.from(context).inflate(R.layout.progress_footer, mRecyclerView,false);
  57. View emptyView = LayoutInflater.from(context).inflate(R.layout.view_empty, mRecyclerView,false);
  58. View errorView = LayoutInflater.from(context).inflate(R.layout.view_error, mRecyclerView,false);
  59.  
  60. Button btRetry=GeneralFunction.findViewByIdAndCast(errorView,R.id.retry_button);
  61. DrawableState.buttonSelector(btRetry);
  62. btRetry.setOnClickListener(this);
  63.  
  64. TextView tvEmptyText=GeneralFunction.findViewByIdAndCast(emptyView,R.id.empty_text);
  65. tvEmptyText.setText(String.format(Locale.getDefault(),getString(R.string.s_empty_error_no),getString(R.string.s_empty_error_past)));
  66.  
  67. orderData = new ArrayList<>();
  68. AOrders aOrdersAdapter = new AOrders(orderData,this);
  69. // aOrdersAdapter.setHasStableIds(true);
  70. statesRecyclerViewAdapter = new StatesRecyclerViewAdapter(aOrdersAdapter,loadingView,emptyView,errorView, this);
  71. mRecyclerView.setAdapter(statesRecyclerViewAdapter);
  72.  
  73. IntentFilter filter = new IntentFilter(EnumStringConstants.LocalReceivers.RATING.toString());
  74. filter.addAction(EnumStringConstants.LocalReceivers.PUSH_DATA.toString());
  75. LocalBroadcastManager.getInstance(context).registerReceiver(mMessageReceiver, filter);
  76. }
  77.  
  78. @Override
  79. public <IResponse, IMethod> void processedResult(IResponse iResponse, IMethod iMethod) {
  80. switch (iMethod.toString())
  81. {
  82. case CallBackConstants.LOAD_MORE_METHOD:
  83. {
  84. if (totalSize == skip)
  85. {
  86. statesRecyclerViewAdapter.onDataReady(false);
  87. return;
  88. }
  89.  
  90. Map<String, String> data= new HashMap<>();
  91. data.put(RestConstants.KEY_BOOKING_TYPE,String.valueOf(1));
  92. data.put(RestConstants.LIMIT,String.valueOf(7));
  93. data.put(RestConstants.SKIP,String.valueOf(skip));
  94.  
  95. RetrofitAdapter.createRetrofitService().getOrders(data).enqueue(new ResponseResolver<MOrders>(this,CallBackConstants.ParsingConstants.BOOKING_UPCOMING,true,true));
  96. }
  97. break;
  98. case CallBackConstants.ParsingConstants.BOOKING_UPCOMING:
  99. if (iResponse instanceof Response) {
  100. MOrders mOrders = (MOrders)((Response<?>) iResponse).body();
  101. totalSize = mOrders.getData().getCount();
  102. int size = mOrders.getData().getBookings().size();
  103. skip += size;
  104. if (size > 0) {
  105. statesRecyclerViewAdapter.onDataReady(true);
  106.  
  107. int prevousSize = orderData.size();
  108. orderData.addAll(mOrders.getData().getBookings());
  109. getProductAdapter().notifyItemRangeInserted(prevousSize, size);
  110. }else if(orderData.isEmpty())
  111. statesRecyclerViewAdapter.setState(StatesRecyclerViewAdapter.STATE_EMPTY);
  112. }
  113. else
  114. statesRecyclerViewAdapter.setState(StatesRecyclerViewAdapter.STATE_ERROR);
  115. break;
  116. break;
  117. }
  118. }
  119.  
  120. private AOrders getProductAdapter()
  121. {
  122. return (AOrders)statesRecyclerViewAdapter.getWrappedAdapter();
  123. }
  124.  
  125. @Override
  126. public void onClick(View v) {
  127. switch (v.getId())
  128. {
  129. case R.id.retry_button: {
  130. statesRecyclerViewAdapter.setState(StatesRecyclerViewAdapter.STATE_ENDLESS);
  131. statesRecyclerViewAdapter.restartAppending();
  132. }
  133. break;
  134. }
  135. }
  136. }
Add Comment
Please, Sign In to add comment