Skip to content

Commit

Permalink
[refactor] #92 LiveData -> StateFlow ์ด์ „
Browse files Browse the repository at this point in the history
  • Loading branch information
leeeha committed Aug 8, 2023
1 parent 1148523 commit 8482a2d
Showing 1 changed file with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -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<String>()
val content: String get() = _content.value ?: ""
val _content = MutableStateFlow("")
val content: String get() = _content.value

val isValidContent: LiveData<Boolean> = _content.map { validateLength(it) }
val isValidContent: StateFlow<Boolean> = _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
}
}

0 comments on commit 8482a2d

Please sign in to comment.