-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor] #92 LiveData -> StateFlow ์ด์
- Loading branch information
Showing
1 changed file
with
17 additions
and
8 deletions.
There are no files selected for viewing
25 changes: 17 additions & 8 deletions
25
.../java/com/android/go/sopt/winey/presentation/main/feed/upload/content/ContentViewModel.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |