Skip to content

Instantly share code, notes, and snippets.

View ferPrieto's full-sized avatar
💪
Attitute beats talent

Fernando Prieto Moyano ferPrieto

💪
Attitute beats talent
View GitHub Profile
@ferPrieto
ferPrieto / FilterIcon.kt
Created January 4, 2022 12:03
This is an example of a Composable function to define an Icon with a Content description
@Composable
private fun FilterIcon(modifier: Modifier, onClick: () -> Unit) {
Box(modifier) {
IconButton(
onClick = onClick
) {
Image(
painter = painterResource(R.drawable.ic_filter),
contentDescription = "Filter Button"
)
// Nested conditions (BAD IDEA)
fun logTeamMembers(teamMembers: List<String>) {
if (teamMembers.isEmpty()) {
Log.d("TAG", "It's empty")
} else if (teamMembers.size == 1) {
if (teamMembers.first() == "foo") {
Log.d("TAG", "The only member is foo")
} else {
Log.d("TAG", "The only member is not foo")
}
@ferPrieto
ferPrieto / NumberSeries.kt
Created August 5, 2020 10:17
Number-series naming example, with the correct and incorrect way to do it
// Noninformative way
fun combineLists(a1: List<Int>, a2: List<Int>): List<Int> = a1.mapIndexed { i, f -> f - a2[i] }
// Meaningful distinction
fun combinePrices(amazonPrices: List<Int>, ebayPrices: List<Int>): List<Int> =
amazonPrices.mapIndexed { index, amazonPrice -> amazonPrice - ebayPrices[index] }
@ferPrieto
ferPrieto / InstagramTypeFactory.kt
Last active June 30, 2020 22:09
This is the first solution implemented to find the ErrorCode by qualifiedName
else -> if ((type.classifier as KClass<out Any>).qualifiedName == "sctestingplayground.ErrorCode") ErrorCode.TimeOut else null
@ferPrieto
ferPrieto / InstagramTypeFactory.kt
Created June 30, 2020 22:08
This is the second solution implemented using the annotation and the inline function `findAnnotation`
else -> if ((type.classifier as KClass<out Any>).findAnnotation<InstagramErrorCode>() != null) ErrorCode.TimeOut else null
internal class InstagramTypeFactory : DefaultTypeFactory() {
override fun KParameter.createArgument(): Any? =
when (type.classifier) {
Int::class -> 0
Byte::class -> 0.toByte()
Short::class -> 0.toShort()
String::class -> ""
Float::class -> 0f
Long::class -> 0L
List::class -> emptyList<String>()
class InstagramErrorByTypeTest {
companion object {
@Suppress("unused")
@JvmStatic
private fun checkSealedClassesByType() = Stream.of(
Arguments.of(
InstagramErrorType.DeclinePost(
photoVideoUrl = "https://www.instagram.com/p/B_2B2nwpS5g/",
tags = listOf("pullup", "bodyweight"),
errorCode = ErrorCode.Declined,
class InstagramErrorByClassTest {
@Nested
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
inner class MixedConstructors {
private val instagramErrors = mutableListOf<String>()
@AfterAll
fun check() {
@InstagramErrorCode
enum class ErrorCode {
Declined,
OverLimit,
TimeOut
}
@ferPrieto
ferPrieto / InstagramErrorType.kt
Created June 30, 2020 01:02
This class represents all of the different Instagram Errors as example
sealed class InstagramErrorType : InstagramErrorContent {
data class DeclinePost(
override val photoVideoUrl: String,
override val tags: List<String>,
override val errorCode: ErrorCode,
val textContent: String,
val location: String
) : InstagramErrorType()