PageRenderTime 75ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/src/GJ/helpinghand/My_Folder_Fragment.java

https://gitlab.com/gj100596/HelpingHand
Java | 382 lines | 305 code | 54 blank | 23 comment | 14 complexity | 553bcaba5ac3d2f3b3eb239187a12368 MD5 | raw file
  1. package GJ.helpinghand;
  2. import java.util.ArrayList;
  3. import org.apache.http.NameValuePair;
  4. import org.apache.http.message.BasicNameValuePair;
  5. import org.json.JSONArray;
  6. import GJ.helpinghand.My_Fragment_Activity.get_content;
  7. import android.app.AlertDialog;
  8. import android.app.ProgressDialog;
  9. import android.content.Context;
  10. import android.content.DialogInterface;
  11. import android.content.DialogInterface.OnClickListener;
  12. import android.content.Intent;
  13. import android.content.SharedPreferences;
  14. import android.os.AsyncTask;
  15. import android.os.Bundle;
  16. import android.support.design.widget.FloatingActionButton;
  17. import android.support.design.widget.Snackbar;
  18. import android.support.v4.app.Fragment;
  19. import android.support.v4.widget.SwipeRefreshLayout;
  20. import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
  21. import android.util.Log;
  22. import android.view.LayoutInflater;
  23. import android.view.View;
  24. import android.view.ViewGroup;
  25. import android.widget.AdapterView;
  26. import android.widget.AbsListView.OnScrollListener;
  27. import android.widget.AdapterView.OnItemClickListener;
  28. import android.widget.AdapterView.OnItemLongClickListener;
  29. import android.widget.AbsListView;
  30. import android.widget.EditText;
  31. import android.widget.ListView;
  32. import android.widget.TextView;
  33. import android.widget.Toast;
  34. public class My_Folder_Fragment extends Fragment implements android.view.View.OnClickListener{
  35. private static final String ARG_SECTION_NUMBER = "section_number";
  36. private ListView content;
  37. private SharedPreferences data,directory;
  38. private ProgressDialog load;
  39. private View vw;
  40. private EditText name;
  41. public static TextView folder_name,selected_folder;
  42. private SwipeRefreshLayout refresh;
  43. public static Fragment newInstance(int sectionNumber) {
  44. My_Folder_Fragment fragment = new My_Folder_Fragment();
  45. Bundle args = new Bundle();
  46. args.putInt(ARG_SECTION_NUMBER, sectionNumber);
  47. fragment.setArguments(args);
  48. return fragment;
  49. }
  50. public My_Folder_Fragment() {
  51. }
  52. @Override
  53. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  54. Bundle savedInstanceState) {
  55. // TODO Auto-generated method stub
  56. vw = inflater.inflate(R.layout.my_folder_layout, null);
  57. content = (ListView) vw.findViewById(R.id.my_work);
  58. refresh = (SwipeRefreshLayout) vw.findViewById(R.id.refresh);
  59. refresh.setColorSchemeResources(R.color.material_blue_grey_800,R.color.my_primary);
  60. data = getActivity().getSharedPreferences(getString(R.string.user_detail),
  61. Context.MODE_PRIVATE);
  62. directory = getActivity().getSharedPreferences(getString(R.string.directory),
  63. Context.MODE_PRIVATE);
  64. new get_folder().execute(new Server_Interaction());
  65. content.setDividerHeight(1);
  66. //----------FAB--------
  67. FloatingActionButton fab = (FloatingActionButton) vw.findViewById(R.id.fab);
  68. fab.setOnClickListener(this);
  69. content.setOnItemClickListener(new OnItemClickListener() {
  70. @Override
  71. public void onItemClick(AdapterView<?> parent, View view,
  72. int position, long id) {
  73. // TODO Auto-generated method stub
  74. View selected = content.getChildAt(position-content.getFirstVisiblePosition());
  75. folder_name = (TextView) selected.findViewById(R.id.title);
  76. String fold = folder_name.getText().toString();
  77. //---Store Folder Name for next Activity-----
  78. SharedPreferences.Editor edit = directory.edit();
  79. edit.putString(getString(R.string.dir_fol), fold);
  80. String path = directory.getString(getString(R.string.dir_path), "main")
  81. + "/" + fold;
  82. edit.putString(getString(R.string.dir_path), path);
  83. edit.commit();
  84. //Start new Activity so that on pressing back it will come one step back!
  85. Intent new_win = new Intent(getActivity(),Home.class);
  86. startActivity(new_win);
  87. }
  88. });
  89. content.setOnScrollListener(new OnScrollListener() {
  90. @Override
  91. public void onScrollStateChanged(AbsListView view, int scrollState) {
  92. // TODO Auto-generated method stub
  93. }
  94. @Override
  95. public void onScroll(AbsListView view, int firstVisibleItem,
  96. int visibleItemCount, int totalItemCount) {
  97. // TODO Auto-generated method stub
  98. int topRowVerticalPosition =
  99. (content == null || content.getChildCount() == 0) ?
  100. 0 : content.getChildAt(0).getTop();
  101. refresh.setEnabled(firstVisibleItem == 0 && topRowVerticalPosition >= 0);
  102. }
  103. });
  104. content.setOnItemLongClickListener(new OnItemLongClickListener() {
  105. @Override
  106. public boolean onItemLongClick(AdapterView<?> parent,
  107. View view, int position, long id) {
  108. final View clicked = (View) content.getChildAt(position-content.getFirstVisiblePosition());
  109. String option_pro[] = new String[]{"Share",
  110. "Rename",
  111. "Remove",
  112. "Report Content"};
  113. AlertDialog.Builder options = new AlertDialog.Builder(getActivity());
  114. options.setItems(option_pro, new OnClickListener() {
  115. @Override
  116. public void onClick(DialogInterface dialog, int which) {
  117. // TODO Auto-generated method stub
  118. switch(which){
  119. case 0:
  120. selected_folder=(TextView) clicked.findViewById(R.id.title);
  121. Intent share = new Intent(getActivity(),Share.class);
  122. share.putExtra("mode", 1);
  123. startActivity(share);
  124. break;
  125. case 1:
  126. selected_folder=(TextView) clicked.findViewById(R.id.title);
  127. name = new EditText(getActivity());
  128. name.setHint(R.string.fold_alert);
  129. new AlertDialog.Builder(getActivity())
  130. .setView(name)
  131. .setPositiveButton("Save", new DialogInterface.OnClickListener() {
  132. public void onClick(DialogInterface dialog, int whichButton) {
  133. String fold = name.getText().toString();
  134. if(!fold.contains("/") || !fold.equals(""))
  135. new rename_folder().execute(new Server_Interaction());
  136. else
  137. Toast.makeText(getActivity(),
  138. "Folder name Can't Contain '/'", Toast.LENGTH_SHORT).show();
  139. }
  140. })
  141. .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  142. public void onClick(DialogInterface dialog, int whichButton) {
  143. }
  144. })
  145. .show();
  146. break;
  147. case 2:
  148. selected_folder=(TextView) clicked.findViewById(R.id.title);
  149. new AlertDialog.Builder(getActivity())
  150. .setMessage("Do you Want to Delete the Folder?")
  151. .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  152. public void onClick(DialogInterface dialog, int whichButton) {
  153. new delete_folder().execute(new Server_Interaction());
  154. }
  155. })
  156. .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  157. public void onClick(DialogInterface dialog, int whichButton) {
  158. }
  159. })
  160. .show();
  161. Toast.makeText(getActivity(), "Removed", Toast.LENGTH_SHORT).show();
  162. break;
  163. case 3:
  164. Toast.makeText(getActivity(), "The Object Will be Checked! Thank You", Toast.LENGTH_SHORT).show();
  165. break;
  166. default:
  167. Toast.makeText(getActivity(), "ERRRR", Toast.LENGTH_SHORT).show();
  168. break;
  169. }
  170. }
  171. });
  172. options.show();
  173. return true;
  174. }
  175. });
  176. refresh.setOnRefreshListener(new OnRefreshListener() {
  177. @Override
  178. public void onRefresh() {
  179. // TODO Auto-generated method stub
  180. new get_folder().execute(new Server_Interaction());
  181. }
  182. });
  183. return vw;
  184. }
  185. @Override
  186. public void onClick(View v) {
  187. // TODO Auto-generated method stub
  188. if(v.getId()==R.id.fab){
  189. name = new EditText(getActivity());
  190. name.setHint(R.string.fold_alert);
  191. new AlertDialog.Builder(getActivity())
  192. .setView(name)
  193. .setPositiveButton("Save", new DialogInterface.OnClickListener() {
  194. public void onClick(DialogInterface dialog, int whichButton) {
  195. String fold = name.getText().toString();
  196. if(!fold.contains("/") || !fold.equals(""))
  197. new new_folder().execute(new Server_Interaction());
  198. else
  199. Toast.makeText(getActivity(),
  200. "Folder name Can't Contain '/'", Toast.LENGTH_SHORT).show();
  201. }
  202. })
  203. .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  204. public void onClick(DialogInterface dialog, int whichButton) {
  205. }
  206. })
  207. .show();
  208. }
  209. }
  210. class new_folder extends AsyncTask<Server_Interaction, String, String>{
  211. ProgressDialog wait;
  212. @Override
  213. protected void onPreExecute() {
  214. // TODO Auto-generated method stub
  215. super.onPreExecute();
  216. wait=new ProgressDialog(getActivity());
  217. wait.setTitle("Creating New Folder");
  218. wait.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  219. wait.setMessage("Setting Things Up...");
  220. }
  221. @Override
  222. protected String doInBackground(Server_Interaction... params) {
  223. // TODO Auto-generated method stub
  224. ArrayList<NameValuePair> arg = new ArrayList<NameValuePair>();
  225. arg.add(new BasicNameValuePair("u_id", data.getString(getString(R.string.shared_user_id),"")));
  226. arg.add(new BasicNameValuePair("path",
  227. directory.getString(getString(R.string.dir_path), "main")));
  228. arg.add(new BasicNameValuePair("folder_name",name.getText().toString()));
  229. return params[0].new_folder(arg);
  230. }
  231. @Override
  232. protected void onPostExecute(String result) {
  233. // TODO Auto-generated method stub
  234. wait.dismiss();
  235. if(result.equalsIgnoreCase("True")){
  236. //---Store Folder Name for next Activity-----
  237. SharedPreferences.Editor edit = directory.edit();
  238. edit.putString(getString(R.string.dir_fol), name.getText().toString());
  239. String path = directory.getString(getString(R.string.dir_path), "main")
  240. + "/" + name.getText().toString();
  241. edit.putString(getString(R.string.dir_path), path);
  242. edit.commit();
  243. //Start new Activity so that on pressing back it will come one step back!
  244. Intent new_win = new Intent(getActivity(),Home.class);
  245. startActivity(new_win);
  246. }
  247. else
  248. Toast.makeText(getActivity(), "Error Occured. Try Latter!", Toast.LENGTH_SHORT).show();
  249. }
  250. }
  251. class rename_folder extends AsyncTask<Server_Interaction, String, String>{
  252. @Override
  253. protected String doInBackground(Server_Interaction... params) {
  254. // TODO Auto-generated method stub
  255. ArrayList<NameValuePair> arg = new ArrayList<NameValuePair>();
  256. arg.add(new BasicNameValuePair("u_id", data.getString(getString(R.string.shared_user_id),"")));
  257. arg.add(new BasicNameValuePair("path",
  258. directory.getString(getString(R.string.dir_path), "main")));
  259. arg.add(new BasicNameValuePair("folder_name",selected_folder.getText().toString()));
  260. arg.add(new BasicNameValuePair("rename",name.getText().toString()));
  261. return params[0].new_folder(arg);
  262. }
  263. @Override
  264. protected void onPostExecute(String result) {
  265. // TODO Auto-generated method stub
  266. if(result.equalsIgnoreCase("True")){
  267. refresh.setRefreshing(true);
  268. new get_folder().execute(new Server_Interaction());
  269. }
  270. }
  271. }
  272. class delete_folder extends AsyncTask<Server_Interaction, String, String>{
  273. @Override
  274. protected String doInBackground(Server_Interaction... params) {
  275. // TODO Auto-generated method stub
  276. ArrayList<NameValuePair> arg = new ArrayList<NameValuePair>();
  277. arg.add(new BasicNameValuePair("u_id", data.getString(getString(R.string.shared_user_id),"")));
  278. arg.add(new BasicNameValuePair("path", directory.getString(getString(R.string.dir_path), "main")));
  279. arg.add(new BasicNameValuePair("folder_name",selected_folder.getText().toString()));
  280. arg.add(new BasicNameValuePair("remove","1"));
  281. return params[0].new_folder(arg);
  282. }
  283. @Override
  284. protected void onPostExecute(String result) {
  285. // TODO Auto-generated method stub
  286. refresh.setRefreshing(true);
  287. new get_folder().execute(new Server_Interaction());
  288. }
  289. }
  290. class get_folder extends AsyncTask<Server_Interaction, JSONArray, JSONArray>{
  291. @Override
  292. protected void onPreExecute() {
  293. // TODO Auto-generated method stub
  294. super.onPreExecute();
  295. if(!refresh.isRefreshing()){
  296. load = new ProgressDialog(getActivity());
  297. load.setProgressStyle(ProgressDialog.STYLE_SPINNER);
  298. load.setTitle("Loading");
  299. load.setMessage("Fetching Your Data...");
  300. load.show();
  301. }
  302. }
  303. @Override
  304. protected JSONArray doInBackground(Server_Interaction... params) {
  305. // TODO Auto-generated method stub
  306. ArrayList<NameValuePair> arg = new ArrayList<NameValuePair>();
  307. arg.add(new BasicNameValuePair("u_id", data.getString(getString(R.string.shared_user_id),"")));
  308. arg.add(new BasicNameValuePair("folder",
  309. directory.getString(getString(R.string.dir_path), "main")));
  310. return params[0].my_folder(arg);
  311. }
  312. @Override
  313. protected void onPostExecute(JSONArray result) {
  314. // TODO Auto-generated method stub
  315. load.dismiss();
  316. refresh.setRefreshing(false);
  317. Custom_content_list adap;
  318. if(result!=null){
  319. adap = new Custom_content_list(getActivity(),result,0);
  320. content.setAdapter(adap);
  321. }
  322. else{
  323. //adap = new Custom_content_list(getActivity(),result,res);
  324. }
  325. super.onPostExecute(result);
  326. }
  327. }
  328. }