Skip to content

Instantly share code, notes, and snippets.

@danherrera
Created December 13, 2018 15:18
Show Gist options
  • Save danherrera/0653bf84e2a19efcb37a7b85adc36a1c to your computer and use it in GitHub Desktop.
Save danherrera/0653bf84e2a19efcb37a7b85adc36a1c to your computer and use it in GitHub Desktop.
Modeling Android Resource
typealias StringResource = Either<String, AndroidStringResource>
data class AndroidStringResource(
val id: Int,
val args: List<String> = emptyList()
)
fun Fragment.getString(resource: StringResource): String {
return when (resource) {
is Left -> resource.a
is Right -> resource.b
.takeIf { it.args.isNotEmpty() }
?.let {
resources.getString(it.id, *it.args.toTypedArray())
} ?: resources.getString(resource.b.id)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment