ppamorim

Untitled

May 7th, 2014
263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 14.48 KB | None | 0 0
  1. package com.fatos.fatos_desconhecidos.app.fragment;
  2.  
  3. import android.app.FragmentManager;
  4. import android.os.Bundle;
  5. import android.support.v4.app.Fragment;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.view.animation.Animation;
  10. import android.view.animation.AnimationUtils;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.ImageView;
  14. import android.widget.ListView;
  15. import android.widget.ProgressBar;
  16. import android.widget.RelativeLayout;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19.  
  20. import com.fatos.fatos_desconhecidos.app.R;
  21. import com.fatos.fatos_desconhecidos.app.adapter.CommentsAdapter;
  22. import com.fatos.fatos_desconhecidos.app.adapter.GenericAdapter;
  23. import com.fatos.fatos_desconhecidos.app.core.AdapterComm;
  24. import com.fatos.fatos_desconhecidos.app.core.model.impl.Comment;
  25. import com.fatos.fatos_desconhecidos.app.fragment.dialog.LoaderDialog;
  26. import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
  27. import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingRightInAnimationAdapter;
  28. import com.parse.FindCallback;
  29. import com.parse.GetCallback;
  30. import com.parse.ParseException;
  31. import com.parse.ParseObject;
  32. import com.parse.ParseQuery;
  33. import com.parse.ParseUser;
  34. import com.parse.SaveCallback;
  35.  
  36. import java.text.Format;
  37. import java.text.SimpleDateFormat;
  38. import java.util.ArrayList;
  39. import java.util.List;
  40.  
  41. /**
  42.  * Created by pedro on 04/05/14.
  43.  */
  44. public class CommentsFragment extends Fragment implements AdapterComm {
  45.  
  46.     private String postID;
  47.     private List<Comment> Comments;
  48.     private CommentsAdapter adapter;
  49.     private ListView mListViewComments;
  50.     private EditText mMessage;
  51.     private TextView mTextError;
  52.     private Button mButton;
  53.     private Button mRefresh;
  54.     private RelativeLayout mError;
  55.     private ProgressBar mProgressBar;
  56.     private SwingBottomInAnimationAdapter swingBottomInAnimationAdapter;
  57.  
  58.     private Animation slideOffButton;
  59.     private Animation slideOnButton;
  60.     private Animation slideOnProgressBar;
  61.     private Animation slideOffProgressBar;
  62.  
  63.     Bundle extras;
  64.  
  65.     @Override
  66.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  67.  
  68.         View view = inflater.inflate(R.layout.fragment_comment, container, false);
  69.         mListViewComments = (ListView) view.findViewById(R.id.comments_list);
  70.         mMessage = (EditText) view.findViewById(R.id.message_content);
  71.         mButton = (Button) view.findViewById(R.id.send_message);
  72.         mRefresh = (Button) view.findViewById(R.id.comment_buttton_refresh);
  73.         mProgressBar = (ProgressBar) view.findViewById(R.id.comment_loading);
  74.         mError = (RelativeLayout) view.findViewById(R.id.comment_list_error);
  75.         mTextError = (TextView) view.findViewById(R.id.comment_textError);
  76.  
  77.         return view;
  78.     }
  79.  
  80.     @Override
  81.     public void onViewCreated(View view, Bundle savedInstanceState) {
  82.         super.onViewCreated(view, savedInstanceState);
  83.         try {
  84.             extras = getArguments();
  85.             postID = extras.getString("postID");
  86.             Comments = new ArrayList<Comment>();
  87.  
  88.             slideOnButton = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_on);
  89.             slideOffButton = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_exit);
  90.             slideOnProgressBar = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_on);
  91.             slideOffProgressBar = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_exit);
  92.  
  93.             getComments();
  94.  
  95.         } catch (Exception e) {
  96.  
  97.         }
  98.  
  99.         mButton.setOnClickListener(onClickButtonSend);
  100.  
  101.     }
  102.  
  103.     private View.OnClickListener onClickRefresh = new View.OnClickListener() {
  104.         @Override
  105.         public void onClick(View view) {
  106.             showListView();
  107.             getComments();
  108.         }
  109.     };
  110.  
  111.     private View.OnClickListener onClickButtonSend = new View.OnClickListener() {
  112.         @Override
  113.         public void onClick(View view) {
  114.             try {
  115.                 if (mMessage.getText().toString().length()>0) {
  116.                     //mMessage.setEnabled(false);
  117.                     mButton.startAnimation(slideOffButton);
  118.                     slideOffButton.setAnimationListener(new Animation.AnimationListener() {
  119.                     @Override
  120.                     public void onAnimationStart(Animation animation) { }
  121.  
  122.                     @Override
  123.                     public void onAnimationRepeat(Animation animation) { }
  124.  
  125.                     @Override
  126.                     public void onAnimationEnd(Animation animation) {
  127.                         mButton.setVisibility(View.INVISIBLE);
  128.                         mProgressBar.setVisibility(View.VISIBLE);
  129.                         mProgressBar.startAnimation(slideOnProgressBar);
  130.                     }
  131.                 });
  132.  
  133.                 ParseObject comments = new ParseObject("Comments");
  134.                 ParseObject post = ParseObject.createWithoutData("FeedPost", postID);
  135.                 ParseObject user = ParseObject.createWithoutData("_User", ParseUser.getCurrentUser().getObjectId());
  136.                 comments.put("Username", ParseUser.getCurrentUser().getString("name"));
  137.                 comments.put("Content", mMessage.getText().toString());
  138.                 comments.put("enable", true);
  139.                 comments.put("User", user);
  140.                 comments.put( "Post", post);
  141.                 mMessage.setText("");
  142.                 comments.saveInBackground(new SaveCallback() {
  143.                     @Override
  144.                     public void done(ParseException e) {
  145.                         mProgressBar.startAnimation(slideOffProgressBar);
  146.                         slideOffProgressBar.setAnimationListener(new Animation.AnimationListener() {
  147.                             @Override
  148.                             public void onAnimationStart(Animation animation) {
  149.                             }
  150.  
  151.                             @Override
  152.                             public void onAnimationRepeat(Animation animation) {
  153.                             }
  154.  
  155.                             @Override
  156.                             public void onAnimationEnd(Animation animation) {
  157.                                 mProgressBar.setVisibility(View.INVISIBLE);
  158.                                 mButton.startAnimation(slideOnButton);
  159.                                 mButton.setVisibility(View.VISIBLE);
  160.                             }
  161.                         });
  162.                         ParseQuery<ParseObject> query = ParseQuery.getQuery("Comments");
  163.                         ParseObject feedPost = ParseObject.createWithoutData("FeedPost", postID);
  164.                         query.whereEqualTo("Post", feedPost);
  165.                         query.whereEqualTo("enable", true);
  166.                         query.orderByAscending("createdAt");
  167.                         query.findInBackground(new FindCallback<ParseObject>() {
  168.                             public void done(List<ParseObject> commentList, ParseException e) {
  169.                                 Format formatter = new SimpleDateFormat("dd/MM/yyyy");
  170.                                 Comments.clear();
  171.                                 for (int i = 0; i < commentList.size(); i++) {
  172.                                     Comments.add(new Comment(commentList.get(i).getObjectId(),
  173.                                             commentList.get(i).getString("Username"),
  174.                                             commentList.get(i).getString("Content"),
  175.                                             formatter.format(commentList.get(i).getCreatedAt())));
  176.                                 }
  177.                                 if(mListViewComments.getAdapter() == null) {
  178.                                     adapter = new CommentsAdapter(getActivity(),CommentsFragment.this, Comments);
  179.                                     swingBottomInAnimationAdapter= new SwingBottomInAnimationAdapter(adapter);
  180.                                     swingBottomInAnimationAdapter.setAbsListView(mListViewComments);
  181.                                     mListViewComments.setAdapter(swingBottomInAnimationAdapter);
  182.                                 } else {
  183.                                 swingBottomInAnimationAdapter.notifyDataSetChanged();
  184.                                 mListViewComments.setAdapter(swingBottomInAnimationAdapter);
  185.                                 }
  186.                                 showListView();
  187.                             }
  188.                         });
  189.                     }
  190.                 });
  191.                 } else {
  192.  
  193.                 }
  194.             } catch (Exception e) {
  195.                 e.printStackTrace();
  196.             }
  197.         }
  198.     };
  199.  
  200.     private void getComments() {
  201.  
  202.         try {
  203.             showListView();
  204.  
  205.             ParseQuery<ParseObject> query = ParseQuery.getQuery("Comments");
  206.             ParseObject feedPost = ParseObject.createWithoutData("FeedPost", postID);
  207.             query.whereEqualTo("Post", feedPost);
  208.             query.whereEqualTo("enable", true);
  209.             query.orderByAscending("createdAt");
  210.             query.findInBackground(new FindCallback<ParseObject>() {
  211.                 public void done(List<ParseObject> commentList, ParseException e) {
  212.                     try {
  213.                     Format formatter = new SimpleDateFormat("dd-MM-yyyy");
  214.                     Comments.clear();
  215.                         if(commentList.size() <= 0) {
  216.                             mTextError.setText(getString(R.string.none_comment));
  217.                             showError(true);
  218.                         } else {
  219.                             for (int i = 0; i < commentList.size(); i++) {
  220.                                 Comments.add(new Comment(commentList.get(i).getObjectId(),
  221.                                         commentList.get(i).getString("Username"),
  222.                                         commentList.get(i).getString("Content"),
  223.                                         formatter.format(commentList.get(i).getCreatedAt())));
  224.                             }
  225.                         adapter = new CommentsAdapter(getActivity(),CommentsFragment.this, Comments);
  226.                         swingBottomInAnimationAdapter= new SwingBottomInAnimationAdapter(adapter);
  227.                         swingBottomInAnimationAdapter.setAbsListView(mListViewComments);
  228.                         mListViewComments.setAdapter(swingBottomInAnimationAdapter);
  229.                         }
  230.                     } catch (Exception er) {
  231.                             showError(false);
  232.                     }
  233.                 }
  234.             });
  235.         } catch (Exception e) {
  236.             showError(false);
  237.         }
  238.     }
  239.  
  240.     @Override
  241.     public void deleteComment(String ID) {
  242.  
  243.         try {
  244.             showLoadDialog();
  245.             ParseQuery<ParseObject> query = ParseQuery.getQuery("Comments");
  246.             query.getInBackground(ID, new GetCallback<ParseObject>() {
  247.                 public void done(ParseObject comments, ParseException e) {
  248.                     if (e == null) {
  249.                         comments.put("enable", false);
  250.                         comments.saveInBackground(new SaveCallback() {
  251.                             @Override
  252.                             public void done(ParseException e) {
  253.                                 ParseQuery<ParseObject> query = ParseQuery.getQuery("Comments");
  254.                                 ParseObject feedPost = ParseObject.createWithoutData("FeedPost", postID);
  255.                                 query.whereEqualTo("Post", feedPost);
  256.                                 query.whereEqualTo("enable", true);
  257.                                 query.orderByAscending("createdAt");
  258.                                 query.findInBackground(new FindCallback<ParseObject>() {
  259.                                     public void done(List<ParseObject> commentList, ParseException e) {
  260.                                         Format formatter = new SimpleDateFormat("dd/MM/yyyy");
  261.                                         Comments.clear();
  262.                                         for (int i = 0; i < commentList.size(); i++) {
  263.                                             Comments.add(new Comment(commentList.get(i).getObjectId(),
  264.                                                     commentList.get(i).getString("Username"),
  265.                                                     commentList.get(i).getString("Content"),
  266.                                                     formatter.format(commentList.get(i).getCreatedAt())));
  267.                                         }
  268.                                         swingBottomInAnimationAdapter.notifyDataSetChanged();
  269.                                         mListViewComments.setAdapter(swingBottomInAnimationAdapter);
  270.  
  271.                                         dismissLoad();
  272.                                         Toast.makeText(getActivity(), getString(R.string.erased_comment), Toast.LENGTH_SHORT).show();
  273.                                     }
  274.                                 });
  275.                             }
  276.                         });
  277.                     }
  278.                 }
  279.             });
  280.         } catch (Exception e) {
  281.             mListViewComments.setVisibility(View.GONE);
  282.             mError.setVisibility(View.VISIBLE);
  283.         }
  284.     }
  285.  
  286.     private void showError(boolean isNull) {
  287.         if(isNull) {
  288.             mTextError.setText(getString(R.string.none_comment));
  289.             mRefresh.setVisibility(View.GONE);
  290.         } else {
  291.             mTextError.setText(getString(R.string.generic_error));
  292.             mRefresh.setVisibility(View.VISIBLE);
  293.         }
  294.  
  295.         mListViewComments.setVisibility(View.GONE);
  296.         mError.setVisibility(View.VISIBLE);
  297.     }
  298.  
  299.     private void showListView() {
  300.         mListViewComments.setVisibility(View.VISIBLE);
  301.         mError.setVisibility(View.GONE);
  302.     }
  303.  
  304.     private void showLoadDialog() {
  305.         LoaderDialog d = (LoaderDialog) getFragmentManager().findFragmentByTag(LoaderDialog.TAG);
  306.         if (d == null) {
  307.             LoaderDialog.Builder builder = new LoaderDialog.Builder();
  308.             builder.setCancelable(false);
  309.             d = builder.build();
  310.             d.show(getFragmentManager(), LoaderDialog.TAG);
  311.         }
  312.     }
  313.  
  314.     protected void dismissLoad() {
  315.         LoaderDialog d = (LoaderDialog) getFragmentManager().findFragmentByTag(
  316.                 LoaderDialog.TAG);
  317.         if (d != null && d.isVisible())
  318.             d.dismiss();
  319.     }
  320.  
  321.  
  322. }
Advertisement
Add Comment
Please, Sign In to add comment