Advertisement
Guest User

Untitled

a guest
Jun 14th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.29 KB | None | 0 0
  1. private void getDetails() {// get details of user from shared preference...
  2. CLoginSessionManagement m_oSessionManagement = new CLoginSessionManagement(getActivity());// crating object of Login Session
  3. HashMap<String, String> user = m_oSessionManagement.getLoginDetails();// get String from Login Session
  4. m_szMobileNumber = user.get(CLoginSessionManagement.s_szKEY_MOBILE).trim();// getting password from saved preferences..........
  5. m_szEncryptedPassword = user.get(CLoginSessionManagement.s_szKEY_PASSWORD).trim();// getting mobile num from shared preferences...
  6. sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// increment of record count
  7. sz_LastCount = String.valueOf(m_n_DeafalutLastCount);// increment of last count...
  8. s_oDataset = new ArrayList<>();// making object of Arraylist
  9.  
  10. }
  11.  
  12. private void init() {// initialize controls
  13.  
  14.  
  15. m_ProgressBar = (ProgressBar) m_Main.findViewById(R.id.progressBar1);// finding Id of progressview
  16. m_ProgressBar.setVisibility(View.GONE);// make profressView Invisible first time
  17. m_ListView = (ListView) m_Main.findViewById(R.id.dealList);// findind Id of Listview
  18. m_ListView.setFadingEdgeLength(0);
  19. /*Swipe to refresh code*/
  20. mSwipeRefresh = (SwipeRefreshLayout) m_Main.findViewById(R.id.activity_main_swipe_refresh_layout);
  21.  
  22.  
  23. if (NetworkUtil.isConnected(getActivity())) {
  24. postDealListingDatatoServer();
  25. } else {
  26.  
  27. Toast.makeText(getActivity(), "Please check internet connection !", Toast.LENGTH_LONG).show();
  28. }
  29. mSwipeRefresh.setColorSchemeResources(R.color.refresh_progress_1);
  30.  
  31. mSwipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
  32. @Override
  33. public void onRefresh() {
  34. /*Here check net connection avialable or not */
  35. if (NetworkUtil.isConnected(getActivity())) {
  36.  
  37. s_oDataset.clear();
  38. m_ListView.removeFooterView(mFooter);
  39. m_ListView.invalidate();
  40. m_n_DeafalutLastCount = 0;
  41. sz_LastCount = String.valueOf(m_n_DeafalutLastCount);// convert int value to string /////
  42. swipeData();
  43.  
  44.  
  45. } else {
  46. Toast.makeText(getActivity(), "Please check internet connection !", Toast.LENGTH_LONG).show();
  47. if (mSwipeRefresh.isRefreshing()) {
  48.  
  49. mSwipeRefresh.setRefreshing(false);
  50. }
  51. }
  52. }
  53. });
  54. m_ListView.setOnScrollListener(new AbsListView.OnScrollListener() {
  55. @Override
  56. public void onScrollStateChanged(AbsListView view, int scrollState) {
  57.  
  58. }
  59.  
  60. @Override
  61. public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
  62. boolean enable = false;
  63. if (m_ListView != null && m_ListView.getChildCount() > 0) {
  64. // check if the first item of the list is visible
  65. boolean firstItemVisible = m_ListView.getFirstVisiblePosition() == 0;
  66. // check if the top of the first item is visible
  67. boolean topOfFirstItemVisible = m_ListView.getChildAt(0).getTop() == 0;
  68. // enabling or disabling the refresh layout
  69. enable = firstItemVisible && topOfFirstItemVisible;
  70. }
  71. mSwipeRefresh.setEnabled(enable);
  72. }
  73. });
  74. m_n_FormImage = new int[]{// defining Images in Integer array
  75. R.drawable.amazon,
  76. R.drawable.whatsapp,
  77. R.drawable.zorpia,
  78. R.drawable.path,
  79. R.drawable.app_me,
  80. R.drawable.evernote,
  81. R.drawable.app_me};
  82.  
  83.  
  84. }
  85.  
  86. /*This is new changes in code ....using Volley instead of AsynkTask*/
  87.  
  88. /*This method send request to server for deallisting*/
  89. // this method send request to server for deal list....
  90. public void postDealListingDatatoServer() {
  91. try {
  92. String json;
  93. // 3. build jsonObject
  94. JSONObject jsonObject = new JSONObject();// making object of Jsons.
  95. jsonObject.put("agentCode", m_szMobileNumber);// put mobile number
  96. jsonObject.put("pin", m_szEncryptedPassword);// put password
  97. jsonObject.put("recordcount", sz_RecordCount);// put record count
  98. jsonObject.put("lastcountvalue", sz_LastCount);// put last count
  99. // 4. convert JSONObject to JSON to String
  100. json = jsonObject.toString();// convert Json object to string
  101.  
  102. System.out.println("Request:-" + json);
  103. m_Dialog = DialogUtils.showProgressDialog(getActivity(), "Please wait while loading deals...");
  104. JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, CServerAPI.m_DealListingURL, jsonObject, new Response.Listener<JSONObject>() {
  105. @Override
  106. public void onResponse(JSONObject response) {
  107. System.out.println("Response:-" + response);
  108. m_Dialog.dismiss();
  109. try {
  110. JSONArray posts = response.optJSONArray("dealList");// get Deal list in array from response
  111. for (int i = 0; i < posts.length(); i++) {// loop for counting deals from server
  112. JSONObject post = posts.getJSONObject(i);// counting deal based on index
  113. item = new CDealAppDatastorage();// creating object of DealAppdata storage
  114. item.setM_szHeaderText(post.getString("dealname"));// get deal name from response
  115. item.setM_szsubHeaderText(post.getString("dealcode"));// get dealcode from response
  116. item.setM_szDealValue(post.getString("dealvalue"));// get deal value from response
  117. item.setM_n_Image(m_n_FormImage[i]);//set Image Index wise(Dummy)
  118. s_oDataset.add(item);// add all items in ArrayList
  119.  
  120. }
  121. mFooter = View.inflate(getActivity(), R.layout.footer, null);
  122.  
  123.  
  124. if (!s_oDataset.isEmpty()) {// condition if data in arraylist is not empty
  125. // Adding Load More button to lisview at bottom
  126. m_ListView.addFooterView(mFooter);// add footer in listview
  127. m_oAdapter = new CDealAppListingAdapter(getActivity(), s_oDataset);// create adapter object and add arraylist to adapter
  128. m_ListView.setAdapter(m_oAdapter);//adding adapter to recyclerview
  129. } else {
  130. mFooter.setVisibility(View.GONE);// else Load buttonvisibility set to Gone
  131. }
  132. m_ListView.setOnScrollListener(new AbsListView.OnScrollListener() {
  133. @Override
  134. public void onScrollStateChanged(AbsListView view, int scrollState) {
  135.  
  136.  
  137. }
  138.  
  139. @Override
  140. public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
  141. if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount != 0) {
  142. if (!isLoading) {
  143. isLoading = true;
  144. if (NetworkUtil.isConnected(getActivity())) {
  145. m_n_DefaultRecordCount = 5;// increment of record count by 5 on next load data
  146. m_n_DeafalutLastCount = m_n_DeafalutLastCount + 5;// same here.....as above
  147. String itemscount = String.valueOf(m_ListView.getAdapter().getCount());
  148. System.out.println("Toatal item:-" + itemscount);
  149.  
  150. sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// convert int value to string
  151. sz_LastCount = String.valueOf(m_n_DeafalutLastCount);// convert int value to string /////
  152. loadmoreData();
  153. } else {
  154. Toast.makeText(getActivity(), "Please check internet connection !", Toast.LENGTH_LONG).show();
  155. }
  156. }
  157. } else {
  158. isLoading = false;
  159. }
  160. }
  161. });
  162.  
  163. if (response.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {//server based conditions
  164. CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Connection Lost !", getActivity());
  165. } else if (response.getString("resultdescription").equalsIgnoreCase("Deal List Not Found")) {// serevr based conditions .....
  166. CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "No more deals available", getActivity());
  167. mFooter.setVisibility(View.GONE);
  168. } else if (response.getString("resultdescription").equalsIgnoreCase("Technical Failure")) {
  169. CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Technical Failure", getActivity());
  170. }
  171.  
  172. } catch (JSONException e) {
  173. e.printStackTrace();
  174. }
  175. }
  176. }, new Response.ErrorListener() {
  177. @Override
  178. public void onErrorResponse(VolleyError error) {
  179. System.out.println("Errror:-" + error);
  180. m_Dialog.dismiss();
  181. if (error instanceof TimeoutError) {
  182. CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.activity_main_swipe_refresh_layout), "Connection lost ! Please try again", getActivity());
  183. } else if (error instanceof NetworkError) {
  184. CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.activity_main_swipe_refresh_layout), "No internet connection", getActivity());
  185. }
  186.  
  187. }
  188. });
  189. RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
  190. requestQueue.add(jsonObjectRequest);
  191. } catch (JSONException e) {
  192. e.printStackTrace();
  193. }
  194.  
  195. }
  196.  
  197. /*This method send request to server for more deals*/
  198. //this method post request to server to fetch more deal
  199. public void loadmoreData() {
  200.  
  201. try {
  202. String json;
  203. // 3. build jsonObject
  204. JSONObject jsonObject = new JSONObject();// making object of Jsons.
  205. jsonObject.put("agentCode", m_szMobileNumber);// put mobile number
  206. jsonObject.put("pin", m_szEncryptedPassword);// put password
  207. jsonObject.put("recordcount", sz_RecordCount);// put record count
  208. jsonObject.put("lastcountvalue", sz_LastCount);// put last count
  209. // 4. convert JSONObject to JSON to String
  210. json = jsonObject.toString();// convert Json object to string
  211.  
  212. System.out.println("Server Request:-" + json);
  213. JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, CServerAPI.m_DealListingURL, jsonObject, new Response.Listener<JSONObject>() {
  214. @Override
  215. public void onResponse(JSONObject response) {
  216. System.out.println("Response:-" + response);
  217. try {
  218. JSONArray posts = response.optJSONArray("dealList");// GETTING DEAL LIST
  219. for (int i = 0; i < posts.length(); i++) {
  220. JSONObject post = posts.getJSONObject(i);// GETTING DEAL AT POSITION AT I
  221. item = new CDealAppDatastorage();// object create of DealAppdatastorage
  222. item.setM_szHeaderText(post.getString("dealname"));//getting deal name
  223. item.setM_szsubHeaderText(post.getString("dealcode"));// getting deal code
  224. item.setM_szDealValue(post.getString("dealvalue"));
  225.  
  226. if (!s_oDataset.contains(item)) {
  227. s_oDataset.add(item);
  228. isLoading = false;
  229. m_oAdapter.notifyDataSetChanged();
  230. }
  231. }
  232. if (response.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {//server based conditions
  233. CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Connection Lost !", getActivity());
  234. } else if (response.getString("resultdescription").equalsIgnoreCase("Deal List Not Found")) {// serevr based conditions .....
  235. CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "No more deals available", getActivity());
  236. mFooter.setVisibility(View.GONE);
  237. } else if (response.getString("resultdescription").equalsIgnoreCase("Technical Failure")) {
  238. CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Technical Failure", getActivity());
  239. }
  240. } catch (JSONException e) {
  241. e.printStackTrace();
  242. }
  243.  
  244. }
  245. }, new Response.ErrorListener() {
  246. @Override
  247. public void onErrorResponse(VolleyError error) {
  248. System.out.println("Error:-" + error);
  249. if (error instanceof TimeoutError) {
  250. CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.activity_main_swipe_refresh_layout), "Connection lost ! Please try again", getActivity());
  251. } else if (error instanceof NetworkError) {
  252. CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.activity_main_swipe_refresh_layout), "No internet connection", getActivity());
  253. }
  254. }
  255. });
  256. RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
  257. requestQueue.add(jsonObjectRequest);
  258. } catch (JSONException e) {
  259. e.printStackTrace();
  260. }
  261. }
  262.  
  263. /*This method invoke on swipe to refresh*/
  264. // this method invoke when swipe to refresh enable ....
  265. public void swipeData() {
  266. try {
  267. String json;
  268. // 3. build jsonObject
  269. JSONObject jsonObject = new JSONObject();// making object of Jsons.
  270. jsonObject.put("agentCode", m_szMobileNumber);// put mobile number
  271. jsonObject.put("pin", m_szEncryptedPassword);// put password
  272. jsonObject.put("recordcount", sz_RecordCount);// put record count
  273. jsonObject.put("lastcountvalue", sz_LastCount);// put last count
  274. // 4. convert JSONObject to JSON to String
  275. json = jsonObject.toString();// convert Json object to string
  276.  
  277. System.out.println("Server Request:-" + json);
  278. JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, CServerAPI.m_DealListingURL, jsonObject, new Response.Listener<JSONObject>() {
  279. @Override
  280. public void onResponse(JSONObject response) {
  281. System.out.println("Response:-" + response);
  282. mSwipeRefresh.setRefreshing(false);
  283. try {
  284. JSONArray posts = response.optJSONArray("dealList");// get Deal list in array from response
  285. for (int i = 0; i < posts.length(); i++) {// loop for counting deals from server
  286. JSONObject post = posts.getJSONObject(i);// counting deal based on index
  287. item = new CDealAppDatastorage();// creating object of DealAppdata storage
  288. item.setM_szHeaderText(post.getString("dealname"));// get deal name from response
  289. item.setM_szsubHeaderText(post.getString("dealcode"));// get dealcode from response
  290. item.setM_szDealValue(post.getString("dealvalue"));// get deal value from response
  291. item.setM_n_Image(m_n_FormImage[i]);//set Image Index wise(Dummy)
  292. s_oDataset.add(item);// add all items in ArrayList
  293.  
  294. }
  295. mFooter = View.inflate(getActivity(), R.layout.footer, null);
  296.  
  297.  
  298. if (!s_oDataset.isEmpty()) {// condition if data in arraylist is not empty
  299. // Adding Load More button to lisview at bottom
  300. m_ListView.addFooterView(mFooter);// add footer in listview
  301. m_oAdapter = new CDealAppListingAdapter(getActivity(), s_oDataset);// create adapter object and add arraylist to adapter
  302. m_ListView.setAdapter(m_oAdapter);//adding adapter to recyclerview
  303. } else {
  304. mFooter.setVisibility(View.GONE);// else Load buttonvisibility set to Gone
  305. }
  306. m_ListView.setOnScrollListener(new AbsListView.OnScrollListener() {
  307. @Override
  308. public void onScrollStateChanged(AbsListView view, int scrollState) {
  309.  
  310.  
  311. }
  312.  
  313. @Override
  314. public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
  315. if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount != 0) {
  316. if (!isLoading) {
  317. isLoading = true;
  318. if (NetworkUtil.isConnected(getActivity())) {
  319. m_n_DefaultRecordCount = 5;// increment of record count by 5 on next load data
  320. m_n_DeafalutLastCount = m_n_DeafalutLastCount + 5;// same here.....as above
  321. String itemscount = String.valueOf(m_ListView.getAdapter().getCount());
  322. System.out.println("Toatal item:-" + itemscount);
  323.  
  324. sz_RecordCount = String.valueOf(m_n_DefaultRecordCount);// convert int value to string
  325. sz_LastCount = String.valueOf(m_n_DeafalutLastCount);// convert int value to string /////
  326. loadmoreData();// this method load more deals from server ....
  327. } else {
  328. Toast.makeText(getActivity(), "Please check internet connection !", Toast.LENGTH_LONG).show();
  329. }
  330.  
  331. }
  332. }
  333. }
  334. });
  335.  
  336. if (response.getString("resultdescription").equalsIgnoreCase("Connection Not Available")) {//server based conditions
  337. CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Connection Lost !", getActivity());
  338. } else if (response.getString("resultdescription").equalsIgnoreCase("Deal List Not Found")) {// serevr based conditions .....
  339. CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "No more deals available", getActivity());
  340.  
  341. } else if (response.getString("resultdescription").equalsIgnoreCase("Technical Failure")) {
  342. CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.mainLayout), "Technical Failure", getActivity());
  343. }
  344. } catch (JSONException e) {
  345. e.printStackTrace();
  346. }
  347.  
  348. }
  349. }, new Response.ErrorListener() {
  350. @Override
  351. public void onErrorResponse(VolleyError error) {
  352. System.out.println("Error:-" + error);
  353. mSwipeRefresh.setRefreshing(false);
  354. if (error instanceof TimeoutError) {
  355. CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.activity_main_swipe_refresh_layout), "Connection lost ! Please try again", getActivity());
  356. } else if (error instanceof NetworkError) {
  357. CSnackBar.getInstance().showSnackBarError(m_Main.findViewById(R.id.activity_main_swipe_refresh_layout), "No internet connection", getActivity());
  358. }
  359. }
  360. });
  361. RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
  362. requestQueue.add(jsonObjectRequest);
  363.  
  364. } catch (JSONException e) {
  365. e.printStackTrace();
  366. }
  367. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement