Skip to content

Instantly share code, notes, and snippets.

View Lamartio's full-sized avatar

Danny Lamarti Lamartio

  • Elements
  • Almere
View GitHub Profile
@Lamartio
Lamartio / AnkoViewHolder.kt
Last active March 25, 2018 17:52
Easy RecyclerView binding with Anko layout
fun <T> ViewGroup.toAnkoViewHolder(createView: AnkoContext<ViewGroup>.(((T) -> Unit) -> Unit) -> View): AnkoViewHolder =
AnkoViewHolder.create(this, createView)
class AnkoViewHolder(itemView: View, private val onBind: (Any) -> Unit) : RecyclerView.ViewHolder(itemView) {
fun bind(item: Any) = onBind(item)
companion object {
@Suppress("UNCHECKED_CAST")
@Lamartio
Lamartio / ViewTestRule.kt
Last active April 30, 2018 11:08
The logic from https://github.com/novoda/espresso-support bundled in a single .kt file
import android.app.Activity
import android.app.Instrumentation
import android.content.Context
import android.support.annotation.LayoutRes
import android.support.test.InstrumentationRegistry
import android.support.test.rule.ActivityTestRule
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
@Lamartio
Lamartio / Example.kt
Created June 11, 2018 07:36
Simplified Spek for creating tests without the need of additional dependencies
private val Store<Car>.frontLeftWheel: Wheel
get() = getState().wheels.entries.first { it.key == Position.FRONT_LEFT }.value
class ChangeWheelFeature : Feature({
lateinit var store: Store<Car>
given("the wheel is a 15 inch one") {
setUp { store = newStore() }
@Lamartio
Lamartio / initial_layout.kt
Last active September 10, 2018 09:49
Kotlin's simple layout trick
LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
addView(TextView(context).apply {
text = "Hello"
})
addView(TextView(context).apply {
text = "click me"
LinearLayout(this).apply {
orientation = LinearLayout.VERTICAL
addView(TextView(context).apply {
text = "Hello"
})
addView(TextView(context).apply {
text = "click me"
LinearLayout(this) layout {
// ...
}
infix fun <V : ViewGroup> V.layout(block: Layout<V>.() -> Unit): V =
also { Layout(it).run(block) }
class Layout<V : ViewGroup>(val layout: V) : ViewManager by layout, ViewParent by layout {}
LinearLayout(this) layout {
+TextView(context).apply {
text = "Hello world"
}
}
class Layout<V : ViewGroup>(val layout: V) : ViewManager by layout, ViewParent by layout {
val context = layout.context
LinearLayout(this) layout {
+TextView(context) {
text = "Hello world"
}
}
class Layout<V : ViewGroup>(val layout: V) : ViewManager by layout, ViewParent by layout {
// ...
LinearLayout(this) layout {
+textView {
text = "Hello world"
}
}
val Layout<*>.textView: TextView get() = TextView(context)
val Layout<*>.button: Button get() = Button(context)
LinearLayout(this) layout {
// ...
}
infix fun <V : ViewGroup> V.layout(block: Layout<V>.() -> Unit): V =
also { Layout(it).run(block) }
class Layout<V : ViewGroup>(val layout: V) : ViewManager by layout, ViewParent by layout {}