CourseItemNewView.kt 11.5 KB
Newer Older
严久程 committed
1 2 3
package com.yidianling.course.widget

import android.annotation.SuppressLint
严久程 committed
4 5
import android.app.Activity
import android.app.AlertDialog
严久程 committed
6 7 8 9 10 11 12
import android.content.Context
import android.graphics.Paint
import android.support.constraint.ConstraintLayout
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
严久程 committed
13
import com.lzf.easyfloat.permission.PermissionUtils
严久程 committed
14 15 16 17 18 19 20
import com.ydl.media.audio.AudioPlayer
import com.ydl.media.audio.model.Music
import com.ydl.media.view.PlayTypeEnum
import com.ydl.media.view.PlayerFloatHelper
import com.ydl.media.view.PlayerFloatView
import com.ydl.webview.H5Params
import com.ydl.webview.NewH5Activity
严久程 committed
21
import com.ydl.ydl_image.module.GlideApp
严久程 committed
22
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils
严久程 committed
23
import com.yidianling.common.tools.RxImageTool
严久程 committed
24
import com.yidianling.common.tools.ToastUtil
严久程 committed
25 26
import com.yidianling.course.CourseConstants
import com.yidianling.course.R
严久程 committed
27
import com.yidianling.course.bean.Course
严久程 committed
28
import com.yidianling.course.constants.CourseBIConstants
严久程 committed
29
import kotlinx.android.synthetic.main.item_course_new_list_view.view.*
严久程 committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

/**
 * @author jiucheng
 * @描述:课程列表页改版后的itemView
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2019/8/13
 */
class CourseItemNewView : ConstraintLayout {
    private var mContext: Context
    private lateinit var course: Course
    private var statusListener: PlayerFloatView.FloatViewPlayListener? = null
    private var headUrl: String = ""
    private var doctorName: String = ""

    constructor(mContext: Context) : super(mContext) {
        this.mContext = mContext
        initView()
    }

严久程 committed
50
    constructor(mContext: Context, attr: AttributeSet) : super(mContext, attr) {
严久程 committed
51 52 53 54 55 56 57
        this.mContext = mContext
        initView()
    }


    fun initView() {
        View.inflate(mContext, R.layout.item_course_new_list_view, this)
严久程 committed
58 59
        val params = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
        layoutParams = params
严久程 committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
        setPadding(RxImageTool.dp2px(13f), 0, RxImageTool.dp2px(15f), 0)
    }

    @SuppressLint("SetTextI18n")
    fun setData(course: Course) {
        this.course = course
        if (!TextUtils.isEmpty(course.hostListPic)) {
            headUrl = course.hostListPic
        } else {
            if (course.courseHostInfo != null && !TextUtils.isEmpty(course.courseHostInfo.doctorHead)) {
                headUrl = course.courseHostInfo.doctorHead
            }
        }

        GlideApp.with(mContext)
严久程 committed
75 76
            .load(headUrl)
            .into(iv_head)
严久程 committed
77

洪国微 committed
78
        //活动标签
79
        if (course.joinActivity && !TextUtils.isEmpty(course.activityImg)) {
洪国微 committed
80
            img_activity.visibility = View.VISIBLE
81 82 83
            GlideApp.with(context).load(course.activityImg).into(img_activity)
            tv_title.text = "\u3000\u3000\u3000\u0020" + course.title
        } else {
洪国微 committed
84 85 86 87
            img_activity.visibility = View.GONE
            tv_title.text = course.title
        }

严久程 committed
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        if (!TextUtils.isEmpty(course.doctorName)) {
            doctorName = course.doctorName
        } else {
            if (course.courseHostInfo != null && !TextUtils.isEmpty(course.courseHostInfo.doctorName)) {
                doctorName = course.courseHostInfo.doctorName
            }
        }
        var info = ""
        if (!TextUtils.isEmpty(course.hostInfo)) {
            info = course.hostInfo
        } else {
            if (course.courseHostInfo != null && !TextUtils.isEmpty(course.courseHostInfo.title)) {
                info = course.courseHostInfo.title
            }
        }

        if (TextUtils.isEmpty(doctorName) || TextUtils.isEmpty(info)) {
            tv_doctor_info.text = "壹点灵课程精选讲师"
        } else {
            tv_doctor_info.text = "$doctorName · $info"
        }

        tv_course_info.text = "共${course.subCourseNum}讲"
        tv_course_num.text = "${course.joinNum}人已学"
        if (course.subCourseNum == 0) {
            tv_course_info.visibility = View.GONE
            view_line.visibility = View.GONE
        } else {
            tv_course_info.visibility = View.VISIBLE
            view_line.visibility = View.VISIBLE
        }

        if (course.isInvite == 0) {
            tv_fan_li.visibility = View.GONE
        } else {
            tv_fan_li.visibility = View.VISIBLE
        }

        if (course.newCourse == 1) {
            iv_new.visibility = View.VISIBLE
        } else {
            iv_new.visibility = View.GONE
        }

        if (course.recommend == 2) {
            iv_hot.visibility = View.VISIBLE
        } else {
            iv_hot.visibility = View.GONE
        }

        if (course.demoType == 0) {
            iv_play.visibility = View.GONE
        } else {
            iv_play.visibility = View.VISIBLE
        }

        iv_play.setOnClickListener {
            playMedia()

严久程 committed
147 148 149 150
            ActionCountUtils.count(
                CourseBIConstants.CourseListEvent.POSITION_COURSE_LIST_AUDITION_CLICK,
                course.id
            )
严久程 committed
151 152 153
        }

        tv_view_course.setOnClickListener {
严久程 committed
154 155 156 157 158
            if (TextUtils.equals(course.courseType, "1") || TextUtils.equals(
                    course.courseType,
                    "7"
                ) || TextUtils.equals(course.courseType, "6")
            ) {
严久程 committed
159
                ARouter.getInstance()
严久程 committed
160 161 162
                    .build("/course/play")
                    .withInt("course_id", course.id.toInt())
                    .navigation()
严久程 committed
163 164 165 166 167
            } else {
                val h5Params = H5Params(CourseConstants.COURSE_DETAIL_H5 + course.id, "课程详情")
                NewH5Activity.start(mContext, h5Params)
            }

严久程 committed
168
            ActionCountUtils.count(CourseBIConstants.CourseListEvent.POSITION_COURSE_LIST_XXX_CLICK + course.id + "_click")
严久程 committed
169 170 171 172 173 174 175 176 177 178 179 180 181
        }


        if (!TextUtils.isEmpty(course.applyFee)) {
            if (course.applyFee.contains(".")) {
                tv_price.text = course.applyFee.split(".")[0]
                tv_price_decimal.text = "." + course.applyFee.split(".")[1]
            } else {
                tv_price.text = course.applyFee
                tv_price_decimal.text = ".00"
            }
        }

严久程 committed
182 183 184 185 186
        if (TextUtils.equals("1", course.isPromotion) || TextUtils.equals(
                "3",
                course.isPromotion
            )
        ) {
严久程 committed
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
            tv_cu_xiao.visibility = View.VISIBLE
            if (TextUtils.equals("1", course.isPromotion)) {
                if (course.promotionApplyFee.contains(".")) {
                    tv_price.text = course.promotionApplyFee.split(".")[0]
                    tv_price_decimal.text = "." + course.promotionApplyFee.split(".")[1]
                } else {
                    tv_price.text = course.promotionApplyFee
                    tv_price_decimal.text = ".00"
                }
            }
        } else {
            tv_cu_xiao.visibility = View.GONE
        }

        if (course.isBuy) {
            tv_view_course.visibility = View.VISIBLE
严久程 committed
203 204 205 206 207
            if (TextUtils.equals(course.courseType, "1") || TextUtils.equals(
                    course.courseType,
                    "7"
                ) || TextUtils.equals(course.courseType, "6")
            ) {
严久程 committed
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
                tv_view_course.text = "立即听课"
            } else {
                tv_view_course.text = "查看课程"
            }


            tv_original_price.visibility = View.GONE
            tv_price_si.visibility = View.GONE
            tv_price.visibility = View.GONE
            tv_price_decimal.visibility = View.GONE
        } else {
            tv_view_course.visibility = View.GONE

            tv_original_price.visibility = View.VISIBLE
            tv_price_si.visibility = View.VISIBLE
            tv_price.visibility = View.VISIBLE
            tv_price_decimal.visibility = View.VISIBLE


严久程 committed
227 228 229 230 231 232 233 234
            if (TextUtils.isEmpty(course.originalApplyFee) || TextUtils.equals(
                    course.originalApplyFee,
                    "0"
                ) || TextUtils.equals(
                    course.originalApplyFee,
                    "0.0"
                ) || TextUtils.equals(course.originalApplyFee, "0.00")
            ) {
严久程 committed
235 236 237 238 239 240 241 242 243 244 245 246 247 248
                tv_original_price.visibility = View.GONE
            } else {
                tv_original_price.visibility = View.VISIBLE
            }
            tv_original_price.text = "¥" + course.originalApplyFee
            tv_original_price.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG

        }
    }

    private fun playMedia() {
        if (PlayerFloatHelper.isShow(mContext)) {
            PlayerFloatHelper.hide()
            PlayerFloatHelper.removeView(mContext)
严久程 committed
249
            AudioPlayer.get().stopPlayer()
严久程 committed
250 251 252 253
        } else {
            PlayerFloatHelper.removeView(mContext)
        }

严久程 committed
254 255 256 257
        val music = Music()
        music.title = course.title
        music.artist = doctorName
        music.coverPath = if (!TextUtils.isEmpty(course.image)) course.image else course.pic
严久程 committed
258
        PlayerFloatHelper.isCanClick = false
严久程 committed
259 260 261


        if (course.demoType == 1) {
严久程 committed
262
            music.path = course.demoFile
严久程 committed
263

严久程 committed
264
            val hashMap = HashMap<String, String>()
严久程 committed
265 266
            PlayerFloatHelper.playTempData.clear()
            hashMap["course_id"] = course.id.toString()
严久程 committed
267
            AudioPlayer.get().singlePlay(music)
严久程 committed
268 269 270 271 272 273
            PlayerFloatHelper.show(
                mContext,
                playTypeEnum = PlayTypeEnum.PLAY_TYPE_COURSE,
                playData = hashMap
            )
            VideoFloatHelper.dismissFloat(mContext as Activity)
严久程 committed
274 275
        }
        if (course.demoType == 2) {
严久程 committed
276 277 278 279 280 281 282 283 284 285 286 287
            VideoFloatHelper.setVideoInfo(
                course.id.toString(),
                course.demoFile,
                false
            )
            checkPermission()

            if (PlayerFloatHelper.isShow(mContext)) {
                PlayerFloatHelper.hide()
                PlayerFloatHelper.removeView(mContext)
                AudioPlayer.get().stopPlayer()
            }
严久程 committed
288 289 290 291 292 293 294 295 296 297
        }


        if (statusListener == null) {
            statusListener = object : PlayerFloatView.FloatViewPlayListener {
                override fun onStartClick() {
                }

                override fun onPlayFinish() {
                    PlayerFloatHelper.hide()
严久程 committed
298
                    AudioPlayer.get().stopPlayer()
严久程 committed
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
                }

                override fun onPauseClick() {
                }

            }
            PlayerFloatHelper.addClickListener(statusListener as PlayerFloatView.FloatViewPlayListener)
        }
    }


    fun setBottomLineGone() {
        view_bottom_line.visibility = View.INVISIBLE
    }

    fun setBottomLineVisible() {
        view_bottom_line.visibility = View.VISIBLE
    }

    fun hideListenerButton() {
        tv_view_course.visibility = View.GONE
    }
严久程 committed
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343

    /**
     * 检测浮窗权限是否开启,若没有给与申请提示框(非必须,申请依旧是EasyFloat内部内保进行)
     */
    private fun checkPermission() {
        if (PermissionUtils.checkPermission(mContext)) {

            VideoFloatHelper.showVideoFloat(mContext as Activity)
        } else {
            AlertDialog.Builder(mContext)
                .setMessage("使用浮窗功能,需要您授权悬浮窗权限。")
                .setPositiveButton("去开启") { _, _ ->
                    VideoFloatHelper.showVideoFloat(mContext as Activity)
                }
                .setNegativeButton("取消") { _, _ ->
                    ToastUtil.toastLong(
                        mContext,
                        "App正常工作需要内部存储使用权限,请开启"
                    )
                }
                .show()
        }
    }
严久程 committed
344
}