ghedeon
02/18/2019, 10:07 PMclass StringResource private constructor(
val string: String?,
@StringRes val stringRes: Int?,
vararg val args: Any?
) {
constructor(@StringRes stringRes: Int, vararg args: Any? = emptyArray()) : this(null, stringRes, args)
constructor(string: String) : this(string, null, null)
fun resolve(context: Context): String =
if (stringRes != null) {
context.getString(stringRes, args)
} else requireNotNull(string)
}
=============
Usage:
ViewModel: StringResource(R.string.foo, arg1, arg2)
View: stringResource.resolve(context)
I'm wondering if other people do something similar and how their wrapper looks like.dewildte
02/18/2019, 10:11 PMViewModel
usually gets it's ViewState
objects from a Channel
and posts them to a LiveData<SpecificViewState>
.
The Presenter
uses a MediatorLiveData<SpecificMappedViewState>
to map the ViewModels data to one containing the final result.ghedeon
02/18/2019, 10:24 PMrusshwolf
02/19/2019, 1:52 AMstring
and stringRes
as parameters. We also added plurals
support on my current project. Not open-source unfortunately so I can’t share any code.ghedeon
02/19/2019, 8:34 AMamadeu01
02/21/2019, 7:00 PMsealed class StringResource(
val string: String?,
val stringRes: Int? = null,
vararg val args: Any? = emptyArray())
{
fun resolve(context: Context? = null): String =
if (stringRes != null) {
context?.getString(stringRes, args) ?: ""
} else requireNotNull(string)
}
class StringRes(val i: Int, vararg val a: Any? = emptyArray()) : StringResource(null, i, a)
data class Pure(val s: String) : StringResource(s)
fun main() {
println(Pure("Hello").resolve())
}
russhwolf
02/21/2019, 7:04 PMstring
and stringRes
in the base class. You can make resolve()
abstract and have the subclasses contain only the fields they need and the implementation relevant to them. But that's mostly a style thing.sngrekov
02/21/2019, 8:52 PMdewildte
02/21/2019, 9:12 PMsngrekov
02/21/2019, 9:57 PMdewildte
02/21/2019, 10:13 PMghedeon
02/22/2019, 12:31 AMResourseManager
is also good.. Similar situation with drawables. Sometimes you'd want to apply some color filter and there comes the context.sngrekov
02/22/2019, 8:04 AM