Skip to content

Commit

Permalink
Launch a coroutines job for each event
Browse files Browse the repository at this point in the history
  • Loading branch information
takahirom committed Aug 24, 2024
1 parent c83399c commit 04df9da
Show file tree
Hide file tree
Showing 25 changed files with 267 additions and 269 deletions.
22 changes: 9 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,16 @@ TimetableScreen ----> timetableScreenPresenter -> sessionsRepository
```kotlin
@Composable
fun timetableScreenPresenter(
events: Flow<TimetableScreenEvent>,
events: EventEmitter<TimetableScreenEvent>,
sessionsRepository: SessionsRepository = localSessionsRepository(),
): TimetableScreenUiState = providePresenterDefaults { userMessageStateHolder ->
...
SafeLaunchedEffect(Unit) {
events.collect { event ->
when (event) {
is Bookmark -> {
sessionsRepository.toggleBookmark(event.timetableItem.id)
}
...
EventEffect(Unit) { event ->
when (event) {
is Bookmark -> {
sessionsRepository.toggleBookmark(event.timetableItem.id)
}
...
}
}
...
Expand Down Expand Up @@ -176,7 +174,7 @@ SessionsRepository ----> timetableScreenPresenter
```kotlin
@Composable
fun timetableScreenPresenter(
events: Flow<TimetableScreenEvent>,
events: EventEmitter<TimetableScreenEvent>,
sessionsRepository: SessionsRepository = localSessionsRepository(),
): TimetableScreenUiState = providePresenterDefaults { userMessageStateHolder ->
// Sessions are updated in the timetable() function
Expand All @@ -189,10 +187,8 @@ fun timetableScreenPresenter(
),
)
...
SafeLaunchedEffect(Unit) {
events.collect { event ->
...
}
EventEffect(events) { event ->
...
}
TimetableScreenUiState(
contentUiState = timetableUiState,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.github.droidkaigi.confsched.compose

import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch

typealias EventFlow<T> = MutableSharedFlow<T>

@Composable
fun <T> rememberEventFlow(): EventFlow<T> {
return remember {
MutableSharedFlow(extraBufferCapacity = 20)
}
}

@Composable
fun <EVENT> EventEffect(
eventFlow: EventFlow<EVENT>,
block: suspend CoroutineScope.(EVENT) -> Unit,
) {
SafeLaunchedEffect(eventFlow) {
eventFlow.collect { event ->
launch {
block(event)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import androidx.compose.ui.platform.testTag
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import conference_app_2024.feature.contributors.generated.resources.contributor_title
import io.github.droidkaigi.confsched.compose.rememberEventEmitter
import io.github.droidkaigi.confsched.compose.rememberEventFlow
import io.github.droidkaigi.confsched.contributors.component.ContributorsItem
import io.github.droidkaigi.confsched.droidkaigiui.SnackbarMessageEffect
import io.github.droidkaigi.confsched.droidkaigiui.UserMessageStateHolder
Expand Down Expand Up @@ -65,7 +65,7 @@ fun ContributorsScreen(
modifier: Modifier = Modifier,
isTopAppBarHidden: Boolean = false,
) {
val eventEmitter = rememberEventEmitter<ContributorsScreenEvent>()
val eventEmitter = rememberEventFlow<ContributorsScreenEvent>()
val uiState = contributorsScreenPresenter(
events = eventEmitter,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@ package io.github.droidkaigi.confsched.contributors
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import io.github.droidkaigi.confsched.compose.SafeLaunchedEffect
import io.github.droidkaigi.confsched.compose.EventEffect
import io.github.droidkaigi.confsched.compose.EventFlow
import io.github.droidkaigi.confsched.droidkaigiui.providePresenterDefaults
import io.github.droidkaigi.confsched.model.ContributorsRepository
import io.github.droidkaigi.confsched.model.localContributorsRepository
import kotlinx.coroutines.flow.Flow

sealed interface ContributorsScreenEvent

@Composable
fun contributorsScreenPresenter(
events: Flow<ContributorsScreenEvent>,
events: EventFlow<ContributorsScreenEvent>,
contributorsRepository: ContributorsRepository = localContributorsRepository(),
): ContributorsUiState = providePresenterDefaults { userMessageStateHolder ->
val contributors by rememberUpdatedState(contributorsRepository.contributors())
SafeLaunchedEffect(Unit) {
events.collect {
}
EventEffect(events) { event ->
}
ContributorsUiState(
contributors = contributors,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.droidkaigi.confsched.contributors

import io.github.droidkaigi.confsched.compose.EventFlow
import io.github.droidkaigi.confsched.data.Repositories
import io.github.droidkaigi.confsched.droidkaigiui.composeViewController
import io.github.droidkaigi.confsched.droidkaigiui.presenterStateFlow
Expand All @@ -22,7 +23,7 @@ fun contributorsViewController(
@Suppress("unused")
fun contributorsScreenPresenterStateFlow(
repositories: Map<KClass<*>, Any>,
events: Flow<ContributorsScreenEvent>,
events: EventFlow<ContributorsScreenEvent>,
): Flow<ContributorsUiState> = presenterStateFlow(
events = events,
repositories = repositories,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import co.touchlab.kermit.Logger
import conference_app_2024.feature.eventmap.generated.resources.eventmap
import io.github.droidkaigi.confsched.compose.rememberEventEmitter
import io.github.droidkaigi.confsched.compose.rememberEventFlow
import io.github.droidkaigi.confsched.droidkaigiui.SnackbarMessageEffect
import io.github.droidkaigi.confsched.droidkaigiui.UserMessageStateHolder
import io.github.droidkaigi.confsched.droidkaigiui.component.AnimatedTextTopAppBar
Expand Down Expand Up @@ -81,7 +81,7 @@ fun EventMapScreen(
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(),
) {
val eventEmitter = rememberEventEmitter<EventMapScreenEvent>()
val eventEmitter = rememberEventFlow<EventMapScreenEvent>()
val uiState = eventMapScreenPresenter(
events = eventEmitter,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,21 @@ package io.github.droidkaigi.confsched.eventmap
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberUpdatedState
import io.github.droidkaigi.confsched.compose.SafeLaunchedEffect
import io.github.droidkaigi.confsched.compose.EventEffect
import io.github.droidkaigi.confsched.compose.EventFlow
import io.github.droidkaigi.confsched.droidkaigiui.providePresenterDefaults
import io.github.droidkaigi.confsched.model.EventMapRepository
import io.github.droidkaigi.confsched.model.localEventMapRepository
import kotlinx.coroutines.flow.Flow

sealed interface EventMapScreenEvent

@Composable
fun eventMapScreenPresenter(
events: Flow<EventMapScreenEvent>,
events: EventFlow<EventMapScreenEvent>,
eventMapRepository: EventMapRepository = localEventMapRepository(),
): EventMapUiState = providePresenterDefaults { userMessageStateHolder ->
val eventMap by rememberUpdatedState(eventMapRepository.eventMapEvents())
SafeLaunchedEffect(Unit) {
events.collect {
}
EventEffect(events) { event ->
}
EventMapUiState(
eventMap = eventMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ import androidx.navigation.NavGraph.Companion.findStartDestination
import androidx.navigation.NavGraphBuilder
import androidx.navigation.compose.composable
import conference_app_2024.feature.favorites.generated.resources.favorite
import io.github.droidkaigi.confsched.compose.EventEmitter
import io.github.droidkaigi.confsched.compose.rememberEventEmitter
import io.github.droidkaigi.confsched.compose.EventFlow
import io.github.droidkaigi.confsched.compose.rememberEventFlow
import io.github.droidkaigi.confsched.designsystem.theme.KaigiTheme
import io.github.droidkaigi.confsched.droidkaigiui.SnackbarMessageEffect
import io.github.droidkaigi.confsched.droidkaigiui.UserMessageStateHolder
Expand Down Expand Up @@ -79,8 +79,8 @@ fun FavoritesScreen(
onTimetableItemClick: (TimetableItem) -> Unit,
modifier: Modifier = Modifier,
contentPadding: PaddingValues = PaddingValues(),
eventEmitter: EventEmitter<FavoritesScreenEvent> = rememberEventEmitter(),
uiState: FavoritesScreenUiState = favoritesScreenPresenter(events = eventEmitter),
eventFlow: EventFlow<FavoritesScreenEvent> = rememberEventFlow(),
uiState: FavoritesScreenUiState = favoritesScreenPresenter(events = eventFlow),
) {
val snackbarHostState = remember { SnackbarHostState() }

Expand All @@ -93,16 +93,16 @@ fun FavoritesScreen(
snackbarHostState = snackbarHostState,
onTimetableItemClick = onTimetableItemClick,
onAllFilterChipClick = {
eventEmitter.tryEmit(FavoritesScreenEvent.AllFilter)
eventFlow.tryEmit(FavoritesScreenEvent.AllFilter)
},
onDay1FilterChipClick = {
eventEmitter.tryEmit(FavoritesScreenEvent.Day1Filter)
eventFlow.tryEmit(FavoritesScreenEvent.Day1Filter)
},
onDay2FilterChipClick = {
eventEmitter.tryEmit(FavoritesScreenEvent.Day2Filter)
eventFlow.tryEmit(FavoritesScreenEvent.Day2Filter)
},
onBookmarkClick = { timetableItem ->
eventEmitter.tryEmit(FavoritesScreenEvent.Bookmark(timetableItem))
eventFlow.tryEmit(FavoritesScreenEvent.Bookmark(timetableItem))
},
contentPadding = contentPadding,
modifier = modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import io.github.droidkaigi.confsched.compose.SafeLaunchedEffect
import io.github.droidkaigi.confsched.compose.EventEffect
import io.github.droidkaigi.confsched.compose.EventFlow
import io.github.droidkaigi.confsched.droidkaigiui.providePresenterDefaults
import io.github.droidkaigi.confsched.favorites.FavoritesScreenEvent.AllFilter
import io.github.droidkaigi.confsched.favorites.FavoritesScreenEvent.Bookmark
Expand All @@ -26,7 +27,6 @@ import kotlinx.collections.immutable.PersistentSet
import kotlinx.collections.immutable.toPersistentList
import kotlinx.collections.immutable.toPersistentMap
import kotlinx.collections.immutable.toPersistentSet
import kotlinx.coroutines.flow.Flow

sealed interface FavoritesScreenEvent {
data class Bookmark(val timetableItem: TimetableItem) : FavoritesScreenEvent
Expand All @@ -38,7 +38,7 @@ sealed interface FavoritesScreenEvent {

@Composable
fun favoritesScreenPresenter(
events: Flow<FavoritesScreenEvent>,
events: EventFlow<FavoritesScreenEvent>,
sessionsRepository: SessionsRepository = localSessionsRepository(),
): FavoritesScreenUiState = providePresenterDefaults { userMessageStateHolder ->
val favoriteSessions by rememberUpdatedState(
Expand All @@ -56,33 +56,32 @@ fun favoritesScreenPresenter(
),
)

SafeLaunchedEffect(Unit) {
events.collect { event ->
when (event) {
is Bookmark -> {
sessionsRepository.toggleBookmark(event.timetableItem.id)
}
EventEffect(events) { event ->
when (event) {
is Bookmark -> {
sessionsRepository.toggleBookmark(event.timetableItem.id)
}

AllFilter -> {
allFilterSelected = true
currentDayFilters = emptySet()
}
AllFilter -> {
allFilterSelected = true
currentDayFilters = emptySet()
}

Day1Filter, Day2Filter -> {
allFilterSelected = false
Day1Filter, Day2Filter -> {
allFilterSelected = false

val dayType = if (event is Day1Filter) {
ConferenceDay1
} else {
ConferenceDay2
}
val dayType = if (event is Day1Filter) {
ConferenceDay1
} else {
ConferenceDay2
}

currentDayFilters = if (currentDayFilters.contains(dayType) && currentDayFilters.size >= 2) {
currentDayFilters =
if (currentDayFilters.contains(dayType) && currentDayFilters.size >= 2) {
currentDayFilters - dayType
} else {
currentDayFilters + dayType
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ import conference_app_2024.feature.main.generated.resources.timetable
import dev.chrisbanes.haze.HazeState
import dev.chrisbanes.haze.HazeStyle
import dev.chrisbanes.haze.haze
import io.github.droidkaigi.confsched.compose.EventEmitter
import io.github.droidkaigi.confsched.compose.rememberEventEmitter
import io.github.droidkaigi.confsched.compose.EventFlow
import io.github.droidkaigi.confsched.compose.rememberEventFlow
import io.github.droidkaigi.confsched.designsystem.DesignSystemRes
import io.github.droidkaigi.confsched.droidkaigiui.SnackbarMessageEffect
import io.github.droidkaigi.confsched.droidkaigiui.UserMessageStateHolder
Expand Down Expand Up @@ -112,8 +112,8 @@ fun MainScreen(
windowSize: WindowSizeClass,
mainNestedGraphStateHolder: MainNestedGraphStateHolder,
mainNestedNavGraph: NavGraphBuilder.(NavController, PaddingValues) -> Unit,
eventEmitter: EventEmitter<MainScreenEvent> = rememberEventEmitter(),
uiState: MainScreenUiState = mainScreenPresenter(eventEmitter),
eventFlow: EventFlow<MainScreenEvent> = rememberEventFlow(),
uiState: MainScreenUiState = mainScreenPresenter(eventFlow),
) {
val snackbarHostState = remember { SnackbarHostState() }

Expand Down
Loading

0 comments on commit 04df9da

Please sign in to comment.