在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):orbit-mvi/orbit-mvi开源软件地址(OpenSource Url):https://github.com/orbit-mvi/orbit-mvi开源编程语言(OpenSource Language):Kotlin 97.8%开源软件介绍(OpenSource Introduction):Orbit MultiplatformGet in touchWhat is OrbitOrbit is a Redux/MVI-like library - but without the baggage. It's so simple we think of it as MVVM+.
DocumentationArticles & TalksGetting startedimplementation("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 contractdata class CalculatorState(
val total: Int = 0
)
sealed class CalculatorSideEffect {
data class Toast(val text: String) : CalculatorSideEffect()
} Create the ViewModel
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 Connect to the ViewModel in your Activity or Fragmentclass 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)
}
} ContributingPlease read contributing for details on our code of conduct, and the process for submitting pull requests to us. VersioningWe use SemVer for versioning. For the versions available, see the tags on this repository. LicenseThis project is licensed under the Apache License, Version 2.0 - see the license file for details |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论