Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.fatos.fatos_desconhecidos.app.fragment;
- import android.app.FragmentManager;
- import android.os.Bundle;
- import android.support.v4.app.Fragment;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.ProgressBar;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import android.widget.Toast;
- import com.fatos.fatos_desconhecidos.app.R;
- import com.fatos.fatos_desconhecidos.app.adapter.CommentsAdapter;
- import com.fatos.fatos_desconhecidos.app.adapter.GenericAdapter;
- import com.fatos.fatos_desconhecidos.app.core.AdapterComm;
- import com.fatos.fatos_desconhecidos.app.core.model.impl.Comment;
- import com.fatos.fatos_desconhecidos.app.fragment.dialog.LoaderDialog;
- import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
- import com.nhaarman.listviewanimations.swinginadapters.prepared.SwingRightInAnimationAdapter;
- import com.parse.FindCallback;
- import com.parse.GetCallback;
- import com.parse.ParseException;
- import com.parse.ParseObject;
- import com.parse.ParseQuery;
- import com.parse.ParseUser;
- import com.parse.SaveCallback;
- import java.text.Format;
- import java.text.SimpleDateFormat;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * Created by pedro on 04/05/14.
- */
- public class CommentsFragment extends Fragment implements AdapterComm {
- private String postID;
- private List<Comment> Comments;
- private CommentsAdapter adapter;
- private ListView mListViewComments;
- private EditText mMessage;
- private TextView mTextError;
- private Button mButton;
- private Button mRefresh;
- private RelativeLayout mError;
- private ProgressBar mProgressBar;
- private SwingBottomInAnimationAdapter swingBottomInAnimationAdapter;
- private Animation slideOffButton;
- private Animation slideOnButton;
- private Animation slideOnProgressBar;
- private Animation slideOffProgressBar;
- Bundle extras;
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragment_comment, container, false);
- mListViewComments = (ListView) view.findViewById(R.id.comments_list);
- mMessage = (EditText) view.findViewById(R.id.message_content);
- mButton = (Button) view.findViewById(R.id.send_message);
- mRefresh = (Button) view.findViewById(R.id.comment_buttton_refresh);
- mProgressBar = (ProgressBar) view.findViewById(R.id.comment_loading);
- mError = (RelativeLayout) view.findViewById(R.id.comment_list_error);
- mTextError = (TextView) view.findViewById(R.id.comment_textError);
- return view;
- }
- @Override
- public void onViewCreated(View view, Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- try {
- extras = getArguments();
- postID = extras.getString("postID");
- Comments = new ArrayList<Comment>();
- slideOnButton = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_on);
- slideOffButton = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_exit);
- slideOnProgressBar = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_on);
- slideOffProgressBar = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_exit);
- getComments();
- } catch (Exception e) {
- }
- mButton.setOnClickListener(onClickButtonSend);
- }
- private View.OnClickListener onClickRefresh = new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- showListView();
- getComments();
- }
- };
- private View.OnClickListener onClickButtonSend = new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- try {
- if (mMessage.getText().toString().length()>0) {
- //mMessage.setEnabled(false);
- mButton.startAnimation(slideOffButton);
- slideOffButton.setAnimationListener(new Animation.AnimationListener() {
- @Override
- public void onAnimationStart(Animation animation) { }
- @Override
- public void onAnimationRepeat(Animation animation) { }
- @Override
- public void onAnimationEnd(Animation animation) {
- mButton.setVisibility(View.INVISIBLE);
- mProgressBar.setVisibility(View.VISIBLE);
- mProgressBar.startAnimation(slideOnProgressBar);
- }
- });
- ParseObject comments = new ParseObject("Comments");
- ParseObject post = ParseObject.createWithoutData("FeedPost", postID);
- ParseObject user = ParseObject.createWithoutData("_User", ParseUser.getCurrentUser().getObjectId());
- comments.put("Username", ParseUser.getCurrentUser().getString("name"));
- comments.put("Content", mMessage.getText().toString());
- comments.put("enable", true);
- comments.put("User", user);
- comments.put( "Post", post);
- mMessage.setText("");
- comments.saveInBackground(new SaveCallback() {
- @Override
- public void done(ParseException e) {
- mProgressBar.startAnimation(slideOffProgressBar);
- slideOffProgressBar.setAnimationListener(new Animation.AnimationListener() {
- @Override
- public void onAnimationStart(Animation animation) {
- }
- @Override
- public void onAnimationRepeat(Animation animation) {
- }
- @Override
- public void onAnimationEnd(Animation animation) {
- mProgressBar.setVisibility(View.INVISIBLE);
- mButton.startAnimation(slideOnButton);
- mButton.setVisibility(View.VISIBLE);
- }
- });
- ParseQuery<ParseObject> query = ParseQuery.getQuery("Comments");
- ParseObject feedPost = ParseObject.createWithoutData("FeedPost", postID);
- query.whereEqualTo("Post", feedPost);
- query.whereEqualTo("enable", true);
- query.orderByAscending("createdAt");
- query.findInBackground(new FindCallback<ParseObject>() {
- public void done(List<ParseObject> commentList, ParseException e) {
- Format formatter = new SimpleDateFormat("dd/MM/yyyy");
- Comments.clear();
- for (int i = 0; i < commentList.size(); i++) {
- Comments.add(new Comment(commentList.get(i).getObjectId(),
- commentList.get(i).getString("Username"),
- commentList.get(i).getString("Content"),
- formatter.format(commentList.get(i).getCreatedAt())));
- }
- if(mListViewComments.getAdapter() == null) {
- adapter = new CommentsAdapter(getActivity(),CommentsFragment.this, Comments);
- swingBottomInAnimationAdapter= new SwingBottomInAnimationAdapter(adapter);
- swingBottomInAnimationAdapter.setAbsListView(mListViewComments);
- mListViewComments.setAdapter(swingBottomInAnimationAdapter);
- } else {
- swingBottomInAnimationAdapter.notifyDataSetChanged();
- mListViewComments.setAdapter(swingBottomInAnimationAdapter);
- }
- showListView();
- }
- });
- }
- });
- } else {
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- };
- private void getComments() {
- try {
- showListView();
- ParseQuery<ParseObject> query = ParseQuery.getQuery("Comments");
- ParseObject feedPost = ParseObject.createWithoutData("FeedPost", postID);
- query.whereEqualTo("Post", feedPost);
- query.whereEqualTo("enable", true);
- query.orderByAscending("createdAt");
- query.findInBackground(new FindCallback<ParseObject>() {
- public void done(List<ParseObject> commentList, ParseException e) {
- try {
- Format formatter = new SimpleDateFormat("dd-MM-yyyy");
- Comments.clear();
- if(commentList.size() <= 0) {
- mTextError.setText(getString(R.string.none_comment));
- showError(true);
- } else {
- for (int i = 0; i < commentList.size(); i++) {
- Comments.add(new Comment(commentList.get(i).getObjectId(),
- commentList.get(i).getString("Username"),
- commentList.get(i).getString("Content"),
- formatter.format(commentList.get(i).getCreatedAt())));
- }
- adapter = new CommentsAdapter(getActivity(),CommentsFragment.this, Comments);
- swingBottomInAnimationAdapter= new SwingBottomInAnimationAdapter(adapter);
- swingBottomInAnimationAdapter.setAbsListView(mListViewComments);
- mListViewComments.setAdapter(swingBottomInAnimationAdapter);
- }
- } catch (Exception er) {
- showError(false);
- }
- }
- });
- } catch (Exception e) {
- showError(false);
- }
- }
- @Override
- public void deleteComment(String ID) {
- try {
- showLoadDialog();
- ParseQuery<ParseObject> query = ParseQuery.getQuery("Comments");
- query.getInBackground(ID, new GetCallback<ParseObject>() {
- public void done(ParseObject comments, ParseException e) {
- if (e == null) {
- comments.put("enable", false);
- comments.saveInBackground(new SaveCallback() {
- @Override
- public void done(ParseException e) {
- ParseQuery<ParseObject> query = ParseQuery.getQuery("Comments");
- ParseObject feedPost = ParseObject.createWithoutData("FeedPost", postID);
- query.whereEqualTo("Post", feedPost);
- query.whereEqualTo("enable", true);
- query.orderByAscending("createdAt");
- query.findInBackground(new FindCallback<ParseObject>() {
- public void done(List<ParseObject> commentList, ParseException e) {
- Format formatter = new SimpleDateFormat("dd/MM/yyyy");
- Comments.clear();
- for (int i = 0; i < commentList.size(); i++) {
- Comments.add(new Comment(commentList.get(i).getObjectId(),
- commentList.get(i).getString("Username"),
- commentList.get(i).getString("Content"),
- formatter.format(commentList.get(i).getCreatedAt())));
- }
- swingBottomInAnimationAdapter.notifyDataSetChanged();
- mListViewComments.setAdapter(swingBottomInAnimationAdapter);
- dismissLoad();
- Toast.makeText(getActivity(), getString(R.string.erased_comment), Toast.LENGTH_SHORT).show();
- }
- });
- }
- });
- }
- }
- });
- } catch (Exception e) {
- mListViewComments.setVisibility(View.GONE);
- mError.setVisibility(View.VISIBLE);
- }
- }
- private void showError(boolean isNull) {
- if(isNull) {
- mTextError.setText(getString(R.string.none_comment));
- mRefresh.setVisibility(View.GONE);
- } else {
- mTextError.setText(getString(R.string.generic_error));
- mRefresh.setVisibility(View.VISIBLE);
- }
- mListViewComments.setVisibility(View.GONE);
- mError.setVisibility(View.VISIBLE);
- }
- private void showListView() {
- mListViewComments.setVisibility(View.VISIBLE);
- mError.setVisibility(View.GONE);
- }
- private void showLoadDialog() {
- LoaderDialog d = (LoaderDialog) getFragmentManager().findFragmentByTag(LoaderDialog.TAG);
- if (d == null) {
- LoaderDialog.Builder builder = new LoaderDialog.Builder();
- builder.setCancelable(false);
- d = builder.build();
- d.show(getFragmentManager(), LoaderDialog.TAG);
- }
- }
- protected void dismissLoad() {
- LoaderDialog d = (LoaderDialog) getFragmentManager().findFragmentByTag(
- LoaderDialog.TAG);
- if (d != null && d.isVisible())
- d.dismiss();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment