Skip to content

Instantly share code, notes, and snippets.

View ditn's full-sized avatar

Adam Bennett ditn

View GitHub Profile
Verifying that "iateyourmic.id" is my Blockstack ID. https://onename.com/iateyourmic

Keybase proof

I hereby claim:

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

To claim this, I am signing this object:

@ditn
ditn / setCustomFont.kt
Created March 6, 2018 16:18
An example of an Extension Function that applies a loaded font to a TextView
/**
* Loads a font via the Support Library downloadable font system and applies it to a TextView. If this
* function fails, it will do so silently.
*
* @param customFont A [CustomFont] object that encapsulates the query to be sent to the fonts provider
*/
fun TextView.setCustomFont(customFont: CustomFont) {
loadFont(context, customFont) {
this.typeface = it
}
@ditn
ditn / kotlin_test_conversion.kt
Created March 6, 2018 17:30
An example of a better converted Kotlin class
class ExampleTest {
private val subject: ClassToTest = mock()
@Test
fun `verify this causes that`() {
// Arrange
val aValue = 10
whenever(mockedClass.getValue()).thenReturn(aValue)
// Act
@ditn
ditn / kotlin_version.kt
Last active March 20, 2018 15:29
An example of a poorly converted Kotlin class
class ExampleTest {
@Mock private var subject: ClassToTest?
@Before
fun setUp() {
MockitoAnnotations.initMocks(this)
}
@Test
@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())
}