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

102 lines
4.1 KiB
Kotlin
Raw Normal View History

2022-02-12 20:00:36 +05:30
package com.github.libretube.adapters
import android.os.Bundle
2022-02-15 02:17:50 +05:30
import android.text.format.DateUtils
2022-02-12 20:00:36 +05:30
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.constraintlayout.motion.widget.MotionLayout
import androidx.core.os.bundleOf
import androidx.fragment.app.FragmentManager
2022-02-12 20:00:36 +05:30
import androidx.recyclerview.widget.RecyclerView
2022-05-27 07:11:42 +05:30
import com.github.libretube.R
2022-07-01 20:25:21 +05:30
import com.github.libretube.activities.MainActivity
import com.github.libretube.databinding.TrendingRowBinding
2022-06-03 00:40:16 +05:30
import com.github.libretube.dialogs.VideoOptionsDialog
import com.github.libretube.fragments.PlayerFragment
2022-05-10 22:05:51 +05:30
import com.github.libretube.obj.StreamItem
2022-06-10 18:33:48 +05:30
import com.github.libretube.util.formatShort
2022-05-10 22:05:51 +05:30
import com.squareup.picasso.Picasso
2022-02-12 20:00:36 +05:30
class SubscriptionAdapter(
private val videoFeed: List<StreamItem>,
private val childFragmentManager: FragmentManager
) : RecyclerView.Adapter<SubscriptionViewHolder>() {
private val TAG = "SubscriptionAdapter"
2022-02-14 09:54:46 +05:30
var i = 0
2022-02-12 20:00:36 +05:30
override fun getItemCount(): Int {
return i
}
2022-05-20 03:52:10 +05:30
fun updateItems() {
2022-02-12 20:00:36 +05:30
i += 10
2022-06-24 20:56:36 +05:30
if (i > videoFeed.size) {
2022-05-20 03:52:10 +05:30
i = videoFeed.size
2022-06-24 20:56:36 +05:30
}
2022-02-12 20:00:36 +05:30
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SubscriptionViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
2022-07-02 20:31:24 +05:30
val binding = TrendingRowBinding.inflate(layoutInflater, parent, false)
return SubscriptionViewHolder(binding)
2022-02-12 20:00:36 +05:30
}
override fun onBindViewHolder(holder: SubscriptionViewHolder, position: Int) {
val trending = videoFeed[position]
2022-07-02 20:31:24 +05:30
holder.binding.apply {
textViewTitle.text = trending.title
textViewChannel.text =
trending.uploaderName + "" +
trending.views.formatShort() + "" +
DateUtils.getRelativeTimeSpanString(trending.uploaded!!)
if (trending.duration != -1L) {
thumbnailDuration.text = DateUtils.formatElapsedTime(trending.duration!!)
} else {
2022-07-02 20:31:24 +05:30
thumbnailDuration.text = root.context.getString(R.string.live)
thumbnailDuration.setBackgroundColor(R.attr.colorPrimaryDark)
}
channelImage.setOnClickListener {
2022-07-02 20:31:24 +05:30
val activity = root.context as MainActivity
val bundle = bundleOf("channel_id" to trending.uploaderUrl)
2022-07-01 21:59:47 +05:30
activity.navController.navigate(R.id.channelFragment, bundle)
try {
2022-07-01 15:41:30 +05:30
val mainMotionLayout =
activity.findViewById<MotionLayout>(R.id.mainMotionLayout)
if (mainMotionLayout.progress == 0.toFloat()) {
mainMotionLayout.transitionToEnd()
2022-07-01 15:41:30 +05:30
activity.findViewById<MotionLayout>(R.id.playerMotionLayout)
.transitionToEnd()
}
} catch (e: Exception) {
2022-02-12 20:00:36 +05:30
}
}
Picasso.get().load(trending.thumbnail).into(thumbnail)
Picasso.get().load(trending.uploaderAvatar).into(channelImage)
root.setOnClickListener {
val bundle = Bundle()
bundle.putString("videoId", trending.url!!.replace("/watch?v=", ""))
val frag = PlayerFragment()
frag.arguments = bundle
2022-07-02 20:31:24 +05:30
val activity = root.context as AppCompatActivity
activity.supportFragmentManager.beginTransaction()
.remove(PlayerFragment())
.commit()
activity.supportFragmentManager.beginTransaction()
.replace(R.id.container, frag)
.commitNow()
}
root.setOnLongClickListener {
val videoId = trending.url!!.replace("/watch?v=", "")
2022-07-02 20:31:24 +05:30
VideoOptionsDialog(videoId, root.context)
.show(childFragmentManager, VideoOptionsDialog.TAG)
true
}
}
2022-02-12 20:00:36 +05:30
}
}
2022-05-21 13:32:04 +05:30
2022-07-02 21:53:24 +05:30
class SubscriptionViewHolder(val binding: TrendingRowBinding) :
RecyclerView.ViewHolder(binding.root)