-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
replace livedata with coroutine flows and channels
- Loading branch information
1 parent
b34346a
commit 17a4a01
Showing
25 changed files
with
272 additions
and
312 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
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
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
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
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
53 changes: 53 additions & 0 deletions
53
app/src/main/kotlin/com/adesso/movee/internal/extension/Flow.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,53 @@ | ||
package com.adesso.movee.internal.extension | ||
|
||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.flowWithLifecycle | ||
import androidx.lifecycle.lifecycleScope | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.launch | ||
|
||
inline fun <T> Flow<T>.collect( | ||
scope: CoroutineScope, | ||
crossinline action: suspend (T) -> Unit | ||
) { | ||
scope.launch { | ||
collect { action(it) } | ||
} | ||
} | ||
|
||
inline fun <T> Flow<T?>.collectNonNull( | ||
scope: CoroutineScope, | ||
crossinline action: suspend (T) -> Unit | ||
) { | ||
scope.launch { | ||
collect { value -> | ||
value?.let { action(it) } | ||
} | ||
} | ||
} | ||
|
||
inline fun <T> Flow<T>.collectWithLifecycle( | ||
lifecycleOwner: LifecycleOwner, | ||
minActiveState: Lifecycle.State = Lifecycle.State.STARTED, | ||
crossinline action: suspend (T) -> Unit | ||
) { | ||
lifecycleOwner.lifecycleScope.launch { | ||
flowWithLifecycle(lifecycleOwner.lifecycle, minActiveState) | ||
.collect { action.invoke(it) } | ||
} | ||
} | ||
|
||
inline fun <T> Flow<T?>.collectWithLifecycleNonNull( | ||
lifecycleOwner: LifecycleOwner, | ||
minActiveState: Lifecycle.State = Lifecycle.State.STARTED, | ||
crossinline action: suspend (T) -> Unit | ||
) { | ||
lifecycleOwner.lifecycleScope.launch { | ||
flowWithLifecycle(lifecycleOwner.lifecycle, minActiveState) | ||
.collect { value -> | ||
value?.let { action.invoke(it) } | ||
} | ||
} | ||
} |
Oops, something went wrong.