Skip to content

Commit

Permalink
[FEAT/#42] 내 옐로 전체보기 서버통신 디테일 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
kkk5474096 committed Jul 17, 2023
1 parent b6ee42e commit f0de0e5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yello.presentation.main.myyello

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.flowWithLifecycle
Expand All @@ -12,6 +13,7 @@ import com.example.ui.fragment.toast
import com.example.ui.view.UiState
import com.yello.R
import com.yello.databinding.FragmentMyYelloBinding
import com.yello.presentation.main.myyello.read.MyYelloReadActivity
import com.yello.presentation.util.BaseLinearRcvItemDeco
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.launchIn
Expand All @@ -32,7 +34,9 @@ class MyYelloFragment : BindingFragment<FragmentMyYelloBinding>(R.layout.fragmen

private fun initView() {
viewModel.getMyYelloList()
adapter = MyYelloAdapter()
adapter = MyYelloAdapter {
startActivity(MyYelloReadActivity.getIntent(requireContext(), it.id))
}
binding.rvMyYelloReceive.addItemDecoration(
BaseLinearRcvItemDeco(
8,
Expand All @@ -56,7 +60,6 @@ class MyYelloFragment : BindingFragment<FragmentMyYelloBinding>(R.layout.fragmen
when (it) {
is UiState.Success -> {
adapter?.addItem(it.data.yello)
binding.tvCount.text = it.data.totalCount.toString()
}

is UiState.Failure -> {
Expand All @@ -66,25 +69,27 @@ class MyYelloFragment : BindingFragment<FragmentMyYelloBinding>(R.layout.fragmen
else -> {}
}
}.launchIn(viewLifecycleOwner.lifecycleScope)

viewModel.totalCount.flowWithLifecycle(viewLifecycleOwner.lifecycle)
.onEach {
binding.tvCount.text = it.toString()
}.launchIn(viewLifecycleOwner.lifecycleScope)
}

// 페이지네이션
private fun infinityScroll() {
binding.rvMyYelloReceive.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
// 화면에 보이는 마지막 아이템의 position
val lastVisibleItemPosition =
(recyclerView.layoutManager as LinearLayoutManager).findLastCompletelyVisibleItemPosition()
// 어댑터에 등록된 아이템의 총 개수 -1
val itemTotalCount = adapter?.itemCount?.minus(1)
// 스크롤이 끝에 도달했는지 확인
if (lastVisibleItemPosition != -1 && itemTotalCount != -1) {
if (lastVisibleItemPosition == itemTotalCount) {
if (dy > 0) {
if (!binding.rvMyYelloReceive.canScrollVertically(1) &&
(recyclerView.layoutManager as LinearLayoutManager).findLastVisibleItemPosition() == adapter!!.itemCount - 1
) {
viewModel.getMyYelloList()
}
}
}
})
}
})
}

override fun onDestroyView() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,49 @@
package com.yello.presentation.main.myyello

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.example.domain.entity.MyYello
import com.example.domain.repository.YelloRepository
import com.example.ui.view.UiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class MyYelloViewModel @Inject constructor(
private val repository: YelloRepository
) : ViewModel() {
private val _myYelloData = MutableStateFlow<UiState<MyYello>>(UiState.Loading)
private val _myYelloData = MutableSharedFlow<UiState<MyYello>>()
val myYelloData: SharedFlow<UiState<MyYello>> = _myYelloData.asSharedFlow()

private var currentPage = 0
private val _totalCount = MutableStateFlow<Int>(0)
val totalCount: StateFlow<Int> = _totalCount.asStateFlow()

private var currentPage = -1
private var isPagingFinish = false
private var totalPage = Int.MAX_VALUE

fun getMyYelloList() {
if (isPagingFinish) return
_myYelloData.value = UiState.Loading
_myYelloData.tryEmit(UiState.Loading)
viewModelScope.launch {
repository.getMyYelloList(++currentPage)
.onSuccess {
totalPage = Math.ceil((it.totalCount * 0.1)).toInt()
if (totalPage == currentPage) isPagingFinish = true
_myYelloData.value = when {
it.yello.isEmpty() -> UiState.Empty
else -> UiState.Success(it)
}

_myYelloData.emit(
when {
it.yello.isEmpty() -> UiState.Empty
else -> UiState.Success(it)
}
)
_totalCount.value = it.totalCount
}.onFailure {
_myYelloData.emit(UiState.Failure("옐로 리스트 서버 통신 실패"))
}
Expand Down

0 comments on commit f0de0e5

Please sign in to comment.