From 9e09b3a634bc2347284b5866d62e52ebc9e0222b Mon Sep 17 00:00:00 2001 From: Bnyro Date: Fri, 22 Jul 2022 23:58:23 +0200 Subject: [PATCH] highlight current chapter --- .../libretube/adapters/ChaptersAdapter.kt | 16 ++++++ .../libretube/fragments/PlayerFragment.kt | 11 +++- .../com/github/libretube/util/ThemeHelper.kt | 2 +- app/src/main/res/layout/chapter_column.xml | 51 +++++++++++-------- 4 files changed, 56 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt b/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt index c595c08d0..7bb0abeaf 100644 --- a/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/ChaptersAdapter.kt @@ -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() { 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 } diff --git a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt index 7f824000f..eca462cf9 100644 --- a/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/PlayerFragment.kt @@ -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) } } diff --git a/app/src/main/java/com/github/libretube/util/ThemeHelper.kt b/app/src/main/java/com/github/libretube/util/ThemeHelper.kt index 10e56715f..4637f57ea 100644 --- a/app/src/main/java/com/github/libretube/util/ThemeHelper.kt +++ b/app/src/main/java/com/github/libretube/util/ThemeHelper.kt @@ -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 diff --git a/app/src/main/res/layout/chapter_column.xml b/app/src/main/res/layout/chapter_column.xml index 595130de6..d8dc611a4 100644 --- a/app/src/main/res/layout/chapter_column.xml +++ b/app/src/main/res/layout/chapter_column.xml @@ -1,27 +1,36 @@ - + android:backgroundTint="@android:color/transparent" + app:strokeWidth="0dp"> - + - + - \ No newline at end of file + + + + + \ No newline at end of file