Skip to content

Commit 4308c6d

Browse files
committed
sample: pass event between screens
1 parent 1b6a550 commit 4308c6d

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

sample/app/src/main/java/com/hoc081098/channeleventbus/sample/android/Route.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal fun rememberCurrentRouteAsState(currentBackStackEntryAsState: State<Nav
1515
currentBackStackEntryAsState.value
1616
?.destination
1717
?.route
18-
?.let(Route.Companion::ofOrNull)
18+
?.let(Route::ofOrNull)
1919
}
2020
}
2121

@@ -55,6 +55,8 @@ internal sealed class Route {
5555
RegisterStepOne,
5656
RegisterStepTwo,
5757
RegisterStepThree,
58+
Home,
59+
Detail,
5860
)
5961
}
6062

sample/app/src/main/java/com/hoc081098/channeleventbus/sample/android/ui/home/detail/DetailScreen.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fun DetailScreen(
4343
Column(
4444
modifier = Modifier.matchParentSize(),
4545
horizontalAlignment = Alignment.CenterHorizontally,
46-
verticalArrangement = Arrangement.Center,
46+
verticalArrangement = Arrangement.Top,
4747
) {
4848
Text(
4949
text = "Detail",
@@ -69,6 +69,7 @@ fun DetailScreen(
6969
Spacer(modifier = Modifier.height(16.dp))
7070

7171
ElevatedButton(
72+
enabled = text.isNotBlank(),
7273
onClick = {
7374
vm.sendResultToHome()
7475
navigateBack()

sample/app/src/main/java/com/hoc081098/channeleventbus/sample/android/ui/home/home/HomeVM.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import kotlinx.collections.immutable.plus
1010
import kotlinx.coroutines.CancellationException
1111
import kotlinx.coroutines.flow.SharingStarted
1212
import kotlinx.coroutines.flow.StateFlow
13-
import kotlinx.coroutines.flow.map
13+
import kotlinx.coroutines.flow.mapNotNull
1414
import kotlinx.coroutines.flow.onCompletion
1515
import kotlinx.coroutines.flow.scan
1616
import kotlinx.coroutines.flow.stateIn
@@ -25,8 +25,14 @@ class HomeVM(
2525
// Close the bus when this ViewModel is cleared.
2626
channelEventBus.closeKey(DetailResultToHomeEvent)
2727
}
28-
.map { it.value }
29-
.scan(persistentListOf<String>()) { acc, e -> acc + e }
28+
.mapNotNull { it.value.takeIf(String::isNotBlank) }
29+
.scan(persistentListOf<String>()) { acc, e ->
30+
if (e in acc) {
31+
acc
32+
} else {
33+
acc + e
34+
}
35+
}
3036
.stateIn(
3137
scope = viewModelScope,
3238
// Using SharingStarted.Lazily is enough, because the event bus is backed by a channel.

0 commit comments

Comments
 (0)