Skip to content

Instantly share code, notes, and snippets.

Avatar

Danny Lamarti Lamartio

  • Elements
  • Almere
View GitHub Profile
View ViewModelFactory.kt
/*
* Copyright 2019 Danny Lamarti
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
View kotlins_simple_layout_trick_pt2_result.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(LinearLayout(this).layout(User.observable) {
layout.orientation = LinearLayout.VERTICAL
layout.gravity = Gravity.CENTER
+textView {
View kotlins_simple_layout_trick_pt2_binding_example.kt
+textView {
text = "This is plain text"
bind { user -> text = "${user.name}: ${user.age}" } // User is the argument
bind { text = "$name: $age" } // User is the this-pointer
}
typealias Observer<T> = T.(T) -> Unit
@Lamartio
Lamartio / BindableLayout.kt
Created Sep 24, 2018
kotlins_simple_layout_trick_pt2_view_extensions.kt
View BindableLayout.kt
open class BindableLayout<T, V : ViewGroup>(
layout: V,
private val observable: Observable<T>
) : Layout<V>(layout) {
init {
layout.addOnAttachStateChangeListener(DisposableOnAttachStateChangeListener {
observable.subscribe(observers)
})
}
@Lamartio
Lamartio / BindableLayout.kt
Last active Sep 24, 2018
kotlins_simple_layout_trick_pt2_bind_implementation.kt
View BindableLayout.kt
open class BindableLayout<T, V : ViewGroup>(layout: V) : Layout<V>(layout) {
private val observers = Observers()
fun bind(observer: Observer<T>) {
observers.add(observer)
}
private inner class Observers : ArrayList<Observer<T>>(), Consumer<T> {
View kotlins_simple_layout_trick_pt2_data.kt
data class User(val name: String = "Danny", val age: Int = 27) {
companion object {
fun asDataSource(): Observable<User> =
Observable
.interval(1, TimeUnit.SECONDS)
.scan(User()) { user, _ -> user.copy(age = user.age + 1) }
.observeOn(AndroidSchedulers.mainThread())
View kotlins_simple_layout_trick_viewgroup_dsl.kt
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 {}
View kotlins_simple_layout_trick_view_extensions.kt
LinearLayout(this) layout {
+textView {
text = "Hello world"
}
}
val Layout<*>.textView: TextView get() = TextView(context)
val Layout<*>.button: Button get() = Button(context)
View kotlins_simple_layout_trick_initialization_with_invoke.kt
LinearLayout(this) layout {
+TextView(context) {
text = "Hello world"
}
}
class Layout<V : ViewGroup>(val layout: V) : ViewManager by layout, ViewParent by layout {
// ...
View kotlins_simple_layout_trick_unary_plus.kt
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