Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
renetik committed Jul 24, 2023
1 parent 33f70c9 commit be6405e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import renetik.android.core.lang.CSFunc
import renetik.android.core.lang.void
import renetik.android.event.registration.CSHasRegistrations
import renetik.android.event.registration.CSRegistration
import renetik.android.event.registration.registerLater
import renetik.android.event.util.CSLater.later

class CSLaterOnceFunc(
private val parent: CSHasRegistrations,
Expand All @@ -20,7 +20,7 @@ class CSLaterOnceFunc(
var registration: CSRegistration? = null
override operator fun invoke() {
registration?.cancel()
registration = parent.registerLater(after, function)
registration = parent.later(after, function)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,37 @@ import androidx.annotation.AnyThread
import renetik.android.core.lang.CSHandler.mainHandler
import renetik.android.event.registration.CSRegistration.Companion.CSRegistration
import renetik.android.event.registration.task.CSBackground.background
import renetik.android.event.util.CSLater.later

@AnyThread
fun CSHasRegistrations.registerLater(
delay: Int = 0, function: () -> Unit,
): CSRegistration {
lateinit var registration: CSRegistration
registration = register(later(if (delay < 10) 10 else delay) {
cancel(registration)
function()
})
return CSRegistration { if (!registration.isCanceled) cancel(registration) }
}
//@AnyThread
//fun CSHasRegistrations.registerLater(
// after: Int = 0, function: () -> Unit,
//): CSRegistration {
// lateinit var registration: CSRegistration
// registration = register(later(if (after < 10) 10 else after) {
// cancel(registration)
// function()
// })
// return CSRegistration { if (!registration.isCanceled) cancel(registration) }
//}

@Deprecated("Can we use instead later extension on HasDestroy ?")
@AnyThread
fun CSHasRegistrations.registerLater(
function: () -> Unit,
): CSRegistration {
lateinit var registration: CSRegistration
registration = register(later(10) {
cancel(registration)
function()
})
return CSRegistration { if (!registration.isCanceled) cancel(registration) }
}
//@Deprecated("Can we use instead later extension on HasDestroy ?")
//@AnyThread
//fun CSHasRegistrations.registerLater(
// function: () -> Unit,
//): CSRegistration {
// lateinit var registration: CSRegistration
// registration = register(later(10) {
// cancel(registration)
// function()
// })
// return CSRegistration { if (!registration.isCanceled) cancel(registration) }
//}

@AnyThread
fun CSHasRegistrations.registerLaterEach(
delay: Int, period: Int = delay, function: () -> Unit,
after: Int, period: Int = after, function: () -> Unit,
): CSRegistration {
val registration = register(mainHandler.laterEach(delay, period, function))
val registration = register(mainHandler.laterEach(after, period, function))
return CSRegistration { cancel(registration) }
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ inline fun Handler.laterEach(
after: Int, period: Int = after, crossinline function: Func
): CSRegistration {
val token = object {}
var isCanceled = false
lateinit var registration: CSRegistration
lateinit var runnable: Func
runnable = {
if (!isCanceled) {
function()
if (!isCanceled) postAtTime(runnable, token, uptimeMillis() + period.toLong())
if (!registration.isCanceled) {
if (registration.isActive) function()
if (!registration.isCanceled) postAtTime(
runnable,
token,
uptimeMillis() + period.toLong()
)
}
}
postAtTime(runnable, token, uptimeMillis() + after.toLong())
return CSRegistration(isActive = true) {
isCanceled = true
removeCallbacksAndMessages(token)
}
registration = CSRegistration(isActive = true) { removeCallbacksAndMessages(token) }
return registration
}

//fun laterEach(
Expand Down
24 changes: 18 additions & 6 deletions library/src/main/java/renetik/android/event/util/CSLater.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import renetik.android.core.java.lang.isThreadMain
import renetik.android.core.lang.CSHandler.mainHandler
import renetik.android.core.lang.CSHandler.postOnMain
import renetik.android.event.common.CSHasDestruct
import renetik.android.event.registration.CSHasRegistrations
import renetik.android.event.registration.CSRegistration
import renetik.android.event.registration.CSRegistration.Companion.CSRegistration
import renetik.android.event.registration.cancel
import renetik.android.event.registration.later
import renetik.android.event.registration.laterEach
import renetik.android.event.registration.register

object CSLater {
inline fun later(
Expand All @@ -17,18 +21,26 @@ object CSLater {
crossinline function: () -> Unit
): CSRegistration = later(5, function)

inline fun CSHasDestruct.later(
after: Int, crossinline function: () -> Unit
): CSRegistration = CSLater.later(after) { if (!isDestructed) function() }

inline fun CSHasDestruct.later(crossinline function: () -> Unit) =
postOnMain { if (!isDestructed) function() }

inline fun CSHasDestruct.onMain(crossinline function: () -> Unit) =
if (isThreadMain) function() else postOnMain { if (!isDestructed) function() }
if (isThreadMain) function()
else postOnMain { if (!isDestructed) function() }

// = CSLater.later(after) { if (!isDestructed) function() }
inline fun CSHasRegistrations.later(
after: Int, crossinline function: () -> Unit
): CSRegistration {
lateinit var registration: CSRegistration
registration = register(mainHandler.later(if (after < 10) 10 else after) {
cancel(registration)
function()
})
return CSRegistration { if (!registration.isCanceled) cancel(registration) }
}

inline fun laterEach(
after: Int, period: Int = after, crossinline function: () -> Unit
): CSRegistration = mainHandler.laterEach(after, period, function)

}

0 comments on commit be6405e

Please sign in to comment.