AquaStream/app/src/main/java/com/lagradost/cloudstream3/ui/result/UiText.kt

172 lines
4.5 KiB
Kotlin
Raw Normal View History

2022-08-01 01:00:48 +00:00
package com.lagradost.cloudstream3.ui.result
import android.content.Context
2022-08-02 00:43:42 +00:00
import android.util.Log
2022-08-01 01:00:48 +00:00
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import androidx.core.view.isGone
import androidx.core.view.isVisible
2022-08-03 00:04:03 +00:00
import com.lagradost.cloudstream3.mvvm.Some
2022-08-01 02:46:43 +00:00
import com.lagradost.cloudstream3.mvvm.logError
2022-08-01 01:00:48 +00:00
import com.lagradost.cloudstream3.utils.AppUtils.html
import com.lagradost.cloudstream3.utils.UIHelper.setImage
sealed class UiText {
2022-08-02 00:43:42 +00:00
companion object {
const val TAG = "UiText"
}
data class DynamicString(val value: String) : UiText() {
override fun toString(): String = value
}
2022-08-04 22:26:33 +00:00
2022-08-01 01:00:48 +00:00
class StringResource(
@StringRes val resId: Int,
2022-08-02 00:43:42 +00:00
val args: List<Any>
) : UiText() {
2022-08-04 22:26:33 +00:00
override fun toString(): String =
"resId = $resId\nargs = ${args.toList().map { "(${it::class} = $it)" }}"
2022-08-02 00:43:42 +00:00
}
2022-08-01 01:00:48 +00:00
fun asStringNull(context: Context?): String? {
2022-08-01 02:46:43 +00:00
try {
return asString(context ?: return null)
} catch (e: Exception) {
2022-08-02 00:43:42 +00:00
Log.e(TAG, "Got invalid data from $this")
2022-08-01 02:46:43 +00:00
logError(e)
return null
}
2022-08-01 01:00:48 +00:00
}
fun asString(context: Context): String {
return when (this) {
is DynamicString -> value
2022-08-02 00:43:42 +00:00
is StringResource -> {
val str = context.getString(resId)
if (args.isEmpty()) {
str
} else {
str.format(*args.map {
when (it) {
is UiText -> it.asString(context)
else -> it
}
}.toTypedArray())
}
}
2022-08-01 01:00:48 +00:00
}
}
}
sealed class UiImage {
data class Image(
val url: String,
val headers: Map<String, String>? = null,
@DrawableRes val errorDrawable: Int? = null
) : UiImage()
data class Drawable(@DrawableRes val resId: Int) : UiImage()
}
fun ImageView?.setImage(value: UiImage?, fadeIn: Boolean = true) {
2022-08-01 01:00:48 +00:00
when (value) {
is UiImage.Image -> setImageImage(value, fadeIn)
2022-08-01 01:00:48 +00:00
is UiImage.Drawable -> setImageDrawable(value)
null -> {
this?.isVisible = false
}
}
}
fun ImageView?.setImageImage(value: UiImage.Image, fadeIn: Boolean = true) {
2022-08-01 01:00:48 +00:00
if (this == null) return
this.isVisible = setImage(value.url, value.headers, value.errorDrawable, fadeIn)
2022-08-01 01:00:48 +00:00
}
fun ImageView?.setImageDrawable(value: UiImage.Drawable) {
if (this == null) return
this.isVisible = true
this.setImage(UiImage.Drawable(value.resId))
2022-08-01 01:00:48 +00:00
}
@JvmName("imgNull")
fun img(
url: String?,
headers: Map<String, String>? = null,
@DrawableRes errorDrawable: Int? = null
): UiImage? {
if (url.isNullOrBlank()) return null
return UiImage.Image(url, headers, errorDrawable)
}
fun img(
url: String,
headers: Map<String, String>? = null,
@DrawableRes errorDrawable: Int? = null
): UiImage {
return UiImage.Image(url, headers, errorDrawable)
}
fun img(@DrawableRes drawable: Int): UiImage {
return UiImage.Drawable(drawable)
}
fun txt(value: String): UiText {
return UiText.DynamicString(value)
}
@JvmName("txtNull")
fun txt(value: String?): UiText? {
return UiText.DynamicString(value ?: return null)
}
fun txt(@StringRes resId: Int, vararg args: Any): UiText {
2022-08-02 00:43:42 +00:00
return UiText.StringResource(resId, args.toList())
2022-08-01 01:00:48 +00:00
}
@JvmName("txtNull")
fun txt(@StringRes resId: Int?, vararg args: Any?): UiText? {
if (resId == null || args.any { it == null }) {
return null
}
2022-08-02 00:43:42 +00:00
return UiText.StringResource(resId, args.filterNotNull().toList())
2022-08-01 01:00:48 +00:00
}
fun TextView?.setText(text: UiText?) {
if (this == null) return
if (text == null) {
this.isVisible = false
} else {
2022-08-04 22:26:33 +00:00
val str = text.asStringNull(context)?.let {
if (this.maxLines == 1) {
it.replace("\n", " ")
} else {
it
}
}
2022-08-01 01:00:48 +00:00
this.isGone = str.isNullOrBlank()
this.text = str
}
}
fun TextView?.setTextHtml(text: UiText?) {
if (this == null) return
if (text == null) {
this.isVisible = false
} else {
val str = text.asStringNull(context)
this.isGone = str.isNullOrBlank()
this.text = str.html()
}
}
2022-08-03 00:04:03 +00:00
2022-08-03 17:27:49 +00:00
fun TextView?.setTextHtml(text: Some<UiText>?) {
2022-08-04 22:26:33 +00:00
setTextHtml(if (text is Some.Success) text.value else null)
2022-08-03 00:04:03 +00:00
}
2022-08-03 17:27:49 +00:00
fun TextView?.setText(text: Some<UiText>?) {
2022-08-04 22:26:33 +00:00
setText(if (text is Some.Success) text.value else null)
2022-08-03 00:04:03 +00:00
}