Replace some listeners with flowbindings

This commit is contained in:
arkon 2020-04-24 19:37:34 -04:00
parent 9f974c9401
commit dad010a891
8 changed files with 50 additions and 116 deletions

View File

@ -19,7 +19,6 @@ import android.view.View
import android.view.WindowManager
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.SeekBar
import androidx.core.view.ViewCompat
import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
import eu.kanade.tachiyomi.R
@ -52,7 +51,6 @@ import eu.kanade.tachiyomi.util.view.isDefaultBar
import eu.kanade.tachiyomi.util.view.showBar
import eu.kanade.tachiyomi.util.view.visible
import eu.kanade.tachiyomi.widget.SimpleAnimationListener
import eu.kanade.tachiyomi.widget.SimpleSeekBarListener
import java.io.File
import java.util.concurrent.TimeUnit
import kotlin.math.abs
@ -61,6 +59,7 @@ import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.sample
import nucleus.factory.RequiresPresenter
import reactivecircus.flowbinding.android.widget.progressChanges
import rx.Observable
import rx.android.schedulers.AndroidSchedulers
import rx.subscriptions.CompositeSubscription
@ -284,13 +283,13 @@ class ReaderActivity : BaseRxActivity<ReaderActivityBinding, ReaderPresenter>()
}
// Init listeners on bottom menu
binding.pageSeekbar.setOnSeekBarChangeListener(object : SimpleSeekBarListener() {
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
if (viewer != null && fromUser) {
moveToPageIndex(value)
binding.pageSeekbar.progressChanges()
.onEach {
if (viewer != null) {
moveToPageIndex(it)
}
}
})
.launchIn(scope)
binding.leftChapter.setOnClickListener {
if (viewer != null) {
if (viewer is R2LPagerViewer)

View File

@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.ui.reader
import android.graphics.Color
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import androidx.annotation.ColorInt
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialog
@ -12,7 +11,6 @@ import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.util.view.gone
import eu.kanade.tachiyomi.util.view.visible
import eu.kanade.tachiyomi.widget.IgnoreFirstSpinnerListener
import eu.kanade.tachiyomi.widget.SimpleSeekBarListener
import kotlin.math.abs
import kotlinx.android.synthetic.main.reader_color_filter.brightness_seekbar
import kotlinx.android.synthetic.main.reader_color_filter.color_filter_mode
@ -29,9 +27,13 @@ import kotlinx.android.synthetic.main.reader_color_filter.txt_color_filter_green
import kotlinx.android.synthetic.main.reader_color_filter.txt_color_filter_red_value
import kotlinx.android.synthetic.main.reader_color_filter_sheet.brightness_overlay
import kotlinx.android.synthetic.main.reader_color_filter_sheet.color_overlay
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.sample
import reactivecircus.flowbinding.android.widget.progressChanges
import uy.kohesive.injekt.injectLazy
/**
@ -39,6 +41,8 @@ import uy.kohesive.injekt.injectLazy
*/
class ReaderColorFilterSheet(private val activity: ReaderActivity) : BottomSheetDialog(activity) {
private val scope = CoroutineScope(Job() + Dispatchers.Main)
private val preferences by injectLazy<PreferencesHelper>()
private var sheetBehavior: BottomSheetBehavior<*>? = null
@ -93,45 +97,25 @@ class ReaderColorFilterSheet(private val activity: ReaderActivity) : BottomSheet
}
color_filter_mode.setSelection(preferences.colorFilterMode().get(), false)
seekbar_color_filter_alpha.setOnSeekBarChangeListener(object : SimpleSeekBarListener() {
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
if (fromUser) {
setColorValue(value, ALPHA_MASK, 24)
}
}
})
seekbar_color_filter_alpha.progressChanges()
.onEach { setColorValue(it, ALPHA_MASK, 24) }
.launchIn(scope)
seekbar_color_filter_red.setOnSeekBarChangeListener(object : SimpleSeekBarListener() {
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
if (fromUser) {
setColorValue(value, RED_MASK, 16)
}
}
})
seekbar_color_filter_red.progressChanges()
.onEach { setColorValue(it, RED_MASK, 16) }
.launchIn(scope)
seekbar_color_filter_green.setOnSeekBarChangeListener(object : SimpleSeekBarListener() {
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
if (fromUser) {
setColorValue(value, GREEN_MASK, 8)
}
}
})
seekbar_color_filter_green.progressChanges()
.onEach { setColorValue(it, GREEN_MASK, 8) }
.launchIn(scope)
seekbar_color_filter_blue.setOnSeekBarChangeListener(object : SimpleSeekBarListener() {
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
if (fromUser) {
setColorValue(value, BLUE_MASK, 0)
}
}
})
seekbar_color_filter_blue.progressChanges()
.onEach { setColorValue(it, BLUE_MASK, 0) }
.launchIn(scope)
brightness_seekbar.setOnSeekBarChangeListener(object : SimpleSeekBarListener() {
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
if (fromUser) {
preferences.customBrightnessValue().set(value)
}
}
})
brightness_seekbar.progressChanges()
.onEach { preferences.customBrightnessValue().set(it) }
.launchIn(scope)
}
override fun onStart() {

View File

@ -10,10 +10,17 @@ import eu.davidea.flexibleadapter.items.IFlexible
import eu.davidea.viewholders.FlexibleViewHolder
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.source.model.Filter
import eu.kanade.tachiyomi.widget.SimpleTextWatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import reactivecircus.flowbinding.android.widget.textChanges
open class TextItem(val filter: Filter.Text) : AbstractFlexibleItem<TextItem.Holder>() {
private val scope = CoroutineScope(Job() + Dispatchers.Main)
override fun getLayoutRes(): Int {
return R.layout.navigation_view_text
}
@ -25,11 +32,9 @@ open class TextItem(val filter: Filter.Text) : AbstractFlexibleItem<TextItem.Hol
override fun bindViewHolder(adapter: FlexibleAdapter<IFlexible<RecyclerView.ViewHolder>>, holder: Holder, position: Int, payloads: List<Any?>?) {
holder.wrapper.hint = filter.name
holder.edit.setText(filter.state)
holder.edit.addTextChangedListener(object : SimpleTextWatcher() {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
filter.state = s.toString()
}
})
holder.edit.textChanges()
.onEach { filter.state = it.toString() }
.launchIn(scope)
}
override fun equals(other: Any?): Boolean {

View File

@ -12,6 +12,12 @@ import kotlinx.android.synthetic.main.download_custom_amount.view.btn_decrease_1
import kotlinx.android.synthetic.main.download_custom_amount.view.btn_increase
import kotlinx.android.synthetic.main.download_custom_amount.view.btn_increase_10
import kotlinx.android.synthetic.main.download_custom_amount.view.myNumber
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import reactivecircus.flowbinding.android.widget.textChanges
import timber.log.Timber
/**
@ -20,6 +26,8 @@ import timber.log.Timber
class DialogCustomDownloadView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
LinearLayout(context, attrs) {
private val scope = CoroutineScope(Job() + Dispatchers.Main)
/**
* Current amount of custom download chooser.
*/
@ -73,16 +81,16 @@ class DialogCustomDownloadView @JvmOverloads constructor(context: Context, attrs
}
// When user inputs custom number set amount equal to input.
myNumber.addTextChangedListener(object : SimpleTextWatcher() {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
myNumber.textChanges()
.onEach {
try {
amount = getAmount(Integer.parseInt(s.toString()))
amount = getAmount(Integer.parseInt(it.toString()))
} catch (error: NumberFormatException) {
// Catch NumberFormatException to prevent parse exception when input is empty.
Timber.e(error)
}
}
})
.launchIn(scope)
}
/**

View File

@ -1,25 +0,0 @@
package eu.kanade.tachiyomi.widget
import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
class PreCachingLayoutManager(context: Context) : LinearLayoutManager(context) {
init {
isItemPrefetchEnabled = false
}
companion object {
const val DEFAULT_EXTRA_LAYOUT_SPACE = 600
}
var extraLayoutSpace = 0
override fun getExtraLayoutSpace(state: RecyclerView.State): Int {
if (extraLayoutSpace > 0) {
return extraLayoutSpace
}
return DEFAULT_EXTRA_LAYOUT_SPACE
}
}

View File

@ -1,14 +0,0 @@
package eu.kanade.tachiyomi.widget
import android.widget.SeekBar
open class SimpleSeekBarListener : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, value: Int, fromUser: Boolean) {
}
override fun onStartTrackingTouch(seekBar: SeekBar) {
}
override fun onStopTrackingTouch(seekBar: SeekBar) {
}
}

View File

@ -1,12 +0,0 @@
package eu.kanade.tachiyomi.widget
import android.text.Editable
import android.text.TextWatcher
open class SimpleTextWatcher : TextWatcher {
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
override fun afterTextChanged(s: Editable) {}
}

View File

@ -13,7 +13,6 @@ import com.dd.processbutton.iml.ActionProcessButton
import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
import eu.kanade.tachiyomi.ui.base.controller.DialogController
import eu.kanade.tachiyomi.widget.SimpleTextWatcher
import kotlinx.android.synthetic.main.pref_account_login.view.login
import kotlinx.android.synthetic.main.pref_account_login.view.password
import kotlinx.android.synthetic.main.pref_account_login.view.show_password
@ -66,16 +65,6 @@ abstract class LoginDialogPreference(
login.setOnClickListener { checkLogin() }
setCredentialsOnView(this)
show_password.isEnabled = password.text.isNullOrEmpty()
password.addTextChangedListener(object : SimpleTextWatcher() {
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
if (s.isEmpty()) {
show_password.isEnabled = true
}
}
})
}
}