Created
December 13, 2018 15:18
-
-
Save danherrera/0653bf84e2a19efcb37a7b85adc36a1c to your computer and use it in GitHub Desktop.
Modeling Android Resource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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