VideoShowAdapter.kt 3.96 KB
Newer Older
1 2 3 4 5 6
package com.ydl.confide.home.adapter

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ObservableField
万齐军 committed
7
import androidx.databinding.ObservableInt
8 9 10 11 12
import androidx.recyclerview.widget.RecyclerView
import com.ydl.confide.R
import com.ydl.confide.databinding.ItemConfideHomeRecentBinding
import com.ydl.confide.databinding.ItemVideoShowBinding
import com.ydl.confide.home.bean.ConfideHomeBodyBean
13
import com.ydl.confide.home.event.IConfideHomeEvent
14
import com.ydl.confide.intro.BindingViewHolder
万齐军 committed
15
import com.ydl.ydlcommon.utils.actionutil.ActionCountUtils
16

17
class VideoShowAdapter(private val data: List<ConfideHomeBodyBean>?, private val event: IConfideHomeEvent) :
18
    RecyclerView.Adapter<BindingViewHolder<ItemVideoShowBinding>>() {
万齐军 committed
19
    private val dataList: List<ItemVideoShowViewModel> = data?.map { ItemVideoShowViewModel().mapOf(it) } ?: emptyList()
20 21 22 23 24 25 26 27 28 29 30
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BindingViewHolder<ItemVideoShowBinding> {
        val layoutInflater = LayoutInflater.from(parent.context)
        val binding =
            DataBindingUtil.inflate<ItemVideoShowBinding>(layoutInflater, R.layout.item_video_show, parent, false)
        return BindingViewHolder(binding)
    }

    override fun onBindViewHolder(holder: BindingViewHolder<ItemVideoShowBinding>, position: Int) {
31
        val itemVideoShowViewModel = dataList[position]
32
        holder.binding.item = itemVideoShowViewModel
万齐军 committed
33 34 35 36
        holder.itemView.setOnClickListener {
            ActionCountUtils.record("listen_counselor_list_page", "video_card_click")
            event.videoShowClick(position, data)
        }
37 38
    }

39
    override fun getItemCount() = dataList.size
40 41 42
}


43
class RecentConfideAdapter(val data: List<ItemVideoShowViewModel>, private val event: IConfideHomeEvent) :
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
    RecyclerView.Adapter<BindingViewHolder<ItemConfideHomeRecentBinding>>() {
    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BindingViewHolder<ItemConfideHomeRecentBinding> {
        val layoutInflater = LayoutInflater.from(parent.context)
        val binding =
            DataBindingUtil.inflate<ItemConfideHomeRecentBinding>(
                layoutInflater,
                R.layout.item_confide_home_recent,
                parent,
                false
            )
        return BindingViewHolder(binding)
    }

    override fun onBindViewHolder(holder: BindingViewHolder<ItemConfideHomeRecentBinding>, position: Int) {
61 62
        val itemVideoShowViewModel = data[position]
        holder.binding.item = itemVideoShowViewModel
万齐军 committed
63 64 65 66 67 68 69
        holder.itemView.setOnClickListener {
            ActionCountUtils.record("listen_counselor_list_page", "head_portrait_click", "1")
            ActionCountUtils.record(
                "listen_counselor_popupwindows_page",
                "popupwindows_page_visit",
                itemVideoShowViewModel.confideId ?: "", "2"
            )
万齐军 committed
70 71
            event.consultantClick(
                itemVideoShowViewModel.doctorId,
万齐军 committed
72 73
                itemVideoShowViewModel.confideId,
                itemVideoShowViewModel.uid
万齐军 committed
74
            )
万齐军 committed
75
        }
76 77 78 79 80 81 82 83 84
    }

    override fun getItemCount() = data.size
}

class ItemVideoShowViewModel {
    val name = ObservableField<String>("")
    val coverUrl = ObservableField<String>("")
    val videoCoverUrl = ObservableField<String>("")
万齐军 committed
85
    val state = ObservableInt()
霍志良 committed
86
    var doctorId: String? = null
万齐军 committed
87
    var confideId: String? = null
万齐军 committed
88
    var uid: String? = null
89 90 91
}

internal fun ItemVideoShowViewModel.mapOf(bean: ConfideHomeBodyBean): ItemVideoShowViewModel {
万齐军 committed
92
    name.set(bean.videoTitle ?: "")
93
    coverUrl.set(bean.confidedIcon)
万齐军 committed
94 95 96 97 98
    if (bean.coverVideoPicture.isNullOrBlank()) {
        videoCoverUrl.set(bean.coverPicture)
    } else {
        videoCoverUrl.set(bean.coverVideoPicture)
    }
万齐军 committed
99
    state.set(bean.confideLine ?: 0)
霍志良 committed
100
    doctorId = bean.doctorId
万齐军 committed
101
    confideId = bean.confidedId
万齐军 committed
102
    uid = bean.uid?.toString()
103 104 105 106 107 108 109
    return this
}