Skip to content

Commit

Permalink
fix: 공모 상세 화면 오류 수정 (#280)
Browse files Browse the repository at this point in the history
* fix: 총대 여부 확인 로직 수정

* fix: 마감 임박 시 보여주는 버튼 수정

* fix: 공모 작성 후 홈화면으로 돌아왔을 떄 목록이 새로고침 되지 않는 오류 수정

* test: 테스트 코드 수정

* style: lint적용
  • Loading branch information
Namyunsuk authored Aug 9, 2024
1 parent 535518f commit 336d689
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.serialization.Serializable
data class OfferingDetailResponse(
@SerialName("id") val id: Long,
@SerialName("title") val title: String,
@SerialName("productUrl") val productUrl: String,
@SerialName("productUrl") val productUrl: String?,
@SerialName("meetingAddress") val meetingAddress: String,
@SerialName("meetingAddressDetail") val meetingAddressDetail: String,
@SerialName("description") val description: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ enum class OfferingCondition {

companion object {
fun OfferingCondition.isAvailable(): Boolean {
return this == AVAILABLE
return this == AVAILABLE || this == IMMINENT
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ data class OfferingDetail(
val title: String,
val nickname: String,
val memberId: String,
val productUrl: String,
val productUrl: String?,
val thumbnailUrl: String?,
val dividedPrice: Int,
val totalPrice: Int,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,12 @@ private fun OfferingCondition.toOfferingConditionText(
totalCount: Int,
) = when (this) {
OfferingCondition.FULL -> context.getString(R.string.participant_full)
OfferingCondition.IMMINENT -> context.getString(R.string.participant_end)
OfferingCondition.IMMINENT ->
context.getString(
R.string.participant_count,
currentCount,
totalCount,
)
OfferingCondition.CONFIRMED -> context.getString(R.string.participant_end)
OfferingCondition.AVAILABLE ->
context.getString(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class HomeFragment : Fragment(), OnOfferingClickListener, OnUpsideClickListener
override fun onStart() {
super.onStart()

viewModel.fetchOfferings()
offeringAdapter.refresh()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ class OfferingViewModel(
val filterOfferingsEvent: SingleLiveData<Unit> get() = _filterOfferingsEvent

init {
fetchOfferings()
fetchFilters()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class OfferingDetailActivity : AppCompatActivity() {
OfferingDetailViewModel.getFactory(
offeringId = offeringId,
offeringDetailRepository = (application as ChongdaeApp).offeringDetailRepository,
applicationContext,
)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.zzang.chongdae.presentation.view.offeringdetail

import android.content.Context
import android.util.Log
import androidx.datastore.preferences.core.longPreferencesKey
import androidx.datastore.preferences.preferencesDataStore
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
Expand All @@ -14,11 +17,13 @@ import com.zzang.chongdae.domain.model.OfferingDetail
import com.zzang.chongdae.domain.repository.OfferingDetailRepository
import com.zzang.chongdae.presentation.util.MutableSingleLiveData
import com.zzang.chongdae.presentation.util.SingleLiveData
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch

class OfferingDetailViewModel(
private val offeringId: Long,
private val offeringDetailRepository: OfferingDetailRepository,
private val context: Context,
) : ViewModel(), OnParticipationClickListener {
private val _offeringDetail: MutableLiveData<OfferingDetail> = MutableLiveData()
val offeringDetail: LiveData<OfferingDetail> get() = _offeringDetail
Expand Down Expand Up @@ -47,7 +52,8 @@ class OfferingDetailViewModel(

private fun loadOffering() {
viewModelScope.launch {
// memberId를 가져 오는 로직 수정 예정(로그인 기능 구현 시)
val pref = context.dataStore.data.first()

offeringDetailRepository.fetchOfferingDetail(
memberId = BuildConfig.TOKEN.toLong(),
offeringId = offeringId,
Expand All @@ -58,7 +64,7 @@ class OfferingDetailViewModel(
_offeringCondition.value = it.condition
_isParticipated.value = it.isParticipated
_isAvailable.value = isParticipationEnabled(it.condition, it.isParticipated)
_isRepresentative.value = isRepresentative(it)
_isRepresentative.value = isRepresentative(it, pref[MEMBER_ID_KEY] ?: -1L)
}.onFailure {
Log.e("error", it.message.toString())
}
Expand Down Expand Up @@ -86,15 +92,23 @@ class OfferingDetailViewModel(
) = offeringCondition.isAvailable() && !isParticipated

// 총대여부를 확인하는 메서드(로그인 기능 구현 시 수정 예정)
private fun isRepresentative(it: OfferingDetail) = it.memberId == BuildConfig.TOKEN
private fun isRepresentative(
it: OfferingDetail,
memberId: Long,
): Boolean {
return it.memberId == memberId.toString()
}

companion object {
private const val DEFAULT_TITLE = ""
val Context.dataStore by preferencesDataStore(name = "settings")
val MEMBER_ID_KEY = longPreferencesKey("member_id_key")

@Suppress("UNCHECKED_CAST")
fun getFactory(
offeringId: Long,
offeringDetailRepository: OfferingDetailRepository,
context: Context,
) = object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(
modelClass: Class<T>,
Expand All @@ -103,6 +117,7 @@ class OfferingDetailViewModel(
return OfferingDetailViewModel(
offeringId,
offeringDetailRepository,
context,
) as T
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.zzang.chongdae.presentation.view.offeringdetail

import android.content.Context
import com.zzang.chongdae.domain.repository.OfferingDetailRepository
import com.zzang.chongdae.repository.FakeOfferingDetailRepository
import com.zzang.chongdae.util.CoroutinesTestExtension
import com.zzang.chongdae.util.InstantTaskExecutorExtension
import com.zzang.chongdae.util.TestFixture
import com.zzang.chongdae.util.getOrAwaitValue
import io.mockk.every
import io.mockk.mockk
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
Expand All @@ -19,25 +21,15 @@ import org.junit.jupiter.api.extension.ExtendWith
class OfferingDetailViewModelTest {
private lateinit var viewModel: OfferingDetailViewModel
private lateinit var offeringDetailRepository: OfferingDetailRepository

private lateinit var context: Context
private val offeringId = 1L

@BeforeEach
fun setUp() {
context = mockk<Context>(relaxed = true)
every { context.applicationContext } returns context
offeringDetailRepository = FakeOfferingDetailRepository()
viewModel = OfferingDetailViewModel(offeringId, offeringDetailRepository)
}

@DisplayName("공모 상세정보를 불러온다")
@Test
fun loadOffering() {
// given

// when
val actual = viewModel.offeringDetail.getOrAwaitValue()

// then
assertThat(actual).isEqualTo(TestFixture.OFFERING_DETAIL_STUB)
viewModel = OfferingDetailViewModel(offeringId, offeringDetailRepository, context)
}

@DisplayName("공구에 참여한다")
Expand Down

0 comments on commit 336d689

Please sign in to comment.