IntroAdapter.kt 6.84 KB
Newer Older
1 2
package com.ydl.confide.intro

3
import android.app.Activity
4 5 6 7 8 9 10 11 12 13 14 15 16
import android.content.Context
import android.net.Uri
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.OnLifecycleEvent
import androidx.recyclerview.widget.RecyclerView
import com.dou361.ijkplayer.widget.IjkVideoView
import com.ydl.confide.R
import com.ydl.confide.databinding.ItemExpertIntroBinding
17
import com.ydl.confide.home.http.ConfideHomeApi
18
import com.ydl.confide.home.util.ConfideNetworkUtil
19
import com.ydl.ydlcommon.utils.TimeUtil
20
import com.ydl.ydlcommon.view.dialog.CommonDialog
21 22 23 24 25
import com.ydl.ydlnet.YDLHttpUtils
import com.yidianling.common.tools.ToastUtil
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
26 27 28 29 30 31 32 33 34

internal class IntroAdapter(
    private val context: Context,
    private val data: List<VideoViewModel>,
    private val lifecycleOwner: LifecycleOwner
) : RecyclerView.Adapter<ItemIntroHolder>(), LifecycleObserver {

    private val videoViews = hashMapOf<Int, IjkVideoView>()

35 36 37 38
    private var hasAgreePlayWithoutWiFi = false

    private var curPos = 0

39 40
    private var dispose: Disposable? = null

41 42 43 44 45 46 47 48 49 50 51
    init {
        lifecycleOwner.lifecycle.addObserver(this)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemIntroHolder {
        val binding = DataBindingUtil.inflate<ItemExpertIntroBinding>(
            LayoutInflater.from(parent.context),
            R.layout.item_expert_intro,
            parent,
            false
        )
万齐军 committed
52
        return ItemIntroHolder(binding)
53 54 55 56
    }

    override fun onBindViewHolder(holder: ItemIntroHolder, position: Int) {
        val item = data[position]
57
        holder.onBind(item)
58 59 60 61 62 63 64
        holder.binding.item = item
    }


    override fun onViewAttachedToWindow(holder: ItemIntroHolder) {
        val adapterPosition = holder.adapterPosition
        val videoView = IjkVideoView(context)
万齐军 committed
65 66 67 68
        val playUrl = data[adapterPosition].playUrl
        if (!playUrl.isNullOrBlank()) {
            if (hasAgreePlayWithoutWiFi || ConfideNetworkUtil.isWifi(context)) {
                videoView.setVideoURI(Uri.parse(playUrl))
69 70 71
                if (curPos == adapterPosition) {
                    videoView.start()
                }
万齐军 committed
72 73 74
            } else {
                videoView.tag = playUrl
            }
75
        }
76 77 78 79
        videoViews.put(adapterPosition, videoView)
        holder.onAttach(videoView)
    }

80 81 82 83 84 85 86 87 88
    internal fun checkNetwork() {
        if (!ConfideNetworkUtil.isWifi(context)) {
            val dialog = CommonDialog.create(context)
                .setTitle(context.getString(R.string.confide_tip))
                .setMessage(context.getString(R.string.confide_video_wifi_tip))
                .setLeftOnclick("继续播放") {
                    hasAgreePlayWithoutWiFi = true
                    for (entry in videoViews.entries) {
                        val value = entry.value
万齐军 committed
89 90 91
                        val playUrl = value.tag as? String
                        if (!playUrl.isNullOrBlank()) {
                            value.setVideoURI(Uri.parse(playUrl))
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
                            if (curPos == entry.key) {
                                value.start()
                            }
                        }
                    }
                }
                .setLeftButton_color(R.color.platform_but_text_color_selected)
                .setRightButton_color(R.color.platform_text_bright_color)
                .setRightClick("取消") {
                    if (context is Activity) {
                        context.onBackPressed()
                    }
                }
                .setCancelAble(true)
            dialog.setOnCancelListener {
                if (context is Activity) {
                    context.onBackPressed()
                }
            }
            dialog.show()
        }
    }

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
    override fun onViewDetachedFromWindow(holder: ItemIntroHolder) {
        super.onViewDetachedFromWindow(holder)
        val adapterPosition = holder.adapterPosition
        val video = videoViews.get(adapterPosition)
        video?.release(true)
        videoViews.remove(adapterPosition)
        holder.onDetach()
    }

    override fun getItemCount(): Int {
        return data.size
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
    private fun onDestroy() {
        for (entry in videoViews.entries) {
            entry.value.release(true)
        }
    }

135 136 137 138 139 140 141 142 143 144 145 146
    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    private fun onResume() {
        val ijkVideoView = videoViews[curPos]
        ijkVideoView?.start()
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    private fun onPause() {
        val ijkVideoView = videoViews[curPos]
        ijkVideoView?.pause()
    }

147
    fun onSelect(position: Int) {
148
        curPos = position
万齐军 committed
149 150 151 152 153 154
        onLoadDialStatus(position)
        if (!ConfideNetworkUtil.isWifi(context) && !hasAgreePlayWithoutWiFi) {
            return
        }
        for (entry in videoViews.entries) {
            if (entry.key == position) {
155
//                entry.value.seekTo(0)
万齐军 committed
156 157
                entry.value.start()
            } else {
158
                entry.value.seekTo(0)
万齐军 committed
159 160 161 162 163 164
                entry.value.pause()
            }
        }
    }

    private fun onLoadDialStatus(position: Int) {
165 166 167 168 169 170 171 172 173 174
        val confideApi = YDLHttpUtils.obtainApi(ConfideHomeApi::class.java)
        val curUid = data[position].uid
        if (curUid != null) {
            dispose?.dispose()
            dispose = confideApi.getDialStatus(curUid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ resp ->
                    if (resp.code == "200") {
                        val lineStatus = resp.data?.confideLine ?: 2
175 176 177 178 179 180 181 182 183 184 185 186
                        if (lineStatus == 4) {//继续倾诉
                            val t = resp?.data?.remainingTime?.remainingTime
                            if (t != null) {
                                val remain = TimeUtil.getElapseTimeForShow(t * 1000)
                                data[position].remainingTime.set(
                                    context.getString(
                                        R.string.confide_tip_remain_time,
                                        remain
                                    )
                                )
                            }
                        }
187 188 189 190 191 192 193 194
                        data[position].lineStatus.set(lineStatus)
                    } else {
                        if (!resp.msg.isNullOrEmpty()) {
                            ToastUtil.toastShort(resp.msg)
                        }
                    }
                }, { throwable -> throwable.printStackTrace() })
        }
195
    }
万齐军 committed
196

197
}