From dad010a89173d8ae4fe6ddd08c5dbf4f7347babb Mon Sep 17 00:00:00 2001 From: arkon Date: Fri, 24 Apr 2020 19:37:34 -0400 Subject: [PATCH] Replace some listeners with flowbindings --- .../tachiyomi/ui/reader/ReaderActivity.kt | 13 ++--- .../ui/reader/ReaderColorFilterSheet.kt | 58 +++++++------------ .../tachiyomi/ui/source/filter/TextItem.kt | 17 ++++-- .../widget/DialogCustomDownloadView.kt | 16 +++-- .../widget/PreCachingLayoutManager.kt | 25 -------- .../tachiyomi/widget/SimpleSeekBarListener.kt | 14 ----- .../tachiyomi/widget/SimpleTextWatcher.kt | 12 ---- .../preference/LoginDialogPreference.kt | 11 ---- 8 files changed, 50 insertions(+), 116 deletions(-) delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/PreCachingLayoutManager.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/SimpleSeekBarListener.kt delete mode 100644 app/src/main/java/eu/kanade/tachiyomi/widget/SimpleTextWatcher.kt diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt index 0b89b122e..81172e00f 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderActivity.kt @@ -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() } // 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) diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderColorFilterSheet.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderColorFilterSheet.kt index c4f5a18e2..d7f909225 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderColorFilterSheet.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/reader/ReaderColorFilterSheet.kt @@ -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() 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() { diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/source/filter/TextItem.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/source/filter/TextItem.kt index 86b1cc8d6..86a0c60b1 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/source/filter/TextItem.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/source/filter/TextItem.kt @@ -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() { + 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>, holder: Holder, position: Int, payloads: List?) { 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 { diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/DialogCustomDownloadView.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/DialogCustomDownloadView.kt index d1363df48..45de86d37 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/DialogCustomDownloadView.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/DialogCustomDownloadView.kt @@ -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) } /** diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/PreCachingLayoutManager.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/PreCachingLayoutManager.kt deleted file mode 100644 index b08acbf32..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/PreCachingLayoutManager.kt +++ /dev/null @@ -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 - } -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/SimpleSeekBarListener.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/SimpleSeekBarListener.kt deleted file mode 100644 index fdade75f1..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/SimpleSeekBarListener.kt +++ /dev/null @@ -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) { - } -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/SimpleTextWatcher.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/SimpleTextWatcher.kt deleted file mode 100644 index ee39d5871..000000000 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/SimpleTextWatcher.kt +++ /dev/null @@ -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) {} -} diff --git a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LoginDialogPreference.kt b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LoginDialogPreference.kt index 694887175..64e545905 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LoginDialogPreference.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/widget/preference/LoginDialogPreference.kt @@ -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 - } - } - }) } }