Android MVVM framework write in kotlin, develop Android has never been so fun.

Related tags

Kotlin KBinding
Overview

KBinding

中文版

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

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() {
    @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}!")
    }
}

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() {
    @delegate:Property
    var name: String by Delegates.property("[email protected]")
    
    @delegate:Property
    var password: String by Delegates.property("xxxxxx")
    
    @Command
    val login() {
        //login processing
    }
}

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() : 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 }
}

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

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.2.3'
    kapt 'com.benny.library:kbinding-compiler:0.2.3'
    
    // for common bindings, View, TextView, and ...
    compile 'com.benny.library:kbinding-common:0.2.3'
    // for recyclerview bindings
    compile 'com.benny.library:kbinding-adapterview:0.2.3'
    // for support v4 bindings
    compile 'com.benny.library:kbinding-support-v4:0.2.3'
}

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

You might also like...
Minimum-mvvm-room-database - MVVM With Simple Room Database
Minimum-mvvm-room-database - MVVM With Simple Room Database

Minimum MVVM Koin (Kotlin Dependency Injection) Coroutines View Model Lifecycle

Firebase with MVVM is a series of videos in which you will learn how to implement firebase with MVVM along with UI designs, GitHub branches, merging, and resolving conflicts.
Firebase with MVVM is a series of videos in which you will learn how to implement firebase with MVVM along with UI designs, GitHub branches, merging, and resolving conflicts.

In this project we will learn about Firebase Implementation with MVVM Architecture. It is a basic level Course and will go with project based approach so can understand better that how the things are working.

A framework for writing composable parsers based on Kotlin Coroutines.

Parsus A framework for writing composable parsers based on Kotlin Coroutines. val booleanGrammar = object : GrammarBooleanExpression() { val ws

Kotlin Multiplatform lifecycle-aware business logic components (aka BLoCs) with routing functionality and pluggable UI (Jetpack Compose, SwiftUI, JS React, etc.), inspired by Badoos RIBs fork of the Uber RIBs framework
Kotlin Multiplatform lifecycle-aware business logic components (aka BLoCs) with routing functionality and pluggable UI (Jetpack Compose, SwiftUI, JS React, etc.), inspired by Badoos RIBs fork of the Uber RIBs framework

Decompose Please see the project website for documentation and APIs. Decompose is a Kotlin Multiplatform library for breaking down your code into life

Kotlin Multiplatform Mobile + Mobile Declarative UI Framework (Jetpack Compose and SwiftUI)

Kotlin Multiplatform Mobile + Mobile Declarative UI Framework (Jetpack Compose and SwiftUI)

Framework for quickly creating connected applications in Kotlin with minimal effort
Framework for quickly creating connected applications in Kotlin with minimal effort

Ktor is an asynchronous framework for creating microservices, web applications and more. Written in Kotlin from the ground up. import io.ktor.server.n

A modular object storage framework for Kotlin multiplatform projects.

ObjectStore A modular object storage framework for Kotlin multiplatform projects. Usage ObjectStore provides a simple key/value storage interface whic

This is powerful android framework
This is powerful android framework

FlairFramework This is an android framework for build complex application with different architectures (MVC ready/MVP/MVVM/MVI ets). It's create on to

Ivy FRP is a Functional Reactive Programming framework for declarative-style programming for Android

FRP (Functional Reactive Programming) framework for declarative-style programming for Andorid. :rocket: (compatible with Jetpack Compose)

Comments
  • Exposing more of the Rx to users

    Exposing more of the Rx to users

    This is some great stuff! We've started playing around with it at Jongla (in Finland) and we really like the approach!

    One thing that we'd love to see if possible is an API for OneWay binding that gets rid of the converter and the whole "one time" thing and functions purely on Rx. Internally this is happening in your code base (for example, you use Observable.take to implement one-time binding), but if the inside of the observable chain could be manipulated (for example, via "compose") then arbitrary mappings of bound variables could make their way down to the UI.

    opened by mikesol 3
  • anko-common artifact is deprecated and  renamed to anko-commons

    anko-common artifact is deprecated and renamed to anko-commons

    The dependency of anko-common is deprecated and renamed to anko-commons at version 0.10 This cause the Gradle can't solve the dependency conflict problem with newer version of anko-commons. I had to manually to exclude the anko-common from dependency as a workaround.

    configurations {
        //手動排除一個舊版模組,因為差了一個字所以沒辦法被正確處理版本衝突
        all*.exclude group: 'org.jetbrains.anko', module: 'anko-common'
    }
    dependencies {
    ...
     compile "org.jetbrains.anko:anko-commons:$anko_version"
    ...
    }
    
    opened by LaysDragon 1
  • Binding according to the generated associated classes

    Binding according to the generated associated classes

    Currently, this library is binding according to the simple name of binded member, what is a string programmer may typo. I highly recommend you refer to DBFlow's approach, generates the associated classes for each model, contains special properties for binded members, and binding according to the special properties. E.g. The model:

    class SimpleModel : ViewModel() {
        @delegate:Property
        var name by Delegates.property<String>()
    }
    

    The associated class:

    class SimpleModel_KB {
        val name: BindingProperty<String> = ...
    }
    

    Using in UI:

    textView {
        bind { text(SimpleModel_KB.name) }
    }
    
    opened by ztc1997 1
Releases(0.2.0)
Owner
Benny
Am I a full stack developer? Aha!
Benny
SharedPreference usage made fun in Kotlin

PreferenceHolder Kotlin Android Library, that makes preference usage simple and fun. To stay up-to-date with news about library This library is younge

Marcin Moskała 155 Dec 15, 2022
Easy to use cryptographic framework for data protection: secure messaging with forward secrecy and secure data storage. Has unified APIs across 14 platforms.

Themis provides strong, usable cryptography for busy people General purpose cryptographic library for storage and messaging for iOS (Swift, Obj-C), An

Cossack Labs 1.6k Jan 8, 2023
An example for who are all going to start learning Kotlin programming language to develop Android application.

Kotlin Example Here is an example for who are all going to start learning Kotlin programming language to develop Android application. First check this

Prabhakar Thota 56 Sep 16, 2022
🍼Debug Bottle is an Android runtime debug / develop tools written using kotlin language.

???? 中文 / ???? 日本語 / ???? English ?? Debug Bottle An Android debug / develop tools written using Kotlin language. All the features in Debug bottle are

Yuriel Arlencloyn 846 Nov 14, 2022
Develop an API that moves a rover around on a grid

Mars Rover Kata Develop an API that moves a rover around on a grid. Rules: You are given the initial starting 2D point (x,y) of a rover and the direct

Alvaro Martin Rodriguez 1 Dec 17, 2021
The KPy gradle plugin allows you to write Kotlin/Native code and use it from python.

The KPy gradle plugin allows you to write Kotlin/Native code and use it from python.

Martmists 14 Dec 26, 2022
Don't write a RecyclerView adapter again. Not even a ViewHolder!

LastAdapter Don't write a RecyclerView adapter again. Not even a ViewHolder! Based on Android Data Binding Written in Kotlin No need to write the adap

Miguel Ángel Moreno 781 Dec 19, 2022
use kmm to write a flutter plugin

use KMM to write a flutter plugin The reference plugin_codelab example plugin that accompanies the How to write a Flutter plugin codelab. I changed pl

libill 8 Nov 9, 2022
This repository is part of a Uni-Project to write a complete Compiler for a subset of Java.

Compiler This repository is part of a Uni-Project to write a complete Compiler for a subset of Java. Features error recovery using context sensitive a

null 3 Jan 10, 2022
Write a Ghidra Extension without using Java or Eclipse!

Ghidra Extension in Kotlin using IntelliJ IDEA Write a Ghidra Extension without using Java or Eclipse! Setup Hit Use this template at the top of the r

Florian Magin 7 Dec 15, 2022