diff --git a/app/src/main/java/com/android/go/sopt/winey/presentation/main/feed/upload/content/ContentViewModel.kt b/app/src/main/java/com/android/go/sopt/winey/presentation/main/feed/upload/content/ContentViewModel.kt index 07764a95..195f5845 100644 --- a/app/src/main/java/com/android/go/sopt/winey/presentation/main/feed/upload/content/ContentViewModel.kt +++ b/app/src/main/java/com/android/go/sopt/winey/presentation/main/feed/upload/content/ContentViewModel.kt @@ -1,21 +1,30 @@ package com.android.go.sopt.winey.presentation.main.feed.upload.content -import androidx.lifecycle.LiveData -import androidx.lifecycle.MutableLiveData import androidx.lifecycle.ViewModel -import androidx.lifecycle.map +import androidx.lifecycle.viewModelScope +import kotlinx.coroutines.flow.MutableStateFlow +import kotlinx.coroutines.flow.SharingStarted.Companion.WhileSubscribed +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.map +import kotlinx.coroutines.flow.stateIn class ContentViewModel : ViewModel() { - val _content = MutableLiveData() - val content: String get() = _content.value ?: "" + val _content = MutableStateFlow("") + val content: String get() = _content.value - val isValidContent: LiveData = _content.map { validateLength(it) } + val isValidContent: StateFlow = _content.map { validateContent(it) } + .stateIn( + initialValue = false, + scope = viewModelScope, + started = WhileSubscribed(PRODUCE_STOP_TIMEOUT) + ) - private fun validateLength(content: String): Boolean = + private fun validateContent(content: String): Boolean = content.length in MIN_CONTENT_LENGTH..MAX_CONTENT_LENGTH companion object { - const val MIN_CONTENT_LENGTH = 6 + private const val MIN_CONTENT_LENGTH = 6 const val MAX_CONTENT_LENGTH = 36 + private const val PRODUCE_STOP_TIMEOUT = 5000L } }