From bdc441a5be2503ff35c783d37e44ebd74b87a2b1 Mon Sep 17 00:00:00 2001 From: arkon Date: Sat, 25 Jul 2020 11:34:44 -0400 Subject: [PATCH] Hide manga title in toolbar when at top --- .../ui/base/controller/BaseController.kt | 4 +- .../tachiyomi/ui/library/LibraryController.kt | 8 +-- .../tachiyomi/ui/manga/MangaController.kt | 49 +++++++++++++++++-- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/BaseController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/BaseController.kt index 74a82d7118..f1e980a7de 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/BaseController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/base/controller/BaseController.kt @@ -74,7 +74,7 @@ abstract class BaseController(bundle: Bundle? = null) : return null } - fun setTitle() { + fun setTitle(title: String? = null) { var parentController = parentController while (parentController != null) { if (parentController is BaseController<*> && parentController.getTitle() != null) { @@ -83,7 +83,7 @@ abstract class BaseController(bundle: Bundle? = null) : parentController = parentController.parentController } - (activity as? AppCompatActivity)?.supportActionBar?.title = getTitle() + (activity as? AppCompatActivity)?.supportActionBar?.title = title ?: getTitle() } private fun Controller.instance(): String { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt index 21315cad38..29b5727f02 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/library/LibraryController.kt @@ -130,7 +130,7 @@ class LibraryController( retainViewMode = RetainViewMode.RETAIN_DETACH } - private var title: String? = null + private var currentTitle: String? = null set(value) { if (field != value) { field = value @@ -139,15 +139,15 @@ class LibraryController( } override fun getTitle(): String? { - return title ?: resources?.getString(R.string.label_library) + return currentTitle ?: resources?.getString(R.string.label_library) } private fun updateTitle() { if (preferences.categoryTabs().get()) { - title = resources?.getString(R.string.label_library) + currentTitle = resources?.getString(R.string.label_library) } else { adapter?.categories?.get(binding.libraryPager.currentItem)?.let { - title = it.name + currentTitle = it.name } } } diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt index 5ed61f9bc1..c36903eeeb 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/manga/MangaController.kt @@ -4,6 +4,7 @@ import android.animation.Animator import android.animation.AnimatorListenerAdapter import android.app.Activity import android.content.Intent +import android.graphics.Color import android.os.Bundle import android.view.LayoutInflater import android.view.Menu @@ -61,16 +62,20 @@ import eu.kanade.tachiyomi.ui.recent.history.HistoryController import eu.kanade.tachiyomi.ui.recent.updates.UpdatesController import eu.kanade.tachiyomi.ui.webview.WebViewActivity import eu.kanade.tachiyomi.util.hasCustomCover +import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.view.getCoordinates import eu.kanade.tachiyomi.util.view.gone import eu.kanade.tachiyomi.util.view.shrinkOnScroll import eu.kanade.tachiyomi.util.view.snack import eu.kanade.tachiyomi.util.view.visible +import kotlin.math.min import kotlinx.android.synthetic.main.main_activity.root_coordinator +import kotlinx.android.synthetic.main.main_activity.toolbar import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.onEach import reactivecircus.flowbinding.android.view.clicks +import reactivecircus.flowbinding.recyclerview.scrollEvents import reactivecircus.flowbinding.swiperefreshlayout.refreshes import timber.log.Timber import uy.kohesive.injekt.Injekt @@ -118,6 +123,9 @@ class MangaController : private val preferences: PreferencesHelper by injectLazy() private val coverCache: CoverCache by injectLazy() + private val toolbarTextColor by lazy { view!!.context.getResourceColor(R.attr.colorOnPrimary) } + private var toolbarTextAlpha = 255 + private var mangaInfoAdapter: MangaInfoHeaderAdapter? = null private var chaptersHeaderAdapter: MangaChaptersHeaderAdapter? = null private var chaptersAdapter: ChaptersAdapter? = null @@ -151,10 +159,6 @@ class MangaController : setHasOptionsMenu(true) } - override fun getTitle(): String? { - return manga?.title - } - override fun onChangeEnded(handler: ControllerChangeHandler, type: ControllerChangeType) { super.onChangeEnded(handler, type) if (manga == null || source == null) { @@ -198,8 +202,18 @@ class MangaController : if (!fromSource && preferences.jumpToChapters()) { (binding.recycler.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(1, 0) } + + // Delayed in case we need to jump to chapters + binding.recycler.post { + updateToolbarTitleAlpha() + setTitle(manga?.title) + } } + binding.recycler.scrollEvents() + .onEach { updateToolbarTitleAlpha() } + .launchIn(scope) + binding.swipeRefresh.refreshes() .onEach { fetchMangaInfoFromSource(manualFetch = true) @@ -219,6 +233,32 @@ class MangaController : updateFilterIconState() } + private fun updateToolbarTitleAlpha(alpha: Int? = null) { + val calculatedAlpha = when { + // Specific alpha provided + alpha != null -> alpha + + // First item isn't in view, full opacity + ((binding.recycler.layoutManager as LinearLayoutManager).findFirstVisibleItemPosition() > 0) -> 255 + + // Based on scroll amount when first item is in view + else -> min(binding.recycler.computeVerticalScrollOffset(), 255) + } + + if (calculatedAlpha != toolbarTextAlpha) { + toolbarTextAlpha = calculatedAlpha + + activity?.toolbar?.setTitleTextColor( + Color.argb( + toolbarTextAlpha, + Color.red(toolbarTextColor), + Color.green(toolbarTextColor), + Color.blue(toolbarTextColor) + ) + ) + } + } + private fun updateFilterIconState() { chaptersHeaderAdapter?.setHasActiveFilters(settingsSheet?.filters?.hasActiveFilters() == true) } @@ -268,6 +308,7 @@ class MangaController : chaptersHeaderAdapter = null chaptersAdapter = null settingsSheet = null + updateToolbarTitleAlpha(255) super.onDestroyView(view) }