Skip to content

Commit

Permalink
Merge pull request #35 from etiennelenhart/#34-peek-parameter
Browse files Browse the repository at this point in the history
Add 'event' parameter to 'handled' lambda in 'ViewEvent' 'peek()'
  • Loading branch information
Etienne Lenhart authored Oct 8, 2018
2 parents 4253d1e + 136b652 commit 1856695
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
12 changes: 6 additions & 6 deletions MIGRATION3-4.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ viewModel.state.observe(this, Observer {
```
New way with support for exhaustive when expressions:
```kotlin
viewModel.observeState(this) {
it.event?.peek {
when (it.event) {
viewModel.observeState(this) { state ->
state.event?.peek {
when (it) {
is CatViewEvent.Meow -> {
// show Meow! dialog
true
Expand All @@ -51,9 +51,9 @@ viewModel.observeState(this) {
```
To handle only some of the possible events just use `else -> false` in the when expression:
```kotlin
viewModel.observeState(this) {
it.event?.peek {
when (it.event) {
viewModel.observeState(this) { state ->
state.event?.peek {
when (it) {
is CatViewEvent.Meow -> {
// show Meow! dialog
true
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ updateState { it.copy(event = CatViewEvent.Meow()) }
```
The only violation of an immutable state that Eiffel permits is to mark a `ViewEvent` as "handled". Since these are one-off events, the possibility of inconsistent UI elements is low and the benefit of keeping the ViewModel's public functions lean prevails.

To process and handle an event from an `Activity` you can use a when expression inside the `peek()` function of a `ViewEvent`:
To process and handle an event from an `Activity` you can use a when expression inside the `peek()` extension function of a `ViewEvent`:
```kotlin
viewModel.observeState(this) {
it.event?.peek {
when (it.event) {
viewModel.observeState(this) { state ->
state.event?.peek {
when (it) {
is CatViewEvent.Meow -> {
// show Meow! dialog
true
Expand All @@ -164,9 +164,9 @@ viewModel.observeState(this) {
```
If the event could be handled just return `true` which internally marks the `ViewEvent` as handled. Handling only some of the possible events from an observer is as easy as using `else -> false` in the when expression:
```kotlin
viewModel.observeState(this) {
it.event?.peek {
when (it.event) {
viewModel.observeState(this) { state ->
state.event?.peek {
when (it) {
is CatViewEvent.Meow -> {
// show Meow! dialog
true
Expand Down
28 changes: 15 additions & 13 deletions eiffel/src/main/java/com/etiennelenhart/eiffel/state/ViewEvent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ package com.etiennelenhart.eiffel.state
* Observers can then check for unhandled events like this:
*
* ```
* viewModel.observeState(this) {
* it.event?.peek {
* when (it.event) {
* viewModel.observeState(this) { state ->
* state.event?.peek {
* when (it) {
* is SampleViewEvent.ShowSample -> {
* ...
* true
Expand All @@ -33,18 +33,20 @@ package com.etiennelenhart.eiffel.state
* }
* }
* ```
*
* @property[handled] `true` if this event has been handled.
*/
abstract class ViewEvent {

private var alreadyHandled = false
var handled = false
}

/**
* Allows observers to look at the event's type and decide if they can handle it.
*
* @param[handled] Lambda expression that is called when this event is unhandled. Should return `true` if the event
* was handled and `false` if it was ignored or could not be handled.
*/
fun peek(handled: () -> Boolean) {
if (!alreadyHandled) alreadyHandled = handled()
}
/**
* Allows observers to look at the event's type and decide if they can handle it.
*
* @param[handled] Lambda expression that is called with an unhandled event. Should return `true` if the event
* was handled and `false` if it was ignored or could not be handled.
*/
fun <T : ViewEvent> T.peek(handled: (event: T) -> Boolean) {
if (!this.handled) this.handled = handled(this)
}

0 comments on commit 1856695

Please sign in to comment.