2019-11-27 19:47:20 +01:00
|
|
|
package com.topjohnwu.magisk.utils
|
|
|
|
|
|
|
|
import android.content.res.Resources
|
|
|
|
import android.widget.TextView
|
|
|
|
import androidx.databinding.BindingAdapter
|
|
|
|
import androidx.databinding.InverseBindingAdapter
|
2020-02-01 16:21:53 +01:00
|
|
|
import com.topjohnwu.magisk.extensions.get
|
2019-11-27 19:47:20 +01:00
|
|
|
|
|
|
|
sealed class TransitiveText {
|
|
|
|
|
|
|
|
abstract val isEmpty: Boolean
|
|
|
|
abstract fun getText(resources: Resources): CharSequence
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
class String(
|
|
|
|
private val value: CharSequence
|
|
|
|
) : TransitiveText() {
|
|
|
|
|
|
|
|
override val isEmpty = value.isEmpty()
|
|
|
|
override fun getText(resources: Resources) = value
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
class Res(
|
|
|
|
private val value: Int,
|
|
|
|
private vararg val params: Any
|
|
|
|
) : TransitiveText() {
|
|
|
|
|
|
|
|
override val isEmpty = value == 0
|
|
|
|
override fun getText(resources: Resources) =
|
|
|
|
resources.getString(value, *params)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---
|
|
|
|
|
|
|
|
companion object {
|
2020-01-17 17:02:40 +08:00
|
|
|
val EMPTY = String("")
|
2019-11-27 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
fun Int.asTransitive(vararg params: Any) = TransitiveText.Res(this, *params)
|
|
|
|
fun CharSequence.asTransitive() = TransitiveText.String(this)
|
|
|
|
|
|
|
|
|
|
|
|
@BindingAdapter("android:text")
|
|
|
|
fun TextView.setText(text: TransitiveText) {
|
2020-02-01 16:21:53 +01:00
|
|
|
this.text = text.getText(get())
|
2019-11-27 19:47:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@InverseBindingAdapter(attribute = "android:text", event = "android:textAttrChanged")
|
2020-01-17 17:02:40 +08:00
|
|
|
fun TextView.getTransitiveText() = text.asTransitive()
|