-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathToastText.kt
More file actions
47 lines (40 loc) · 1.43 KB
/
ToastText.kt
File metadata and controls
47 lines (40 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package to.bitkit.models
import android.content.Context
import androidx.annotation.StringRes
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.ui.res.stringResource
@Stable
sealed interface ToastText {
@JvmInline
value class Resource(@StringRes val resId: Int) : ToastText
data class Parameterized(
@StringRes val resId: Int,
val params: Map<String, String>,
) : ToastText
@JvmInline
value class Literal(val value: String) : ToastText
companion object {
operator fun invoke(value: String): ToastText = Literal(value)
operator fun invoke(@StringRes resId: Int): ToastText = Resource(resId)
operator fun invoke(
@StringRes resId: Int,
params: Map<String, String>,
): ToastText = Parameterized(resId, params)
}
}
@Composable
fun ToastText.asString(): String = when (this) {
is ToastText.Resource -> stringResource(resId)
is ToastText.Parameterized -> params.entries.fold(stringResource(resId)) { acc, (key, value) ->
acc.replace("{$key}", value)
}
is ToastText.Literal -> value
}
fun ToastText.asString(context: Context): String = when (this) {
is ToastText.Resource -> context.getString(resId)
is ToastText.Parameterized -> params.entries.fold(context.getString(resId)) { acc, (key, value) ->
acc.replace("{$key}", value)
}
is ToastText.Literal -> value
}