Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package info.rekayasa.donordarah.activity;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.support.v7.widget.LinearLayoutManager;
- import android.support.v7.widget.RecyclerView;
- import android.text.TextUtils;
- import android.view.View;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.TextView;
- import com.firebase.ui.database.FirebaseRecyclerAdapter;
- import com.google.firebase.database.DataSnapshot;
- import com.google.firebase.database.DatabaseError;
- import com.google.firebase.database.DatabaseReference;
- import com.google.firebase.database.FirebaseDatabase;
- import com.google.firebase.database.Query;
- import com.google.firebase.database.ServerValue;
- import com.google.firebase.database.ValueEventListener;
- import java.security.Timestamp;
- import java.text.DateFormat;
- import java.text.SimpleDateFormat;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.TimeZone;
- import info.rekayasa.donordarah.R;
- import info.rekayasa.donordarah.adapter.CommentViewHolder;
- import info.rekayasa.donordarah.entity.Comment;
- import static android.icu.lang.UCharacter.GraphemeClusterBreak.T;
- public class CommentActivity extends BaseActivity {
- EditText mCommentField;
- Button mSendButton;
- TextView mNoComments;
- RecyclerView mRecyclerView;
- LinearLayoutManager mManager;
- DatabaseReference mDatabase, mRef;
- long timestamp;
- String comKey;
- String postId, fullName;
- int commentCount;
- FirebaseRecyclerAdapter<Comment, CommentViewHolder> mAdapter;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_comment);
- Bundle extras = getIntent().getExtras();
- postId = extras.getString("postKey");
- fullName = extras.getString("fullName");
- mCommentField = (EditText) findViewById(R.id.etComment);
- mSendButton = (Button) findViewById(R.id.btnSend);
- mNoComments = (TextView) findViewById(R.id.tvNoComments);
- // mDatabase = FirebaseDatabase.getInstance().getReference();
- mSendButton.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- String commentTime = String.valueOf(ServerValue.TIMESTAMP);
- // System.out.println("timestamp: " + commentTime);
- final String comment = mCommentField.getText().toString();
- if (TextUtils.isEmpty(comment)) {
- mCommentField.setError("Komentar harus berisi");
- } else {
- comKey = mDatabase.child("comments").child(postId).push().getKey();
- mDatabase.child("comments").child(postId).child(comKey).child("timestamp").setValue(ServerValue.TIMESTAMP);
- final Query comCount = mDatabase.child("bleeds").child(postId).child("commentCount");
- comCount.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- commentCount = dataSnapshot.getValue(int.class);
- // System.out.println("commentCount: " + commentCount);
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- Query query = mDatabase.child("comments").child(postId).child(comKey).child("timestamp");
- query.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- timestamp = dataSnapshot.getValue(long.class);
- // posting komentar
- Comment com = new Comment(comment, fullName, postId, timestamp, null);
- mDatabase.child("comments").child(postId).child(comKey).setValue(com);
- mDatabase.child("bleeds").child(postId).child("commentCount").setValue(commentCount + 1);
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- mCommentField.setText("");
- }
- }
- });
- mRecyclerView = (RecyclerView) findViewById(R.id.commentList);
- mRecyclerView.setHasFixedSize(true);
- mManager = new LinearLayoutManager(this);
- // mManager.setReverseLayout(true);
- // mManager.setStackFromEnd(true);
- mRecyclerView.setLayoutManager(mManager);
- }
- @Override
- protected void onStart() {
- super.onStart();
- mDatabase = FirebaseDatabase.getInstance().getReference();
- mRef = FirebaseDatabase.getInstance().getReference().child("comments").child(postId);
- mAdapter = new FirebaseRecyclerAdapter<Comment, CommentViewHolder>(Comment.class, R.layout.item_comment,
- CommentViewHolder.class, mRef) {
- @Override
- protected void populateViewHolder(CommentViewHolder viewHolder, Comment model, int position) {
- viewHolder.bindToPost(model);
- // System.out.println("model: " + model.comment);
- // if (model.comment.length() > 0){
- mNoComments.setVisibility(View.GONE);
- // }
- }
- };
- mRecyclerView.setAdapter(mAdapter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement