{"id":13428850,"url":"https://github.com/BennyWang/KBinding","last_synced_at":"2025-03-16T02:30:35.368Z","repository":{"id":201626374,"uuid":"46047656","full_name":"BennyWang/KBinding","owner":"BennyWang","description":"Android MVVM framework write in kotlin, develop Android has never been so fun.","archived":false,"fork":false,"pushed_at":"2017-05-19T08:29:20.000Z","size":714,"stargazers_count":408,"open_issues_count":0,"forks_count":42,"subscribers_count":22,"default_branch":"develop","last_synced_at":"2024-10-27T06:39:16.133Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BennyWang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2015-11-12T11:09:01.000Z","updated_at":"2024-10-25T03:17:59.000Z","dependencies_parsed_at":null,"dependency_job_id":"51983e9e-0e6b-4028-a5f2-431b83f90b32","html_url":"https://github.com/BennyWang/KBinding","commit_stats":null,"previous_names":["bennywang/kbinding"],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BennyWang%2FKBinding","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BennyWang%2FKBinding/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BennyWang%2FKBinding/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BennyWang%2FKBinding/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BennyWang","download_url":"https://codeload.github.com/BennyWang/KBinding/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243815579,"owners_count":20352194,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T01:01:06.769Z","updated_at":"2025-03-16T02:30:34.948Z","avatar_url":"https://github.com/BennyWang.png","language":"Kotlin","readme":"KBinding\n======================\n[中文版](https://github.com/EndSmile/KBinding/blob/develop/README_CN.md)\n\nAndroid MVVM framework write in kotlin, base on anko, simple but powerful.\nIt depends on my another project [AutoAdapter](https://github.com/BennyWang/AutoAdapter)(A library for simplifying adapter creation)\n\n## Contents\n\n### BindingMode\n\n  - OneWay: Binding from model to view\n  - TwoWay: Binding from model to view and view to model\n  - OneWayToSource: Binding from view to model\n  - OneTime: Binding from model to view, and auto release after first emit\n\n### Simple Binding\n\n```kotlin\nverticalLayout {\n    editText {\n        bind { text(\"name\", mode = TwoWay) }\n    }\n    button {\n        bind { click(\"hello\") }\n    }\n}\nclass SimpleViewModel() : ViewModel() {\n    @delegate:Property\n    var name: String by Delegates.property(\"Jason\")\n    \n    // all the parameter for Command is optional, first parameter pass by event Observable, second parameter is lambda (Boolean) -\u003e Unit\n    @Command\n    val hello(canExecute: (Boolean) -\u003e Unit) {\n        toast(\"Hello, ${name}!\")\n    }\n}\n```\n\n### Multiple Binding\n\n```kotlin\n//login button enabled only when name and password not empty\nclass ArrayToBooleanConverter : MultipleConverter\u003cBoolean\u003e {\n    override fun convert(params: Array\u003cAny\u003e): Boolean {\n        params.forEach {\n            if(it.toString().isEmpty()) return false\n        }\n        return true\n    }\n}\nverticalLayout {\n    editText {\n        bind { text(\"name\", mode = TwoWay) }\n    }\n    editText {\n            bind { text(\"password\", mode = TwoWay) }\n        }\n    button {\n        bind { enabled(\"name\", \"password\", mode = OneWay, converter = ArrayToBooleanConverter()) }\n        bind { click(\"login\") }\n    }\n}\nclass LoginViewModel() : ViewModel() {\n    @delegate:Property\n    var name: String by Delegates.property(\"xxx@xxxx.co\")\n    \n    @delegate:Property\n    var password: String by Delegates.property(\"xxxxxx\")\n    \n    @Command\n    val login() {\n        //login processing\n    }\n}\n```\n\n### DependencyProperty and ExtractProperty\n```kotlin\n// @DependencyProperty will generate binding for nameAndSymbol depends on stock, stock changes then nameAndSymbol changes\n// @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.\nclass StockViewModel() : ViewModel() {\n    @delegate:ExtractProperty(\n        \"name\", \"price\",\n        hasPrefix = false\n    )\n    var stock: Stock? by Delegates.property()\n    \n    @delegate:DependencyProperty(\"stock\")\n    var nameAndSymbol: String by Delegates.property { stock?.name + stock?.symbol }\n}\n```\n\n### Wait/Until\n\n```kotlin\n//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\nrelativeLayout {\n    wait { until(\"market\", converter = viewOfMarket) { inflate(it, this@verticalLayout) }  }\n}\n```\n    \n## Extend Binding Property\n\nEvent\n\n```kotlin   \n    fun View.click(path: String) : PropertyBinding = commandBinding(path, clicks(), enabled())\n```  \n\nProperty\n\n```kotlin\n    fun View.enabled(vararg paths: String, mode: OneWay = BindingMode.OneWay, converter: OneWayConverter\u003cBoolean\u003e = EmptyOneWayConverter()) : PropertyBinding = oneWayPropertyBinding(paths, enabled(), false, converter) \n    \n    //this implements four binding mode for TextView, if just need OneWay mode, remove last three lines, some for other mode\n    fun TextView.text(vararg paths: String, mode: OneWay = BindingMode.OneWay, converter: OneWayConverter\u003cout CharSequence\u003e = EmptyOneWayConverter()) : PropertyBinding = oneWayPropertyBinding(paths, text(), false, converter)\n    fun TextView.text(vararg paths: String, mode: OneTime, converter: OneWayConverter\u003cout CharSequence\u003e = EmptyOneWayConverter()) : PropertyBinding = oneWayPropertyBinding(paths, text(), true, converter)\n    fun TextView.text(path: String, mode: OneWayToSource, converter: OneWayConverter\u003c*\u003e = EmptyOneWayConverter\u003cString\u003e()) : PropertyBinding = oneWayPropertyBinding(path, textChanges2(), converter)\n    fun TextView.text(path: String, mode: TwoWay, converter: TwoWayConverter\u003cString, *\u003e = EmptyTwoWayConverter\u003cString, String\u003e()) : PropertyBinding = twoWayPropertyBinding(path, textChanges2(), text(), converter) \n```\n\n## Using with Gradle\n```gradle\n\ndependencies {\n    compile 'com.benny.library:kbinding:0.2.3'\n    kapt 'com.benny.library:kbinding-compiler:0.2.3'\n    \n    // for common bindings, View, TextView, and ...\n    compile 'com.benny.library:kbinding-common:0.2.3'\n    // for recyclerview bindings\n    compile 'com.benny.library:kbinding-adapterview:0.2.3'\n    // for support v4 bindings\n    compile 'com.benny.library:kbinding-support-v4:0.2.3'\n}\n```\n\n## Contribute\n\nNow is just the beginning of KBinding, so everyone interested in this library, just fork it and pull requests to me.\nLet's make it a little better.\n\n## Discussion\n\n### QQ Group: 516157585\n","funding_links":[],"categories":["Libraries","开源库","Kotlin","\u003ca name=\"ui\"\u003e\u003c/a\u003eUI \u003csup\u003e[Back ⇈](#contents)\u003c/sup\u003e","etc"],"sub_categories":["框架"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBennyWang%2FKBinding","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FBennyWang%2FKBinding","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FBennyWang%2FKBinding/lists"}