LibreTube/app/src/main/java/com/github/libretube/adapters/ChannelAdapter.kt

64 lines
2.4 KiB
Kotlin
Raw Normal View History

2022-02-05 15:00:29 +05:30
package com.github.libretube.adapters
2022-02-15 02:17:50 +05:30
import android.text.format.DateUtils
2022-02-05 15:00:29 +05:30
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.fragment.app.FragmentManager
2022-02-05 15:00:29 +05:30
import androidx.recyclerview.widget.RecyclerView
2022-07-28 11:32:21 +05:30
import com.github.libretube.databinding.VideoRowBinding
import com.github.libretube.dialogs.VideoOptionsDialog
2022-05-10 22:05:51 +05:30
import com.github.libretube.obj.StreamItem
2022-07-16 01:07:44 +05:30
import com.github.libretube.util.ConnectionHelper
2022-07-20 01:01:56 +05:30
import com.github.libretube.util.NavigationHelper
2022-06-10 18:33:48 +05:30
import com.github.libretube.util.formatShort
2022-07-28 12:48:32 +05:30
import com.github.libretube.util.setWatchProgressLength
2022-07-29 12:30:13 +05:30
import com.github.libretube.util.toID
2022-02-05 15:00:29 +05:30
class ChannelAdapter(
private val videoFeed: MutableList<StreamItem>,
private val childFragmentManager: FragmentManager
) :
2022-05-21 13:32:04 +05:30
RecyclerView.Adapter<ChannelViewHolder>() {
2022-07-01 15:07:14 +05:30
2022-02-05 15:00:29 +05:30
override fun getItemCount(): Int {
return videoFeed.size
}
2022-05-21 13:32:04 +05:30
2022-05-20 03:52:10 +05:30
fun updateItems(newItems: List<StreamItem>) {
2022-08-01 12:25:38 +05:30
val feedSize = videoFeed.size
2022-02-05 19:39:50 +05:30
videoFeed.addAll(newItems)
2022-08-01 12:25:38 +05:30
notifyItemRangeInserted(feedSize, newItems.size)
2022-02-05 19:39:50 +05:30
}
2022-02-05 15:00:29 +05:30
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChannelViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
2022-07-28 11:32:21 +05:30
val binding = VideoRowBinding.inflate(layoutInflater, parent, false)
2022-07-02 20:31:24 +05:30
return ChannelViewHolder(binding)
2022-02-05 15:00:29 +05:30
}
override fun onBindViewHolder(holder: ChannelViewHolder, position: Int) {
val trending = videoFeed[position]
2022-07-02 20:31:24 +05:30
holder.binding.apply {
2022-07-28 11:32:21 +05:30
videoTitle.text = trending.title
videoInfo.text =
2022-07-01 15:07:14 +05:30
trending.views.formatShort() + "" +
2022-07-01 15:41:30 +05:30
DateUtils.getRelativeTimeSpanString(trending.uploaded!!)
2022-07-28 11:32:21 +05:30
thumbnailDuration.text =
2022-07-01 15:07:14 +05:30
DateUtils.formatElapsedTime(trending.duration!!)
2022-07-28 11:32:21 +05:30
ConnectionHelper.loadImage(trending.thumbnail, thumbnail)
2022-07-01 15:07:14 +05:30
root.setOnClickListener {
2022-07-20 01:01:56 +05:30
NavigationHelper.navigateVideo(root.context, trending.url)
2022-07-01 15:07:14 +05:30
}
2022-07-29 12:30:13 +05:30
val videoId = trending.url.toID()
2022-07-01 15:07:14 +05:30
root.setOnLongClickListener {
2022-07-02 20:31:24 +05:30
VideoOptionsDialog(videoId, root.context)
2022-07-19 01:12:50 +05:30
.show(childFragmentManager, "VideoOptionsDialog")
2022-07-01 15:07:14 +05:30
true
}
2022-07-28 12:48:32 +05:30
watchProgress.setWatchProgressLength(videoId, trending.duration!!)
}
2022-02-05 15:00:29 +05:30
}
}
2022-05-21 13:32:04 +05:30
2022-07-28 11:32:21 +05:30
class ChannelViewHolder(val binding: VideoRowBinding) : RecyclerView.ViewHolder(binding.root)