Skip to content

Commit

Permalink
refactor: 공모 목록, 공모 상세 에러 핸들링 (#418)
Browse files Browse the repository at this point in the history
* refactor: 공모 목록에서 401에러를 제외하고는 에러코드 올 시 빈화면 보여주도록 에러핸들링 수정

* refactor: 필터및 업데이트된 공모 목록 가져오는 로직 에러핸들링 수정

- 400: 토스트 메시지 띄어줌
- 401: refresh
- 그외에는 로그로 에러 코드를 보여줌

* refactor: strings네이밍 통일

* refactor: 공모 상세 에러 핸들링 수정

* refactor: strings정리

- offering_detail부분 정리
  • Loading branch information
Namyunsuk authored Aug 22, 2024
1 parent 5169fc3 commit 2a750e7
Show file tree
Hide file tree
Showing 11 changed files with 150 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.paging.PagingState
import com.zzang.chongdae.domain.model.Offering
import com.zzang.chongdae.domain.repository.AuthRepository
import com.zzang.chongdae.domain.repository.OfferingRepository
import com.zzang.chongdae.domain.util.DataError
import com.zzang.chongdae.domain.util.Result

class OfferingPagingSource(
Expand All @@ -27,9 +28,23 @@ class OfferingPagingSource(

when (offerings) {
is Result.Error -> {
authRepository.saveRefresh()
retry()
load(params)
when (offerings.error) {
DataError.Network.UNAUTHORIZED -> {
authRepository.saveRefresh()
retry()
load(params)
}

else -> {
val prevKey: Long? = null
val nextKey: Long? = null
LoadResult.Page(
data = emptyList<Offering>(),
prevKey = prevKey,
nextKey = nextKey,
)
}
}
}

is Result.Success -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ private fun OfferingCondition.toOfferingComment(
context: Context,
remaining: Int,
) = when (this) {
OfferingCondition.FULL -> context.getString(R.string.main_offering_condition_full_comment)
OfferingCondition.FULL -> context.getString(R.string.home_offering_condition_full_comment)
OfferingCondition.IMMINENT ->
Html.fromHtml(
context.getString(R.string.main_offering_condition_continue_comment)
context.getString(R.string.home_offering_condition_continue_comment)
.format(remaining),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE,
)
Expand Down Expand Up @@ -171,10 +171,10 @@ private fun TextView.setColor(colorId: Int) {

private fun OfferingCondition.toOfferingConditionText(context: Context) =
when (this) {
OfferingCondition.FULL -> context.getString(R.string.main_offering_full) // 인원 만석
OfferingCondition.IMMINENT -> context.getString(R.string.main_offering_imminent) // 마감임박
OfferingCondition.CONFIRMED -> context.getString(R.string.main_offering_closed) // 공구마감
OfferingCondition.AVAILABLE -> context.getString(R.string.main_offering_continue) // 모집중
OfferingCondition.FULL -> context.getString(R.string.home_offering_full) // 인원 만석
OfferingCondition.IMMINENT -> context.getString(R.string.home_offering_imminent) // 마감임박
OfferingCondition.CONFIRMED -> context.getString(R.string.home_offering_closed) // 공구마감
OfferingCondition.AVAILABLE -> context.getString(R.string.home_offering_continue) // 모집중
}

private fun OfferingCondition.toStyle() =
Expand All @@ -193,7 +193,7 @@ fun View.setIsVisible(isVisible: Boolean) {
@BindingAdapter("formattedDate")
fun TextView.bindFormattedDate(datetime: LocalDateTime?) {
this.text =
datetime?.format(DateTimeFormatter.ofPattern(context.getString(R.string.due_datetime)))
datetime?.format(DateTimeFormatter.ofPattern(context.getString(R.string.all_due_datetime)))
}

@BindingAdapter("currentCount", "totalCount", "condition")
Expand All @@ -210,18 +210,18 @@ private fun OfferingCondition.toOfferingConditionText(
currentCount: Int,
totalCount: Int,
) = when (this) {
OfferingCondition.FULL -> context.getString(R.string.participant_full)
OfferingCondition.FULL -> context.getString(R.string.offering_detail_participant_full)
OfferingCondition.IMMINENT ->
context.getString(
R.string.participant_count,
R.string.offering_detail_participant_count,
currentCount,
totalCount,
)

OfferingCondition.CONFIRMED -> context.getString(R.string.participant_end)
OfferingCondition.CONFIRMED -> context.getString(R.string.offering_detail_participant_end)
OfferingCondition.AVAILABLE ->
context.getString(
R.string.participant_count,
R.string.offering_detail_participant_count,
currentCount,
totalCount,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.CheckBox
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResultListener
Expand Down Expand Up @@ -34,6 +36,7 @@ import kotlinx.coroutines.launch
class HomeFragment : Fragment(), OnOfferingClickListener {
private var _binding: FragmentHomeBinding? = null
private val binding get() = _binding!!
private var toast: Toast? = null

private lateinit var offeringAdapter: OfferingAdapter
private val viewModel: OfferingViewModel by viewModels {
Expand Down Expand Up @@ -88,6 +91,10 @@ class HomeFragment : Fragment(), OnOfferingClickListener {
viewModel.selectedFilter.observe(viewLifecycleOwner) { selectedFilter ->
updateCheckBoxStates(selectedFilter)
}

viewModel.error.observe(viewLifecycleOwner) { errMsgId ->
showToast(errMsgId)
}
}

private fun handleCheckBoxSelection(
Expand Down Expand Up @@ -219,6 +226,19 @@ class HomeFragment : Fragment(), OnOfferingClickListener {
}
}

private fun showToast(
@StringRes messageId: Int,
) {
toast?.cancel()
toast =
Toast.makeText(
requireActivity(),
getString(messageId),
Toast.LENGTH_SHORT,
)
toast?.show()
}

companion object {
const val OFFERING_ID = "offering_id"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zzang.chongdae.presentation.view.home

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
Expand All @@ -12,6 +13,7 @@ import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import androidx.paging.map
import com.zzang.chongdae.R
import com.zzang.chongdae.domain.model.Filter
import com.zzang.chongdae.domain.model.FilterName
import com.zzang.chongdae.domain.model.Offering
Expand Down Expand Up @@ -68,6 +70,9 @@ class OfferingViewModel(
private val _offeringsRefreshEvent: MutableSingleLiveData<Unit> = MutableSingleLiveData()
val offeringsRefreshEvent: SingleLiveData<Unit> get() = _offeringsRefreshEvent

private val _error: MutableSingleLiveData<Int> = MutableSingleLiveData()
val error: SingleLiveData<Int> get() = _error

init {
fetchOfferings()
fetchFilters()
Expand Down Expand Up @@ -127,7 +132,14 @@ class OfferingViewModel(
authRepository.saveRefresh()
fetchFilters()
}
else -> DataError.Network.UNKNOWN

DataError.Network.BAD_REQUEST -> {
_error.setValue(R.string.home_filter_error_message)
}

else -> {
Log.e("error", "fetchFilters Error: ${result.error.name}")
}
}
}

Expand Down Expand Up @@ -166,7 +178,14 @@ class OfferingViewModel(
authRepository.saveRefresh()
fetchUpdatedOffering(offeringId)
}
else -> DataError.Network.UNKNOWN

DataError.Network.BAD_REQUEST -> {
_error.setValue(R.string.home_updated_offering_error_mesasge)
}

else -> {
Log.e("error", "fetchUpdatedOffering Error: ${result.error.name}")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.StringRes
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.fragment.app.setFragmentResult
Expand All @@ -21,6 +23,7 @@ import com.zzang.chongdae.presentation.view.home.HomeFragment
class OfferingDetailFragment : Fragment() {
private var _binding: FragmentOfferingDetailBinding? = null
private val binding get() = _binding!!
private var toast: Toast? = null
private val offeringId by lazy {
arguments?.getLong(HomeFragment.OFFERING_ID)
?: throw IllegalArgumentException()
Expand Down Expand Up @@ -69,6 +72,10 @@ class OfferingDetailFragment : Fragment() {
openUrlInBrowser(getString(reportUrlId))
}

viewModel.error.observe(viewLifecycleOwner) { errMsgId ->
showToast(errMsgId)
}

viewModel.productLinkRedirectEvent.observe(viewLifecycleOwner) { productURL ->
openUrlInBrowser(productURL)
}
Expand Down Expand Up @@ -108,6 +115,19 @@ class OfferingDetailFragment : Fragment() {
}
}

private fun showToast(
@StringRes messageId: Int,
) {
toast?.cancel()
toast =
Toast.makeText(
requireActivity(),
getString(messageId),
Toast.LENGTH_SHORT,
)
toast?.show()
}

companion object {
const val OFFERING_DETAIL_BUNDLE_KEY = "offering_detail_bundle_key"
const val UPDATED_OFFERING_ID_KEY = "updated_offering_id"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.zzang.chongdae.presentation.view.offeringdetail

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
Expand Down Expand Up @@ -57,6 +58,9 @@ class OfferingDetailViewModel(
private val _productLinkRedirectEvent: MutableSingleLiveData<String> = MutableSingleLiveData()
val productLinkRedirectEvent: SingleLiveData<String> get() = _productLinkRedirectEvent

private val _error: MutableSingleLiveData<Int> = MutableSingleLiveData()
val error: SingleLiveData<Int> get() = _error

init {
loadOffering()
}
Expand All @@ -71,7 +75,13 @@ class OfferingDetailViewModel(
loadOffering()
}

else -> DataError.Network.UNKNOWN
DataError.Network.BAD_REQUEST -> {
_error.setValue(R.string.offering_detail_load_error_mesage)
}

else -> {
Log.e("error", "loadOffering Error: ${result.error.name}")
}
}

is Result.Success -> {
Expand All @@ -97,7 +107,13 @@ class OfferingDetailViewModel(
onClickParticipation()
}

else -> DataError.Network.UNKNOWN
DataError.Network.BAD_REQUEST -> {
_error.setValue(R.string.offering_detail_participation_error)
}

else -> {
Log.e("error", "onClickParticipation Error: ${result.error.name}")
}
}

is Result.Success -> {
Expand Down
2 changes: 1 addition & 1 deletion android/app/src/main/res/layout/fragment_home.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
android:layout_marginTop="28dp"
android:backgroundTint="@color/main_color"
android:fontFamily="@font/suit_medium"
android:hint="@string/main_search_hint_text"
android:hint="@string/home_search_hint_text"
android:inputType="text"
android:paddingStart="15dp"
android:paddingEnd="30dp"
Expand Down
10 changes: 5 additions & 5 deletions android/app/src/main/res/layout/fragment_offering_detail.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@
android:id="@+id/tv_product_link_comment"
style="@style/Theme.AppCompat.TextView.Bold.Gray900.Size16"
android:layout_marginTop="18dp"
android:text="@string/detail_product_url_title"
android:text="@string/offering_detail_product_url_title"
app:isVisible="@{vm.offeringDetail.productUrl != null}"
app:layout_constraintBottom_toTopOf="@id/horizon_line"
app:layout_constraintStart_toStartOf="@id/tv_title"
Expand All @@ -163,8 +163,8 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="@font/suit_bold"
android:text="@string/offering_detail_product_url_text"
android:onClick="@{() -> vm.onClickProductRedirectText(vm.offeringDetail.productUrl)}"
android:text="@string/detail_product_url_text"
android:textColor="@color/main_color"
android:textSize="16sp"
app:isVisible="@{vm.offeringDetail.productUrl != null}"
Expand Down Expand Up @@ -197,7 +197,7 @@
style="@style/Theme.AppCompat.TextView.Bold.Gray900.Size16"
android:layout_marginStart="8dp"
android:layout_marginTop="15dp"
android:text="@string/detail_divided_money_title"
android:text="@string/offering_detail_divided_money_title"
app:layout_constraintBottom_toBottomOf="@id/iv_money"
app:layout_constraintStart_toEndOf="@id/iv_money"
app:layout_constraintTop_toBottomOf="@id/horizon_line" />
Expand All @@ -222,7 +222,7 @@
android:layout_marginStart="5dp"
android:layout_marginTop="8dp"
android:fontFamily="@font/suit_medium"
android:text="@{@string/total_price(vm.offeringDetail.totalPrice)}"
android:text="@{@string/offering_detail_total_price(vm.offeringDetail.totalPrice)}"
android:textColor="@color/gray_500"
android:textSize="11sp"
app:layout_constraintBottom_toBottomOf="@id/tv_divided_price"
Expand Down Expand Up @@ -374,7 +374,7 @@
android:background="@drawable/btn_participation_opened"
android:fontFamily="@font/suit_medium"
android:onClick="@{() -> vm.onClickMoveCommentDetail()}"
android:text="@string/detail_move_comment_detail"
android:text="@string/offering_detail_move_comment_detail"
android:textColor="@color/white"
app:isVisible="@{vm.isParticipated}"
app:layout_constraintBottom_toBottomOf="parent"
Expand Down
6 changes: 3 additions & 3 deletions android/app/src/main/res/layout/item_offering.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
app:layout_constraintStart_toStartOf="@id/iv_product"
app:layout_constraintTop_toTopOf="@id/iv_product"
app:offeringCondition="@{offering.status}"
tools:text="@string/main_offering_closed" />
tools:text="@string/home_offering_closed" />

<TextView
android:id="@+id/tv_offering_title"
Expand Down Expand Up @@ -109,7 +109,7 @@
android:layout_marginBottom="17dp"
android:ellipsize="end"
android:maxLines="1"
android:text='@{offering.originPrice != null ? @string/strike_money_amount_text(offering.originPrice) : ""}'
android:text='@{offering.originPrice != null ? @string/all_money_amount_text(offering.originPrice) : ""}'
app:layout_constraintBottom_toBottomOf="@id/tv_divided_price"
app:layout_constraintStart_toStartOf="@id/tv_meeting_address"
app:layout_constraintTop_toTopOf="@id/tv_divided_price" />
Expand All @@ -119,7 +119,7 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:text='@{offering.originPrice != null ? @string/main_discount_rate_text(offering.calculateDiscountRate()) : ""}'
android:text='@{offering.originPrice != null ? @string/home_discount_rate_text(offering.calculateDiscountRate()) : ""}'
android:textColor="@color/main_color"
android:textSize="@dimen/size_13"
android:textStyle="bold"
Expand Down
Loading

0 comments on commit 2a750e7

Please sign in to comment.