Skip to content

Instantly share code, notes, and snippets.

View ditn's full-sized avatar

Adam Bennett ditn

View GitHub Profile
@ditn
ditn / ConwayCompose.kt
Created October 11, 2020 07:10
Conway's Composable Game of Life
private val rand = Random(0)
private const val CELL_SIZE = 50
private val ALIVE_COLOR = Color.White
private val DEAD_COLOR = Color.Black
private val BooleanToVector: TwoWayConverter<Boolean, AnimationVector1D> = TwoWayConverter(
{ AnimationVector1D(if (it) 1f else 0f) },
{ it.value == 1f }
)
@Composable
@ditn
ditn / DynamicMeshNetwork.kt
Created September 30, 2020 08:03
Dynamic Mesh Network animation in Compose
private val PARTICLE_COLOR = Color.White
private val LINE_COLOR = Color.White
private const val PARTICLE_QUANTITY = 100
private const val DEFAULT_SPEED = 2
private const val VARIANT_SPEED = 1
private const val DEFAULT_RADIUS = 4
private const val VARIANT_RADIUS = 2
private const val LINK_RADIUS = 200
// TODO: 30/09/2020 These should be measured but are only used to calculate
// the initial position
fun TransactionSender.logTransaction(eventLogger: EventLogger): TransactionSender =
TransactionLogger(this, eventLogger)
@ditn
ditn / ktlint.gradle.kts
Created December 7, 2018 12:53
ktlint with Gradle Kotlin DSL
val ktlint by configurations.creating
dependencies {
ktlint(Libraries.ktlint)
}
tasks.register<JavaExec>("ktlint") {
group = "verification"
description = "Check Kotlin code style."
classpath = ktlint
@ditn
ditn / blockstack_verification.txt
Created May 21, 2018 17:44
Verifying my Blockstack ID
Verifying my Blockstack ID is secured with the address 16RnXJ8orEyoEdZGWYQRRxudjrYuQNPoYS https://explorer.blockstack.org/address/16RnXJ8orEyoEdZGWYQRRxudjrYuQNPoYS
@ditn
ditn / idiomatic_ternary_function.kt
Created April 26, 2018 10:36
Idiomatic ternary function in Kotlin
val builder = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(icon)
// Excluded for brevity
.apply {
if (AndroidUtils.is19orHigher()) {
setVibrate(longArrayOf(100))
} else {
setVibrate(longArrayOf())
}
}
@ditn
ditn / generic_ternary_function.kt
Last active April 26, 2018 11:02
Generic ternary function
fun <T> T.ternaryBuilder(
predicate: () -> Boolean,
trueFunc: T.() -> T,
falseFunc: T.() -> T
): T = if (predicate()) this.trueFunc() else this.falseFunc()
@ditn
ditn / naive_ternary_builder_use.kt
Created April 26, 2018 10:31
Naive Kotlin builder use
val builder = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(icon)
// Excluded for brevity
.ternaryBuilder(
{ AndroidUtils.is19orHigher() },
{ setVibrate(longArrayOf(100)) },
{ setVibrate(longArrayOf()) }
)
.setContentText(text)
@ditn
ditn / naive_builder_extension.kt
Last active April 26, 2018 11:02
naive_high_order_extension
private fun NotificationCompat.Builder.ternaryBuilder(
predicate: () -> Boolean,
trueFunc: NotificationCompat.Builder.() -> NotificationCompat.Builder,
falseFunc: NotificationCompat.Builder.() -> NotificationCompat.Builder
): NotificationCompat.Builder = if (predicate()) this.trueFunc() else this.falseFunc()
@ditn
ditn / kotlin_builder_breakout.kt
Last active April 26, 2018 11:02
builder pattern in kotlin with breakout
val builder = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
.setSmallIcon(icon)
// Excluded for brevity
.setContentText(text)
if (AndroidUtils.is19orHigher()) {
builder.setVibrate(longArrayOf(100))
} else {
builder.setVibrate(longArrayOf())
}