Skip to content

Commit

Permalink
[FIX/#68] 마이투두 뷰모델 플로우 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchbreeze committed Jan 10, 2024
1 parent ac8424c commit 4e1fffa
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.going.domain.entity.response.TodoModel
import com.going.domain.repository.TodoRepository
import com.going.presentation.todo.ourtodo.OurTodoViewModel
import com.going.ui.extension.UiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
Expand All @@ -19,23 +20,39 @@ class MyTodoViewModel @Inject constructor(
private val _totalUncompletedTodoCount = MutableStateFlow<Int>(0)
val totalUncompletedTodoCount: StateFlow<Int> = _totalUncompletedTodoCount

private val _todoListState = MutableStateFlow<UiState<List<TodoModel>>>(UiState.Empty)
val todoListState: StateFlow<UiState<List<TodoModel>>> = _todoListState
private val _todoUncompleteListState = MutableStateFlow<UiState<List<TodoModel>>>(UiState.Empty)
val todoUncompleteListState: StateFlow<UiState<List<TodoModel>>> = _todoUncompleteListState

private val _todoCompleteListState = MutableStateFlow<UiState<List<TodoModel>>>(UiState.Empty)
val todoCompleteListState: StateFlow<UiState<List<TodoModel>>> = _todoCompleteListState

fun decreaseTodoCount() {
_totalUncompletedTodoCount.value = _totalUncompletedTodoCount.value - 1
}

fun getTodoListFromServer(tripId: Long, category: String, progress: String) {
_todoListState.value = UiState.Loading
fun getUncompleteTodoListFromServer(tripId: Long, category: String, progress: String) {
_todoUncompleteListState.value = UiState.Loading
viewModelScope.launch {
todoRepository.getTodoList(tripId, category, progress)
.onSuccess { response ->
_todoListState.value = UiState.Success(response)
_todoUncompleteListState.value = UiState.Success(response)
_totalUncompletedTodoCount.value = response.size
}
.onFailure {
_todoListState.value = UiState.Failure(it.message.orEmpty())
_todoUncompleteListState.value = UiState.Failure(it.message.orEmpty())
}
}
}

fun getCompleteTodoListFromServer(tripId: Long, category: String, progress: String) {
_todoCompleteListState.value = UiState.Loading
viewModelScope.launch {
todoRepository.getTodoList(tripId, category, progress)
.onSuccess { response ->
_todoCompleteListState.value = UiState.Success(response)
}
.onFailure {
_todoCompleteListState.value = UiState.Failure(it.message.orEmpty())
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ class MyTodoCompleteFragment() :
private fun setTodoList() {
// 추후 tripId 설정
val tripId: Long = 1
viewModel.getTodoListFromServer(tripId, MY_TODO, COMPLETE)
viewModel.getCompleteTodoListFromServer(tripId, MY_TODO, COMPLETE)
}

private fun observeTodoListState() {
viewModel.todoListState.flowWithLifecycle(lifecycle).onEach { state ->
viewModel.todoCompleteListState.flowWithLifecycle(lifecycle).onEach { state ->
when (state) {
is UiState.Success -> adapter.setItemList(state.data)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ class MyTodoUncompleteFragment() :
private fun setTodoList() {
// 추후 tripId 설정
val tripId: Long = 1
viewModel.getTodoListFromServer(tripId, MY_TODO, UNCOMPLETE)
viewModel.getUncompleteTodoListFromServer(tripId, MY_TODO, UNCOMPLETE)
}

private fun observeTodoListState() {
viewModel.todoListState.flowWithLifecycle(lifecycle).onEach { state ->
viewModel.todoUncompleteListState.flowWithLifecycle(lifecycle).onEach { state ->
when (state) {
is UiState.Success -> adapter.setItemList(state.data)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class OurTodoCompleteFragment() :
}

private fun observeTodoListState() {
viewModel.todoListState.flowWithLifecycle(lifecycle).onEach { state ->
viewModel.todoCompleteListState.flowWithLifecycle(lifecycle).onEach { state ->
when (state) {
is UiState.Success -> adapter.submitList(state.data)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class OurTodoUncompleteFragment() :
}

private fun observeTodoListState() {
viewModel.todoListState.flowWithLifecycle(lifecycle).onEach { state ->
viewModel.todoUncompleteListState.flowWithLifecycle(lifecycle).onEach { state ->
when (state) {
is UiState.Success -> adapter.submitList(state.data)

Expand Down

0 comments on commit 4e1fffa

Please sign in to comment.