Skip to content

Fix single event #10

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

Merged
merged 1 commit into from
Jan 31, 2021
Merged
Show file tree
Hide file tree
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
8 changes: 5 additions & 3 deletions feature-add/src/main/java/com/hoc/flowmvi/ui/add/AddVM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.hoc.flowmvi.domain.entity.User
import com.hoc.flowmvi.domain.usecase.AddUserUseCase
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharingStarted
Expand All @@ -25,6 +26,7 @@ import kotlinx.coroutines.flow.mapNotNull
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.scan
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
Expand All @@ -35,11 +37,11 @@ internal class AddVM(
private val addUser: AddUserUseCase,
private val savedStateHandle: SavedStateHandle
) : ViewModel() {
private val _eventFlow = MutableSharedFlow<SingleEvent>(extraBufferCapacity = 64)
private val _eventChannel = Channel<SingleEvent>(Channel.BUFFERED)
private val _intentFlow = MutableSharedFlow<ViewIntent>(extraBufferCapacity = 64)

val viewState: StateFlow<ViewState>
val singleEvent: Flow<SingleEvent> get() = _eventFlow
val singleEvent: Flow<SingleEvent> get() = _eventChannel.receiveAsFlow()

suspend fun processIntent(intent: ViewIntent) = _intentFlow.emit(intent)

Expand Down Expand Up @@ -76,7 +78,7 @@ internal class AddVM(
is PartialStateChange.FormValueChange.FirstNameChanged -> return@onEach
is PartialStateChange.FormValueChange.LastNameChanged -> return@onEach
}
_eventFlow.emit(event)
_eventChannel.send(event)
}
}

Expand Down
8 changes: 5 additions & 3 deletions feature-main/src/main/java/com/hoc/flowmvi/ui/main/MainVM.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.hoc.flowmvi.domain.usecase.RefreshGetUsersUseCase
import com.hoc.flowmvi.domain.usecase.RemoveUserUseCase
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharingStarted
Expand All @@ -25,6 +26,7 @@ import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.merge
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.flow.scan
import kotlinx.coroutines.flow.shareIn
import kotlinx.coroutines.flow.stateIn
Expand All @@ -38,11 +40,11 @@ internal class MainVM(
private val refreshGetUsers: RefreshGetUsersUseCase,
private val removeUser: RemoveUserUseCase,
) : ViewModel() {
private val _eventFlow = MutableSharedFlow<SingleEvent>(extraBufferCapacity = 64)
private val _eventChannel = Channel<SingleEvent>(Channel.BUFFERED)
private val _intentFlow = MutableSharedFlow<ViewIntent>(extraBufferCapacity = 64)

val viewState: StateFlow<ViewState>
val singleEvent: Flow<SingleEvent> get() = _eventFlow
val singleEvent: Flow<SingleEvent> get() = _eventChannel.receiveAsFlow()

suspend fun processIntent(intent: ViewIntent) = _intentFlow.emit(intent)

Expand Down Expand Up @@ -80,7 +82,7 @@ internal class MainVM(
is PartialChange.GetUser.Data -> return@onEach
PartialChange.Refresh.Loading -> return@onEach
}
_eventFlow.emit(event)
_eventChannel.send(event)
}
}

Expand Down