Giter Club home page Giter Club logo

konduit's Introduction

License

Konduit - make Android Views fun again

Konduit - is a React inspired way to bind data to Android Views

Konduit is a layer on top of MVP where a Presenter talks to a View interface. Instead of calling the View interface directly you declare your UI using Widgets and Konduit will inform the View about changes automatically.

In traditional MVP implementations you have to update the View when

  • data changes (partially for each change) or
  • a new View attaches (orientation change). Having two implementations is hard to maintain and it's nothing new that mutation is the main cause of many problems.

Widgets are immutable data objects which describe the state of a View and provides listeners for user interactions. Each Widget will be bound to the corresponding Android View, a 1:1 mapping. The immutability of the Widgets allows clever diffing and reduces unnecessary updates of the Views.

Instead of calling the View directly you have to implement the build() function which should return a collection of Widgets describing your UI state. The build() function will be automatically called by Konduit when you change the state by calling setState { }. You don't have to worry anymore if your view is currently detached (view == null) causing NullPointerExceptions.

override fun build(context: BuildContext): List<Widget> {
    return widgetList {
    
        text {
            key = R.id.counter_label
            text = "Clicked $count times"
        }

        button {
            key = R.id.increment
            text = if (count == 0) "Click me" else "Increment"
            onClick = onButtonClicked
        }
    }
}

When Konduit detects changes compared to the previous Widgets it will inform the View by calling the single render(List<Widget>) method. This method receives all Widgets at once which is crucial for testing (see later). Konduit then performs a second diff per Widget and binds changed properties to the correct Android View.

Testing

The main goal of Konduit was to ease testing. Espresso testing is great, but it requires an Android Emulator. This makes testing slow and flaky.

Konduit allows you write espresso like tests on the JVM.

    @Test
    fun `button click increments`() {
        val ui = MainPresenter().testUi()

        // Given the UI shows the initial state
        assertThat(ui.widget<Text>(R.id.counter_label).text).contains("0")

        // When clicking the increment button
        ui.widget<Button>(R.id.increment).click()

        // Then the counter will be incremented by 1
        assertThat(ui.widget<Text>(R.id.counter_label).text).contains("1")

    }

//TODO add more details

Internationalization

One of the goals of MVP is the separation from the untestable Android Framework allowing us to write JVM test. When it comes to i18n this can be hard to achieve.

//TODO

Download

repositories {
    jcenter()
}

dependencies {
    compile 'com.pascalwelsch.konduit:konduit:0.3.0'
}

Usage

See samples

  • EditText - Two-way binding of an EditText and i18n
  • Alert - Custom Widget and dynamic View
  • RecyclerView - ListsWidget with click listener bound to custom RecyclerView.Adapter
  • Options Menu - Options menu with Widgets / manual binding

License

Copyright 2018 Pascal Welsch

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

konduit's People

Contributors

passsy avatar pakoito avatar stefma avatar

Stargazers

milan jurkulák avatar R18 avatar  avatar Caio Fabio de Araujo avatar Piyush Malaviya avatar Calvin M avatar 炸鸡叔 avatar jpgpuyo avatar  avatar Pranav Lathigara avatar Jamolkhon Khakimov avatar  avatar Adam Wilson avatar FlyingSnowBean avatar Mohamed Fathy avatar Jagdeep Singh avatar Christoph Kührer avatar Lin Zhang avatar linch avatar Shinya Kumagai avatar xxc avatar Ahmed Wahdan avatar  avatar Allen Zhang avatar Ishaan Kakkar avatar Evgenii Shishkin avatar TOYAMA Yosaku avatar touge avatar Alex Krupa avatar Quinn avatar Abdelrahman avatar Brandon Wever avatar Aidan Follestad avatar Benoit Billington avatar Tevin avatar rayworks avatar Shaishav Gandhi avatar Shohei Kawano avatar Subinkrishna Gopi avatar Daniele Campogiani avatar Vitus avatar Kirill Biakov avatar Spiros Economakis avatar  avatar Wei Qin avatar Jesse Wilson avatar Omer Karakose avatar Miguel Panelo avatar James Barr avatar Sajjad Yousefnia avatar Ranu Goldan Yeara Elzatar avatar Farhan Yuda Pahlevi avatar Ar Firman Syahputra avatar Alfian Yusuf Abdullah avatar Pratama Nur Wijaya avatar Esa Firman avatar Muhammad Abraham Al Kindy avatar raditya gumay avatar atsumo avatar Paweł Szymański avatar Gokberk Ergun avatar Ersin Ertan avatar Dayan Ruben avatar Kasim Rangwala avatar Subrahmanya S M avatar Maxwell Russek avatar Jossi Wolf avatar Dmitriy Voronin avatar Jorge Castillo avatar Kosh Sergani avatar

Watchers

milan jurkulák avatar  avatar Ersin Ertan avatar  avatar

konduit's Issues

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.