Skip to content

Commit

Permalink
fix type cast for ConfirmationEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex committed Dec 8, 2021
1 parent 2653d21 commit 0c95dd1
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 2 deletions.
2 changes: 1 addition & 1 deletion reactiveviewmodel/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ afterEvaluate {
release(MavenPublication) {
from components.release
groupId = 'com.alexdeww.reactiveviewmodel'
version = '2.3.9'
version = '2.4.0'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class ConfirmationEvent<T : Any> internal constructor(debounceInterval: Long? =
private sealed class EventType {
data class Pending(val data: Any) : EventType()
object Confirmed : EventType()

@Suppress("UNCHECKED_CAST")
fun <T> tryGetData(): T? = if (this is Pending) data as T else null
}

private val eventState = State<EventType>(EventType.Confirmed, debounceInterval)
Expand Down Expand Up @@ -69,7 +72,7 @@ class ConfirmationEvent<T : Any> internal constructor(debounceInterval: Long? =
@Suppress("UNCHECKED_CAST")
override fun onActive() {
super.onActive()
addSource(eventState.liveData) { value = (it as? EventType.Pending)?.data as T }
addSource(eventState.liveData) { value = it.tryGetData<T>() }
}

override fun onInactive() {
Expand Down

0 comments on commit 0c95dd1

Please sign in to comment.