-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
71 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
library/src/main/java/renetik/android/event/registration/CSHasChangeValue+Boolean.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package renetik.android.event.registration | ||
|
||
import kotlinx.coroutines.suspendCancellableCoroutine | ||
import renetik.android.core.kotlin.primitives.isFalse | ||
import renetik.android.core.kotlin.primitives.isTrue | ||
import renetik.android.core.lang.value.isFalse | ||
import renetik.android.core.lang.value.isTrue | ||
import renetik.android.event.registration.CSHasChangeValue.Companion.delegate | ||
import kotlin.Result.Companion.success | ||
|
||
suspend fun CSHasChangeValue<Boolean>.waitIsTrue() { | ||
if (isFalse) suspendCancellableCoroutine { | ||
var registration: CSRegistration? = null | ||
registration = onTrue { | ||
registration?.cancel() | ||
registration = null | ||
it.resumeWith(success(Unit)) | ||
} | ||
it.invokeOnCancellation { registration?.cancel() } | ||
} | ||
} | ||
|
||
suspend fun CSHasChangeValue<Boolean>.waitIsFalse() { | ||
if (isTrue) suspendCancellableCoroutine { | ||
var registration: CSRegistration? = null | ||
registration = onFalse { | ||
registration?.cancel() | ||
registration = null | ||
it.resumeWith(success(Unit)) | ||
} | ||
it.invokeOnCancellation { registration?.cancel() } | ||
} | ||
} | ||
|
||
fun CSHasChangeValue<Boolean>.onFalse(function: () -> Unit) = | ||
onChange { if (it.isFalse) function() } | ||
|
||
|
||
fun CSHasChangeValue<Boolean>.onTrue(function: () -> Unit) = | ||
onChange { if (it.isTrue) function() } | ||
|
||
fun CSHasChangeValue<Boolean>.actionTrue(function: () -> Unit): CSRegistration { | ||
if (isTrue) function() | ||
return onTrue(function) | ||
} | ||
|
||
fun CSHasChangeValue<Boolean>.actionFalse(function: () -> Unit): CSRegistration { | ||
if (isFalse) function() | ||
return onFalse(function) | ||
} | ||
|
||
operator fun CSHasChangeValue<Boolean>.not() = delegate(from = { !it }) | ||
|
||
infix fun CSHasChangeValue<Boolean>.and(other: CSHasChangeValue<Boolean>) = | ||
(this to other).delegate(from = { first, second -> first && second }) | ||
|
||
infix fun CSHasChangeValue<Boolean>.or(other: CSHasChangeValue<Boolean>) = | ||
(this to other).delegate(from = { first, second -> first || second }) |