PageRenderTime 50ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 1ms

/app/src/main/java/com/uprint/android_pack/cloudprint4androidmanager/fragment/OrdersFragment.java

https://gitlab.com/zhangxiaang/CloudPrint4AndroidManager
Java | 177 lines | 157 code | 12 blank | 8 comment | 25 complexity | 73da3f9b246fa20c5fdf6c7f04965d96 MD5 | raw file
  1. package com.uprint.android_pack.cloudprint4androidmanager.fragment;
  2. import android.content.Intent;
  3. import android.os.Bundle;
  4. import android.os.Handler;
  5. import android.support.annotation.Nullable;
  6. import android.support.v4.app.Fragment;
  7. import android.support.v4.widget.SwipeRefreshLayout;
  8. import android.support.v7.widget.LinearLayoutManager;
  9. import android.support.v7.widget.RecyclerView;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15. import com.alibaba.fastjson.JSON;
  16. import com.alibaba.fastjson.JSONObject;
  17. import com.uprint.android_pack.cloudprint4androidmanager.R;
  18. import com.uprint.android_pack.cloudprint4androidmanager.activity.DetailOrderActivity;
  19. import com.uprint.android_pack.cloudprint4androidmanager.adapter.OrdersListAdapter;
  20. import com.uprint.android_pack.cloudprint4androidmanager.network.ICallBack;
  21. import com.uprint.android_pack.cloudprint4androidmanager.network.NetValue;
  22. import com.uprint.android_pack.cloudprint4androidmanager.network.biz.CommonGetBiz;
  23. import com.uprint.android_pack.cloudprint4androidmanager.utils.EmptyViewUtil;
  24. import com.uprint.android_pack.cloudprint4androidmanager.utils.ItemClickSupport;
  25. import com.uprint.android_pack.cloudprint4androidmanager.utils.SharedPreferenceUtil;
  26. import com.uprint.android_pack.cloudprint4androidmanager.widgets.EmptyRecyclerView;
  27. import java.util.HashMap;
  28. /**
  29. * Created by zhangxiaang on 15/10/7.
  30. * TODO data save before recycled
  31. * 检查下adapter 的数据顺序吧
  32. */
  33. public class OrdersFragment extends Fragment implements SwipeRefreshLayout.OnRefreshListener {
  34. private EmptyRecyclerView recyclerView;
  35. private LinearLayoutManager manager;
  36. private OrdersListAdapter adapter;
  37. private SwipeRefreshLayout swipeRefreshLayout;
  38. private String token;
  39. private HashMap undelivered_params;
  40. private boolean hasToken;
  41. @Override
  42. public void onCreate(@Nullable Bundle savedInstanceState) {
  43. super.onCreate(savedInstanceState);
  44. undelivered_params = new HashMap<>();
  45. SharedPreferenceUtil.checkToken(getActivity());
  46. token = SharedPreferenceUtil.get_Token(getActivity());
  47. undelivered_params.put("token", token);
  48. if (token != "") {
  49. hasToken = true;
  50. }
  51. }
  52. @Override
  53. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
  54. savedInstanceState) {
  55. View view = inflater.inflate(R.layout.order_page, container, false);
  56. swipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipeLayout);
  57. swipeRefreshLayout.setOnRefreshListener(this);
  58. swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
  59. android.R.color.holo_green_light, android.R.color.holo_orange_light,
  60. android.R.color.holo_red_light);
  61. recyclerView = (EmptyRecyclerView) view.findViewById(R.id.recyclerView);
  62. manager = new LinearLayoutManager(getActivity());
  63. recyclerView.setLayoutManager(manager);
  64. TextView emptyView = (TextView) view.findViewById(R.id.empty_view);
  65. recyclerView.setEmptyView(emptyView);
  66. //todo checkout the ItemAnimation result of the white gap
  67. // recyclerView.setItemAnimator(new SlideInOutRightItemAnimator(recyclerView));
  68. if (hasToken) {
  69. EmptyViewUtil.showLoadingDialog(getActivity(), "加载订单中...", false);
  70. CommonGetBiz biz = new CommonGetBiz(getActivity());
  71. biz.request(new ICallBack() {
  72. @Override
  73. public void displayResult(int status, Object... params) {
  74. EmptyViewUtil.closeLoadingDialog();
  75. JSONObject result = (JSONObject) params[0];
  76. if (result.getInteger("error") == 0) {
  77. if (result.getJSONArray("results").size() != 0) {
  78. adapter = new OrdersListAdapter(getActivity(), result.getJSONArray("results"));
  79. SharedPreferenceUtil.setUserOrderFirstId(getActivity().
  80. getApplicationContext(), result.getJSONArray("results").getJSONObject(0).getInteger("ID"));
  81. recyclerView.setAdapter(adapter);
  82. } else {
  83. Toast.makeText(getActivity(), "当前无最新订单", Toast.LENGTH_SHORT).show();
  84. }
  85. }
  86. }
  87. }, NetValue.GET_ORDERS(), undelivered_params);
  88. }
  89. ItemClickSupport.addTo(recyclerView).setOnItemClickListener(new ItemClickSupport.OnItemClickListener() {
  90. @Override
  91. public void onItemClicked(RecyclerView recyclerView, int position, View v) {
  92. if (adapter.getJsonObject(position) != null) {
  93. int orderId = adapter.getJsonObject(position).getInteger("ID");
  94. Intent intent = new Intent(getActivity(), DetailOrderActivity.class);
  95. intent.putExtra("orderId", orderId);
  96. intent.putExtra("nickname", adapter.getJsonObject(position).getString("nickname"));
  97. startActivity(intent);
  98. getActivity().overridePendingTransition(R.anim.slide_in_right,
  99. R.anim.slide_out_right);
  100. }
  101. }
  102. });
  103. return view;
  104. }
  105. @Override
  106. public void onActivityCreated(@Nullable Bundle savedInstanceState) {
  107. super.onActivityCreated(savedInstanceState);
  108. if (savedInstanceState != null) {
  109. //todo get data
  110. String result = savedInstanceState.getString("adapterValue", "");
  111. if (result.length() > 0) {
  112. if (adapter != null) {
  113. adapter.clear();
  114. }
  115. adapter = new OrdersListAdapter(getActivity(), JSON.parseArray(result));
  116. recyclerView.setAdapter(adapter);
  117. }
  118. }
  119. }
  120. @Override
  121. public void onRefresh() {
  122. new Handler().postDelayed(new Runnable() {
  123. @Override
  124. public void run() {
  125. swipeRefreshLayout.setRefreshing(false);
  126. HashMap map = undelivered_params;
  127. map.put("firstID", SharedPreferenceUtil.getUserOderFirstId(getActivity()));
  128. CommonGetBiz biz = new CommonGetBiz(getActivity());
  129. biz.request(new ICallBack() {
  130. @Override
  131. public void displayResult(int status, Object... params) {
  132. JSONObject result = (JSONObject) params[0];
  133. if (result.getInteger("error") == 0) {
  134. if (result.getJSONArray("results").size() != 0) {
  135. int first_id = result.getJSONArray("results").getJSONObject(0).getInteger("ID");
  136. SharedPreferenceUtil.setUserOrderFirstId(getActivity(), first_id);
  137. if (adapter == null) {
  138. adapter = new OrdersListAdapter(getActivity(), result.getJSONArray("results"));
  139. } else
  140. adapter.addAll(result.getJSONArray("results"));
  141. int size = result.getJSONArray("results").size();
  142. recyclerView.setAdapter(adapter);
  143. Toast.makeText(getActivity(), "获取到" + size + "单", Toast.LENGTH_SHORT).show();
  144. } else
  145. Toast.makeText(getActivity(), "当前没有最新订单啦", Toast.LENGTH_SHORT).show();
  146. }
  147. }
  148. }, NetValue.GET_ORDERS(), map);
  149. }
  150. }, 2000);
  151. }
  152. public boolean isRefreshing() {
  153. if (swipeRefreshLayout != null) {
  154. return swipeRefreshLayout.isRefreshing();
  155. }
  156. return false;
  157. }
  158. @Override
  159. public void onSaveInstanceState(Bundle outState) {
  160. super.onSaveInstanceState(outState);
  161. if (adapter != null) {
  162. outState.putString("adapterValue", adapter.getData().toString());
  163. }
  164. }
  165. }