You've already forked Magisk
mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-09-06 06:36:58 +00:00
Rename TransitiveText
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package com.topjohnwu.magisk.utils
|
||||
|
||||
import android.content.res.Resources
|
||||
import android.widget.TextView
|
||||
import androidx.databinding.BindingAdapter
|
||||
|
||||
sealed class TextHolder {
|
||||
|
||||
abstract val isEmpty: Boolean
|
||||
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)
|
||||
}
|
||||
@@ -1,53 +0,0 @@
|
||||
package com.topjohnwu.magisk.utils
|
||||
|
||||
import android.content.res.Resources
|
||||
import android.widget.TextView
|
||||
import androidx.databinding.BindingAdapter
|
||||
import androidx.databinding.InverseBindingAdapter
|
||||
|
||||
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 {
|
||||
val EMPTY = String("")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun Int.asTransitive(vararg params: Any): TransitiveText = TransitiveText.Res(this, *params)
|
||||
fun CharSequence.asTransitive(): TransitiveText = TransitiveText.String(this)
|
||||
|
||||
|
||||
@BindingAdapter("android:text")
|
||||
fun TextView.setText(text: TransitiveText) {
|
||||
this.text = text.getText(context.resources)
|
||||
}
|
||||
|
||||
@InverseBindingAdapter(attribute = "android:text", event = "android:textAttrChanged")
|
||||
fun TextView.getTransitiveText() = text.asTransitive()
|
||||
Reference in New Issue
Block a user