Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,21 @@ class SpaceItemDecoration(private var space: Int) : RecyclerView.ItemDecoration(
state: RecyclerView.State
) {
val position = parent.getChildAdapterPosition(view)
val isLast = position == parent.adapter?.itemCount?.minus(1)

if (position == RecyclerView.NO_POSITION) return

when (val layoutManager = parent.layoutManager) {
is LinearLayoutManager -> {
when (layoutManager.orientation) {
RecyclerView.HORIZONTAL -> {
outRect.right += getSpaceByLocation(isLast, view.context.dpToPx(space))
outRect.right += view.context.dpToPx(space)
}

RecyclerView.VERTICAL -> {
outRect.bottom += getSpaceByLocation(isLast, view.context.dpToPx(space))
outRect.bottom += view.context.dpToPx(space)
}
}
}
}
}

private fun getSpaceByLocation(
isLast: Boolean,
space: Int
): Int = if (isLast) 0 else space
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,9 @@ fun Chip.bindChipBackground(isSelected: Boolean) {
)
fun Chip.bindChipStatus(
status: ExchangeRequestStatus,
count: Map<ExchangeRequestStatus, Int>?,
count: Int,
isSelected: Boolean
) {
val chipCount = count?.get(status) ?: 0

val statusText = when (status) {
ExchangeRequestStatus.PENDING -> {
context.getString(info.imdang.component.R.string.waiting)
Expand All @@ -93,7 +91,7 @@ fun Chip.bindChipStatus(
}

val text = if (isSelected) {
"$statusText ($chipCount)"
"$statusText ($count)"
} else {
statusText
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ fun TextView.bindSpan(text: String, spanText: String) {
value = ["bindChipSelectedId", "bindChipSelectedCounts"],
requireAll = true
)
fun TextView.bindChipAlarmDescription(chipId: Int, chipCounts: Map<ExchangeRequestStatus, Int>?) {
fun TextView.bindChipAlarmDescription(chipId: Int, count: Int) {
val status = when (chipId) {
1 -> ExchangeRequestStatus.PENDING
2 -> ExchangeRequestStatus.REJECTED
3 -> ExchangeRequestStatus.ACCEPTED
else -> null
}

val count = status?.let { chipCounts?.get(it) } ?: 0

val description = when (status) {
ExchangeRequestStatus.PENDING -> if (count > 0) {
context.getString(info.imdang.component.R.string.waiting_details_existence)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package info.imdang.imdang.ui.main.home.exchange

import androidx.paging.PagingData
import info.imdang.imdang.model.insight.InsightVo

sealed class HomeExchangeEvent {

data class UpdateMyExchanges(val exchanges: PagingData<InsightVo>) : HomeExchangeEvent()

data class UpdateOthersExchanges(val exchanges: PagingData<InsightVo>) : HomeExchangeEvent()
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package info.imdang.imdang.ui.main.home.exchange

import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import androidx.paging.map
import dagger.hilt.android.lifecycle.HiltViewModel
import info.imdang.domain.model.common.MyExchangesParams
import info.imdang.domain.model.common.PagingParams
Expand All @@ -10,13 +12,15 @@ import info.imdang.domain.usecase.myexchange.GetMyExchangeUseCase
import info.imdang.domain.usecase.myexchange.GetOthersExchangeUseCase
import info.imdang.imdang.base.BaseViewModel
import info.imdang.imdang.common.util.logEvent
import info.imdang.imdang.model.common.PagingState
import info.imdang.imdang.model.coupon.CouponVo
import info.imdang.imdang.model.coupon.mapper
import info.imdang.imdang.model.insight.ExchangeRequestStatus
import info.imdang.imdang.model.insight.InsightVo
import info.imdang.imdang.model.insight.mapper
import info.imdang.imdang.ui.main.home.history.ExchangeType
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand All @@ -28,27 +32,21 @@ class HomeExchangeViewModel @Inject constructor(
private val getCouponCountUseCase: GetCouponUseCase
) : BaseViewModel() {

private val _event = MutableSharedFlow<HomeExchangeEvent>()
val event = _event.asSharedFlow()

private val _currentExchangeType = MutableStateFlow(ExchangeType.REQUESTED)
val currentExchangeType = _currentExchangeType.asStateFlow()

private val _selectedChipId = MutableStateFlow(1)
val selectedChipId = _selectedChipId.asStateFlow()

private val _mySelectedChipCounts = MutableStateFlow(mapOf<ExchangeRequestStatus, Int>())
val mySelectedChipCounts = _mySelectedChipCounts.asStateFlow()

private val _othersSelectedChipCounts = MutableStateFlow(mapOf<ExchangeRequestStatus, Int>())
val othersSelectedChipCounts = _othersSelectedChipCounts.asStateFlow()

private val _myExchanges = MutableStateFlow<List<InsightVo>>(emptyList())
val myExchanges = _myExchanges.asStateFlow()

private val _othersExchanges = MutableStateFlow<List<InsightVo>>(emptyList())
val othersExchanges = _othersExchanges.asStateFlow()

private val _coupon = MutableStateFlow(CouponVo.init())
val coupon = _coupon.asStateFlow()

private val _pagingState = MutableStateFlow(PagingState())
val pagingState = _pagingState.asStateFlow()

init {
fetchMyExchange(ExchangeRequestStatus.PENDING)
fetchOthersExchange(ExchangeRequestStatus.PENDING)
Expand Down Expand Up @@ -96,37 +94,39 @@ class HomeExchangeViewModel @Inject constructor(

private fun fetchMyExchange(exchangeRequestStatus: ExchangeRequestStatus) {
viewModelScope.launch {
val response = getMyExchangeUseCase(
getMyExchangeUseCase(
MyExchangesParams(
exchangeRequestStatus = exchangeRequestStatus.name,
pagingParams = PagingParams()
pagingParams = PagingParams(
totalCountListener = {
updatePagingState(itemCount = it)
}
)
)
)

val totalCount = response?.totalElements ?: 0
_mySelectedChipCounts.value = _mySelectedChipCounts.value.toMutableMap().apply {
this[exchangeRequestStatus] = totalCount
}

_myExchanges.value = response?.content?.map(InsightDto::mapper) ?: emptyList()
?.cachedIn(this)
?.collect {
_event.emit(HomeExchangeEvent.UpdateMyExchanges(it.map(InsightDto::mapper)))
}
}
}

private fun fetchOthersExchange(exchangeRequestStatus: ExchangeRequestStatus) {
viewModelScope.launch {
val response = getOthersExchangeUseCase(
getOthersExchangeUseCase(
MyExchangesParams(
exchangeRequestStatus = exchangeRequestStatus.name,
pagingParams = PagingParams()
pagingParams = PagingParams(
totalCountListener = {
updatePagingState(itemCount = it)
}
)
)
)

val totalCount = response?.totalElements ?: 0
_othersSelectedChipCounts.value = _othersSelectedChipCounts.value.toMutableMap().apply {
this[exchangeRequestStatus] = totalCount
}

_othersExchanges.value = response?.content?.map(InsightDto::mapper) ?: emptyList()
?.cachedIn(this)
?.collect {
_event.emit(HomeExchangeEvent.UpdateOthersExchanges(it.map(InsightDto::mapper)))
}
}
}

Expand All @@ -137,4 +137,16 @@ class HomeExchangeViewModel @Inject constructor(
}
}
}

fun updatePagingState(
isLoading: Boolean? = null,
itemCount: Int? = null,
error: String? = null
) {
_pagingState.value = pagingState.value.copy(
isLoading = isLoading ?: pagingState.value.isLoading,
itemCount = itemCount ?: pagingState.value.itemCount,
error = error ?: pagingState.value.error
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import dagger.hilt.android.AndroidEntryPoint
import info.imdang.imdang.R
import info.imdang.imdang.base.BaseFragment
import info.imdang.imdang.common.SpaceItemDecoration
import info.imdang.imdang.common.bindingadapter.BaseSingleViewAdapter
import info.imdang.imdang.common.bindingadapter.BaseSingleViewPagingAdapter
import info.imdang.imdang.common.ext.startActivity
import info.imdang.imdang.common.util.logEvent
import info.imdang.imdang.databinding.FragmentHomeHistoryRequestedBinding
import info.imdang.imdang.model.insight.InsightVo
import info.imdang.imdang.ui.insight.InsightDetailActivity
import info.imdang.imdang.ui.insight.InsightDetailActivity.Companion.INSIGHT_ID
import info.imdang.imdang.ui.main.home.exchange.HomeExchangeEvent
import info.imdang.imdang.ui.main.home.exchange.HomeExchangeViewModel
import kotlinx.coroutines.launch

Expand All @@ -29,11 +30,14 @@ class HomeHistoryRequestedFragment :

private var type: ExchangeType = ExchangeType.REQUESTED

private lateinit var adapter: BaseSingleViewPagingAdapter<InsightVo>

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

viewModel.updateExchangeType(type)
setupBinding()
setupCollect()
}

private fun setupBinding() {
Expand All @@ -42,7 +46,7 @@ class HomeHistoryRequestedFragment :

rvHomeHistoryRequested.run {
addItemDecoration(SpaceItemDecoration(space = 12))
adapter = BaseSingleViewAdapter(
this@HomeHistoryRequestedFragment.adapter = BaseSingleViewPagingAdapter(
layoutResourceId = R.layout.item_insight_horizontal,
bindingItemId = BR.item,
viewModel = mapOf(BR.viewModel to this@HomeHistoryRequestedFragment.viewModel),
Expand Down Expand Up @@ -81,21 +85,61 @@ class HomeHistoryRequestedFragment :
)
}
}
}
when (type) {
ExchangeType.REQUESTED -> {
setupLoadStateListener(
scope = lifecycleScope,
onLoading = {
this@HomeHistoryRequestedFragment.viewModel.updatePagingState(
isLoading = it
)
},
onError = {
this@HomeHistoryRequestedFragment.viewModel.updatePagingState(
error = it
)
}
)
}

val dataObserve = when (type) {
ExchangeType.REQUESTED -> {
this@HomeHistoryRequestedFragment.viewModel.myExchanges
ExchangeType.RECEIVED -> {
setupLoadStateListener(
scope = lifecycleScope,
onLoading = {
this@HomeHistoryRequestedFragment.viewModel.updatePagingState(
isLoading = it
)
},
onError = {
this@HomeHistoryRequestedFragment.viewModel.updatePagingState(
error = it
)
}
)
}
}
}
adapter = this@HomeHistoryRequestedFragment.adapter
}
}
}

ExchangeType.RECEIVED -> {
this@HomeHistoryRequestedFragment.viewModel.othersExchanges
private fun setupCollect() {
lifecycleScope.launch {
when (type) {
ExchangeType.REQUESTED -> {
viewModel.event.collect { event ->
if (event is HomeExchangeEvent.UpdateMyExchanges) {
adapter.submitData(lifecycle, event.exchanges)
}
}
}

lifecycleScope.launch {
dataObserve.collect { items ->
(adapter as BaseSingleViewAdapter<InsightVo>).submitList(items)
ExchangeType.RECEIVED -> {
viewModel.event.collect { event ->
if (event is HomeExchangeEvent.UpdateOthersExchanges) {
adapter.submitData(lifecycle, event.exchanges)
}
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/res/layout/fragment_home_history_requested.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

<com.google.android.material.chip.Chip
bindChipBackground="@{viewModel.selectedChipId == 1}"
bindChipCount="@{viewModel.currentExchangeType == ExchangeType.REQUESTED ? viewModel.mySelectedChipCounts : viewModel.othersSelectedChipCounts}"
bindChipCount="@{viewModel.currentExchangeType == ExchangeType.REQUESTED ? viewModel.pagingState.itemCount : viewModel.pagingState.itemCount}"
bindChipSelected="@{viewModel.selectedChipId == 1}"
bindChipStatus="@{ExchangeRequestStatus.PENDING}"
android:layout_width="wrap_content"
Expand All @@ -54,7 +54,7 @@

<com.google.android.material.chip.Chip
bindChipBackground="@{viewModel.selectedChipId == 2}"
bindChipCount="@{viewModel.currentExchangeType == ExchangeType.REQUESTED ? viewModel.mySelectedChipCounts : viewModel.othersSelectedChipCounts}"
bindChipCount="@{viewModel.currentExchangeType == ExchangeType.REQUESTED ? viewModel.pagingState.itemCount : viewModel.pagingState.itemCount}"
bindChipSelected="@{viewModel.selectedChipId == 2}"
bindChipStatus="@{ExchangeRequestStatus.REJECTED}"
android:layout_width="wrap_content"
Expand All @@ -70,7 +70,7 @@

<com.google.android.material.chip.Chip
bindChipBackground="@{viewModel.selectedChipId == 3}"
bindChipCount="@{viewModel.currentExchangeType == ExchangeType.REQUESTED ? viewModel.mySelectedChipCounts : viewModel.othersSelectedChipCounts}"
bindChipCount="@{viewModel.currentExchangeType == ExchangeType.REQUESTED ? viewModel.pagingState.itemCount : viewModel.pagingState.itemCount}"
bindChipSelected="@{viewModel.selectedChipId == 3}"
bindChipStatus="@{ExchangeRequestStatus.ACCEPTED}"
android:layout_width="wrap_content"
Expand Down Expand Up @@ -111,7 +111,7 @@

<TextView
style="@style/T_500_14_19_6"
bindChipSelectedCounts="@{viewModel.currentExchangeType == ExchangeType.REQUESTED ? viewModel.mySelectedChipCounts : viewModel.othersSelectedChipCounts}"
bindChipSelectedCounts="@{viewModel.currentExchangeType == ExchangeType.REQUESTED ? viewModel.pagingState.itemCount : viewModel.pagingState.itemCount}"
bindChipSelectedId="@{viewModel.selectedChipId}"
android:layout_width="0dp"
android:layout_height="wrap_content"
Expand All @@ -129,6 +129,7 @@
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="20dp"
android:clipToPadding="false"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="@+id/cl_requested_alarm"
Expand Down
Loading