Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mudando forma de usar transform #104

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -359,84 +359,13 @@ fun <T, R> LiveData<T>.chainWith(
* @param X The type of the value after transformation.
*/
fun <T, R, X> LiveData<T>.chainWith(
context: CoroutineContext,
context: CoroutineContext = EmptyCoroutineContext,
other: suspend (T?) -> LiveData<R>,
condition: suspend (T?) -> Boolean,
transform: Transform.Nullable<T, R, X>
): LiveData<X?> = liveData(context) {
internalChainWith(other, condition).applyTransformation(context, transform).collect(::emit)
}

/**
* @see LiveData.chainWith
*/
fun <T, R, X> LiveData<T>.chainWith(
context: CoroutineContext,
other: suspend (T?) -> LiveData<R>,
condition: suspend (T?) -> Boolean,
transform: Pair<CoroutineDispatcher, suspend (T?, R?) -> X?>
): LiveData<X?> = chainWith(
context = context,
other = other,
condition = condition,
transform = Transform.Nullable.OmitFail(transform.first, transform.second)
)

/**
* @see LiveData.chainWith
*/
fun <T, R, X> LiveData<T>.chainWith(
context: CoroutineContext,
other: suspend (T?) -> LiveData<R>,
condition: suspend (T?) -> Boolean,
transform: suspend (T?, R?) -> X?
): LiveData<X?> = chainWith(
context = context,
other = other,
condition = condition,
transform = Dispatchers.IO to transform
)

/**
* @see LiveData.chainWith
*/
fun <T, R, X> LiveData<T>.chainWith(
other: suspend (T?) -> LiveData<R>,
condition: suspend (T?) -> Boolean,
transform: Transform.Nullable<T, R, X>
): LiveData<X?> = chainWith(
context = EmptyCoroutineContext,
other = other,
condition = condition,
transform = transform
)

/**
* @see LiveData.chainWith
*/
fun <T, R, X> LiveData<T>.chainWith(
other: suspend (T?) -> LiveData<R>,
condition: suspend (T?) -> Boolean,
transform: Pair<CoroutineDispatcher, suspend (T?, R?) -> X?>
): LiveData<X?> = chainWith(
context = EmptyCoroutineContext,
other = other,
condition = condition,
transform = transform
)

/**
* @see LiveData.chainWith
*/
fun <T, R, X> LiveData<T>.chainWith(
other: suspend (T?) -> LiveData<R>,
condition: suspend (T?) -> Boolean,
transform: suspend (T?, R?) -> X?
): LiveData<X?> = chainWith(
other = other,
condition = condition,
transform = Dispatchers.IO to transform
)
/* endregion ------------------------------------------------------------------------------------ */

/* region Non Nullable -------------------------------------------------------------------------- */
@@ -591,7 +520,7 @@ fun <T, R> LiveData<T>.chainNotNullWith(
* @param X The type of the value after applying the transformation.
*/
fun <T, R, X> LiveData<T>.chainNotNullWith(
context: CoroutineContext,
context: CoroutineContext = EmptyCoroutineContext,
other: suspend (T) -> LiveData<R>,
condition: suspend (T) -> Boolean,
transform: Transform.NotNull<T, R, X>
@@ -600,89 +529,18 @@ fun <T, R, X> LiveData<T>.chainNotNullWith(
.applyTransformation(context, transform)
.collect(::emit)
}

/**
* @see LiveData.chainNotNullWith
*/
fun <T, R, X> LiveData<T>.chainNotNullWith(
context: CoroutineContext,
other: suspend (T) -> LiveData<R>,
condition: suspend (T) -> Boolean,
transform: Pair<CoroutineDispatcher, suspend (T, R) -> X>
): LiveData<X> = chainNotNullWith(
context = context,
other = other,
condition = condition,
transform = Transform.NotNull.OmitFail(transform.first, transform.second)
)

/**
* @see LiveData.chainNotNullWith
*/
fun <T, R, X> LiveData<T>.chainNotNullWith(
context: CoroutineContext,
other: suspend (T) -> LiveData<R>,
condition: suspend (T) -> Boolean,
transform: suspend (T, R) -> X
): LiveData<X> = chainNotNullWith(
context = context,
other = other,
condition = condition,
transform = Dispatchers.IO to transform
)

/**
* @see LiveData.chainNotNullWith
*/
fun <T, R, X> LiveData<T>.chainNotNullWith(
other: suspend (T) -> LiveData<R>,
condition: suspend (T) -> Boolean,
transform: Transform.NotNull<T, R, X>
): LiveData<X> = chainNotNullWith(
context = EmptyCoroutineContext,
other = other,
condition = condition,
transform = transform
)

/**
* @see LiveData.chainNotNullWith
*/
fun <T, R, X> LiveData<T>.chainNotNullWith(
other: suspend (T) -> LiveData<R>,
condition: suspend (T) -> Boolean,
transform: Pair<CoroutineDispatcher, suspend (T, R) -> X>
): LiveData<X> = chainNotNullWith(
context = EmptyCoroutineContext,
other = other,
condition = condition,
transform = transform
)

/**
* @see LiveData.chainNotNullWith
*/
fun <T, R, X> LiveData<T>.chainNotNullWith(
other: suspend (T) -> LiveData<R>,
condition: suspend (T) -> Boolean,
transform: suspend (T, R) -> X
): LiveData<X> = chainNotNullWith(
other = other,
condition = condition,
transform = Dispatchers.IO to transform
)
/* endregion ------------------------------------------------------------------------------------ */

/* region Auxiliary Functions ------------------------------------------------------------------- */
internal suspend inline fun <T, R> LiveData<T>.internalChainNotNullWith(
private suspend inline fun <T, R> LiveData<T>.internalChainNotNullWith(
noinline other: suspend (T) -> LiveData<R>,
noinline condition: suspend (T) -> Boolean,
) = internalChainWith(
other = { data -> data?.let { other(it) } ?: error("Data null in chainNotNullWith") },
condition = { data -> data?.let { condition(it) } ?: false }
).mapNotNull { it.onlyWithValues() }

internal suspend inline fun <T, R> LiveData<T>.internalChainWith(
private suspend inline fun <T, R> LiveData<T>.internalChainWith(
noinline other: suspend (T?) -> LiveData<R>,
noinline condition: suspend (T?) -> Boolean,
) = channelFlow {
Original file line number Diff line number Diff line change
@@ -6,7 +6,6 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.asFlow
import androidx.lifecycle.liveData
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.cancel
import kotlinx.coroutines.currentCoroutineContext
@@ -291,73 +290,12 @@ fun <T, R> LiveData<T>.combine(
* @param X The type of the value after applying the transformation.
*/
fun <T, R, X> LiveData<T>.combine(
context: CoroutineContext,
context: CoroutineContext = EmptyCoroutineContext,
other: LiveData<R>,
transform: Transform.Nullable<T, R, X>
): LiveData<X?> = liveData(context) {
internalCombine(other).applyTransformation(context, transform).collect(::emit)
}

/**
* @see LiveData.combine
*/
fun <T, R, X> LiveData<T>.combine(
context: CoroutineContext,
other: LiveData<R>,
transform: Pair<CoroutineDispatcher, suspend (T?, R?) -> X?>
): LiveData<X?> = combine(
context = context,
other = other,
transform = Transform.Nullable.OmitFail(transform.first, transform.second)
)

/**
* @see LiveData.combine
*/
fun <T, R, X> LiveData<T>.combine(
context: CoroutineContext,
other: LiveData<R>,
transform: suspend (T?, R?) -> X?
): LiveData<X?> = combine(
context = context,
other = other,
transform = Dispatchers.IO to transform
)

/**
* @see LiveData.combine
*/
fun <T, R, X> LiveData<T>.combine(
other: LiveData<R>,
transform: Transform.Nullable<T, R, X>
): LiveData<X?> = combine(
context = EmptyCoroutineContext,
other = other,
transform = transform
)

/**
* @see LiveData.combine
*/
fun <T, R, X> LiveData<T>.combine(
other: LiveData<R>,
transform: Pair<CoroutineDispatcher, suspend (T?, R?) -> X?>
): LiveData<X?> = combine(
context = EmptyCoroutineContext,
other = other,
transform = transform
)

/**
* @see LiveData.combine
*/
fun <T, R, X> LiveData<T>.combine(
other: LiveData<R>,
transform: suspend (T?, R?) -> X?
): LiveData<X?> = combine(
other = other,
transform = Dispatchers.IO to transform
)
/* endregion ------------------------------------------------------------------------------------ */

/* region Non Nullable -------------------------------------------------------------------------- */
@@ -484,83 +422,22 @@ fun <T, R> LiveData<T>.combineNotNull(
* @param X The type of the value after applying the transformation.
*/
fun <T, R, X> LiveData<T>.combineNotNull(
context: CoroutineContext,
context: CoroutineContext = EmptyCoroutineContext,
other: LiveData<R>,
transform: Transform.NotNull<T, R, X>
): LiveData<X> = liveData(context) {
internalCombineNotNull(other).applyTransformation(context, transform).collect(::emit)
}

/**
* @see LiveData.combineNotNull
*/
fun <T, R, X> LiveData<T>.combineNotNull(
context: CoroutineContext,
other: LiveData<R>,
transform: Pair<CoroutineDispatcher, suspend (T, R) -> X>
): LiveData<X> = combineNotNull(
context = context,
other = other,
transform = Transform.NotNull.OmitFail(transform.first, transform.second)
)

/**
* @see LiveData.combineNotNull
*/
fun <T, R, X> LiveData<T>.combineNotNull(
context: CoroutineContext,
other: LiveData<R>,
transform: suspend (T, R) -> X
): LiveData<X> = combineNotNull(
context = context,
other = other,
transform = Dispatchers.IO to transform
)

/**
* @see LiveData.combineNotNull
*/
fun <T, R, X> LiveData<T>.combineNotNull(
other: LiveData<R>,
transform: Transform.NotNull<T, R, X>
): LiveData<X> = combineNotNull(
context = EmptyCoroutineContext,
other = other,
transform = transform
)

/**
* @see LiveData.combineNotNull
*/
fun <T, R, X> LiveData<T>.combineNotNull(
other: LiveData<R>,
transform: Pair<CoroutineDispatcher, suspend (T, R) -> X>
): LiveData<X> = combineNotNull(
context = EmptyCoroutineContext,
other = other,
transform = transform
)

/**
* @see LiveData.combineNotNull
*/
fun <T, R, X> LiveData<T>.combineNotNull(
other: LiveData<R>,
transform: suspend (T, R) -> X
): LiveData<X> = combineNotNull(
other = other,
transform = Dispatchers.IO to transform
)
/* endregion ------------------------------------------------------------------------------------ */

/* region Auxiliary Functions ------------------------------------------------------------------- */
internal suspend inline fun <T, R> LiveData<T>.internalCombineNotNull(other: LiveData<R>) =
private suspend inline fun <T, R> LiveData<T>.internalCombineNotNull(other: LiveData<R>) =
internalCombine(other).mapNotNull { it.onlyWithValues() }

internal suspend inline fun <T, R> LiveData<T>.internalCombine(other: LiveData<R>) = channelFlow {
val aFlow = this@internalCombine.asFlow()
val bFlow = other.asFlow()
val cFlow: Flow<Pair<T?, R?>> = aFlow.combine(bFlow) { a, b -> (a to b) }
val aFlow: Flow<T> = asFlow()
val bFlow: Flow<R> = other.asFlow()
val cFlow: Flow<Pair<T?, R?>> = aFlow.combine(bFlow) { a, b -> a to b }

withContext(currentCoroutineContext()) {
launch { aFlow.collect { if (other.isInitialized.not()) trySend(it to other.value) else cancel() } }
Loading
Loading