Giter Site home page Giter Site logo

kbinding's Introduction

KBinding

Android MVVM framework write in kotlin, base on anko, simple but powerful.

Contents

BindingMode

  • OneWay: Binding from model to view
  • TwoWay: Binding from model to view and view to model
  • OneWayToSource: Binding from view to model
  • OneTime: Binding from model to view, and auto release after first emit

Simple Binding

verticalLayout {
    editText {
        bind { text("name", mode = TwoWay) }
    }
    button {
        bind { click("hello") }
    }
}
class SimpleViewModel() : ViewModel() {
    var name: String by bindProperty("name") { "Jason" }
    val hello: Command by bindCommand("hello") { params, canExecute ->
        toast("Hello, ${name}!")
    }
}

Multiple Binding

//login button enabled only when name and password not empty
class ArrayToBooleanConverter : MultipleConverter<Boolean> {
    override fun convert(params: Array<Any>): Boolean {
        params.forEach {
            if(it.toString().isEmpty()) return false
        }
        return true
    }
}
verticalLayout {
    editText {
        bind { text("name", mode = TwoWay) }
    }
    editText {
            bind { text("password", mode = TwoWay) }
        }
    button {
        bind { enabled("name", "password", mode = OneWay, converter = ArrayToBooleanConverter()) }
        bind { click("login") }
    }
}
class LoginViewModel() : ViewModel() {
    var name: String by bindProperty("name") { "[email protected]" }
    var password: String by bindProperty("password") { "xxxxxx" }
    val login: Command by bindCommand("login") { params, canExecute ->
        //login processing
    }
}

View Model property depends on other properties

//name and price property will be updated when new stock is set
class StockViewModel() : ViewModel() {
    var stock: Stock? by bindProperty("stock")
    val name: String? by bindProperty("name", "stock") { stock!!.name }
    val price: Float by bindProperty("price", "stock") { stock!!.price }
}

Wait/Until

//wait/until just like OneTime binding, but it need apply action, for example below, it wait for market from model, then decide how to display
relativeLayout {
    wait { until("market", converter = viewOfMarket) { inflate(it, this@verticalLayout) }  }
}

Extend Binding Property(Depend on RxBinding heavily)

Event

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

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) 

Using with Gradle

dependencies {
    compile 'com.benny.library:kbinding:0.1.4'
    
    //add this for common bindings, for example View/enabled, TextView/text
    compile 'com.benny.library:kbinding-common:0.1.4'
    compile 'com.benny.library:kbinding-support-v4:0.1.4'
    compile 'com.benny.library:kbinding-recyclerview-v7:0.1.4'
}

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.

Discussion

QQ Group: 516157585

kbinding's People

Contributors

bennywang avatar pt2121 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.