mirror of
https://github.com/libre-tube/LibreTube.git
synced 2024-12-14 22:30:30 +05:30
highlight current chapter
This commit is contained in:
parent
9050874561
commit
9e09b3a634
@ -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
|
||||||
}
|
}
|
||||||
|
@ -628,10 +628,14 @@ 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 (chapters.isNotEmpty()) {
|
if (this::chapters.isInitialized && chapters.isNotEmpty()) {
|
||||||
|
val chapterIndex = getCurrentChapterIndex()
|
||||||
// scroll to the current chapter in the chapterRecView in the description
|
// scroll to the current chapter in the chapterRecView in the description
|
||||||
val layoutManager = binding.chaptersRecView.layoutManager as LinearLayoutManager
|
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
|
// change the chapter name textView text to the chapterName
|
||||||
if (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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -1,27 +1,36 @@
|
|||||||
<?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:orientation="vertical"
|
android:backgroundTint="@android:color/transparent"
|
||||||
android:paddingHorizontal="5dp">
|
app:strokeWidth="0dp">
|
||||||
|
|
||||||
<com.google.android.material.imageview.ShapeableImageView
|
<LinearLayout
|
||||||
android:id="@+id/chapter_image"
|
android:id="@+id/chapterLL"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="100dp"
|
||||||
android:layout_height="55dp"
|
android:layout_height="match_parent"
|
||||||
android:src="@mipmap/ic_launcher"
|
android:orientation="vertical"
|
||||||
app:shapeAppearanceOverlay="@style/roundedImageViewRounded" />
|
android:paddingHorizontal="5dp">
|
||||||
|
|
||||||
<TextView
|
<com.google.android.material.imageview.ShapeableImageView
|
||||||
android:id="@+id/chapter_title"
|
android:id="@+id/chapter_image"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="55dp"
|
||||||
android:layout_marginTop="3dp"
|
android:src="@mipmap/ic_launcher"
|
||||||
android:ellipsize="end"
|
app:shapeAppearanceOverlay="@style/roundedImageViewRounded" />
|
||||||
android:maxLines="3"
|
|
||||||
android:text="Title"
|
|
||||||
android:textSize="13sp" />
|
|
||||||
|
|
||||||
</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>
|
Loading…
Reference in New Issue
Block a user