All Projects → OlegSheliakin → State Binder

OlegSheliakin / State Binder

StateBinder is a tiny library for view state management

Programming Languages

kotlin
9241 projects

Labels

Projects that are alternatives of or similar to State Binder

Westore
更好的小程序项目架构
Stars: ✭ 3,897 (+16137.5%)
Mutual labels:  state, mvp
Reamp
A painkiller for your Android apps
Stars: ✭ 51 (+112.5%)
Mutual labels:  mvp, state
Devutils
🔥 ( 持续更新,目前含 160+ 工具类 ) DevUtils 是一个 Android 工具库,主要根据不同功能模块,封装快捷使用的工具类及 API 方法调用。该项目尽可能的便于开发人员,快捷、高效开发安全可靠的项目。
Stars: ✭ 680 (+2733.33%)
Mutual labels:  mvp
Interviewqa
面试题app,面试题库持续更新,包含Java,Android,C++,机器算法,前端
Stars: ✭ 17 (-29.17%)
Mutual labels:  mvp
Supermvp
MVP“美”图+新闻+天气预报+Material+RxJava3+Retrofit2+Glide4+AndroidX+Leakcanary+Butterknife
Stars: ✭ 763 (+3079.17%)
Mutual labels:  mvp
Pullstate
Simple state stores using immer and React hooks - re-use parts of your state by pulling it anywhere you like!
Stars: ✭ 683 (+2745.83%)
Mutual labels:  state
Mvpart
🎨 A new Android MVP architecture (此框架旨在解决传统 MVP 类和接口太多, 并且 Presenter 和 View 通过接口通信过于繁琐, 重用 Presenter 代价太大等问题).
Stars: ✭ 776 (+3133.33%)
Mutual labels:  mvp
Universal
Seed project for Angular Universal apps featuring Server-Side Rendering (SSR), Webpack, CLI scaffolding, dev/prod modes, AoT compilation, HMR, SCSS compilation, lazy loading, config, cache, i18n, SEO, and TSLint/codelyzer
Stars: ✭ 669 (+2687.5%)
Mutual labels:  state
Fcfrtmvp
🔥FcfrtMvp+RxHttp+RxJava(Kotlin和JAVA共用完美支持)支持一键创建MVP项目,框架简约风格及详细注释,欢迎 star or fork!
Stars: ✭ 23 (-4.17%)
Mutual labels:  mvp
Easygank
💊 The project build framework based on the Rx series and MVP pattern.
Stars: ✭ 750 (+3025%)
Mutual labels:  mvp
Mvp Retrofit Rxjava Dagger2
MVP+Retrofit+RxJava+Dagger2
Stars: ✭ 6 (-75%)
Mutual labels:  mvp
Ribs
Uber's cross-platform mobile architecture framework.
Stars: ✭ 6,641 (+27570.83%)
Mutual labels:  mvp
Sticky State
StickyState is a high performant module making native position:sticky statefull and polyfilling the missing sticky browser feature
Stars: ✭ 692 (+2783.33%)
Mutual labels:  state
Salty Whales
Extra ordinary Docker images for Salt. Whales love Salt.
Stars: ✭ 5 (-79.17%)
Mutual labels:  state
Stent
Stent is combining the ideas of redux with the concept of state machines
Stars: ✭ 681 (+2737.5%)
Mutual labels:  state
Mithril Data
A rich data model library for Mithril javascript framework
Stars: ✭ 17 (-29.17%)
Mutual labels:  state
Wanandroid
🐔🏀【停止维护,已使用Jetpack+Mvvm重构】根据鸿神提供的WanAndroid开放Api来制作的产品级玩安卓App,采用Kotlin语言,基于Material Design+AndroidX +MVP+RxJava+Retrofit等框架开发,注释超详细,方便大家练手
Stars: ✭ 674 (+2708.33%)
Mutual labels:  mvp
Gankdaily
A application used to show technical information in every working days, use MVP pattern.
Stars: ✭ 704 (+2833.33%)
Mutual labels:  mvp
Multityperecyclerviewadapter
一个专注于RecyclerView优雅刷新(接管资源和数据源)、高灵活、低耦合、健壮性以及高效性的MVP模式库,支持大多数Adapter
Stars: ✭ 763 (+3079.17%)
Mutual labels:  mvp
Redux Tree
An alternative way to compose Redux reducers
Stars: ✭ 23 (-4.17%)
Mutual labels:  state

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].