2021-08-28 12:06:29 -04:00
|
|
|
package eu.kanade.tachiyomi.widget
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.util.AttributeSet
|
|
|
|
import android.widget.EditText
|
|
|
|
import androidx.core.view.inputmethod.EditorInfoCompat
|
|
|
|
import com.google.android.material.textfield.TextInputEditText
|
|
|
|
import eu.kanade.tachiyomi.R
|
|
|
|
import eu.kanade.tachiyomi.data.preference.PreferencesHelper
|
2022-07-16 18:26:30 -04:00
|
|
|
import eu.kanade.tachiyomi.util.preference.asHotFlow
|
2021-12-31 16:32:24 -05:00
|
|
|
import eu.kanade.tachiyomi.widget.TachiyomiTextInputEditText.Companion.setIncognito
|
2021-08-28 12:06:29 -04:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.SupervisorJob
|
|
|
|
import kotlinx.coroutines.cancel
|
|
|
|
import kotlinx.coroutines.flow.launchIn
|
|
|
|
import uy.kohesive.injekt.Injekt
|
|
|
|
import uy.kohesive.injekt.api.get
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A custom [TextInputEditText] that sets [EditorInfoCompat.IME_FLAG_NO_PERSONALIZED_LEARNING] to imeOptions
|
|
|
|
* if [PreferencesHelper.incognitoMode] is true. Some IMEs may not respect this flag.
|
|
|
|
*
|
|
|
|
* @see setIncognito
|
|
|
|
*/
|
|
|
|
class TachiyomiTextInputEditText @JvmOverloads constructor(
|
|
|
|
context: Context,
|
|
|
|
attrs: AttributeSet? = null,
|
2022-04-08 15:30:30 -04:00
|
|
|
defStyleAttr: Int = R.attr.editTextStyle,
|
2021-08-28 12:06:29 -04:00
|
|
|
) : TextInputEditText(context, attrs, defStyleAttr) {
|
|
|
|
|
|
|
|
private var scope: CoroutineScope? = null
|
|
|
|
|
|
|
|
override fun onAttachedToWindow() {
|
|
|
|
super.onAttachedToWindow()
|
|
|
|
scope = CoroutineScope(SupervisorJob() + Dispatchers.Main)
|
|
|
|
setIncognito(scope!!)
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onDetachedFromWindow() {
|
|
|
|
super.onDetachedFromWindow()
|
|
|
|
scope?.cancel()
|
|
|
|
scope = null
|
|
|
|
}
|
|
|
|
|
|
|
|
companion object {
|
|
|
|
/**
|
|
|
|
* Sets Flow to this [EditText] that sets [EditorInfoCompat.IME_FLAG_NO_PERSONALIZED_LEARNING] to imeOptions
|
|
|
|
* if [PreferencesHelper.incognitoMode] is true. Some IMEs may not respect this flag.
|
|
|
|
*/
|
|
|
|
fun EditText.setIncognito(viewScope: CoroutineScope) {
|
2022-09-17 11:48:24 -04:00
|
|
|
Injekt.get<PreferencesHelper>().incognitoMode()
|
|
|
|
.asHotFlow {
|
|
|
|
imeOptions = if (it) {
|
|
|
|
imeOptions or EditorInfoCompat.IME_FLAG_NO_PERSONALIZED_LEARNING
|
|
|
|
} else {
|
|
|
|
imeOptions and EditorInfoCompat.IME_FLAG_NO_PERSONALIZED_LEARNING.inv()
|
|
|
|
}
|
2021-08-28 12:06:29 -04:00
|
|
|
}
|
2022-09-17 11:48:24 -04:00
|
|
|
.launchIn(viewScope)
|
2021-08-28 12:06:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|