Git Product home page Git Product logo

state-binder's Introduction

StateBinder

Download

StateBinder is a tiny library for view state management.

If you use the MVI pattern or any other pattern using the concept of states to develop your applications, you may have encountered the problem of frequently updating widgets, which leads to poor performance especially when you perform frequent screen state updates. StateBinder eliminates redundant view rendering when the state changes.

Download

dependencies {
  impelentation 'com.olegsheliakin:statebinder:latest'
}

How to use?

  1. Create State class for View:
data class MainState(
    val label: String,
    val errorText: String?
) : State
  1. Create StateBinder:
private val stateBinder: StateBinder<MainState> = StateBinder.create()
  1. Bind state's properties to actions:
stateBinder.apply {
            bind(MainState::label) {
                tvLabel.text = it
            }
            bindNullable(MainState::errorText) {
                etText.error = it
            }
        }

Actions will be called only when properties change.

  1. Create ViewModel/Presenter or any other class that is responsible for changing and emitting State:

    class MainViewModel : ViewModel() {
    
        private val internalState = MutableLiveData<MainState>()
    
        val state: LiveData<MainState> = internalState
    
        init {
            internalState.value = MainState("", null)
        }
    
        fun loadText() {
            internalState.value = MainState("Text", null)
        }
    
        fun initError() {
            internalState.value = MainState("", "Error")
        }
    
    }
  2. Update state by calling:

stateBinder.newState(newState)

Full code:

class MainFragment : Fragment() {

    private val viewModel: MainViewModel by lazy {
        return@lazy ViewModelProviders.of(this@MainFragment)[MainViewModel::class.java]
    }

    //creates StateBinder
    private val stateBinder: StateBinder<MainState> = StateBinder.create()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_main, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //refreshes the current state when view is created
        stateBinder.applyCurrentState() 
      
        //binds properties to actions
        stateBinder.apply {
            bind(MainState::label) {
                tvLabel.text = it
            }
            bindNullable(MainState::errorText) {
                etText.error = it
            }
        }
        
        //observes MainState
        viewModel.state.observe(viewLifecycleOwner, Observer {
            it?.let(stateBinder::newState)
        })

    }
}

License

The MIT License (MIT)
=====================

Copyright © 2019 Oleg Sheliakin

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the “Software”), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.

state-binder's People

Contributors

olegsheliakin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

state-binder's Issues

How do you combine 2 or more elements of state?

So first of all, thank you for this great stuff!
It's much pretty, than the previous solution what I used for it.

On the screen sometimes there are parts, which depend on more data, but I also use these data alone on different parts.
I guess it will be cool if I could bind 2 or more part of state to avoid unnecessary redundant data it the viewstate.

I will try to extend the library with this on this week, if I will have time, but I appreciate every ideas :)

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.