Merge pull request #857 from Bnyro/master

scroll to current chapter and highlight it
This commit is contained in:
Bnyro 2022-07-22 23:59:11 +02:00 committed by GitHub
commit be2a40b791
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 68 additions and 30 deletions

View File

@ -1,11 +1,13 @@
package com.github.libretube.adapters package com.github.libretube.adapters
import android.graphics.Color
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.ChapterColumnBinding import com.github.libretube.databinding.ChapterColumnBinding
import com.github.libretube.obj.ChapterSegment import com.github.libretube.obj.ChapterSegment
import com.github.libretube.util.ConnectionHelper import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.ThemeHelper
import com.google.android.exoplayer2.ExoPlayer import com.google.android.exoplayer2.ExoPlayer
class ChaptersAdapter( class ChaptersAdapter(
@ -13,6 +15,7 @@ class ChaptersAdapter(
private val exoPlayer: ExoPlayer private val exoPlayer: ExoPlayer
) : RecyclerView.Adapter<ChaptersViewHolder>() { ) : RecyclerView.Adapter<ChaptersViewHolder>() {
val TAG = "ChaptersAdapter" val TAG = "ChaptersAdapter"
private var selectedPosition = 0
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChaptersViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChaptersViewHolder {
val layoutInflater = LayoutInflater.from(parent.context) val layoutInflater = LayoutInflater.from(parent.context)
@ -26,13 +29,26 @@ class ChaptersAdapter(
ConnectionHelper.loadImage(chapter.image, chapterImage) ConnectionHelper.loadImage(chapter.image, chapterImage)
chapterTitle.text = chapter.title chapterTitle.text = chapter.title
if (selectedPosition == position) {
// get the color for highlighted controls
val color = ThemeHelper.getThemeColor(root.context, android.R.attr.colorControlHighlight)
chapterLL.setBackgroundColor(color)
} else chapterLL.setBackgroundColor(Color.TRANSPARENT)
root.setOnClickListener { root.setOnClickListener {
updateSelectedPosition(position)
val chapterStart = chapter.start!! * 1000 // s -> ms val chapterStart = chapter.start!! * 1000 // s -> ms
exoPlayer.seekTo(chapterStart) exoPlayer.seekTo(chapterStart)
} }
} }
} }
fun updateSelectedPosition(newPosition: Int) {
val oldPosition = selectedPosition
selectedPosition = newPosition
notifyItemChanged(oldPosition)
notifyItemChanged(newPosition)
}
override fun getItemCount(): Int { override fun getItemCount(): Int {
return chapters.size return chapters.size
} }

View File

@ -628,6 +628,15 @@ class PlayerFragment : Fragment() {
binding.playerDescriptionArrow.animate().rotation(180F).setDuration(250).start() binding.playerDescriptionArrow.animate().rotation(180F).setDuration(250).start()
binding.descLinLayout.visibility = View.VISIBLE binding.descLinLayout.visibility = View.VISIBLE
} }
if (this::chapters.isInitialized && chapters.isNotEmpty()) {
val chapterIndex = getCurrentChapterIndex()
// scroll to the current chapter in the chapterRecView in the description
val layoutManager = binding.chaptersRecView.layoutManager as LinearLayoutManager
layoutManager.scrollToPositionWithOffset(chapterIndex, 0)
// set selected
val chaptersAdapter = binding.chaptersRecView.adapter as ChaptersAdapter
chaptersAdapter.updateSelectedPosition(chapterIndex)
}
} }
private fun toggleComments() { private fun toggleComments() {
@ -1256,27 +1265,31 @@ class PlayerFragment : Fragment() {
// call the function again in 100ms // call the function again in 100ms
exoPlayerView.postDelayed(this::setCurrentChapterName, 100) exoPlayerView.postDelayed(this::setCurrentChapterName, 100)
val chapterName = getCurrentChapterName() val chapterIndex = getCurrentChapterIndex()
val chapterName = chapters[chapterIndex].title
// change the chapter name textView text to the chapterName // change the chapter name textView text to the chapterName
if (chapterName != null && chapterName != playerBinding.chapterName.text) { if (chapterName != playerBinding.chapterName.text) {
playerBinding.chapterName.text = chapterName playerBinding.chapterName.text = chapterName
// update the selected item
val chaptersAdapter = binding.chaptersRecView.adapter as ChaptersAdapter
chaptersAdapter.updateSelectedPosition(chapterIndex)
} }
} }
// get the name of the currently played chapter // get the name of the currently played chapter
private fun getCurrentChapterName(): String? { private fun getCurrentChapterIndex(): Int {
val currentPosition = exoPlayer.currentPosition val currentPosition = exoPlayer.currentPosition
var chapterName: String? = null var chapterIndex: Int? = null
chapters.forEach { chapters.forEachIndexed { index, chapter ->
// check whether the chapter start is greater than the current player position // check whether the chapter start is greater than the current player position
if (currentPosition >= it.start!! * 1000) { if (currentPosition >= chapter.start!! * 1000) {
// save chapter title if found // save chapter title if found
chapterName = it.title chapterIndex = index
} }
} }
return chapterName return chapterIndex!!
} }
private fun setMediaSource( private fun setMediaSource(

View File

@ -111,7 +111,7 @@ object ThemeHelper {
android.os.Process.killProcess(android.os.Process.myPid()) android.os.Process.killProcess(android.os.Process.myPid())
} }
private fun getThemeColor(context: Context, colorCode: Int): Int { fun getThemeColor(context: Context, colorCode: Int): Int {
val value = TypedValue() val value = TypedValue()
context.theme.resolveAttribute(colorCode, value, true) context.theme.resolveAttribute(colorCode, value, true)
return value.data return value.data

View File

@ -1,9 +1,16 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="100dp" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="match_parent"
android:background="?attr/selectableItemBackground" android:background="?attr/selectableItemBackground"
android:backgroundTint="@android:color/transparent"
app:strokeWidth="0dp">
<LinearLayout
android:id="@+id/chapterLL"
android:layout_width="100dp"
android:layout_height="match_parent"
android:orientation="vertical" android:orientation="vertical"
android:paddingHorizontal="5dp"> android:paddingHorizontal="5dp">
@ -24,4 +31,6 @@
android:text="Title" android:text="Title"
android:textSize="13sp" /> android:textSize="13sp" />
</LinearLayout> </LinearLayout>
</com.google.android.material.card.MaterialCardView>