Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/getspherelabs/meteor-kmp
Meteor is a simple framework that helps you to create application using the MVI architecture.
https://github.com/getspherelabs/meteor-kmp
hacktoberfest hacktoberfest2023 kmm kotlin meteor multiplatform mvi
Last synced: 25 days ago
JSON representation
Meteor is a simple framework that helps you to create application using the MVI architecture.
- Host: GitHub
- URL: https://github.com/getspherelabs/meteor-kmp
- Owner: getspherelabs
- License: apache-2.0
- Created: 2023-05-28T03:13:04.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-10-06T01:27:37.000Z (about 1 year ago)
- Last Synced: 2024-10-01T05:05:48.727Z (about 1 month ago)
- Topics: hacktoberfest, hacktoberfest2023, kmm, kotlin, meteor, multiplatform, mvi
- Language: Kotlin
- Homepage: https://getspherelabs.github.io/meteor-kmp/
- Size: 6.12 MB
- Stars: 56
- Watchers: 1
- Forks: 4
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
- kmp-awesome - Meteor - MVI framework with CommonViewModel, CommonStateFlow, UseCase (Libraries / 🏗 Architecture)
README
Meteor KMP
Meteor is a simple framework that helps you to create application using the MVI architecture. It provides a robust structure for organizing your code and separating concerns. Additionally, Meteor integrates with Kotlin Coroutine that helps you to write asynchronous and concurrent code.
## Features
- Meteor follows the Model-View-Intent (MVI) architecture pattern.
- It provides a unidirectional data flow (UDF), allowing you to handle state changes and propagate them to the UI efficiently.
- Supports Kotlin Coroutines: asynchronous and concurrent code in a more concise and structured way.
- Meteor supports Kotlin Multiplatform, allowing you to share code across multiple platforms.
- Easy to test (integration and unit tests).
- Additional features like common use cases, state flow, view model and logger.## Documentation 📖
- [Getting Started](https://getspherelabs.github.io/meteor/guides/getting-started)
- [Core Concepts](https://getspherelabs.github.io/meteor/guides/concepts)
- [Common Components](https://getspherelabs.github.io/meteor/common/vm)
- [Common ViewModel](https://getspherelabs.github.io/meteor/common/vm)
- [Common Flow](https://getspherelabs.github.io/meteor/common/flow)
- [Common UseCase](https://getspherelabs.github.io/meteor/common/usecase)
- [Testing](https://getspherelabs.github.io/meteor/test/tests)## Setup
Add the dependency below into your module's `build.gradle.kts` file:
```kt
// It includes viewmodel, stateflow and core components.
implementation("io.github.behzodhalil:meteor-mvi:")
// If only need common use case
implementation("io.github.behzodhalil:meteor-usecase:")
// For testing
testImplementation("io.github.behzodhalil:meteor-test:")
```
### Define the contract```kt
data class CountState(
val count: Int = 0
)sealed interface CountWish {
object Increase : CountWish
object Decrease : CountWish
object Reset : CountWish
object ZeroValue : CountWish
}sealed interface CountEffect {
data class Failure(val message: String) : CountEffect
}
````State` represents the current state of your application. `Effects` are a way to handle side effects in Meteor. Side effects can include operations such as making network requests, updating a database, displaying UI messages, or triggering external actions. A `Wish` in Meteor represents an action or an intention to change the state of the application.
### Create the ViewModel
Define a ViewModel class, such as `MainViewModel`, that extends `CommonViewModel` with the appropriate state, wish, and effect types:
```kt
class MainViewModel : CommonViewModel() {
// ....
}
```Inside your ViewModel, override the store property by creating a `Meteor` store using the `createMeteor` function and providing the necessary configurations:
```kt
override val store: Store = createMeteor(
configs = MeteorConfigs.build {
initialState = CountState.Empty
storeName = "MainViewModel"
reducer = CountReducer
middleware = CountMiddleware
}
)
```Define properties for effect and state in your ViewModel, which will expose the effect and state as `CommonFlow` and `CommonStateFlow`, respectively:
```kt
val effect: NonNullCommonFlow = store.effect.asCommonFlow()
val state: NonNullCommonStateFlow = store.state.asCommonStateFlow()```
### Define Reducer and Middleware
Create an object / class , such as `CountReducer`, that implements the `Reducer` interface with the appropriate state, wish, and effect types:
```kt
object CountReducer : Reducer {
override fun reduce(state: CountState, wish: CountWish): Change {
return when (wish) {
CountWish.Decrease -> {
expect { state.copy(count = state.count - 1) }
}
CountWish.Increase -> {
expect { state.copy(count = state.count + 1) }
}
CountWish.Reset -> {
expect { state.copy(count = 0) }
}
CountWish.ZeroValue -> {
effect {
CountEffect.Failure("The value is zero")
}
}
}
}}
```
### Using in Fragment / Compose
```kt
viewModel.state.onEach { state ->
// Handle the state
}.launchIn(lifecycleScope)viewModel.effect.onEach { effect ->
// Handle the effect, such as displaying a toast message or triggering an action
}.launchIn(lifecycleScope)```
## Inspired by* [Orbit MVI](https://github.com/orbit-mvi/orbit-mvi)
* [MVIKotlin](https://github.com/arkivanov/MVIKotlin)## Credits
* [kmp-viewmodel](https://github.com/hoc081098/kmp-viewmodel/tree/master/viewmodel)
## License[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE.md)
This project is licensed under the Apache License, Version 2.0 - see the
[license](LICENSE.md) file for details