android kt框架,KBinding 使用kotlin实现的Android MVVM框架

KBinding

Android View Model binding 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 {

override fun convert(params: Array): 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") { "xxx@xxxx.com" }

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 = EmptyOneWayConverter()) : PropertyBinding = oneWayPropertyBinding(enabled(), false, converter, *paths)

//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 = EmptyOneWayConverter()) : PropertyBinding = oneWayPropertyBinding(text(), false, converter, *paths)

fun TextView.text(vararg paths: String, mode: OneTime, converter: OneWayConverter = EmptyOneWayConverter()) : PropertyBinding = oneWayPropertyBinding(text(), true, converter, *paths)

fun TextView.text(path: String, mode: OneWayToSource, converter: OneWayConverter = EmptyOneWayConverter()) : PropertyBinding = oneWayPropertyBinding(path, textChanges2(), converter)

fun TextView.text(path: String, mode: TwoWay, converter: TwoWayConverter = EmptyTwoWayConverter()) : PropertyBinding = twoWayPropertyBinding(path, textChanges2(), text(), converter)

Using with Gradle

dependencies {

compile 'com.benny.kbinding:library:0.1.0'}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值