HomeMuseView.kt 5.79 KB
Newer Older
徐健 committed
1 2 3 4
package com.yidianling.home.ui.view

import android.annotation.SuppressLint
import android.content.Context
范玉宾 committed
5
import android.graphics.Color
6 7 8 9 10
import android.graphics.Typeface
import android.media.MediaPlayer
import android.text.SpannableString
import android.text.Spanned
import android.text.style.StyleSpan
徐健 committed
11 12 13
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
YKai committed
14
import androidx.recyclerview.widget.RecyclerView
15 16
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
范玉宾 committed
17 18 19
import com.ydl.webview.H5Params
import com.ydl.webview.NewH5Activity
import com.ydl.ydlcommon.utils.Utils
刘鹏 committed
20
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils
21
import com.yidianling.home.adapter.MeditationViewPagerAdapter
徐健 committed
22
import com.yidianling.home.R
刘鹏 committed
23
import com.yidianling.home.constants.HomeBIConstants
24
import com.yidianling.home.constract.HomeViewConfig
徐健 committed
25
import com.yidianling.home.event.IHomeEvent
26
import com.yidianling.home.model.bean.MeditationModuleBean
konghaorui committed
27
import kotlinx.android.synthetic.ydl.home_muse_view.view.*
徐健 committed
28 29 30 31 32 33 34 35

/**
 * @author <a href="https://www.jianshu.com/u/c1e5310dd724">xujian</a>
 * @描述: 冥想模块
 * @Copyright Copyright (c) 2019
 * @Company 壹点灵
 * @date 2019/09/10
 */
36 37
class HomeMuseView(private val mContext: Context, private var homeEvent: IHomeEvent?) :
    LinearLayout(mContext) {
徐健 committed
38

39
    private val meditationTitles = mutableListOf<String>()
40

徐健 committed
41 42 43 44 45 46 47
    init {
        initView()
    }

    @SuppressLint("RestrictedApi")
    private fun initView() {
        orientation = VERTICAL
48 49 50 51
        val params = RecyclerView.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT
        )
徐健 committed
52
        layoutParams = params
konghaorui committed
53
        View.inflate(mContext, R.layout.home_muse_view, this)
54
        homeModuleMuseViewHomeCommonTitleView.setTitle(HomeViewConfig.getOrder().museTitle)
徐健 committed
55 56
    }

57
    fun initData(bean: MeditationModuleBean?) {
徐健 committed
58 59 60

        if (null != bean) {

范玉宾 committed
61 62
            val moreLink = bean.mditationIndexInfo?.upperFloorMore

徐健 committed
63
            homeModuleMuseViewHomeCommonTitleView.setOnClickListener {
范玉宾 committed
64 65 66 67 68

                if (Utils.isFastClick()) {
                    //防止连击
                    return@setOnClickListener
                }
刘鹏 committed
69 70
                //冥想助眠更多点击事件
                ActionCountUtils.count(HomeBIConstants.YDL_HOME_MEDITATION_MORE_CLICK)
范玉宾 committed
71 72 73 74
                if (!moreLink.isNullOrBlank()) {
                    NewH5Activity.start(context, H5Params(moreLink, null))
                }

徐健 committed
75 76
            }

范玉宾 committed
77
            fl_layout?.setOnClickListener {
范玉宾 committed
78 79 80 81 82

                if (Utils.isFastClick()) {
                    //防止连击
                    return@setOnClickListener
                }
83 84
                //冥想助眠更多点击事件
                ActionCountUtils.count(HomeBIConstants.YDL_HOME_MEDITATION_MORE_CLICK)
范玉宾 committed
85 86 87 88 89 90 91
                if (!moreLink.isNullOrBlank()) {
                    NewH5Activity.start(context, H5Params(moreLink, null))
                }

            }


92
            val greeting = bean.mditationIndexInfo?.greetings ?: "你好"
93 94
            tv_time.text = greeting
            val slogan =
95
                if (bean.mditationIndexInfo?.desc.isNullOrBlank()) {
96
                    "无常的日子里,呼吸间拾起力量"
97
                } else {
98
                    bean.mditationIndexInfo?.desc
徐健 committed
99
                }
100 101
            tv_meditation_slogan.text = slogan

102
            val videoUrl = bean.mditationIndexInfo?.videoLink
103 104 105 106 107 108 109 110 111 112

            video_view?.setVideoPath(videoUrl)
            video_view?.setOnErrorListener { mp, what, extra ->
                true
            }
            video_view?.setOnPreparedListener {
                it?.setVideoScalingMode(MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING)
                video_view?.setOnInfoListener { mp, what, extra ->
                    if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                        video_view.setBackgroundColor(Color.TRANSPARENT)
113
                    }
114
                    true
范玉宾 committed
115
                }
116 117
                it?.start()
                it?.setVolume(0.0F, 0.0F)
118
            }
119

范玉宾 committed
120

121
            video_view?.setOnCompletionListener { video_view?.start() }
122

123
            bean.mditationListResponse?.forEach {
124 125 126
                meditationTitles.add(it.comment)
            }

范玉宾 committed
127
            view_pager.isUserInputEnabled = false
128
            view_pager.adapter = MeditationViewPagerAdapter(mContext, bean)
129

130
            tab_layout.addOnTabSelectedListener(object : TabLayout.OnTabSelectedListener {
131
                override fun onTabSelected(tab: TabLayout.Tab?) {
刘鹏 committed
132 133 134 135
                    //冥想|声音|睡眠点击事件
                    ActionCountUtils.count(HomeBIConstants.YDL_HOME_MEDITATION_SUBJECT_CLICK,
                        tab?.let { bean?.mditationListResponse?.get(it.position)?.buried } ?:"")

136
                    var text: String? = tab?.text.toString().trim()
137 138
                    val spStr = SpannableString(text)
                    val styleSpan = StyleSpan(Typeface.BOLD)
139
                    spStr.setSpan(styleSpan, 0, text?.length ?: 0, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
140 141 142 143
                    tab?.text = spStr
                }

                override fun onTabUnselected(tab: TabLayout.Tab?) {
144
                    var text: String? = tab?.text.toString().trim()
145 146
                    val spStr = SpannableString(text)
                    val styleSpan = StyleSpan(Typeface.NORMAL)
147
                    spStr.setSpan(styleSpan, 0, text?.length ?: 0, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
148 149 150 151 152 153 154 155 156
                    tab?.text = spStr
                }

                override fun onTabReselected(tab: TabLayout.Tab?) {

                }

            })

157 158
            TabLayoutMediator(tab_layout, view_pager) { tab, position ->
                tab.text = meditationTitles[position]
159 160
            }.attach()

徐健 committed
161 162 163 164
        }
    }

}