PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/src/main/java/com/fyp/atom/linc/Main/Chats/ChatsFragment.java

https://gitlab.com/jaadchacra/Linc
Java | 251 lines | 202 code | 40 blank | 9 comment | 14 complexity | f330df5eca678cb337bb37ddc87e926d MD5 | raw file
  1. package com.fyp.atom.linc.Main.Chats;
  2. import android.os.Bundle;
  3. import android.support.v4.app.Fragment;
  4. import android.support.v7.widget.LinearLayoutManager;
  5. import android.support.v7.widget.RecyclerView;
  6. import android.util.Log;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.Button;
  11. import android.widget.ProgressBar;
  12. import android.widget.TextView;
  13. import com.android.volley.NetworkError;
  14. import com.android.volley.NoConnectionError;
  15. import com.android.volley.Request;
  16. import com.android.volley.Response;
  17. import com.android.volley.TimeoutError;
  18. import com.android.volley.VolleyError;
  19. import com.android.volley.toolbox.StringRequest;
  20. import com.fyp.atom.linc.Main.Sessions.SessionsAdapter;
  21. import com.fyp.atom.linc.Models.Approval;
  22. import com.fyp.atom.linc.Models.Offer;
  23. import com.fyp.atom.linc.Models.Session;
  24. import com.fyp.atom.linc.Models.User;
  25. import com.fyp.atom.linc.R;
  26. import com.fyp.atom.linc.Utils.MySingleton;
  27. import com.fyp.atom.linc.Utils.SQLiteHandler;
  28. import org.json.JSONArray;
  29. import org.json.JSONException;
  30. import org.json.JSONObject;
  31. import java.io.UnsupportedEncodingException;
  32. import java.net.URLEncoder;
  33. import java.text.ParseException;
  34. import java.text.SimpleDateFormat;
  35. import java.util.ArrayList;
  36. import java.util.Calendar;
  37. import java.util.Date;
  38. import java.util.HashMap;
  39. import java.util.List;
  40. public class ChatsFragment extends Fragment {
  41. private SQLiteHandler db;
  42. String userID;
  43. View view;
  44. Bundle mSavedInstanceState;
  45. ProgressBar progressBarCenter;
  46. TextView errorMessage;
  47. Button retryButton;
  48. private RecyclerView recyclerView;
  49. private RecyclerView.Adapter chatsAdapter;
  50. private List<Approval> approvalsList;
  51. @Override
  52. public void setUserVisibleHint(boolean isVisibleToUser) {
  53. super.setUserVisibleHint(isVisibleToUser);
  54. // Make sure that we are currently visible
  55. if (this.isVisible()) {
  56. // If we are becoming invisible, then...
  57. if (isVisibleToUser) {
  58. }
  59. }
  60. }
  61. @Override
  62. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  63. Bundle savedInstanceState) {
  64. // Inflate the layout for this fragment
  65. view = inflater.inflate(R.layout.fragment_chats, container, false);
  66. //This is basically never used, it's just to use getData from onScrolled function
  67. mSavedInstanceState = savedInstanceState;
  68. init();
  69. try {
  70. getChats(mSavedInstanceState);
  71. } catch (UnsupportedEncodingException e) {
  72. e.printStackTrace();
  73. }
  74. return view;
  75. }
  76. private void init() {
  77. db = new SQLiteHandler(getActivity().getApplicationContext());
  78. HashMap<String, String> user = db.getUserDetails();
  79. userID = user.get("uid");
  80. progressBarCenter = (ProgressBar) view.findViewById(R.id.progressBarCenter);
  81. progressBarCenter.setVisibility(View.VISIBLE);
  82. errorMessage = (TextView) view.findViewById(R.id.studentFeedErrorMessage);
  83. retryButton = (Button) view.findViewById(R.id.studentFeedRetryButton);
  84. errorMessage.setVisibility(View.GONE);
  85. retryButton.setVisibility(View.GONE);
  86. recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
  87. recyclerView.setHasFixedSize(true);
  88. LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
  89. linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
  90. recyclerView.setLayoutManager(linearLayoutManager);
  91. approvalsList = new ArrayList<>();
  92. }
  93. @Override
  94. public void onSaveInstanceState(Bundle outState) {
  95. super.onSaveInstanceState(outState);
  96. }
  97. private String dateStrNow() {
  98. Calendar now = Calendar.getInstance();
  99. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  100. String nowStr = sdf.format(now.getTime());
  101. return nowStr;
  102. }
  103. private void getChats(final Bundle savedInstanceState) throws UnsupportedEncodingException {
  104. MySingleton.getInstance(getActivity()).addToRequestQueue(getChatsFromServer(savedInstanceState));
  105. }
  106. private StringRequest getChatsFromServer(final Bundle savedInstanceState) throws UnsupportedEncodingException {
  107. //Displaying Progressbar
  108. progressBarCenter.setVisibility(View.VISIBLE);
  109. String myDate = URLEncoder.encode(dateStrNow(), "utf-8");;
  110. //JsonArrayRequest of volley
  111. StringRequest postRequest = new StringRequest(Request.Method.GET, " http://kcapplications.com/Linc/chat.php?user_id="+userID+"&date="+myDate,
  112. new Response.Listener<String>() {
  113. @Override
  114. public void onResponse(String response) {
  115. progressBarCenter.setVisibility(View.GONE);
  116. Log.d("sessions", response.toString());
  117. if (response != null) {
  118. parseData(response);
  119. } else {
  120. progressBarCenter.setVisibility(View.VISIBLE);
  121. try {
  122. getChats(savedInstanceState);
  123. } catch (UnsupportedEncodingException e) {
  124. e.printStackTrace();
  125. }
  126. }
  127. }
  128. },
  129. new Response.ErrorListener() {
  130. @Override
  131. public void onErrorResponse(VolleyError error) {
  132. progressBarCenter.setVisibility(View.GONE);
  133. errorMessage.setVisibility(View.VISIBLE);
  134. retryButton.setVisibility(View.VISIBLE);
  135. Log.d("sessions", "in error");
  136. if (error instanceof NoConnectionError) {
  137. errorMessage.setText("Connection error\nPlease check your internet connection");
  138. } else if (error instanceof NetworkError) {
  139. errorMessage.setText("Connection error\nPlease check your internet connection");
  140. } else if (error instanceof TimeoutError) {
  141. errorMessage.setText("Connection error\nFailed to connect to server");
  142. } else {
  143. errorMessage.setText("404 Not Found");
  144. }
  145. retryButton.setOnClickListener(new View.OnClickListener() {
  146. @Override
  147. public void onClick(View v) {
  148. progressBarCenter.setVisibility(View.VISIBLE);
  149. errorMessage.setVisibility(View.GONE);
  150. retryButton.setVisibility(View.GONE);
  151. try {
  152. getChats(savedInstanceState);
  153. } catch (UnsupportedEncodingException e) {
  154. e.printStackTrace();
  155. }
  156. }
  157. });
  158. }
  159. });
  160. return postRequest;
  161. }
  162. //This method will parse json data
  163. private void parseData(String response) {
  164. try {
  165. JSONArray jsonArray = new JSONArray(response);
  166. for (int i = 0; i < jsonArray.length(); i++) {
  167. Approval approval = new Approval();
  168. User user = new User();
  169. JSONObject approvalJsonObject = null;
  170. try {
  171. //Getting json
  172. approvalJsonObject = jsonArray.getJSONObject(i);
  173. user.setUserID(Integer.parseInt(approvalJsonObject.getString("userID")));
  174. user.setUserFirstName(approvalJsonObject.getString("userFirstName"));
  175. user.setUserLastName(approvalJsonObject.getString("userLastName"));
  176. user.setUserEmail(approvalJsonObject.getString("userEmail"));
  177. approval.setUser(user);
  178. approval.setDateStr(getDateStr(approvalJsonObject.getString("offerStartDate")));
  179. } catch (JSONException e) {
  180. e.printStackTrace();
  181. } catch (ParseException e) {
  182. e.printStackTrace();
  183. }
  184. approvalsList.add(0, approval);
  185. }
  186. // //Notifying the adapter that data has been added or changed
  187. chatsAdapter = new ChatsAdapter(approvalsList, getActivity());
  188. recyclerView.setAdapter(chatsAdapter);
  189. } catch (JSONException e) {
  190. e.printStackTrace();
  191. }
  192. }
  193. public String getDateStr(String dateStr) throws ParseException {
  194. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  195. Date startDate = sdf.parse(dateStr);
  196. Calendar cal = Calendar.getInstance();
  197. cal.setTime(startDate);
  198. SimpleDateFormat formatter = new SimpleDateFormat("EEE");
  199. String dayOfWeekStr = formatter.format(cal.getTime());
  200. int startHour = cal.get(Calendar.HOUR_OF_DAY); // gets hour in 24h format
  201. String startHourStr = String.format("%02d", startHour);
  202. int startMinute = cal.get(Calendar.MINUTE);
  203. String startMinuteStr = String.format("%02d", startMinute);
  204. int endHour = startHour + 1;
  205. if(endHour == 25)
  206. endHour = 0;
  207. String endHourStr = String.format("%02d", endHour);
  208. String offerDate = dayOfWeekStr+" from "+startHourStr+":"+startMinuteStr+" till "+endHourStr+":"+startMinuteStr;
  209. return offerDate;
  210. }
  211. }