Skip to content

Instantly share code, notes, and snippets.

View danherrera's full-sized avatar
👋

Danilo Herrera danherrera

👋
View GitHub Profile
@danherrera
danherrera / Handle4CasesFrom2Options.kt
Last active August 23, 2020 20:41
Handle the 4 possible cases when dealing with 2 `Option` objects.
sealed class QuaternaryCase<T> {
data class Both<T>(val a: T, val b: T) : QuaternaryCase<T>()
data class OnlyA<T>(val a: T) : QuaternaryCase<T>()
data class OnlyB<T>(val b: T) : QuaternaryCase<T>()
class None<T> : QuaternaryCase<T>()
}
fun <T, R> Option<T>.map2Case(b: Option<T>, f: (case: QuaternaryCase<T>) -> R): R {
return Ior.fromOptions(this, b)
.map { ior ->
@danherrera
danherrera / Kotlin React Importing Material UI Button.md
Last active May 15, 2020 16:49
Importing Material UI Button into KotlinJS create-react-kotlin-app

Adding the npm package dependency:

yarn add @material-ui/core

Usage in Javascript (ECMAScript):

import Button from 'material-ui/core/Button';
import PropTypes from 'prop-types';
@danherrera
danherrera / View.kt
Last active April 16, 2020 13:25
Android Pin/Code Entry Hack
// Within onViewCreated
fun updateViewBasedOnPin(pin: String) {
binding.digit1.text = pin.getOrNull(0)?.toString()
binding.digit2.text = pin.getOrNull(1)?.toString()
binding.digit3.text = pin.getOrNull(2)?.toString()
binding.digit4.text = pin.getOrNull(3)?.toString()
binding.digit1Underline.setBackgroundColor(if (pin.isNotEmpty()) Color.YELLOW else Color.DKGRAY)
binding.digit2Underline.setBackgroundColor(if (pin.length > 1) Color.YELLOW else Color.DKGRAY)
binding.digit3Underline.setBackgroundColor(if (pin.length > 2) Color.YELLOW else Color.DKGRAY)
binding.digit4Underline.setBackgroundColor(if (pin.length > 3) Color.YELLOW else Color.DKGRAY)
@danherrera
danherrera / keybase.md
Created August 1, 2019 15:01
Keybase Proof

Keybase proof

I hereby claim:

  • I am danherrera on github.
  • I am danherrera (https://keybase.io/danherrera) on keybase.
  • I have a public key ASCXdlSUJqcKoITjr1Sabpl6s2M_0Dh5STSx4HUBXY5Pugo

To claim this, I am signing this object:

@danherrera
danherrera / StringResource.kt
Created December 13, 2018 15:18
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
@danherrera
danherrera / Event.kt
Created October 7, 2018 18:53
LiveData Event that only emits if it has not been consumed. From Florina Muntenescu's KotlinConf 2018 presentation: https://youtu.be/Sy6ZdgqrQp0?t=1579
open class Event<out T>(private val content: T) {
var consumed = false
private set
fun consume(): T? {
return if (consumed) {
null
} else {
consumed = true
content
Video Player*
Play Video*
preroll -> Ad
no ad -> First Frame
First Frame
continue -> Video Playing