Skip to content

Commit

Permalink
More stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
galex committed May 18, 2024
1 parent 97c758c commit f53776f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 14 deletions.
10 changes: 3 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@

Yamvil is a library and a compiler plugin to help you write MVI Screens in the easiest way possible on Android and Compose Multiplatform.

The runtime library helps enforcing the MVI pattern using Generics on ViewModels and Fragments, and the compiler plugin checks for the correct implementation of the MVI pattern in your @Composable functions.
## Documentation

The full documentation of Yamvil [is available right here](https://docs.galex.dev/yamvil/).

## Introduction

Yamvil is a tool built to help us write Screens that follow the MVI Pattern on Android and Compose Multiplatform.

-[MVIViewModel](https://github.com/galex/yamvil/blob/main/runtime/src/commonMain/kotlin/dev/galex/yamvil/viewmodels/MVIViewModel.kt) as a base ViewModel class that requires an `UiState`, `UiEvent` and `UiAction` classes
- ✅ Built on top of [ViewModel of Kotlin Multiplatform](https://www.jetbrains.com/help/kotlin-multiplatform-dev/compose-lifecycle.html#viewmodel-implementation)
- ✅ Avoids [losing any events](https://medium.com/androiddevelopers/viewmodel-one-off-event-antipatterns-16a1da869b95) by using a `Consumable` class built into a `BaseUIState` for one-off events
- ✅ Supports Android Fragments via inheritance of [MVIFragment](https://github.com/galex/yamvil/blob/main/runtime/src/androidMain/kotlin/dev/galex/yamvil/fragments/MVIFragment.kt)
- ✅ Checks for correct implementation of MVI in @Composable function which name finishes by "Screen"

## Installation

In your libs.version.toml, add the following:
Expand Down
2 changes: 1 addition & 1 deletion runtime/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ android {
}

dependencies {
implementation(libs.androidx.fragment.ktx)
api(libs.androidx.fragment.ktx)
testImplementation(libs.robolectric)
testImplementation(libs.truth)
testDebugImplementation(libs.androidx.fragment.testing)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package dev.galex.yamvil.sample.features.dashboard

import androidx.fragment.app.viewModels
import dev.galex.yamvil.fragments.base.MVIFragment

class DashboardFragment: MVIFragment<DashboardUiState, DashboardUiEvent, DashboardUiAction>() {

override val viewModel: DashboardViewModel by viewModels()
override fun observeUiState(state: DashboardUiState) {
when(state.state) {
DashboardUiState.ContentState.Loading -> onLoading()
is DashboardUiState.ContentState.Content -> onContent(state.state)
DashboardUiState.ContentState.Error -> onError()
}

state.onAction { action ->
when(action) {
DashboardUiAction.NavigateToNext -> navigateToNext()
}
}
}

private fun navigateToNext() {
// Do something with navigateToNext
}

private fun onLoading() {
// Do something with loading
}

private fun onContent(content: DashboardUiState.ContentState.Content) {
// Do something with content
}

private fun onError() {
// Do something with error
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package dev.galex.yamvil.sample.features.dashboard

import dev.galex.yamvil.models.BaseUiState
import dev.galex.yamvil.models.Consumable
import dev.galex.yamvil.sample.features.dashboard.DashboardUiAction
import dev.galex.yamvil.models.base.BaseUiState
import dev.galex.yamvil.models.base.Consumable


data class DashboardUiState(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package dev.galex.yamvil.sample.features.dashboard

import dev.galex.yamvil.models.Consumable
import dev.galex.yamvil.viewmodels.MVIViewModel

class DashboardViewModel: MVIViewModel<DashboardUiState, DashboardUiEvent>() {
override fun handleEvent(event: DashboardUiEvent) {

}

override fun initializeUiState(): DashboardUiState {
return DashboardUiState(state = DashboardUiState.ContentState.Loading)
}
override fun handleEvent(event: DashboardUiEvent) {
when (event) {
is DashboardUiEvent.ClickOnNext -> onClickOnNext()
}
}

private fun onClickOnNext() {
update { copy(action = Consumable(DashboardUiAction.NavigateToNext)) }
}
}

0 comments on commit f53776f

Please sign in to comment.