Skip to content

Commit

Permalink
chore: readme updated
Browse files Browse the repository at this point in the history
  • Loading branch information
programadorthi committed Feb 23, 2024
1 parent 5bbaa42 commit f1f32bf
Showing 1 changed file with 21 additions and 39 deletions.
60 changes: 21 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ There are a lot of ways to use a Value/State Manager
```kotlin
class CounterViewModel {
val counter = basicValueManager(initialValue = 0) // Basic value manager is already compose State
val counterFlow = flowValueManager(initialValue = 0) // StateFlow version
val counterFlow = counter.asMutableStateFlow() // StateFlow version

var value by basicValueManager(initialValue = 0) // Delegate property version available by compose getValue and setValue
}
Expand Down Expand Up @@ -47,43 +47,35 @@ class CounterViewModel {
@Composable
fun HomeScreen() {
val counter = basicValueManager(initialValue = 0)
var counterRemembered by rememberBasicValueManager(initialValue = 0)

val counterState by counter.collectAsState() // Available when using FlowValueManager
var counterRemembered by remember { counter.asState() }

// Update and listen operations are same
}
```

### Listening for errors
```kotlin
class ErrorHandlerImpl : ErrorHandler {
override fun onError(exception: Throwable) {
// error thrown by update operation
}
}

class CounterViewModel {
val counter = basicValueManager(
initialValue = 0,
errorHandler = ErrorHandlerImpl()
)
val counter = basicValueManager(initialValue = 0)

init {
counter.onError {

}
}
}
```

### Listening for changes
```kotlin
class ChangeHandlerImpl : ChangeHandler<T> {
override fun onChanged(previous: T, next: T) {
// ...
}
}

class CounterViewModel {
val counter = basicValueManager(
initialValue = 0,
changeHandler = ChangeHandlerImpl()
)
val counter = basicValueManager(initialValue = 0)

init {
counter.onChanged {

}
}
}
```

Expand All @@ -101,6 +93,10 @@ counter.addValidator(PositiveValidator())
// or
counter += PositiveValidator()

counter.onValidated {
// Listen on each validation operation
}

// Put a value don't trigger validations
counter.value = -1
// Call validate() to trigger validations
Expand All @@ -117,25 +113,11 @@ counter.messages()
```

### Prefer inheritance over composition?
All value manager types has a base class if you need transform your wrapper class in a value manager

```mermaid
classDiagram
MutableState <|-- ValueManager
ValueManager <|-- FlowValueManager
```

#### Any sync context version
```kotlin
class CounterViewModel : BaseValueManager<Int>(initialValue = 0) {
class CounterValueManager : BaseValueManager<Int>(initialValue = 0) {
// Now all operations is available here
}
```

#### A coroutines context version
```kotlin
class CounterViewModel : BaseFlowValueManager<Int>(initialValue = 0) {
// Now all operations is available here
}
```

0 comments on commit f1f32bf

Please sign in to comment.