feat(gift): 선물 상세 표시 모델을 추가한다

This commit is contained in:
2026-10-02 18:30:32 +09:00
parent d3be7263d7
commit a1cfff458b
3 changed files with 562 additions and 0 deletions
@@ -0,0 +1,73 @@
package kr.co.vividnext.sodalive.v2.gift
sealed interface GiftDetailText {
enum class Key : GiftDetailText {
RECEIVED_LABEL,
RECEIVED_TITLE,
RECEIVED_BODY,
TRACKING_REGISTERED_LABEL,
TRACKING_REGISTERED_TITLE,
ARRIVED_AT_MAILBOX_LABEL,
ARRIVED_AT_MAILBOX_TITLE,
INSPECTION_COMPLETED_LABEL,
INSPECTION_COMPLETED_TITLE,
DELIVERED_LABEL,
DELIVERED_TITLE,
UNDELIVERABLE_LABEL,
UNDELIVERABLE_TITLE,
UNDELIVERABLE_BODY,
CANCELED_LABEL,
CANCELED_TITLE,
CANCELED_BODY,
DISPOSAL_NON_RETURN_GUIDANCE
}
data class Raw(val value: String) : GiftDetailText
data class CancellationBody(val creatorNickname: String?, val applicationNo: String) : GiftDetailText
data class CanAmount(val amount: Int) : GiftDetailText
}
data class GiftDetailUiModel(
val applicationNo: String,
val status: String,
val hero: GiftDetailHero,
val timeline: List<GiftDetailTimelineStep>,
val progressSegments: List<GiftDetailTimelineStep>,
val showProgress: Boolean,
val cancellationRows: List<GiftDetailInfoRow>,
val giftInfo: GiftDetailInfo,
val senderInfo: GiftDetailAddress?,
val mailbox: GiftDetailAddress?,
val recipientAddress: GiftDetailAddress?,
val undeliverable: GiftDetailUndeliverable?,
val cancellation: GiftDetailCancellation?,
val actions: Set<GiftDetailAction>
)
data class GiftDetailHero(val chip: GiftDetailText?, val title: GiftDetailText, val body: GiftDetailText?)
data class GiftDetailInfoRow(val label: Int, val value: GiftDetailText)
data class GiftDetailTimelineStep(val status: String, val title: GiftDetailText, val date: String?) {
val completed: Boolean get() = date != null
}
data class GiftDetailInfo(
val counterpartNickname: String?,
val sizeName: String,
val categoryName: String,
val paidCan: Int?,
val tracking: String?,
val shippingRequestedDate: String?,
val inspectionDate: String?,
val deliveredDate: String?,
val canceledDate: String?
)
data class GiftDetailAddress(val name: String, val phoneNumber: String, val address: String)
data class GiftDetailUndeliverable(val title: GiftDetailText, val guidance: GiftDetailText)
data class GiftDetailCancellation(val date: String?, val refundCan: Int?)
enum class GiftDetailAction { TRACKING, CANCEL, INQUIRY }
@@ -0,0 +1,145 @@
package kr.co.vividnext.sodalive.v2.gift
import kr.co.vividnext.sodalive.R
import kr.co.vividnext.sodalive.v2.gift.GiftDetailText.Key
import kr.co.vividnext.sodalive.v2.gift.GiftDetailText.Raw
import kr.co.vividnext.sodalive.v2.gift.data.GiftAddressResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftCancelResponse
import kr.co.vividnext.sodalive.v2.gift.data.GiftDetailResponse
import java.text.ParsePosition
import java.text.SimpleDateFormat
import java.util.Locale
import java.util.TimeZone
object GiftDetailPresenter {
private val normalStates = linkedMapOf(
"RECEIVED" to Key.RECEIVED_LABEL,
"TRACKING_REGISTERED" to Key.TRACKING_REGISTERED_LABEL,
"ARRIVED_AT_MAILBOX" to Key.ARRIVED_AT_MAILBOX_LABEL,
"INSPECTION_COMPLETED" to Key.INSPECTION_COMPLETED_LABEL,
"DELIVERED" to Key.DELIVERED_LABEL
)
private val knownStates = normalStates.keys + setOf("UNDELIVERABLE", "CANCELED")
private val utcPattern = Regex("([0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2})(?:\\.[0-9]{1,9})?(?:Z|\\+00:00)")
fun present(detail: GiftDetailResponse, cancel: GiftCancelResponse? = null): GiftDetailUiModel {
val status = if (cancel != null) "CANCELED" else detail.status
val info = detail.giftInfo
val history = detail.statusTimeline.associateBy { it.status }
val canceledDate = formatCardDate(if (cancel != null) cancel.canceledAt else detail.delivery.canceledAt)
val timeline = normalStates.map { (state, title) ->
val entry = history[state]
GiftDetailTimelineStep(state, entry?.statusName?.let(::Raw) ?: title, formatProgressDate(entry?.occurredAt))
}
val cancellation = if (status == "CANCELED") {
GiftDetailCancellation(canceledDate, cancel?.priceCan ?: info.paidCan)
} else {
null
}
return GiftDetailUiModel(
applicationNo = detail.applicationNo,
status = status,
hero = hero(status, detail.statusName).let {
if (cancellation == null) {
it
} else {
it.copy(body = GiftDetailText.CancellationBody(info.recipientCreatorNickname, detail.applicationNo))
}
},
timeline = timeline,
progressSegments = timeline.filter { it.status != "INSPECTION_COMPLETED" }.map { step ->
step.copy(
title = normalStates.getValue(step.status),
date = if (step.status == "ARRIVED_AT_MAILBOX") {
step.date ?: timeline.first { it.status == "INSPECTION_COMPLETED" }.date
} else {
step.date
}
)
},
showProgress = status in normalStates.keys - "RECEIVED",
cancellationRows = if (cancellation == null) {
emptyList()
} else {
listOfNotNull(
info.recipientCreatorNickname?.let { GiftDetailInfoRow(R.string.gift_detail_creator, Raw(it)) },
GiftDetailInfoRow(R.string.gift_send_size, Raw(info.sizeName)),
GiftDetailInfoRow(R.string.gift_send_category, Raw(info.categoryName)),
GiftDetailInfoRow(R.string.gift_detail_application_no, Raw(detail.applicationNo)),
formatCardDate(history["RECEIVED"]?.occurredAt)?.let {
GiftDetailInfoRow(R.string.gift_tracking_application_date, Raw(it))
},
canceledDate?.let { GiftDetailInfoRow(R.string.gift_detail_canceled_date, Raw(it)) },
cancellation.refundCan?.let {
GiftDetailInfoRow(R.string.gift_detail_refund, GiftDetailText.CanAmount(it))
}
)
},
giftInfo = GiftDetailInfo(
counterpartNickname = if (detail.direction == "RECEIVED") info.senderNickname else info.recipientCreatorNickname,
sizeName = info.sizeName,
categoryName = info.categoryName,
paidCan = info.paidCan,
tracking = info.tracking,
shippingRequestedDate = formatCardDate(info.shippingRequestedAt),
inspectionDate = formatCardDate(history["INSPECTION_COMPLETED"]?.occurredAt),
deliveredDate = formatCardDate(history["DELIVERED"]?.occurredAt),
canceledDate = canceledDate
),
senderInfo = detail.senderInfo?.toUiModel(),
mailbox = detail.mailbox?.toUiModel(),
recipientAddress = if (detail.direction == "RECEIVED") detail.recipientAddress?.toUiModel() else null,
undeliverable = if (status == "UNDELIVERABLE") {
GiftDetailUndeliverable(
title = detail.delivery.undeliverableReason?.let(::Raw) ?: Key.UNDELIVERABLE_LABEL,
guidance = Key.DISPOSAL_NON_RETURN_GUIDANCE
)
} else {
null
},
cancellation = cancellation,
actions = buildSet {
if (status in knownStates && detail.trackingRequired) add(GiftDetailAction.TRACKING)
if (detail.direction == "SENT" && status == "RECEIVED") add(GiftDetailAction.CANCEL)
add(GiftDetailAction.INQUIRY)
}
)
}
internal fun showsTrackingAction(state: GiftDetailUiState): Boolean {
val detail = (state as? GiftDetailUiState.Content)?.detail ?: return false
val model = present(detail)
return model.cancellation == null && GiftDetailAction.TRACKING in model.actions
}
fun formatProgressDate(occurredAt: String?): String? = formatDate(occurredAt, "MM.dd")
fun formatCardDate(occurredAt: String?): String? = formatDate(occurredAt, "yyyy.MM.dd")
private fun formatDate(occurredAt: String?, pattern: String): String? {
val seconds = occurredAt?.let { utcPattern.matchEntire(it)?.groupValues?.get(1) } ?: return null
val parser = SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.US).apply {
isLenient = false
timeZone = TimeZone.getTimeZone("UTC")
}
val date = parser.parse(seconds, ParsePosition(0)) ?: return null
return SimpleDateFormat(pattern, Locale.US).format(date)
}
private fun GiftAddressResponse.toUiModel() = GiftDetailAddress(name, phoneNumber, address)
private fun hero(status: String, statusName: String): GiftDetailHero = when (status) {
"RECEIVED" -> GiftDetailHero(null, Key.RECEIVED_TITLE, Key.RECEIVED_BODY)
"TRACKING_REGISTERED" -> GiftDetailHero(Key.TRACKING_REGISTERED_LABEL, Key.TRACKING_REGISTERED_TITLE, null)
"ARRIVED_AT_MAILBOX" -> GiftDetailHero(Key.ARRIVED_AT_MAILBOX_LABEL, Key.ARRIVED_AT_MAILBOX_TITLE, null)
"INSPECTION_COMPLETED" -> GiftDetailHero(Key.INSPECTION_COMPLETED_LABEL, Key.INSPECTION_COMPLETED_TITLE, null)
"DELIVERED" -> GiftDetailHero(Key.DELIVERED_LABEL, Key.DELIVERED_TITLE, null)
"UNDELIVERABLE" -> GiftDetailHero(
Key.UNDELIVERABLE_LABEL,
Key.UNDELIVERABLE_TITLE,
Key.UNDELIVERABLE_BODY
)
"CANCELED" -> GiftDetailHero(Key.CANCELED_LABEL, Key.CANCELED_TITLE, Key.CANCELED_BODY)
else -> GiftDetailHero(null, Raw(statusName), null)
}
}