Fix the bottom sheet and remove the bookmark button at the top of the reader

This commit is contained in:
Jobobby04
2020-08-24 19:25:47 -04:00
parent 0a502fcf31
commit eafe3a62e4
6 changed files with 127 additions and 206 deletions
@@ -26,7 +26,6 @@ import androidx.core.view.setPadding
import com.afollestad.materialdialogs.MaterialDialog
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
import com.elvishew.xlog.XLog
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.snackbar.Snackbar
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.database.models.Chapter
@@ -36,7 +35,6 @@ import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.asImmediateFlow
import eu.kanade.tachiyomi.databinding.ReaderActivityBinding
import eu.kanade.tachiyomi.databinding.ReaderChaptersSheetBinding
import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.source.online.HttpSource
@@ -45,6 +43,7 @@ import eu.kanade.tachiyomi.ui.base.activity.BaseRxActivity
import eu.kanade.tachiyomi.ui.reader.ReaderPresenter.SetAsCoverResult.AddToLibraryFirst
import eu.kanade.tachiyomi.ui.reader.ReaderPresenter.SetAsCoverResult.Error
import eu.kanade.tachiyomi.ui.reader.ReaderPresenter.SetAsCoverResult.Success
import eu.kanade.tachiyomi.ui.reader.chapter.ReaderChapterSheet
import eu.kanade.tachiyomi.ui.reader.loader.HttpPageLoader
import eu.kanade.tachiyomi.ui.reader.model.ReaderChapter
import eu.kanade.tachiyomi.ui.reader.model.ReaderPage
@@ -67,10 +66,7 @@ import eu.kanade.tachiyomi.util.view.showBar
import eu.kanade.tachiyomi.util.view.snack
import eu.kanade.tachiyomi.widget.SimpleAnimationListener
import eu.kanade.tachiyomi.widget.SimpleSeekBarListener
import exh.util.collapse
import exh.util.defaultReaderType
import exh.util.hide
import exh.util.isExpanded
import java.io.File
import kotlin.math.abs
import kotlin.math.roundToLong
@@ -128,7 +124,7 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
private val logger = XLog.tag("ReaderActivity")
lateinit var readerBottomSheetBinding: ReaderChaptersSheetBinding
private lateinit var chapterBottomSheet: ReaderChapterSheet
// SY <--
/**
@@ -193,11 +189,6 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
// <-- EH
}
// SY -->
readerBottomSheetBinding = ReaderChaptersSheetBinding.bind(binding.readerChaptersSheet.root)
readerBottomSheetBinding.chaptersBottomSheet.setup(this)
// SY <--
config = ReaderConfig()
initializeMenu()
@@ -244,7 +235,7 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
viewer?.destroy()
viewer = null
// SY -->
readerBottomSheetBinding.chaptersBottomSheet.adapter = null
chapterBottomSheet.adapter = null
// SY <--
config = null
progressDialog?.dismiss()
@@ -294,9 +285,9 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.reader, menu)
val isChapterBookmarked = presenter?.getCurrentChapter()?.chapter?.bookmark ?: false
/*val isChapterBookmarked = presenter?.getCurrentChapter()?.chapter?.bookmark ?: false
menu.findItem(R.id.action_bookmark).isVisible = !isChapterBookmarked
menu.findItem(R.id.action_remove_bookmark).isVisible = isChapterBookmarked
menu.findItem(R.id.action_remove_bookmark).isVisible = isChapterBookmarked*/
return true
}
@@ -307,22 +298,15 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
*/
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_bookmark -> {
/*R.id.action_bookmark -> {
presenter.bookmarkCurrentChapter(true)
invalidateOptionsMenu()
}
}*/
R.id.action_remove_bookmark -> {
presenter.bookmarkCurrentChapter(false)
invalidateOptionsMenu()
}
R.id.action_settings -> {
ReaderSettingsSheet(this).show()
// SY -->
if (readerBottomSheetBinding.chaptersBottomSheet.sheetBehavior.isExpanded()) {
readerBottomSheetBinding.chaptersBottomSheet.sheetBehavior?.collapse()
}
// SY <--
}
R.id.action_settings -> ReaderSettingsSheet(this).show()
R.id.action_custom_filter -> {
val sheet = ReaderColorFilterSheet(this)
// Remove dimmed backdrop so changes can be previewed
@@ -333,11 +317,6 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
setMenuVisibility(false)
sheet.show()
// SY -->
if (readerBottomSheetBinding.chaptersBottomSheet.sheetBehavior.isExpanded()) {
readerBottomSheetBinding.chaptersBottomSheet.sheetBehavior?.collapse()
}
// SY <--
}
}
return super.onOptionsItemSelected(item)
@@ -393,7 +372,7 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
}
// Init listeners on bottom menu
/* SY --> */readerBottomSheetBinding.pageSeekbar.setOnSeekBarChangeListener(object : SimpleSeekBarListener() { /* SY <-- */
binding.pageSeekbar.setOnSeekBarChangeListener(object : SimpleSeekBarListener() {
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
if (viewer != null && fromUser) {
moveToPageIndex(value)
@@ -568,14 +547,18 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
.show()
}
.launchIn(scope)
chapterBottomSheet = ReaderChapterSheet(this)
binding.chaptersButton.clicks()
.onEach {
chapterBottomSheet.show()
}.launchIn(scope)
// <-- EH
// Set initial visibility
setMenuVisibility(menuVisible)
// --> EH
readerBottomSheetBinding.chaptersBottomSheet.sheetBehavior?.isHideable = !menuVisible
if (!menuVisible) readerBottomSheetBinding.chaptersBottomSheet.sheetBehavior?.hide()
setEhUtilsVisibility(ehUtilsVisible)
// <-- EH
}
@@ -601,10 +584,6 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
}
binding.readerMenu.isVisible = true
if (readerBottomSheetBinding.chaptersBottomSheet.sheetBehavior.isExpanded()) {
readerBottomSheetBinding.chaptersBottomSheet.sheetBehavior?.isHideable = false
}
if (animate) {
val toolbarAnimation = AnimationUtils.loadAnimation(this, R.anim.enter_from_top)
toolbarAnimation.setAnimationListener(object : SimpleAnimationListener() {
@@ -617,9 +596,8 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
binding.header.startAnimation(toolbarAnimation)
// EXH <--
// SY -->
readerBottomSheetBinding.chaptersBottomSheet.sheetBehavior?.collapse()
// SY <--
val bottomAnimation = AnimationUtils.loadAnimation(this, R.anim.enter_from_bottom)
binding.readerMenuBottom.startAnimation(bottomAnimation)
}
if (preferences.showPageNumber().get()) {
@@ -643,10 +621,8 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
binding.header.startAnimation(toolbarAnimation)
// EXH <--
// SY -->
BottomSheetBehavior.from(readerBottomSheetBinding.chaptersBottomSheet).isHideable = true
readerBottomSheetBinding.chaptersBottomSheet.sheetBehavior?.hide()
// SY <--
val bottomAnimation = AnimationUtils.loadAnimation(this, R.anim.exit_to_bottom)
binding.readerMenuBottom.startAnimation(bottomAnimation)
}
if (preferences.showPageNumber().get()) {
@@ -671,7 +647,7 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
}
fun refreshSheetChapters() {
readerBottomSheetBinding.chaptersBottomSheet.refreshList()
chapterBottomSheet.refreshList()
}
// SY <--
@@ -716,9 +692,7 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
binding.toolbar.title = manga.title
// SY -->
readerBottomSheetBinding.pageSeekbar.isRTL = newViewer is R2LPagerViewer
// SY <--
binding.pageSeekbar.isRTL = newViewer is R2LPagerViewer
binding.pleaseWait.isVisible = true
binding.pleaseWait.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in_long))
@@ -804,22 +778,19 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
val newChapter = presenter.onPageSelected(page)
val pages = page.chapter.pages ?: return
// SY -->
if (chapterBottomSheet.selectedChapterId != page.chapter.chapter.id) {
chapterBottomSheet.refreshList()
}
// SY <--
// Set bottom page number
binding.pageNumber.text = "${page.number}/${pages.size}"
// SY -->
readerBottomSheetBinding.pageText.text = "${page.number}/${pages.size}"
if (!newChapter && readerBottomSheetBinding.chaptersBottomSheet.shouldCollapse && readerBottomSheetBinding.chaptersBottomSheet.sheetBehavior.isExpanded()) {
readerBottomSheetBinding.chaptersBottomSheet.sheetBehavior?.collapse()
}
if (readerBottomSheetBinding.chaptersBottomSheet.selectedChapterId != page.chapter.chapter.id) {
readerBottomSheetBinding.chaptersBottomSheet.refreshList()
}
readerBottomSheetBinding.chaptersBottomSheet.shouldCollapse = true
binding.pageText.text = "${page.number}/${pages.size}"
// Set seekbar progress
readerBottomSheetBinding.pageSeekbar.max = pages.lastIndex
readerBottomSheetBinding.pageSeekbar.progress = page.index
// SY <--
binding.pageSeekbar.max = pages.lastIndex
binding.pageSeekbar.progress = page.index
}
/**
@@ -835,9 +806,6 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
} catch (e: WindowManager.BadTokenException) {
logger.e("Caught and ignoring reader page sheet launch exception!", e)
}
if (readerBottomSheetBinding.chaptersBottomSheet.sheetBehavior.isExpanded()) {
readerBottomSheetBinding.chaptersBottomSheet.sheetBehavior?.collapse()
}
// EXH <--
}
@@ -267,7 +267,7 @@ class ReaderPresenter(
.apply { decimalSeparator = '.' }
)
chapterList.reversed().map {
chapterList.map {
ReaderChapterItem(
it.chapter,
manga!!,
@@ -389,8 +389,8 @@ class ReaderPresenter(
* read, update tracking services, enqueue downloaded chapter deletion, and updating the active chapter if this
* [page]'s chapter is different from the currently active.
*/
fun onPageSelected(page: ReaderPage): Boolean {
val currentChapters = viewerChaptersRelay.value ?: return /* SY --> */ false /* SY <-- */
fun onPageSelected(page: ReaderPage) {
val currentChapters = viewerChaptersRelay.value ?: return
val selectedChapter = page.chapter
@@ -420,10 +420,8 @@ class ReaderPresenter(
Observable.just(selectedChapter).subscribeFirst({ view, _ ->
view.refreshSheetChapters()
})
return true
// SY <--
}
return /* SY --> */ false /* SY <-- */
}
/**
@@ -1,108 +1,46 @@
package eu.kanade.tachiyomi.ui.reader.chapter
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.Color
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import androidx.core.graphics.ColorUtils
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import android.view.ViewGroup
import android.widget.SeekBar
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
import com.mikepenz.fastadapter.FastAdapter
import com.mikepenz.fastadapter.adapters.ItemAdapter
import com.mikepenz.fastadapter.listeners.ClickEventHook
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.databinding.ReaderChaptersSheetBinding
import eu.kanade.tachiyomi.ui.reader.ReaderActivity
import eu.kanade.tachiyomi.ui.reader.ReaderPresenter
import eu.kanade.tachiyomi.util.lang.launchUI
import eu.kanade.tachiyomi.util.system.dpToPx
import eu.kanade.tachiyomi.util.system.getResourceColor
import exh.util.collapse
import exh.util.expand
import eu.kanade.tachiyomi.widget.SimpleSeekBarListener
import exh.util.isExpanded
import kotlin.math.max
import kotlin.math.min
import kotlin.math.roundToInt
import kotlinx.android.synthetic.main.reader_chapters_sheet.view.pill
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import reactivecircus.flowbinding.android.view.clicks
class ReaderChapterSheet @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
LinearLayout(context, attrs) {
/**
* Color filter sheet to toggle custom filter and brightness overlay.
*/
class ReaderChapterSheet(private val activity: ReaderActivity) : BottomSheetDialog(activity) {
private var sheetBehavior: BottomSheetBehavior<*>? = null
lateinit var binding: ReaderChaptersSheetBinding
private val binding = ReaderChaptersSheetBinding.inflate(activity.layoutInflater, null, false)
var sheetBehavior: BottomSheetBehavior<View>? = null
lateinit var presenter: ReaderPresenter
var presenter: ReaderPresenter
var adapter: FastAdapter<ReaderChapterItem>? = null
private val itemAdapter = ItemAdapter<ReaderChapterItem>()
var shouldCollapse = true
var selectedChapterId = -1L
fun setup(activity: ReaderActivity) {
init {
setContentView(binding.root)
sheetBehavior = BottomSheetBehavior.from(binding.root.parent as ViewGroup)
presenter = activity.presenter
binding = activity.readerBottomSheetBinding
val fullPrimary = context.getResourceColor(R.attr.colorSurface)
val primary = ColorUtils.setAlphaComponent(fullPrimary, 200)
sheetBehavior = BottomSheetBehavior.from(this)
binding.chaptersButton.setOnClickListener {
if (sheetBehavior.isExpanded()) {
sheetBehavior?.collapse()
} else {
sheetBehavior?.expand()
}
}
binding.webviewButton.setOnClickListener {
activity.openMangaInBrowser()
}
post {
binding.chapterRecycler.alpha = if (sheetBehavior.isExpanded()) 1f else 0f
binding.chapterRecycler.isClickable = sheetBehavior.isExpanded()
binding.chapterRecycler.isFocusable = sheetBehavior.isExpanded()
}
sheetBehavior?.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
override fun onSlide(bottomSheet: View, progress: Float) {
val trueProgress = max(progress, 0f)
binding.pill.alpha = (1 - trueProgress) * 0.25f
binding.chaptersButton.alpha = 1 - trueProgress
binding.webviewButton.alpha = trueProgress
binding.webviewButton.isVisible = binding.webviewButton.alpha > 0
binding.chaptersButton.isInvisible = binding.chaptersButton.alpha <= 0
backgroundTintList =
ColorStateList.valueOf(lerpColor(primary, fullPrimary, trueProgress))
binding.chapterRecycler.alpha = trueProgress
}
override fun onStateChanged(p0: View, state: Int) {
if (state == BottomSheetBehavior.STATE_COLLAPSED) {
shouldCollapse = true
sheetBehavior?.isHideable = false
(binding.chapterRecycler.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(
adapter?.getPosition(presenter.getCurrentChapter()?.chapter?.id ?: 0L) ?: 0,
binding.chapterRecycler.height / 2 - 30.dpToPx
)
binding.chaptersButton.alpha = 1f
binding.webviewButton.alpha = 0f
}
if (state == BottomSheetBehavior.STATE_EXPANDED) {
binding.chapterRecycler.alpha = 1F
binding.chaptersButton.alpha = 0f
binding.webviewButton.alpha = 1f
}
binding.chapterRecycler.isClickable = state == BottomSheetBehavior.STATE_EXPANDED
binding.chapterRecycler.isFocusable = state == BottomSheetBehavior.STATE_EXPANDED
binding.webviewButton.isVisible = state != BottomSheetBehavior.STATE_COLLAPSED
binding.chaptersButton.isInvisible = state == BottomSheetBehavior.STATE_EXPANDED
}
})
adapter = FastAdapter.with(itemAdapter)
binding.chapterRecycler.adapter = adapter
adapter?.onClickListener = { _, _, item, _ ->
@@ -136,18 +74,38 @@ class ReaderChapterSheet @JvmOverloads constructor(context: Context, attrs: Attr
}
})
backgroundTintList = ColorStateList.valueOf(
if (!sheetBehavior.isExpanded()) primary
else fullPrimary
)
binding.chapterRecycler.layoutManager = LinearLayoutManager(context)
refreshList()
binding.webviewButton.clicks()
.onEach { activity.openMangaInBrowser() }
.launchIn(activity.scope)
binding.pageSeekbar.setOnSeekBarChangeListener(object : SimpleSeekBarListener() {
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
if (activity.viewer != null && fromUser) {
binding.pageSeekbar.progress = value
activity.moveToPageIndex(value)
}
}
})
}
override fun onStart() {
super.onStart()
sheetBehavior?.skipCollapsed = true
sheetBehavior?.state = BottomSheetBehavior.STATE_EXPANDED
}
override fun show() {
binding.pageText.text = activity.binding.pageText.text
binding.pageSeekbar.max = activity.binding.pageSeekbar.max
binding.pageSeekbar.progress = activity.binding.pageSeekbar.progress
super.show()
}
fun refreshList() {
launchUI {
val chapters = presenter.getChapters(context)
val chapters = presenter.getChapters(context).sortedBy { it.source_order }
selectedChapterId = chapters.find { it.isCurrent }?.chapter?.id ?: -1L
itemAdapter.clear()
@@ -155,26 +113,8 @@ class ReaderChapterSheet @JvmOverloads constructor(context: Context, attrs: Attr
(binding.chapterRecycler.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(
adapter?.getPosition(presenter.getCurrentChapter()?.chapter?.id ?: 0L) ?: 0,
binding.chapterRecycler.height / 2 - 30.dpToPx
(binding.chapterRecycler.height / 2).dpToPx
)
}
}
fun lerpColor(colorStart: Int, colorEnd: Int, percent: Float): Int {
val perc = (percent * 100).roundToInt()
return Color.argb(
lerpColorCalc(Color.alpha(colorStart), Color.alpha(colorEnd), perc),
lerpColorCalc(Color.red(colorStart), Color.red(colorEnd), perc),
lerpColorCalc(Color.green(colorStart), Color.green(colorEnd), perc),
lerpColorCalc(Color.blue(colorStart), Color.blue(colorEnd), perc)
)
}
fun lerpColorCalc(colorStart: Int, colorEnd: Int, percent: Int): Int {
return (
min(colorStart, colorEnd) * (100 - percent) + max(
colorStart, colorEnd
) * percent
) / 100
}
}