From f1f32bfa047ceb0bb68d4dc9e3858c1615433964 Mon Sep 17 00:00:00 2001 From: Thiago Santos Date: Thu, 22 Feb 2024 22:01:51 -0300 Subject: [PATCH] chore: readme updated --- README.md | 60 +++++++++++++++++++------------------------------------ 1 file changed, 21 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index ca72bd9..28255d4 100644 --- a/README.md +++ b/README.md @@ -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 } @@ -47,9 +47,7 @@ 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 } @@ -57,33 +55,27 @@ fun HomeScreen() { ### 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 { - override fun onChanged(previous: T, next: T) { - // ... - } -} - class CounterViewModel { - val counter = basicValueManager( - initialValue = 0, - changeHandler = ChangeHandlerImpl() - ) + val counter = basicValueManager(initialValue = 0) + + init { + counter.onChanged { + + } + } } ``` @@ -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 @@ -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(initialValue = 0) { +class CounterValueManager : BaseValueManager(initialValue = 0) { // Now all operations is available here } ``` -#### A coroutines context version -```kotlin -class CounterViewModel : BaseFlowValueManager(initialValue = 0) { - // Now all operations is available here -} -```