-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4a3401
commit 375cadb
Showing
8 changed files
with
126 additions
and
5 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
27 changes: 27 additions & 0 deletions
27
core/src/main/java/com/infinitepower/newquiz/core/ui/ObserveAsEvents.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,27 @@ | ||
package com.infinitepower.newquiz.core.ui | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.compose.LocalLifecycleOwner | ||
import androidx.lifecycle.repeatOnLifecycle | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.withContext | ||
|
||
@Composable | ||
fun <T> ObserveAsEvents( | ||
flow: Flow<T>, | ||
vararg keys: Any?, | ||
onEvent: (T) -> Unit | ||
) { | ||
val lifecycleOwner = LocalLifecycleOwner.current | ||
|
||
LaunchedEffect(lifecycleOwner.lifecycle, *keys, flow) { | ||
lifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { | ||
withContext(Dispatchers.Main.immediate) { | ||
flow.collect(onEvent) | ||
} | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
core/src/main/java/com/infinitepower/newquiz/core/ui/SnackbarController.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,26 @@ | ||
package com.infinitepower.newquiz.core.ui | ||
|
||
import androidx.compose.material3.SnackbarDuration | ||
import kotlinx.coroutines.channels.Channel | ||
import kotlinx.coroutines.flow.receiveAsFlow | ||
|
||
object SnackbarController { | ||
private val _events = Channel<SnackbarEvent>() | ||
val events = _events.receiveAsFlow() | ||
|
||
suspend fun sendEvent(event: SnackbarEvent) { | ||
_events.send(event) | ||
} | ||
} | ||
|
||
data class SnackbarEvent( | ||
val message: String, | ||
val action: SnackbarAction? = null, | ||
val withDismissAction: Boolean = false, | ||
val duration: SnackbarDuration = if (action == null) SnackbarDuration.Short else SnackbarDuration.Indefinite | ||
) | ||
|
||
data class SnackbarAction( | ||
val name: String, | ||
val action: () -> Unit | ||
) |
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