• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

orbit-mvi/orbit-mvi: A simple MVI framework for Kotlin Multiplatform and Android

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称(OpenSource Name):

orbit-mvi/orbit-mvi

开源软件地址(OpenSource Url):

https://github.com/orbit-mvi/orbit-mvi

开源编程语言(OpenSource Language):

Kotlin 97.8%

开源软件介绍(OpenSource Introduction):

Orbit Multiplatform

CI status codecov Download License

Logo

Get in touch

slack logo twitter logo

What is Orbit

Orbit is a Redux/MVI-like library - but without the baggage. It's so simple we think of it as MVVM+.

  • Simple, type-safe, coroutine-style, extensible API
  • Multiplatform, targetting Android and iOS (iOS support is in alpha and being actively worked on)
  • Full support for Kotlin Coroutines (it's built on top of them after all)
  • Lifecycle-safe collection of infinite flows
  • ViewModel support, along with SavedState
  • Optional, simple unit test library
  • Built-in espresso idling resource support
  • Compatible with RxJava, LiveData etc. through coroutine wrappers
  • And more...

Documentation

Articles & Talks

Getting started

Download

implementation("org.orbit-mvi:orbit-core:<latest-version>")
// or, if on Android:
implementation("org.orbit-mvi:orbit-viewmodel:<latest-version>")

// Tests
testImplementation("org.orbit-mvi:orbit-test:<latest-version>")

Define the contract

data class CalculatorState(
    val total: Int = 0
)

sealed class CalculatorSideEffect {
    data class Toast(val text: String) : CalculatorSideEffect()
}

Create the ViewModel

  1. Implement the ContainerHost interface
  2. Override the container field and use the ViewModel.container factory function to build an Orbit Container in your ContainerHost
class CalculatorViewModel: ContainerHost<CalculatorState, CalculatorSideEffect>, ViewModel() {

    // Include `orbit-viewmodel` for the factory function
    override val container = container<CalculatorState, CalculatorSideEffect>(CalculatorState())

    fun add(number: Int) = intent {
        postSideEffect(CalculatorSideEffect.Toast("Adding $number to ${state.total}!"))

        reduce {
            state.copy(total = state.total + number)
        }
    }
}

We have used an Android ViewModel as the most common example, but there is no requirement to do so.

Connect to the ViewModel in your Activity or Fragment

class CalculatorActivity: AppCompatActivity() {

    // Example of injection using koin, your DI system might differ
    private val viewModel by viewModel<CalculatorViewModel>()

    override fun onCreate(savedState: Bundle?) {
        ...

        addButton.setOnClickListener { viewModel.add(1234) }

        viewModel.observe(state = ::render, sideEffect = ::handleSideEffect)
    }

    private fun render(state: CalculatorState) {
        ...
    }

    private fun handleSideEffect(sideEffect: CalculatorSideEffect) {
        when (sideEffect) {
            is CalculatorSideEffect.Toast -> toast(sideEffect.text)
        }
    }
}

With Jetpack Compose wire up the ViewModel as follows:

@Composable
fun CalculatorScreen(viewModel: CalculatorViewModel) {

    val state = viewModel.collectAsState().value

    viewModel.collectSideEffect { handleSideEffect(it) }

    // render UI using data from 'state'
    ...
}

private fun handleSideEffect(sideEffect: CalculatorSideEffect) {
    when (sideEffect) {
        is CalculatorSideEffect.Toast -> toast(sideEffect.text)
    }
}

Contributing

Please read contributing for details on our code of conduct, and the process for submitting pull requests to us.

Versioning

We use SemVer for versioning. For the versions available, see the tags on this repository.

License

License

This project is licensed under the Apache License, Version 2.0 - see the license file for details




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Pleiterson/desafios-bootcamps-dio: Desafios em C#, Java, JavaScript, Kotlin, Pyt ...发布时间:2022-07-10
下一篇:
Ramotion/fluid-slider-android: 发布时间:2022-07-10
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap