Skip to content
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

[ADD] BaseViewModel에 EventState (SharedFlow) 추가 #355

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package com.runnect.runnect.presentation.base

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.runnect.runnect.domain.common.toLog
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.CoroutineStart
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.launch
import retrofit2.HttpException
import timber.log.Timber
Expand All @@ -15,16 +19,39 @@ import java.net.UnknownHostException

abstract class BaseViewModel : ViewModel() {

sealed interface EventState {
object Empty : EventState
data class ShowToast(val message: String) : EventState
data class ShowSnackBar(val message: String) : EventState
data class NetworkError(val message: String) : EventState
data class UnknownError(val message: String) : EventState
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네이밍과 관련해서 가장 최근 pr에 코멘트 하나 달아뒀어요! 확인 부탁드립니다 :)


private val _eventState: MutableSharedFlow<EventState> = MutableSharedFlow()
val eventState: SharedFlow<EventState> = _eventState.asSharedFlow()

protected fun sendEvent(event: EventState) {
viewModelScope.launch {
_eventState.emit(event)
}
}

open val exceptionHandler = CoroutineExceptionHandler { _, throwable ->
Timber.tag(throwable::class.java.simpleName).e(throwable)

when (throwable) {
is SocketException,
is HttpException,
is UnknownHostException -> {
sendEvent(
EventState.NetworkError(throwable.toLog())
)
Timber.e(throwable)
}
else -> {
sendEvent(
EventState.UnknownError(throwable.toLog())
)
Timber.e(throwable)
}
}
Expand Down
Loading