Migrate to ViewBinding in ReaderActivity

This commit is contained in:
arkon 2020-04-10 14:23:38 -04:00
parent 8cfd80ba84
commit 1a50f7062a

View File

@ -29,6 +29,7 @@ import eu.kanade.tachiyomi.data.notification.NotificationReceiver
import eu.kanade.tachiyomi.data.notification.Notifications import eu.kanade.tachiyomi.data.notification.Notifications
import eu.kanade.tachiyomi.data.preference.PreferencesHelper import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.data.preference.getOrDefault import eu.kanade.tachiyomi.data.preference.getOrDefault
import eu.kanade.tachiyomi.databinding.ReaderActivityBinding
import eu.kanade.tachiyomi.ui.base.activity.BaseRxActivity 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.AddToLibraryFirst
import eu.kanade.tachiyomi.ui.reader.ReaderPresenter.SetAsCoverResult.Error import eu.kanade.tachiyomi.ui.reader.ReaderPresenter.SetAsCoverResult.Error
@ -56,19 +57,6 @@ import eu.kanade.tachiyomi.widget.SimpleSeekBarListener
import java.io.File import java.io.File
import java.util.concurrent.TimeUnit import java.util.concurrent.TimeUnit
import kotlin.math.abs import kotlin.math.abs
import kotlinx.android.synthetic.main.reader_activity.brightness_overlay
import kotlinx.android.synthetic.main.reader_activity.color_overlay
import kotlinx.android.synthetic.main.reader_activity.left_chapter
import kotlinx.android.synthetic.main.reader_activity.left_page_text
import kotlinx.android.synthetic.main.reader_activity.page_number
import kotlinx.android.synthetic.main.reader_activity.page_seekbar
import kotlinx.android.synthetic.main.reader_activity.please_wait
import kotlinx.android.synthetic.main.reader_activity.reader_menu
import kotlinx.android.synthetic.main.reader_activity.reader_menu_bottom
import kotlinx.android.synthetic.main.reader_activity.right_chapter
import kotlinx.android.synthetic.main.reader_activity.right_page_text
import kotlinx.android.synthetic.main.reader_activity.toolbar
import kotlinx.android.synthetic.main.reader_activity.viewer_container
import nucleus.factory.RequiresPresenter import nucleus.factory.RequiresPresenter
import rx.Observable import rx.Observable
import rx.Subscription import rx.Subscription
@ -114,6 +102,8 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
@Suppress("DEPRECATION") @Suppress("DEPRECATION")
private var progressDialog: ProgressDialog? = null private var progressDialog: ProgressDialog? = null
private lateinit var binding: ReaderActivityBinding
companion object { companion object {
@Suppress("unused") @Suppress("unused")
const val LEFT_TO_RIGHT = 1 const val LEFT_TO_RIGHT = 1
@ -142,7 +132,9 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
else -> R.style.Theme_Reader else -> R.style.Theme_Reader
}) })
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.reader_activity)
binding = ReaderActivityBinding.inflate(layoutInflater)
setContentView(binding.root)
if (presenter.needsInit()) { if (presenter.needsInit()) {
val manga = intent.extras!!.getLong("manga", -1) val manga = intent.extras!!.getLong("manga", -1)
@ -265,15 +257,15 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
*/ */
private fun initializeMenu() { private fun initializeMenu() {
// Set toolbar // Set toolbar
setSupportActionBar(toolbar) setSupportActionBar(binding.toolbar)
supportActionBar?.setDisplayHomeAsUpEnabled(true) supportActionBar?.setDisplayHomeAsUpEnabled(true)
toolbar.setNavigationOnClickListener { binding.toolbar.setNavigationOnClickListener {
onBackPressed() onBackPressed()
} }
ViewCompat.setOnApplyWindowInsetsListener(reader_menu) { _, insets -> ViewCompat.setOnApplyWindowInsetsListener(binding.readerMenu) { _, insets ->
if (!window.isDefaultBar()) { if (!window.isDefaultBar()) {
reader_menu.setPadding( binding.readerMenu.setPadding(
insets.systemWindowInsetLeft, insets.systemWindowInsetLeft,
insets.systemWindowInsetTop, insets.systemWindowInsetTop,
insets.systemWindowInsetRight, insets.systemWindowInsetRight,
@ -283,14 +275,14 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
} }
// Init listeners on bottom menu // Init listeners on bottom menu
page_seekbar.setOnSeekBarChangeListener(object : SimpleSeekBarListener() { binding.pageSeekbar.setOnSeekBarChangeListener(object : SimpleSeekBarListener() {
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) { override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
if (viewer != null && fromUser) { if (viewer != null && fromUser) {
moveToPageIndex(value) moveToPageIndex(value)
} }
} }
}) })
left_chapter.setOnClickListener { binding.leftChapter.setOnClickListener {
if (viewer != null) { if (viewer != null) {
if (viewer is R2LPagerViewer) if (viewer is R2LPagerViewer)
loadNextChapter() loadNextChapter()
@ -298,7 +290,7 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
loadPreviousChapter() loadPreviousChapter()
} }
} }
right_chapter.setOnClickListener { binding.rightChapter.setOnClickListener {
if (viewer != null) { if (viewer != null) {
if (viewer is R2LPagerViewer) if (viewer is R2LPagerViewer)
loadPreviousChapter() loadPreviousChapter()
@ -323,7 +315,7 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
} else { } else {
resetDefaultMenuAndBar() resetDefaultMenuAndBar()
} }
reader_menu.visible() binding.readerMenu.visible()
if (animate) { if (animate) {
val toolbarAnimation = AnimationUtils.loadAnimation(this, R.anim.enter_from_top) val toolbarAnimation = AnimationUtils.loadAnimation(this, R.anim.enter_from_top)
@ -333,10 +325,10 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS) window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
} }
}) })
toolbar.startAnimation(toolbarAnimation) binding.toolbar.startAnimation(toolbarAnimation)
val bottomAnimation = AnimationUtils.loadAnimation(this, R.anim.enter_from_bottom) val bottomAnimation = AnimationUtils.loadAnimation(this, R.anim.enter_from_bottom)
reader_menu_bottom.startAnimation(bottomAnimation) binding.readerMenuBottom.startAnimation(bottomAnimation)
} }
if (preferences.showPageNumber().getOrDefault()) { if (preferences.showPageNumber().getOrDefault()) {
@ -353,13 +345,13 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
val toolbarAnimation = AnimationUtils.loadAnimation(this, R.anim.exit_to_top) val toolbarAnimation = AnimationUtils.loadAnimation(this, R.anim.exit_to_top)
toolbarAnimation.setAnimationListener(object : SimpleAnimationListener() { toolbarAnimation.setAnimationListener(object : SimpleAnimationListener() {
override fun onAnimationEnd(animation: Animation) { override fun onAnimationEnd(animation: Animation) {
reader_menu.gone() binding.readerMenu.gone()
} }
}) })
toolbar.startAnimation(toolbarAnimation) binding.toolbar.startAnimation(toolbarAnimation)
val bottomAnimation = AnimationUtils.loadAnimation(this, R.anim.exit_to_bottom) val bottomAnimation = AnimationUtils.loadAnimation(this, R.anim.exit_to_bottom)
reader_menu_bottom.startAnimation(bottomAnimation) binding.readerMenuBottom.startAnimation(bottomAnimation)
} }
if (preferences.showPageNumber().getOrDefault()) { if (preferences.showPageNumber().getOrDefault()) {
@ -372,7 +364,7 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
* Reset menu padding and system bar * Reset menu padding and system bar
*/ */
private fun resetDefaultMenuAndBar() { private fun resetDefaultMenuAndBar() {
reader_menu.setPadding(0, 0, 0, 0) binding.readerMenu.setPadding(0, 0, 0, 0)
window.defaultBar() window.defaultBar()
} }
@ -393,17 +385,17 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
// Destroy previous viewer if there was one // Destroy previous viewer if there was one
if (prevViewer != null) { if (prevViewer != null) {
prevViewer.destroy() prevViewer.destroy()
viewer_container.removeAllViews() binding.viewerContainer.removeAllViews()
} }
viewer = newViewer viewer = newViewer
viewer_container.addView(newViewer.getView()) binding.viewerContainer.addView(newViewer.getView())
toolbar.title = manga.title binding.toolbar.title = manga.title
page_seekbar.isRTL = newViewer is R2LPagerViewer binding.pageSeekbar.isRTL = newViewer is R2LPagerViewer
please_wait.visible() binding.pleaseWait.visible()
please_wait.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in_long)) binding.pleaseWait.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_in_long))
} }
/** /**
@ -411,9 +403,9 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
* method to the current viewer, but also set the subtitle on the toolbar. * method to the current viewer, but also set the subtitle on the toolbar.
*/ */
fun setChapters(viewerChapters: ViewerChapters) { fun setChapters(viewerChapters: ViewerChapters) {
please_wait.gone() binding.pleaseWait.gone()
viewer?.setChapters(viewerChapters) viewer?.setChapters(viewerChapters)
toolbar.subtitle = viewerChapters.currChapter.chapter.name binding.toolbar.subtitle = viewerChapters.currChapter.chapter.name
// Invalidate menu to show proper chapter bookmark state // Invalidate menu to show proper chapter bookmark state
invalidateOptionsMenu() invalidateOptionsMenu()
@ -482,20 +474,20 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
val pages = page.chapter.pages ?: return val pages = page.chapter.pages ?: return
// Set bottom page number // Set bottom page number
page_number.text = "${page.number}/${pages.size}" binding.pageNumber.text = "${page.number}/${pages.size}"
// Set seekbar page number // Set seekbar page number
if (viewer !is R2LPagerViewer) { if (viewer !is R2LPagerViewer) {
left_page_text.text = "${page.number}" binding.leftPageText.text = "${page.number}"
right_page_text.text = "${pages.size}" binding.rightPageText.text = "${pages.size}"
} else { } else {
right_page_text.text = "${page.number}" binding.rightPageText.text = "${page.number}"
left_page_text.text = "${pages.size}" binding.leftPageText.text = "${pages.size}"
} }
// Set seekbar progress // Set seekbar progress
page_seekbar.max = pages.lastIndex binding.pageSeekbar.max = pages.lastIndex
page_seekbar.progress = page.index binding.pageSeekbar.progress = page.index
} }
/** /**
@ -696,7 +688,7 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
* Sets the visibility of the bottom page indicator according to [visible]. * Sets the visibility of the bottom page indicator according to [visible].
*/ */
fun setPageNumberVisibility(visible: Boolean) { fun setPageNumberVisibility(visible: Boolean) {
page_number.visibility = if (visible) View.VISIBLE else View.INVISIBLE binding.pageNumber.visibility = if (visible) View.VISIBLE else View.INVISIBLE
} }
/** /**
@ -756,7 +748,7 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
subscriptions.add(customFilterColorSubscription) subscriptions.add(customFilterColorSubscription)
} else { } else {
customFilterColorSubscription?.let { subscriptions.remove(it) } customFilterColorSubscription?.let { subscriptions.remove(it) }
color_overlay.gone() binding.colorOverlay.gone()
} }
} }
@ -782,11 +774,11 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
// Set black overlay visibility. // Set black overlay visibility.
if (value < 0) { if (value < 0) {
brightness_overlay.visible() binding.brightnessOverlay.visible()
val alpha = (abs(value) * 2.56).toInt() val alpha = (abs(value) * 2.56).toInt()
brightness_overlay.setBackgroundColor(Color.argb(alpha, 0, 0, 0)) binding.brightnessOverlay.setBackgroundColor(Color.argb(alpha, 0, 0, 0))
} else { } else {
brightness_overlay.gone() binding.brightnessOverlay.gone()
} }
} }
@ -794,8 +786,8 @@ class ReaderActivity : BaseRxActivity<ReaderPresenter>() {
* Sets the color filter [value]. * Sets the color filter [value].
*/ */
private fun setColorFilterValue(value: Int) { private fun setColorFilterValue(value: Int) {
color_overlay.visible() binding.colorOverlay.visible()
color_overlay.setFilterColor(value, preferences.colorFilterMode().getOrDefault()) binding.colorOverlay.setFilterColor(value, preferences.colorFilterMode().getOrDefault())
} }
} }
} }