Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
renetik committed Jul 19, 2023
1 parent db44d2d commit e7998fd
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 40 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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 : CSProperty<*>> T.apply() = apply { fireChange() }

Expand All @@ -33,27 +27,6 @@ fun <T> CSProperty<T>.connect(property: CSProperty<T>): CSRegistration {

fun <T> CSProperty<T?>.clear() = value(null)

fun <T> CSHasChangeValue<T>.action(function: (T) -> Unit): CSRegistration {
function(value)
return onChange(function)
}

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)
}

//fun <T> CSHasChangeValue<T>.onChangeOnce(listener: (argument: T) -> Unit): CSRegistration {
// lateinit var registration: CSRegistration
// registration = onChange { argument: T ->
Expand Down Expand Up @@ -90,7 +63,7 @@ fun CSHasChangeValue<Boolean>.listenUntilFalseOnce(
}

fun CSVariable<Boolean>.connect(property: CSProperty<Boolean>): CSRegistration =
property.action { value = it }
property.action { this.value = it }

fun <T> CSProperty<T>.propertyBoolean(
parent: CSHasRegistrations? = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Unit>

inline fun <Argument> CSHasChange<Argument>.onChange(
crossinline function: () -> Unit,
): CSRegistration = onChange { _ -> function() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,17 @@ import renetik.android.core.lang.void
interface CSHasChange<Argument> {
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<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)
}

inline fun <Value> CSHasChangeValue<Value>.onChangeTo(
value: Value, crossinline onChange: () -> Unit
): CSRegistration = onChange { if (this.value == value) onChange() }
Expand All @@ -27,12 +47,12 @@ fun <Value> CSHasChangeValue<Value>.hasValue(

inline fun <ParentValue, ChildValue> CSHasChangeValue<ParentValue>.onChange(
crossinline child: (ParentValue) -> CSHasChangeValue<ChildValue>,
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()
Expand All @@ -58,7 +78,7 @@ inline fun <ParentValue, ChildValue> CSHasChangeValue<ParentValue>.onChange(

inline fun <ParentValue, ChildValue> CSHasChangeValue<ParentValue>.action(
crossinline child: (ParentValue) -> CSHasChangeValue<ChildValue>,
crossinline onChange: (ChildValue) -> Unit
noinline onChange: (ChildValue) -> Unit
): CSRegistration {
onChange(child(value).value)
return onChange(child, onChange)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ package renetik.android.event.registration
import renetik.android.core.lang.value.CSValue

interface CSHasChangeValue<T> : CSValue<T>, CSHasChange<T> {
companion object
companion object {
fun <T> CSHasChangeValue<T>.action(function: (T) -> Unit): CSRegistration {
function(value)
return onChange(function)
}
}
}

0 comments on commit e7998fd

Please sign in to comment.