This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
open class BindableLayout<T, V : ViewGroup>( | |
layout: V, | |
private val observable: Observable<T> | |
) : Layout<V>(layout) { | |
init { | |
layout.addOnAttachStateChangeListener(DisposableOnAttachStateChangeListener { | |
observable.subscribe(observers) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LinearLayout(this) layout { | |
+textView { | |
text = "Hello world" | |
} | |
} | |
val Layout<*>.textView: TextView get() = TextView(context) | |
val Layout<*>.button: Button get() = Button(context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LinearLayout(this) layout { | |
+TextView(context) { | |
text = "Hello world" | |
} | |
} | |
class Layout<V : ViewGroup>(val layout: V) : ViewManager by layout, ViewParent by layout { | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder