CommentActivity.java 4.7 KB
package com.cnlive.im.ui;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.cnlive.im.R;
import com.cnlive.im.adapter.CommentAdapter;
import com.cnlive.libs.util.comment.CommentUtil;
import com.cnlive.libs.util.comment.base.CommentCallback;
import com.cnlive.libs.util.comment.base.CommentListCallback;
import com.cnlive.libs.util.comment.base.CommentProgramCallback;
import com.cnlive.libs.util.comment.model.CommentItem;
import com.cnlive.libs.util.comment.model.CommentPage;
import com.cnlive.libs.util.comment.model.CommentProgram;
import com.cnlive.libs.util.comment.model.ErrorMessage;

import java.util.List;

/**
 * @author 陈硕
 * @time 2017/6/9  9:28
 * @desc ${TODD}
 */
public class CommentActivity extends AppCompatActivity implements View.OnClickListener, CommentAdapter.OnImageViewClick {


    private Button btn_sent;
    private EditText edit_comment;
    private CommentUtil commentUtil;
    private CommentAdapter commentAdapter;
    private RecyclerView recyclerView;
    private String programID = "123";
    private String userID = "12344";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment);
        commentAdapter = new CommentAdapter(CommentActivity.this);
        initUI();


    }

    private void initUI() {
        recyclerView = (RecyclerView) findViewById(R.id.comment_content);
        recyclerView.setLayoutManager(new LinearLayoutManager(CommentActivity.this));
        btn_sent = (Button) findViewById(R.id.sendBtn_comment);
        edit_comment = (EditText) findViewById(R.id.et_search);
        btn_sent.setOnClickListener(this);
        commentAdapter.setOnImageViewClick(this);
        intiDate();

    }

    private void intiDate() {
        if (commentUtil == null) {
            commentUtil = new CommentUtil();
        }
        getCommentProgram();
        getCommentMesaage();
    }


    @Override
    public void onClick(View v) {
        final String text = edit_comment.getText().toString();
        if (TextUtils.isEmpty(text)) {
            Toast.makeText(CommentActivity.this, "输入内容不能为空", Toast.LENGTH_SHORT).show();
        } else {
            commentUtil.insertComment(programID, userID, text, new CommentCallback() {
                @Override
                public void onResponse(ErrorMessage errorMessage) {
                    if (errorMessage != null && errorMessage.getErrorCode().equals("0")) {
                        Toast.makeText(CommentActivity.this, "评论成功", Toast.LENGTH_SHORT).show();
                        edit_comment.setText("");
                        commentAdapter.clearItems();
                        getCommentMesaage();
                    } else Toast.makeText(CommentActivity.this, "评论失败", Toast.LENGTH_SHORT).show();

                }
            });
        }
    }


    public void getCommentMesaage() {
        commentUtil.readAllComment(programID, 1, 10, new CommentListCallback() {
            @Override
            public void onResponse(CommentPage commentPage) {
                if (commentPage.getData() != null) {
                    Log.v("commentPage", commentPage.getErrorMessage() + "");
                    List<CommentItem> comments = commentPage.getData().getComments();

                    recyclerView.setAdapter(commentAdapter);
                    commentAdapter.addItems(comments);


                }
            }
        });
    }

    public void getCommentProgram() {
        commentUtil.programComment(programID, new CommentProgramCallback() {
            @Override
            public void onResponse(CommentProgram commentProgram) {
                Log.v("commentProgram", commentProgram.getData().getComments().size() + "");

            }
        });
    }

    @Override
    public void praiseComment(CommentItem commentItem) {
        commentUtil.praiseComment(programID, userID, commentItem.getId(), new CommentCallback() {
            @Override
            public void onResponse(ErrorMessage errorMessage) {
                if (errorMessage != null && errorMessage.getErrorCode().equals("0")) {
                    Toast.makeText(CommentActivity.this, "点赞成功", Toast.LENGTH_SHORT).show();
                    commentAdapter.clearItems();
                    getCommentMesaage();
                } else Toast.makeText(CommentActivity.this, "点赞失败", Toast.LENGTH_SHORT).show();

            }
        });
    }


}