From 150699f64d137912794fb64d40cf836f8c391d03 Mon Sep 17 00:00:00 2001 From: Koitharu Date: Sun, 13 Mar 2022 18:37:11 +0200 Subject: [PATCH] Improve thumbnails bs --- .../kotatsu/details/ui/ChaptersFragment.kt | 13 ++++++++++- .../kotatsu/reader/ui/ChaptersBottomSheet.kt | 11 ++------- .../ui/thumbnails/PagesThumbnailsSheet.kt | 23 ++++++++----------- .../ui/thumbnails/adapter/PageThumbnailAD.kt | 7 ++++-- .../adapter/SearchSuggestionsMangaListAD.kt | 4 ++-- .../utils/RecyclerViewScrollCallback.kt | 21 +++++++++++++++++ .../kotatsu/utils/ScrollResetCallback.kt | 13 ----------- .../koitharu/kotatsu/utils/ext/TextViewExt.kt | 9 ++++++++ app/src/main/res/drawable/bg_badge_empty.xml | 15 ++++++++++++ app/src/main/res/layout/item_page_thumb.xml | 16 +++++-------- 10 files changed, 81 insertions(+), 51 deletions(-) create mode 100644 app/src/main/java/org/koitharu/kotatsu/utils/RecyclerViewScrollCallback.kt delete mode 100644 app/src/main/java/org/koitharu/kotatsu/utils/ScrollResetCallback.kt create mode 100644 app/src/main/res/drawable/bg_badge_empty.xml diff --git a/app/src/main/java/org/koitharu/kotatsu/details/ui/ChaptersFragment.kt b/app/src/main/java/org/koitharu/kotatsu/details/ui/ChaptersFragment.kt index 0048e8fe5..ac2b0e163 100644 --- a/app/src/main/java/org/koitharu/kotatsu/details/ui/ChaptersFragment.kt +++ b/app/src/main/java/org/koitharu/kotatsu/details/ui/ChaptersFragment.kt @@ -25,6 +25,7 @@ import org.koitharu.kotatsu.details.ui.model.ChapterListItem import org.koitharu.kotatsu.download.ui.service.DownloadService import org.koitharu.kotatsu.reader.ui.ReaderActivity import org.koitharu.kotatsu.reader.ui.ReaderState +import org.koitharu.kotatsu.utils.RecyclerViewScrollCallback class ChaptersFragment : BaseFragment(), OnListItemClickListener, @@ -211,7 +212,17 @@ class ChaptersFragment : BaseFragment(), } private fun onChaptersChanged(list: List) { - chaptersAdapter?.items = list + val adapter = chaptersAdapter ?: return + if (adapter.itemCount == 0) { + val position = list.indexOfFirst { it.hasFlag(ChapterListItem.FLAG_CURRENT) } - 1 + if (position > 0) { + adapter.setItems(list, RecyclerViewScrollCallback(binding.recyclerViewChapters, position)) + } else { + adapter.items = list + } + } else { + adapter.items = list + } } private fun onLoadingStateChanged(isLoading: Boolean) { diff --git a/app/src/main/java/org/koitharu/kotatsu/reader/ui/ChaptersBottomSheet.kt b/app/src/main/java/org/koitharu/kotatsu/reader/ui/ChaptersBottomSheet.kt index 3b079255a..565538456 100644 --- a/app/src/main/java/org/koitharu/kotatsu/reader/ui/ChaptersBottomSheet.kt +++ b/app/src/main/java/org/koitharu/kotatsu/reader/ui/ChaptersBottomSheet.kt @@ -5,7 +5,6 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.fragment.app.FragmentManager -import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.google.android.material.divider.MaterialDividerItemDecoration import org.koin.android.ext.android.get @@ -19,6 +18,7 @@ import org.koitharu.kotatsu.details.ui.adapter.ChaptersAdapter import org.koitharu.kotatsu.details.ui.model.ChapterListItem import org.koitharu.kotatsu.details.ui.model.toListItem import org.koitharu.kotatsu.utils.BottomSheetToolbarController +import org.koitharu.kotatsu.utils.RecyclerViewScrollCallback import org.koitharu.kotatsu.utils.ext.withArgs class ChaptersBottomSheet : BaseBottomSheet(), OnListItemClickListener { @@ -58,7 +58,7 @@ class ChaptersBottomSheet : BaseBottomSheet(), OnListItemC binding.recyclerView.adapter = ChaptersAdapter(this).also { adapter -> if (currentPosition >= 0) { val targetPosition = (currentPosition - 1).coerceAtLeast(0) - adapter.setItems(items, Scroller(binding.recyclerView, targetPosition)) + adapter.setItems(items, RecyclerViewScrollCallback(binding.recyclerView, targetPosition)) } else { adapter.items = items } @@ -77,13 +77,6 @@ class ChaptersBottomSheet : BaseBottomSheet(), OnListItemC fun onChapterChanged(chapter: MangaChapter) } - private class Scroller(private val recyclerView: RecyclerView, private val position: Int) : Runnable { - override fun run() { - val offset = recyclerView.resources.getDimensionPixelSize(R.dimen.chapter_list_item_height) / 2 - (recyclerView.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(position, offset) - } - } - companion object { private const val ARG_CHAPTERS = "chapters" diff --git a/app/src/main/java/org/koitharu/kotatsu/reader/ui/thumbnails/PagesThumbnailsSheet.kt b/app/src/main/java/org/koitharu/kotatsu/reader/ui/thumbnails/PagesThumbnailsSheet.kt index 314b281dc..cbd82c22d 100644 --- a/app/src/main/java/org/koitharu/kotatsu/reader/ui/thumbnails/PagesThumbnailsSheet.kt +++ b/app/src/main/java/org/koitharu/kotatsu/reader/ui/thumbnails/PagesThumbnailsSheet.kt @@ -6,7 +6,7 @@ import android.view.View import android.view.ViewGroup import androidx.appcompat.widget.Toolbar import androidx.fragment.app.FragmentManager -import androidx.recyclerview.widget.RecyclerView +import androidx.recyclerview.widget.GridLayoutManager import com.google.android.material.bottomsheet.BottomSheetBehavior import org.koin.android.ext.android.get import org.koitharu.kotatsu.R @@ -28,6 +28,7 @@ class PagesThumbnailsSheet : BaseBottomSheet(), private lateinit var thumbnails: List private val spanResolver = MangaListSpanResolver() + private var currentPageIndex = -1 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -36,12 +37,12 @@ class PagesThumbnailsSheet : BaseBottomSheet(), dismissAllowingStateLoss() return } - val current = arguments?.getInt(ARG_CURRENT, -1) ?: -1 + currentPageIndex = requireArguments().getInt(ARG_CURRENT, currentPageIndex) val repository = mangaRepositoryOf(pages.first().source) thumbnails = pages.mapIndexed { i, x -> PageThumbnail( number = i + 1, - isCurrent = i == current, + isCurrent = i == currentPageIndex, repository = repository, page = x ) @@ -68,11 +69,9 @@ class PagesThumbnailsSheet : BaseBottomSheet(), resources.getQuantityString(R.plurals.pages, thumbnails.size, thumbnails.size) } - val initialTopPosition = binding.recyclerView.top - with(binding.recyclerView) { addItemDecoration( - SpacingItemDecoration(view.resources.getDimensionPixelOffset(R.dimen.grid_spacing)) + SpacingItemDecoration(resources.getDimensionPixelOffset(R.dimen.grid_spacing)) ) adapter = PageThumbnailAdapter( thumbnails, @@ -81,16 +80,12 @@ class PagesThumbnailsSheet : BaseBottomSheet(), get(), this@PagesThumbnailsSheet ) - addOnScrollListener(object : RecyclerView.OnScrollListener() { - override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) = Unit - - override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { - super.onScrolled(recyclerView, dx, dy) - binding.appbar.isLifted = getChildAt(0).top < initialTopPosition - } - }) addOnLayoutChangeListener(spanResolver) spanResolver.setGridSize(get().gridSize / 100f, this) + if (currentPageIndex > 0) { + val offset = resources.getDimensionPixelOffset(R.dimen.preferred_grid_width) + (layoutManager as GridLayoutManager).scrollToPositionWithOffset(currentPageIndex, offset) + } } } diff --git a/app/src/main/java/org/koitharu/kotatsu/reader/ui/thumbnails/adapter/PageThumbnailAD.kt b/app/src/main/java/org/koitharu/kotatsu/reader/ui/thumbnails/adapter/PageThumbnailAD.kt index 2ae446b4f..6f06eae33 100644 --- a/app/src/main/java/org/koitharu/kotatsu/reader/ui/thumbnails/adapter/PageThumbnailAD.kt +++ b/app/src/main/java/org/koitharu/kotatsu/reader/ui/thumbnails/adapter/PageThumbnailAD.kt @@ -14,6 +14,8 @@ import org.koitharu.kotatsu.local.data.PagesCache import org.koitharu.kotatsu.reader.ui.thumbnails.PageThumbnail import org.koitharu.kotatsu.utils.ext.IgnoreErrors import org.koitharu.kotatsu.utils.ext.referer +import org.koitharu.kotatsu.utils.ext.setTextColorAttr +import com.google.android.material.R as materialR fun pageThumbnailAD( coil: ImageLoader, @@ -31,7 +33,7 @@ fun pageThumbnailAD( height = (gridWidth * 13f / 18f).toInt() ) - binding.handle.setOnClickListener { + binding.root.setOnClickListener { clickListener.onItemClick(item.page, itemView) } @@ -39,7 +41,8 @@ fun pageThumbnailAD( job?.cancel() binding.imageViewThumb.setImageDrawable(null) with(binding.textViewNumber) { - setBackgroundResource(if (item.isCurrent) R.drawable.bg_badge_accent else R.drawable.bg_badge_default) + setBackgroundResource(if (item.isCurrent) R.drawable.bg_badge_accent else R.drawable.bg_badge_empty) + setTextColorAttr(if (item.isCurrent) materialR.attr.colorOnTertiary else android.R.attr.textColorPrimary) text = (item.number).toString() } job = scope.launch(Dispatchers.Default + IgnoreErrors) { diff --git a/app/src/main/java/org/koitharu/kotatsu/search/ui/suggestion/adapter/SearchSuggestionsMangaListAD.kt b/app/src/main/java/org/koitharu/kotatsu/search/ui/suggestion/adapter/SearchSuggestionsMangaListAD.kt index 9c3eb37b1..549baf947 100644 --- a/app/src/main/java/org/koitharu/kotatsu/search/ui/suggestion/adapter/SearchSuggestionsMangaListAD.kt +++ b/app/src/main/java/org/koitharu/kotatsu/search/ui/suggestion/adapter/SearchSuggestionsMangaListAD.kt @@ -15,7 +15,7 @@ import org.koitharu.kotatsu.core.model.Manga import org.koitharu.kotatsu.databinding.ItemSearchSuggestionMangaGridBinding import org.koitharu.kotatsu.search.ui.suggestion.SearchSuggestionListener import org.koitharu.kotatsu.search.ui.suggestion.model.SearchSuggestionItem -import org.koitharu.kotatsu.utils.ScrollResetCallback +import org.koitharu.kotatsu.utils.RecyclerViewScrollCallback import org.koitharu.kotatsu.utils.ext.enqueueWith import org.koitharu.kotatsu.utils.ext.newImageRequest @@ -37,7 +37,7 @@ fun searchSuggestionMangaListAD( right = recyclerView.paddingRight - spacing, ) recyclerView.addItemDecoration(SpacingItemDecoration(spacing)) - val scrollResetCallback = ScrollResetCallback(recyclerView) + val scrollResetCallback = RecyclerViewScrollCallback(recyclerView, 0) bind { adapter.setItems(item.items, scrollResetCallback) diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/RecyclerViewScrollCallback.kt b/app/src/main/java/org/koitharu/kotatsu/utils/RecyclerViewScrollCallback.kt new file mode 100644 index 000000000..984867ae4 --- /dev/null +++ b/app/src/main/java/org/koitharu/kotatsu/utils/RecyclerViewScrollCallback.kt @@ -0,0 +1,21 @@ +package org.koitharu.kotatsu.utils + +import androidx.recyclerview.widget.LinearLayoutManager +import androidx.recyclerview.widget.RecyclerView +import java.lang.ref.WeakReference + +class RecyclerViewScrollCallback(recyclerView: RecyclerView, private val position: Int) : Runnable { + + private val recyclerViewRef = WeakReference(recyclerView) + + override fun run() { + val rv = recyclerViewRef.get() ?: return + val lm = rv.layoutManager ?: return + rv.stopScroll() + if (lm is LinearLayoutManager) { + lm.scrollToPositionWithOffset(position, 0) + } else { + lm.scrollToPosition(position) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/ScrollResetCallback.kt b/app/src/main/java/org/koitharu/kotatsu/utils/ScrollResetCallback.kt deleted file mode 100644 index 5a279f4ec..000000000 --- a/app/src/main/java/org/koitharu/kotatsu/utils/ScrollResetCallback.kt +++ /dev/null @@ -1,13 +0,0 @@ -package org.koitharu.kotatsu.utils - -import androidx.recyclerview.widget.RecyclerView -import java.lang.ref.WeakReference - -class ScrollResetCallback(recyclerView: RecyclerView) : Runnable { - - private val recyclerViewRef = WeakReference(recyclerView) - - override fun run() { - recyclerViewRef.get()?.scrollToPosition(0) - } -} \ No newline at end of file diff --git a/app/src/main/java/org/koitharu/kotatsu/utils/ext/TextViewExt.kt b/app/src/main/java/org/koitharu/kotatsu/utils/ext/TextViewExt.kt index 05df723de..59620f2b0 100644 --- a/app/src/main/java/org/koitharu/kotatsu/utils/ext/TextViewExt.kt +++ b/app/src/main/java/org/koitharu/kotatsu/utils/ext/TextViewExt.kt @@ -3,7 +3,9 @@ package org.koitharu.kotatsu.utils.ext import android.graphics.drawable.Drawable import android.view.View import android.widget.TextView +import androidx.annotation.AttrRes import androidx.annotation.StringRes +import androidx.core.content.res.use import androidx.core.view.isGone var TextView.textAndVisible: CharSequence? @@ -35,4 +37,11 @@ fun TextView.setTextAndVisible(@StringRes textResId: Int) { setText(textResId) isGone = text.isNullOrEmpty() } +} + +fun TextView.setTextColorAttr(@AttrRes attrResId: Int) { + val colors = context.obtainStyledAttributes(intArrayOf(attrResId)).use { + it.getColorStateList(0) + } + setTextColor(colors) } \ No newline at end of file diff --git a/app/src/main/res/drawable/bg_badge_empty.xml b/app/src/main/res/drawable/bg_badge_empty.xml new file mode 100644 index 000000000..55ad85bd1 --- /dev/null +++ b/app/src/main/res/drawable/bg_badge_empty.xml @@ -0,0 +1,15 @@ + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_page_thumb.xml b/app/src/main/res/layout/item_page_thumb.xml index 6d9924b86..2ed3ec94c 100644 --- a/app/src/main/res/layout/item_page_thumb.xml +++ b/app/src/main/res/layout/item_page_thumb.xml @@ -1,10 +1,12 @@ - + xmlns:app="http://schemas.android.com/apk/res-auto" + style="@style/Widget.Material3.CardView.Outlined" + app:cardBackgroundColor="?scrimBackground"> - - - \ No newline at end of file + \ No newline at end of file