Address coroutine scope leaks in custom views

This commit is contained in:
arkon
2021-01-07 19:16:26 -05:00
parent b18a794eca
commit 8e613d03e3
5 changed files with 33 additions and 21 deletions

View File

@@ -7,10 +7,6 @@ import android.view.LayoutInflater
import android.view.View
import android.widget.LinearLayout
import eu.kanade.tachiyomi.databinding.DownloadCustomAmountBinding
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import reactivecircus.flowbinding.android.widget.textChanges
import timber.log.Timber
/**
@@ -35,8 +31,6 @@ class DialogCustomDownloadView @JvmOverloads constructor(context: Context, attrs
*/
private var max = 0
private val scope = MainScope()
private val binding: DownloadCustomAmountBinding
init {
@@ -71,16 +65,16 @@ class DialogCustomDownloadView @JvmOverloads constructor(context: Context, attrs
}
// When user inputs custom number set amount equal to input.
binding.myNumber.textChanges()
.onEach {
binding.myNumber.addTextChangedListener(object : SimpleTextWatcher() {
override fun onTextChanged(text: CharSequence, start: Int, before: Int, count: Int) {
try {
amount = getAmount(Integer.parseInt(it.toString()))
amount = getAmount(text.toString().toInt())
} catch (error: NumberFormatException) {
// Catch NumberFormatException to prevent parse exception when input is empty.
Timber.e(error)
}
}
.launchIn(scope)
})
}
/**

View File

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