Advertisement
rachmadi

CommentActivity Bleed

Aug 22nd, 2016
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.68 KB | None | 0 0
  1. package info.rekayasa.donordarah.activity;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. import android.os.Bundle;
  5. import android.support.v7.widget.LinearLayoutManager;
  6. import android.support.v7.widget.RecyclerView;
  7. import android.text.TextUtils;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.TextView;
  12.  
  13. import com.firebase.ui.database.FirebaseRecyclerAdapter;
  14. import com.google.firebase.database.DataSnapshot;
  15. import com.google.firebase.database.DatabaseError;
  16. import com.google.firebase.database.DatabaseReference;
  17. import com.google.firebase.database.FirebaseDatabase;
  18. import com.google.firebase.database.Query;
  19. import com.google.firebase.database.ServerValue;
  20. import com.google.firebase.database.ValueEventListener;
  21.  
  22. import java.security.Timestamp;
  23. import java.text.DateFormat;
  24. import java.text.SimpleDateFormat;
  25. import java.util.Calendar;
  26. import java.util.Date;
  27. import java.util.TimeZone;
  28.  
  29. import info.rekayasa.donordarah.R;
  30. import info.rekayasa.donordarah.adapter.CommentViewHolder;
  31. import info.rekayasa.donordarah.entity.Comment;
  32.  
  33. import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
  34.  
  35. public class CommentActivity extends BaseActivity {
  36.  
  37.     EditText mCommentField;
  38.     Button mSendButton;
  39.     TextView mNoComments;
  40.  
  41.     RecyclerView mRecyclerView;
  42.     LinearLayoutManager mManager;
  43.  
  44.     DatabaseReference mDatabase, mRef;
  45.     long timestamp;
  46.     String comKey;
  47.  
  48.     String postId, fullName;
  49.     int commentCount;
  50.  
  51.     FirebaseRecyclerAdapter<Comment, CommentViewHolder> mAdapter;
  52.  
  53.     @Override
  54.     public void onCreate(Bundle savedInstanceState) {
  55.         super.onCreate(savedInstanceState);
  56.         setContentView(R.layout.activity_comment);
  57.  
  58.         Bundle extras = getIntent().getExtras();
  59.         postId = extras.getString("postKey");
  60.         fullName = extras.getString("fullName");
  61.  
  62.         mCommentField = (EditText) findViewById(R.id.etComment);
  63.         mSendButton = (Button) findViewById(R.id.btnSend);
  64.         mNoComments = (TextView) findViewById(R.id.tvNoComments);
  65.  
  66. //        mDatabase = FirebaseDatabase.getInstance().getReference();
  67.  
  68.         mSendButton.setOnClickListener(new View.OnClickListener() {
  69.             @Override
  70.             public void onClick(View view) {
  71.                 String commentTime = String.valueOf(ServerValue.TIMESTAMP);
  72. //                System.out.println("timestamp: " + commentTime);
  73.                 final String comment = mCommentField.getText().toString();
  74.                 if (TextUtils.isEmpty(comment)) {
  75.                     mCommentField.setError("Komentar harus berisi");
  76.                 } else {
  77.                     comKey = mDatabase.child("comments").child(postId).push().getKey();
  78.                     mDatabase.child("comments").child(postId).child(comKey).child("timestamp").setValue(ServerValue.TIMESTAMP);
  79.  
  80.                     final Query comCount = mDatabase.child("bleeds").child(postId).child("commentCount");
  81.                     comCount.addValueEventListener(new ValueEventListener() {
  82.                         @Override
  83.                         public void onDataChange(DataSnapshot dataSnapshot) {
  84.                             commentCount = dataSnapshot.getValue(int.class);
  85. //                            System.out.println("commentCount: " + commentCount);
  86.                         }
  87.  
  88.                         @Override
  89.                         public void onCancelled(DatabaseError databaseError) {
  90.  
  91.                         }
  92.                     });
  93.  
  94.                     Query query = mDatabase.child("comments").child(postId).child(comKey).child("timestamp");
  95.                     query.addValueEventListener(new ValueEventListener() {
  96.                         @Override
  97.                         public void onDataChange(DataSnapshot dataSnapshot) {
  98.                             timestamp = dataSnapshot.getValue(long.class);
  99.  
  100.                             // posting komentar
  101.                             Comment com = new Comment(comment, fullName, postId, timestamp, null);
  102.                             mDatabase.child("comments").child(postId).child(comKey).setValue(com);
  103.                             mDatabase.child("bleeds").child(postId).child("commentCount").setValue(commentCount + 1);
  104.                         }
  105.  
  106.                         @Override
  107.                         public void onCancelled(DatabaseError databaseError) {
  108.  
  109.                         }
  110.                     });
  111.                     mCommentField.setText("");
  112.                 }
  113.             }
  114.         });
  115.  
  116.         mRecyclerView = (RecyclerView) findViewById(R.id.commentList);
  117.         mRecyclerView.setHasFixedSize(true);
  118.         mManager = new LinearLayoutManager(this);
  119. //        mManager.setReverseLayout(true);
  120. //        mManager.setStackFromEnd(true);
  121.         mRecyclerView.setLayoutManager(mManager);
  122.  
  123.  
  124.     }
  125.  
  126.     @Override
  127.     protected void onStart() {
  128.         super.onStart();
  129.         mDatabase = FirebaseDatabase.getInstance().getReference();
  130.         mRef = FirebaseDatabase.getInstance().getReference().child("comments").child(postId);
  131.  
  132.         mAdapter = new FirebaseRecyclerAdapter<Comment, CommentViewHolder>(Comment.class, R.layout.item_comment,
  133.                 CommentViewHolder.class, mRef) {
  134.             @Override
  135.             protected void populateViewHolder(CommentViewHolder viewHolder, Comment model, int position) {
  136.                 viewHolder.bindToPost(model);
  137. //                System.out.println("model: " + model.comment);
  138. //                if (model.comment.length() > 0){
  139.                     mNoComments.setVisibility(View.GONE);
  140. //                }
  141.             }
  142.         };
  143.         mRecyclerView.setAdapter(mAdapter);
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement