CourseCommentActivity.kt 4.01 KB
Newer Older
严久程 committed
1 2
package com.yidianling.course.coursePlay

严久程 committed
3
import android.annotation.SuppressLint
严久程 committed
4 5 6 7 8 9
import android.app.Activity
import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import com.alibaba.android.arouter.facade.annotation.Route
严久程 committed
10 11 12 13
import com.ydl.webview.H5Params
import com.ydl.webview.NewH5Activity
import com.ydl.ydlcommon.base.BaseActivity
import com.ydl.ydlcommon.utils.log.LogHelper
严久程 committed
14 15 16
import com.yidianling.common.tools.ToastUtil
import com.yidianling.course.CourseConstants
import com.yidianling.course.R
严久程 committed
17
import com.yidianling.course.net.Command
严久程 committed
18
import com.yidianling.course.net.CourseRetrofitUtils
严久程 committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_course_commeny.*

/**
 * @author jiucheng
 * @描述:课程写评价页面
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2019/7/2
 */
@Route(path = "/course/comment")
class CourseCommentActivity : BaseActivity() {
    private var courseId: String? = null

    companion object {
        private const val COURSE_ID = "course_id"
        fun startActivity(activity: Activity, courseId: String) {
            var intent = Intent(activity, CourseCommentActivity::class.java)
            intent.putExtra(COURSE_ID, courseId)
            activity.startActivity(intent)
        }
    }

    override fun layoutResId(): Int {
        return R.layout.activity_course_commeny
    }

    override fun initDataAndEvent() {
        courseId = intent.getStringExtra(COURSE_ID)
        if (TextUtils.isEmpty(courseId)) {
            ToastUtil.toastShort("参数错误,请重试")
            return
        }
        iv_back.setOnClickListener {
            finish()
        }

        tv_left_menu.setOnClickListener {
            if (TextUtils.isEmpty(et_comment.text.toString())) {
                ToastUtil.toastShort("请输入评论内容")
                return@setOnClickListener
            }
            commitReply(et_comment.text.toString())
        }

        et_comment.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {
                if (TextUtils.isEmpty(s)) {
                    tv_input_length.text = "0"
严久程 committed
69
                    tv_input_length.setTextColor(resources.getColor(R.color.course_color_B3B3B3))
严久程 committed
70 71
                } else {
                    tv_input_length.text = s!!.length.toString()
严久程 committed
72
                    tv_input_length.setTextColor(resources.getColor(R.color.course_color_242424))
严久程 committed
73 74 75 76 77 78 79 80 81 82 83 84
                }
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }
        })
    }

    //提交评论
严久程 committed
85
    @SuppressLint("CheckResult")
严久程 committed
86 87 88
    private fun commitReply(content: String) {
        showProgressDialog(null)
        val cmd = Command.CourseCommitReply(courseId!!.toInt(), content)
严久程 committed
89
        CourseRetrofitUtils.commitCourseReply(cmd)
严久程 committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ resp ->
                    dismissProgressDialog()
                    if (resp.code == 200) {
                        ToastUtil.toastShort(resp.msg)

                        val h5Params = H5Params(CourseConstants.COURSE_COMMENT_H5 + courseId, "全部评价")
                        NewH5Activity.start(this@CourseCommentActivity, h5Params)

                        this@CourseCommentActivity.finish()
                    } else {
                        ToastUtil.toastShort(resp.msg)
                        LogHelper.getInstance().writeLogSync("提交评论失败:" + resp.msg)
                    }
                }, { t ->
                    dismissProgressDialog()
严久程 committed
107
                    CourseRetrofitUtils.handleError(this, t)
严久程 committed
108 109 110 111
                    LogHelper.getInstance().writeLogSync("提交评论失败:" + t.message)
                })
    }
}