https://github.com/rougsig/actions-dispatcher
https://github.com/rougsig/actions-dispatcher
android annotation-processor dispatcher mvi mvi-architecture reactive state-management
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/rougsig/actions-dispatcher
- Owner: rougsig
- License: apache-2.0
- Created: 2018-08-23T21:17:53.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-08-05T12:50:24.000Z (about 4 years ago)
- Last Synced: 2025-05-16T05:38:24.616Z (5 months ago)
- Topics: android, annotation-processor, dispatcher, mvi, mvi-architecture, reactive, state-management
- Language: Kotlin
- Size: 239 KB
- Stars: 5
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Actions dispatcher
# Example
To enable generation, annotate action class with `@ActionElement` annotation:
```kotlin
class State@ActionElement(state = State::class)
sealed class Actionobject OpenArticleDetail : Action()
object LikeArticle : Action()
object DislikeArticle : Action()
object AddArticleToFavorite : Action()
```After doing that you will get an auto-generated `ActionReceiver` interface:
```kotlin
interface ActionReceiver {
fun processAddArticleToFavorite(
previousState: State,
action: AddArticleToFavorite
): Pair?>
fun processDislikeArticle(
previousState: State,
action: DislikeArticle
): Pair?>
fun processLikeArticle(
previousState: State,
action: LikeArticle
): Pair?>
fun processOpenArticleDetail(
previousState: State,
action: OpenArticleDetail
): Pair?>
}
```And `ActionsReducer` with reduce function:
```kotlin
class ActionsReducer private constructor(private val receiver: ActionReceiver) {
fun reduce(previousState: State, action: Action): Pair?> = when (action) {
is DislikeArticle -> receiver.processDislikeArticle(previousState, action)
is OpenArticleDetail -> receiver.processOpenArticleDetail(previousState, action)
is AddArticleToFavorite -> receiver.processAddArticleToFavorite(previousState, action)
is LikeArticle -> receiver.processLikeArticle(previousState, action)
}
}
```All you have to do after adding an annotation is to use a generated builder which will create this reducer for you and also you will need to implement a receiver:
```kotlin
class MyPresenter : BasePresenter(), ActionReceiver {
private val reducer = ActionsReducer.Builder()
.receiver(this) // <-- a class wich implements ActionReceiver and will receive reduce calls
.build()fun reduce(previusState: State, action: Action) {
reducer.reduce(previusState, action)
}override fun processAddArticleToFavorite(previousState: State, action: Action): Pair?> {
// process add article to favorite
}override fun processDislikeArticle(previousState: State, action: Action): Pair?> {
// process dislike article
}override fun processLikeArticle(previousState: State, action: Action): Pair?> {
// process like article
}override fun processOpenArticleDetail(previousState: State, action: Action): Pair?> {
// process open details article
}
}
```# Download
Add a Gradle dependency:
```gradle
apply plugin: 'kotlin-kapt'sourceSets {
main.java.srcDirs += 'src/main/kotlin'
main.java.srcDirs += 'build/generated/source/kaptKotlin/main' // <-- add to your module
debug.java.srcDirs += 'build/generated/source/kaptKotlin/debug' // <-- add to your module
release.java.srcDirs += 'build/generated/source/kaptKotlin/release' // <-- add to your module
test.java.srcDirs += 'src/test/kotlin'
}implementation 'com.github.rougsig:actions-dispatcher-runtime:2.0.0'
kapt 'com.github.rougsig:actions-dispatcher-processor:2.0.0'
```