diff --git a/library/src/main/java/renetik/android/event/common/CSActionIsRunning.kt b/library/src/main/java/renetik/android/event/common/CSActionIsRunning.kt index 78f8d79..0a1ce4b 100644 --- a/library/src/main/java/renetik/android/event/common/CSActionIsRunning.kt +++ b/library/src/main/java/renetik/android/event/common/CSActionIsRunning.kt @@ -1,12 +1,11 @@ package renetik.android.event.common - import renetik.android.core.kotlin.collections.list import renetik.android.event.property.CSActionInterface import renetik.android.event.property.CSProperty import renetik.android.event.property.CSProperty.Companion.property import renetik.android.event.property.CSPropertyWrapper -import renetik.android.event.property.action +import renetik.android.event.registration.CSHasChangeValue.Companion.action import renetik.android.event.registration.CSRegistration import renetik.android.event.registration.CSRegistration.Companion.CSRegistration import renetik.android.event.registration.cancel @@ -31,7 +30,7 @@ class CSActionIsRunning( property.value(runningActions.size > 0) } - val actionOnChange = register(action.action { updateIsRunning(it) }) + val actionOnChange = register(action.action(::updateIsRunning)) return CSRegistration(onCancel = { cancel(actionOnChange) diff --git a/library/src/main/java/renetik/android/event/property/CSProperty+.kt b/library/src/main/java/renetik/android/event/property/CSProperty+.kt index b9b9ce9..4981ab5 100644 --- a/library/src/main/java/renetik/android/event/property/CSProperty+.kt +++ b/library/src/main/java/renetik/android/event/property/CSProperty+.kt @@ -1,21 +1,15 @@ package renetik.android.event.property -import renetik.android.core.kotlin.primitives.isFalse -import renetik.android.core.kotlin.primitives.isTrue import renetik.android.core.lang.ArgFunc -import renetik.android.core.lang.value.isFalse -import renetik.android.core.lang.value.isTrue import renetik.android.core.lang.variable.CSVariable import renetik.android.core.lang.void -import renetik.android.event.common.CSHasDestruct -import renetik.android.event.common.update import renetik.android.event.property.CSProperty.Companion.property import renetik.android.event.registration.CSHasChangeValue +import renetik.android.event.registration.CSHasChangeValue.Companion.action import renetik.android.event.registration.CSHasRegistrations import renetik.android.event.registration.CSRegistration import renetik.android.event.registration.paused import renetik.android.event.registration.register -import kotlin.properties.Delegates.notNull fun > T.apply() = apply { fireChange() } @@ -33,27 +27,6 @@ fun CSProperty.connect(property: CSProperty): CSRegistration { fun CSProperty.clear() = value(null) -fun CSHasChangeValue.action(function: (T) -> Unit): CSRegistration { - function(value) - return onChange(function) -} - -fun CSHasChangeValue.onFalse(function: () -> Unit) = - onChange { if (it.isFalse) function() } - -fun CSHasChangeValue.onTrue(function: () -> Unit) = - onChange { if (it.isTrue) function() } - -fun CSHasChangeValue.actionTrue(function: () -> Unit): CSRegistration { - if (isTrue()) function() - return onTrue(function) -} - -fun CSHasChangeValue.actionFalse(function: () -> Unit): CSRegistration { - if (isFalse()) function() - return onFalse(function) -} - //fun CSHasChangeValue.onChangeOnce(listener: (argument: T) -> Unit): CSRegistration { // lateinit var registration: CSRegistration // registration = onChange { argument: T -> @@ -90,7 +63,7 @@ fun CSHasChangeValue.listenUntilFalseOnce( } fun CSVariable.connect(property: CSProperty): CSRegistration = - property.action { value = it } + property.action { this.value = it } fun CSProperty.propertyBoolean( parent: CSHasRegistrations? = null, diff --git a/library/src/main/java/renetik/android/event/registration/CSHasChange+.kt b/library/src/main/java/renetik/android/event/registration/CSHasChange+.kt index f9558b2..0f49bad 100644 --- a/library/src/main/java/renetik/android/event/registration/CSHasChange+.kt +++ b/library/src/main/java/renetik/android/event/registration/CSHasChange+.kt @@ -4,8 +4,6 @@ import renetik.android.core.lang.Func import renetik.android.core.lang.void import renetik.android.event.common.CSLaterOnceFunc.Companion.laterOnce -typealias HasChange = CSHasChange - inline fun CSHasChange.onChange( crossinline function: () -> Unit, ): CSRegistration = onChange { _ -> function() } diff --git a/library/src/main/java/renetik/android/event/registration/CSHasChange.kt b/library/src/main/java/renetik/android/event/registration/CSHasChange.kt index bd44790..a8251d5 100644 --- a/library/src/main/java/renetik/android/event/registration/CSHasChange.kt +++ b/library/src/main/java/renetik/android/event/registration/CSHasChange.kt @@ -5,11 +5,17 @@ import renetik.android.core.lang.void interface CSHasChange { companion object { fun onChange( - vararg hasChanges: CSHasChange<*>, function: () -> void): CSRegistration { + vararg hasChanges: CSHasChange<*>, function: () -> void + ): CSRegistration { val registrations = CSRegistrationsList(this) hasChanges.forEach { registrations.register(it.onChange(function)) } return registrations } + + fun CSHasChange<*>.action(function: () -> Unit): CSRegistration { + function() + return onChange(function) + } } fun onChange(function: (Argument) -> void): CSRegistration diff --git a/library/src/main/java/renetik/android/event/registration/CSHasChangeValue+.kt b/library/src/main/java/renetik/android/event/registration/CSHasChangeValue+.kt index 7983cc5..638153b 100644 --- a/library/src/main/java/renetik/android/event/registration/CSHasChangeValue+.kt +++ b/library/src/main/java/renetik/android/event/registration/CSHasChangeValue+.kt @@ -1,13 +1,33 @@ package renetik.android.event.registration +import renetik.android.core.kotlin.primitives.isFalse +import renetik.android.core.kotlin.primitives.isTrue import renetik.android.core.lang.ArgFunc +import renetik.android.core.lang.value.isFalse +import renetik.android.core.lang.value.isTrue import renetik.android.event.common.CSHasDestruct import renetik.android.event.common.update import renetik.android.event.property.CSProperty import renetik.android.event.property.CSProperty.Companion.property -import renetik.android.event.property.action +import renetik.android.event.registration.CSHasChangeValue.Companion.action import renetik.android.event.registration.CSRegistration.Companion.CSRegistration +fun CSHasChangeValue.onFalse(function: () -> Unit) = + onChange { if (it.isFalse) function() } + +fun CSHasChangeValue.onTrue(function: () -> Unit) = + onChange { if (it.isTrue) function() } + +fun CSHasChangeValue.actionTrue(function: () -> Unit): CSRegistration { + if (isTrue()) function() + return onTrue(function) +} + +fun CSHasChangeValue.actionFalse(function: () -> Unit): CSRegistration { + if (isFalse()) function() + return onFalse(function) +} + inline fun CSHasChangeValue.onChangeTo( value: Value, crossinline onChange: () -> Unit ): CSRegistration = onChange { if (this.value == value) onChange() } @@ -27,12 +47,12 @@ fun CSHasChangeValue.hasValue( inline fun CSHasChangeValue.onChange( crossinline child: (ParentValue) -> CSHasChangeValue, - crossinline onChange: (ChildValue) -> Unit + noinline onChange: (ChildValue) -> Unit ): CSRegistration { var childRegistration: CSRegistration? = null val parentRegistration = onChange { childRegistration?.cancel() - childRegistration = child(value).action { onChange(it) } + childRegistration = child(value).action(onChange) } return CSRegistration(isActive = true, onCancel = { parentRegistration.cancel() @@ -58,7 +78,7 @@ inline fun CSHasChangeValue.onChange( inline fun CSHasChangeValue.action( crossinline child: (ParentValue) -> CSHasChangeValue, - crossinline onChange: (ChildValue) -> Unit + noinline onChange: (ChildValue) -> Unit ): CSRegistration { onChange(child(value).value) return onChange(child, onChange) diff --git a/library/src/main/java/renetik/android/event/registration/CSHasChangeValue.kt b/library/src/main/java/renetik/android/event/registration/CSHasChangeValue.kt index 1a0696f..6fb95a0 100644 --- a/library/src/main/java/renetik/android/event/registration/CSHasChangeValue.kt +++ b/library/src/main/java/renetik/android/event/registration/CSHasChangeValue.kt @@ -3,5 +3,10 @@ package renetik.android.event.registration import renetik.android.core.lang.value.CSValue interface CSHasChangeValue : CSValue, CSHasChange { - companion object + companion object { + fun CSHasChangeValue.action(function: (T) -> Unit): CSRegistration { + function(value) + return onChange(function) + } + } } \ No newline at end of file