48 lines
1.1 KiB
Kotlin
Raw Normal View History

2021-04-09 01:32:37 -07:00
package com.topjohnwu.magisk.utils
import android.content.res.Resources
import android.widget.TextView
import androidx.databinding.BindingAdapter
2021-04-18 02:12:53 -07:00
abstract class TextHolder {
2021-04-09 01:32:37 -07:00
2021-04-18 04:46:11 -07:00
open val isEmpty: Boolean get() = false
2021-04-09 01:32:37 -07:00
abstract fun getText(resources: Resources): CharSequence
// ---
class String(
private val value: CharSequence
) : TextHolder() {
override val isEmpty get() = value.isEmpty()
override fun getText(resources: Resources) = value
}
class Resource(
private val value: Int,
private vararg val params: Any
) : TextHolder() {
override val isEmpty get() = value == 0
override fun getText(resources: Resources) = resources.getString(value, *params)
}
// ---
companion object {
val EMPTY = String("")
}
}
fun Int.asText(vararg params: Any): TextHolder = TextHolder.Resource(this, *params)
fun CharSequence.asText(): TextHolder = TextHolder.String(this)
@BindingAdapter("android:text")
fun TextView.setText(text: TextHolder) {
this.text = text.getText(context.resources)
}