CourseItemNewView.kt 12.2 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
import android.content.Context
import android.graphics.Paint
YKai committed
8
import androidx.constraintlayout.widget.ConstraintLayout
严久程 committed
9 10 11 12
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.modular.ModularServiceManager.provide
严久程 committed
23
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils
严久程 committed
24
import com.yidianling.common.tools.RxImageTool
严久程 committed
25
import com.yidianling.common.tools.ToastUtil
严久程 committed
26
import com.yidianling.consultant.api.IConsultantService
严久程 committed
27 28
import com.yidianling.course.CourseConstants
import com.yidianling.course.R
严久程 committed
29
import com.yidianling.course.bean.Course
严久程 committed
30
import com.yidianling.course.constants.CourseBIConstants
严久程 committed
31
import kotlinx.android.synthetic.main.item_course_new_list_view.view.*
严久程 committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

/**
 * @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
52
    constructor(mContext: Context, attr: AttributeSet) : super(mContext, attr) {
严久程 committed
53 54 55 56 57 58 59
        this.mContext = mContext
        initView()
    }


    fun initView() {
        View.inflate(mContext, R.layout.item_course_new_list_view, this)
严久程 committed
60 61
        val params = LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)
        layoutParams = params
严久程 committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
        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
77 78
            .load(headUrl)
            .into(iv_head)
严久程 committed
79

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

严久程 committed
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 147 148
        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
149 150 151 152
            ActionCountUtils.count(
                CourseBIConstants.CourseListEvent.POSITION_COURSE_LIST_AUDITION_CLICK,
                course.id
            )
严久程 committed
153 154 155
        }

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

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


        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
184 185 186 187 188
        if (TextUtils.equals("1", course.isPromotion) || TextUtils.equals(
                "3",
                course.isPromotion
            )
        ) {
严久程 committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
            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
205 206 207 208 209
            if (TextUtils.equals(course.courseType, "1") || TextUtils.equals(
                    course.courseType,
                    "7"
                ) || TextUtils.equals(course.courseType, "6")
            ) {
严久程 committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
                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
229 230 231 232 233 234 235 236
            if (TextUtils.isEmpty(course.originalApplyFee) || TextUtils.equals(
                    course.originalApplyFee,
                    "0"
                ) || TextUtils.equals(
                    course.originalApplyFee,
                    "0.0"
                ) || TextUtils.equals(course.originalApplyFee, "0.00")
            ) {
严久程 committed
237 238 239 240 241 242 243 244 245 246
                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

        }
    }

严久程 committed
247

严久程 committed
248
    fun setGuideData(pic: String, location: String) {
严久程 committed
249 250
        view_bottom_line.visibility = View.INVISIBLE
        iv_guide_image.visibility = View.VISIBLE
严久程 committed
251 252

        GlideApp.with(mContext)
严久程 committed
253
            .load(pic)
严久程 committed
254 255
            .into(iv_guide_image)

严久程 committed
256
        iv_guide_image.setOnClickListener {
严久程 committed
257 258 259 260
            provide(IConsultantService::class.java).jumpConsultAssistant(
                mContext as Activity,
                location.toInt()
            )
严久程 committed
261 262 263
        }
    }

严久程 committed
264 265 266 267 268 269 270 271 272 273 274

    fun setGuideGone(){
        iv_guide_image.visibility = View.GONE
    }


    fun isGuideVisible(): Boolean {
        return iv_guide_image.visibility == View.VISIBLE
    }


严久程 committed
275 276 277 278
    private fun playMedia() {
        if (PlayerFloatHelper.isShow(mContext)) {
            PlayerFloatHelper.hide()
            PlayerFloatHelper.removeView(mContext)
严久程 committed
279
            AudioPlayer.get().stopPlayer()
严久程 committed
280 281 282 283
        } else {
            PlayerFloatHelper.removeView(mContext)
        }

严久程 committed
284 285 286 287
        val music = Music()
        music.title = course.title
        music.artist = doctorName
        music.coverPath = if (!TextUtils.isEmpty(course.image)) course.image else course.pic
严久程 committed
288
        PlayerFloatHelper.isCanClick = false
严久程 committed
289 290 291


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

严久程 committed
294
            val hashMap = HashMap<String, String>()
严久程 committed
295 296
            PlayerFloatHelper.playTempData.clear()
            hashMap["course_id"] = course.id.toString()
严久程 committed
297
            AudioPlayer.get().singlePlay(music)
严久程 committed
298 299 300 301 302 303
            PlayerFloatHelper.show(
                mContext,
                playTypeEnum = PlayTypeEnum.PLAY_TYPE_COURSE,
                playData = hashMap
            )
            VideoFloatHelper.dismissFloat(mContext as Activity)
严久程 committed
304 305
        }
        if (course.demoType == 2) {
严久程 committed
306 307 308 309 310 311 312 313 314 315 316 317
            VideoFloatHelper.setVideoInfo(
                course.id.toString(),
                course.demoFile,
                false
            )
            checkPermission()

            if (PlayerFloatHelper.isShow(mContext)) {
                PlayerFloatHelper.hide()
                PlayerFloatHelper.removeView(mContext)
                AudioPlayer.get().stopPlayer()
            }
严久程 committed
318 319 320 321 322 323 324 325 326 327
        }


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

                override fun onPlayFinish() {
                    PlayerFloatHelper.hide()
严久程 committed
328
                    AudioPlayer.get().stopPlayer()
严久程 committed
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
                }

                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
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373

    /**
     * 检测浮窗权限是否开启,若没有给与申请提示框(非必须,申请依旧是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
374
}