highlight current chapter

This commit is contained in:
Bnyro 2022-07-22 23:58:23 +02:00
parent 9050874561
commit 9e09b3a634
4 changed files with 56 additions and 24 deletions

View File

@ -1,11 +1,13 @@
package com.github.libretube.adapters
import android.graphics.Color
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.databinding.ChapterColumnBinding
import com.github.libretube.obj.ChapterSegment
import com.github.libretube.util.ConnectionHelper
import com.github.libretube.util.ThemeHelper
import com.google.android.exoplayer2.ExoPlayer
class ChaptersAdapter(
@ -13,6 +15,7 @@ class ChaptersAdapter(
private val exoPlayer: ExoPlayer
) : RecyclerView.Adapter<ChaptersViewHolder>() {
val TAG = "ChaptersAdapter"
private var selectedPosition = 0
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ChaptersViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
@ -26,13 +29,26 @@ class ChaptersAdapter(
ConnectionHelper.loadImage(chapter.image, chapterImage)
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 {
updateSelectedPosition(position)
val chapterStart = chapter.start!! * 1000 // s -> ms
exoPlayer.seekTo(chapterStart)
}
}
}
fun updateSelectedPosition(newPosition: Int) {
val oldPosition = selectedPosition
selectedPosition = newPosition
notifyItemChanged(oldPosition)
notifyItemChanged(newPosition)
}
override fun getItemCount(): Int {
return chapters.size
}

View File

@ -628,10 +628,14 @@ class PlayerFragment : Fragment() {
binding.playerDescriptionArrow.animate().rotation(180F).setDuration(250).start()
binding.descLinLayout.visibility = View.VISIBLE
}
if (chapters.isNotEmpty()) {
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(getCurrentChapterIndex(), 0)
layoutManager.scrollToPositionWithOffset(chapterIndex, 0)
// set selected
val chaptersAdapter = binding.chaptersRecView.adapter as ChaptersAdapter
chaptersAdapter.updateSelectedPosition(chapterIndex)
}
}
@ -1267,6 +1271,9 @@ class PlayerFragment : Fragment() {
// change the chapter name textView text to the chapterName
if (chapterName != playerBinding.chapterName.text) {
playerBinding.chapterName.text = chapterName
// update the selected item
val chaptersAdapter = binding.chaptersRecView.adapter as ChaptersAdapter
chaptersAdapter.updateSelectedPosition(chapterIndex)
}
}

View File

@ -111,7 +111,7 @@ object ThemeHelper {
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()
context.theme.resolveAttribute(colorCode, value, true)
return value.data

View File

@ -1,27 +1,36 @@
<?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"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
android:orientation="vertical"
android:paddingHorizontal="5dp">
android:backgroundTint="@android:color/transparent"
app:strokeWidth="0dp">
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/chapter_image"
android:layout_width="match_parent"
android:layout_height="55dp"
android:src="@mipmap/ic_launcher"
app:shapeAppearanceOverlay="@style/roundedImageViewRounded" />
<LinearLayout
android:id="@+id/chapterLL"
android:layout_width="100dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingHorizontal="5dp">
<TextView
android:id="@+id/chapter_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:ellipsize="end"
android:maxLines="3"
android:text="Title"
android:textSize="13sp" />
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/chapter_image"
android:layout_width="match_parent"
android:layout_height="55dp"
android:src="@mipmap/ic_launcher"
app:shapeAppearanceOverlay="@style/roundedImageViewRounded" />
</LinearLayout>
<TextView
android:id="@+id/chapter_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:ellipsize="end"
android:maxLines="3"
android:text="Title"
android:textSize="13sp" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>