Skip to content

Commit

Permalink
Merge pull request #79 from matheus-corregiari/release/1.1.0
Browse files Browse the repository at this point in the history
Release 1.1.0
  • Loading branch information
matheus-corregiari authored Jan 17, 2024
2 parents a8808e0 + 1e6f56e commit 8c27b74
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions toolkit/event-observer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ implementation "io.github.matheus-corregiari:livedata:$latest_version"

```kotlin

class MyViewModel(private val repository: MyRepository): ViewModel() {
class MyViewModel(private val repository: MyRepository) : ViewModel() {

// Use MutableResponseLiveData when you want to change the value
private val _liveData: MutableResponseLiveData<Movies> = MutableResponseLiveData()

val liveData: ResponseLiveData<Movies>
get() = _liveData

fun loadMovies() {
try {
// call postLoading to post loading true
Expand All @@ -49,7 +49,7 @@ class MyViewModel(private val repository: MyRepository): ViewModel() {
}
}

fun loadMoviesAsyncOperation() : ResponseLiveData<Movies> {
fun loadMoviesAsyncOperation(): ResponseLiveData<Movies> {
// When you don't want to implement the code on the function above you can use the function makeAsyncOperation
// She has two implementations, one using a WorkerThread and another using Coroutine

Expand All @@ -58,23 +58,23 @@ class MyViewModel(private val repository: MyRepository): ViewModel() {
}
}

fun loadMoviesAsyncOperationErrorTransformer() : ResponseLiveData<Movies> {
fun loadMoviesAsyncOperationErrorTransformer(): ResponseLiveData<Movies> {
// You can also pass a errorTransformer to makeAsyncOperation
return makeAsyncOperation(errorTransformer = {}) {
repository.loadMovies()
}
}
}

class MyActivity: AppCompatActivity() {
class MyActivity : AppCompatActivity() {

private val viewModel: MyViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel.loadMovies()
viewModel.liveData.observe(this) {
status {
status {
// Called when status change value
// The status can be:
// DataResultStatus.LOADING
Expand All @@ -96,19 +96,19 @@ class MyActivity: AppCompatActivity() {
error { e ->
// called when error change value
}

// When withData is false, the function only will be called when data is null
// This is useful because you might not want to show loading or error state when you already have data
showLoading(withData = false) {

}
loading(withData = false) {

}
error(withData = false) { e ->

}

// When single is true, the function only will be executed one time
status(single = true) { }
success(single = true) { }
Expand Down Expand Up @@ -216,3 +216,5 @@ swapResponseLiveData.clearSource()
swapResponseLiveData.needsRefresh()

```

# NOTE: This module is almost KMM ready

0 comments on commit 8c27b74

Please sign in to comment.