Hide manga title in toolbar when at top

This commit is contained in:
arkon 2020-07-25 11:34:44 -04:00
parent 2dcb73700b
commit bdc441a5be
3 changed files with 51 additions and 10 deletions

View file

@ -74,7 +74,7 @@ abstract class BaseController<VB : ViewBinding>(bundle: Bundle? = null) :
return null return null
} }
fun setTitle() { fun setTitle(title: String? = null) {
var parentController = parentController var parentController = parentController
while (parentController != null) { while (parentController != null) {
if (parentController is BaseController<*> && parentController.getTitle() != null) { if (parentController is BaseController<*> && parentController.getTitle() != null) {
@ -83,7 +83,7 @@ abstract class BaseController<VB : ViewBinding>(bundle: Bundle? = null) :
parentController = parentController.parentController parentController = parentController.parentController
} }
(activity as? AppCompatActivity)?.supportActionBar?.title = getTitle() (activity as? AppCompatActivity)?.supportActionBar?.title = title ?: getTitle()
} }
private fun Controller.instance(): String { private fun Controller.instance(): String {

View file

@ -130,7 +130,7 @@ class LibraryController(
retainViewMode = RetainViewMode.RETAIN_DETACH retainViewMode = RetainViewMode.RETAIN_DETACH
} }
private var title: String? = null private var currentTitle: String? = null
set(value) { set(value) {
if (field != value) { if (field != value) {
field = value field = value
@ -139,15 +139,15 @@ class LibraryController(
} }
override fun getTitle(): String? { override fun getTitle(): String? {
return title ?: resources?.getString(R.string.label_library) return currentTitle ?: resources?.getString(R.string.label_library)
} }
private fun updateTitle() { private fun updateTitle() {
if (preferences.categoryTabs().get()) { if (preferences.categoryTabs().get()) {
title = resources?.getString(R.string.label_library) currentTitle = resources?.getString(R.string.label_library)
} else { } else {
adapter?.categories?.get(binding.libraryPager.currentItem)?.let { adapter?.categories?.get(binding.libraryPager.currentItem)?.let {
title = it.name currentTitle = it.name
} }
} }
} }

View file

@ -4,6 +4,7 @@ import android.animation.Animator
import android.animation.AnimatorListenerAdapter import android.animation.AnimatorListenerAdapter
import android.app.Activity import android.app.Activity
import android.content.Intent import android.content.Intent
import android.graphics.Color
import android.os.Bundle import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.Menu 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.recent.updates.UpdatesController
import eu.kanade.tachiyomi.ui.webview.WebViewActivity import eu.kanade.tachiyomi.ui.webview.WebViewActivity
import eu.kanade.tachiyomi.util.hasCustomCover 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.system.toast
import eu.kanade.tachiyomi.util.view.getCoordinates import eu.kanade.tachiyomi.util.view.getCoordinates
import eu.kanade.tachiyomi.util.view.gone import eu.kanade.tachiyomi.util.view.gone
import eu.kanade.tachiyomi.util.view.shrinkOnScroll import eu.kanade.tachiyomi.util.view.shrinkOnScroll
import eu.kanade.tachiyomi.util.view.snack import eu.kanade.tachiyomi.util.view.snack
import eu.kanade.tachiyomi.util.view.visible 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.root_coordinator
import kotlinx.android.synthetic.main.main_activity.toolbar
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import reactivecircus.flowbinding.android.view.clicks import reactivecircus.flowbinding.android.view.clicks
import reactivecircus.flowbinding.recyclerview.scrollEvents
import reactivecircus.flowbinding.swiperefreshlayout.refreshes import reactivecircus.flowbinding.swiperefreshlayout.refreshes
import timber.log.Timber import timber.log.Timber
import uy.kohesive.injekt.Injekt import uy.kohesive.injekt.Injekt
@ -118,6 +123,9 @@ class MangaController :
private val preferences: PreferencesHelper by injectLazy() private val preferences: PreferencesHelper by injectLazy()
private val coverCache: CoverCache 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 mangaInfoAdapter: MangaInfoHeaderAdapter? = null
private var chaptersHeaderAdapter: MangaChaptersHeaderAdapter? = null private var chaptersHeaderAdapter: MangaChaptersHeaderAdapter? = null
private var chaptersAdapter: ChaptersAdapter? = null private var chaptersAdapter: ChaptersAdapter? = null
@ -151,10 +159,6 @@ class MangaController :
setHasOptionsMenu(true) setHasOptionsMenu(true)
} }
override fun getTitle(): String? {
return manga?.title
}
override fun onChangeEnded(handler: ControllerChangeHandler, type: ControllerChangeType) { override fun onChangeEnded(handler: ControllerChangeHandler, type: ControllerChangeType) {
super.onChangeEnded(handler, type) super.onChangeEnded(handler, type)
if (manga == null || source == null) { if (manga == null || source == null) {
@ -198,8 +202,18 @@ class MangaController :
if (!fromSource && preferences.jumpToChapters()) { if (!fromSource && preferences.jumpToChapters()) {
(binding.recycler.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(1, 0) (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() binding.swipeRefresh.refreshes()
.onEach { .onEach {
fetchMangaInfoFromSource(manualFetch = true) fetchMangaInfoFromSource(manualFetch = true)
@ -219,6 +233,32 @@ class MangaController :
updateFilterIconState() 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() { private fun updateFilterIconState() {
chaptersHeaderAdapter?.setHasActiveFilters(settingsSheet?.filters?.hasActiveFilters() == true) chaptersHeaderAdapter?.setHasActiveFilters(settingsSheet?.filters?.hasActiveFilters() == true)
} }
@ -268,6 +308,7 @@ class MangaController :
chaptersHeaderAdapter = null chaptersHeaderAdapter = null
chaptersAdapter = null chaptersAdapter = null
settingsSheet = null settingsSheet = null
updateToolbarTitleAlpha(255)
super.onDestroyView(view) super.onDestroyView(view)
} }