The Chinese version of

Android MVVM framework write in kotlin, base on anko, simple but powerful. It depends on my another project AutoAdapter(A library for simplifying adapter creation)

Contents

Simple Binding

verticalLayout {
    editText {
        bind { text("name", mode = TwoWay) }
    }
    button {
        bind { click("hello") }
    }
}
class SimpleViewModel(a): ViewModel() {
    @delegate:Property
    var name: String by Delegates.property("Jason")

    // all the parameter for Command is optional, first parameter pass by event Observable, second parameter is lambda (Boolean) -> Unit
    @Command
    val hello(canExecute: (Boolean) -> Unit) {
        toast("Hello, ${name}!")}}Copy the code

DependencyProperty and ExtractProperty

// @DependencyProperty will generate binding for nameAndSymbol depends on stock, stock changes then nameAndSymbol changes
// @ExtractProperty will generate binding for stock properties, for example code below, Property name and price will generated. If hasPrefix = true, then Property stock.name stock.price will generated.
class StockViewModel(a): ViewModel() {
    @delegate:ExtractProperty(
        "name"."price",
        hasPrefix = false
    )
    var stock: Stock? by Delegates.property()

    @delegate:DependencyProperty("stock")
    var nameAndSymbol: String by Delegates.property { stock?.name + stock?.symbol }
}Copy the code

Extend Binding Property

Event

    fun View.click(path: String) : PropertyBinding = commandBinding(path, clicks(), enabled())Copy the code

Property

    fun View.enabled(vararg paths: String.mode: OneWay = BindingMode.OneWay, converter: OneWayConverter<Boolean> = EmptyOneWayConverter()) : PropertyBinding = oneWayPropertyBinding(paths, enabled(), false, converter) 

    //this implements four binding mode for TextView, if just need OneWay mode, remove last three lines, some for other mode
    fun TextView.text(vararg paths: String.mode: OneWay = BindingMode.OneWay, converter: OneWayConverter<out CharSequence> = EmptyOneWayConverter()) : PropertyBinding = oneWayPropertyBinding(paths, text(), false, converter)
    fun TextView.text(vararg paths: String.mode: OneTime, converter: OneWayConverter<out CharSequence> = EmptyOneWayConverter()) : PropertyBinding = oneWayPropertyBinding(paths, text(), true, converter)
    fun TextView.text(path: String.mode: OneWayToSource, converter: OneWayConverter<*> = EmptyOneWayConverter<String> ()): PropertyBinding = oneWayPropertyBinding(path, textChanges2(), converter)
    fun TextView.text(path: String.mode: TwoWay, converter: TwoWayConverter<String.*> = EmptyTwoWayConverter<String.String> ()): PropertyBinding = twoWayPropertyBinding(path, textChanges2(), text(), converter) Copy the code

Contribute

Now is just the beginning of KBinding, so everyone interested in this library, just fork it and pull requests to me. Let’s make it a little better.

QQ Group: 516157585