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 / method_references.kt
Created June 23, 2017 10:03
An example of a gotcha that I found whilst working with method references in RxJava/Kotlin
fun printString(string: String) = println(string)
/**
* Prints successfully
*/
Observable.just("Test string")
.doOnNext{ printString(it) }
.subscribe()
@ditn
ditn / font_xml_example.xml
Created March 6, 2018 16:02
An example of a downloadable font.xml file
<?xml version="1.0" encoding="utf-8"?>
<font-family xmlns:app="http://schemas.android.com/apk/res-auto"
app:fontProviderAuthority="com.google.android.gms.fonts"
app:fontProviderPackage="com.google.android.gms"
app:fontProviderQuery="Montserrat"
app:fontProviderCerts="@array/com_google_android_gms_fonts_certs">
</font-family>
@ditn
ditn / loadFont.kt
Last active March 24, 2019 11:38
An example of how we programmatically load fonts in Kotlin
fun loadFont(context: Context, font: CustomFont, func: (Typeface) -> Unit) {
val handlerThread = HandlerThread("fonts").apply { start() }
val handler = Handler(handlerThread.looper)
val request = FontRequest(
"com.google.android.gms.fonts",
"com.google.android.gms",
font.query,
R.array.com_google_android_gms_fonts_certs
)
@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_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 / 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_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())
}
@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()