Skip to content

Commit

Permalink
[AN/USER] refactor: 티켓 목록 화면 presentation model 을 사용하지 않도록 변경 (#420)
Browse files Browse the repository at this point in the history
* refactor: 티켓 아이템 UiState 팩토리 메서드 생성

* refactor: TicketListItemUiState 변경 사항 화면에 반영
  • Loading branch information
SeongHoonC authored Sep 1, 2023
1 parent 4bface8 commit 0b73941
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
package com.festago.festago.presentation.ui.home.ticketlist

import com.festago.festago.presentation.model.StageUiModel
import com.festago.festago.presentation.model.TicketConditionUiModel
import com.festago.festago.model.Stage
import com.festago.festago.model.Ticket
import com.festago.festago.model.TicketCondition
import java.time.LocalDateTime

data class TicketListItemUiState(
val id: Long = -1,
val number: Int = -1,
val entryTime: LocalDateTime = LocalDateTime.MIN,
val reserveAt: LocalDateTime = LocalDateTime.MIN,
val condition: TicketConditionUiModel = TicketConditionUiModel.BEFORE_ENTRY,
val stage: StageUiModel = StageUiModel(),
val festivalId: Int = -1,
val festivalName: String = "",
val festivalThumbnail: String = "",
val id: Long,
val number: Int,
val entryTime: LocalDateTime,
val reserveAt: LocalDateTime,
val condition: TicketCondition,
val stage: Stage,
val festivalId: Int,
val festivalName: String,
val festivalThumbnail: String,
val canEntry: Boolean,
val onTicketEntry: (ticketId: Long) -> Unit,
)
) {
companion object {
fun of(ticket: Ticket, onTicketEntry: (ticketId: Long) -> Unit) = TicketListItemUiState(
id = ticket.id,
number = ticket.number,
entryTime = ticket.entryTime,
reserveAt = ticket.reserveAt,
condition = ticket.condition,
stage = ticket.stage,
festivalId = ticket.festivalTicket.id,
festivalName = ticket.festivalTicket.name,
festivalThumbnail = ticket.festivalTicket.thumbnail,
canEntry = LocalDateTime.now().isAfter(ticket.entryTime),
onTicketEntry = onTicketEntry,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.widget.Button
import androidx.recyclerview.widget.RecyclerView
import com.festago.festago.R
import com.festago.festago.databinding.ItemTicketListBinding
import com.festago.festago.model.TicketCondition
import java.time.format.DateTimeFormatter

class TicketListItemViewHolder(
Expand All @@ -14,13 +15,22 @@ class TicketListItemViewHolder(

fun bind(item: TicketListItemUiState) {
binding.ticket = item
setTicketConditionText(item)
setTicketEntryBtn(item)
}

private fun setTicketConditionText(item: TicketListItemUiState) {
val ticketConditionResId = when (item.condition) {
TicketCondition.BEFORE_ENTRY -> R.string.all_ticket_state_before_entry
TicketCondition.AFTER_ENTRY -> R.string.all_ticket_state_after_entry
TicketCondition.AWAY -> R.string.all_ticket_state_away
}
binding.tvTicketCondition.setText(ticketConditionResId)
}

private fun setTicketEntryBtn(item: TicketListItemUiState) {
val btn = binding.btnTicketEntry
btn.isEnabled = item.canEntry

setTicketEntryBtnText(isAfterEntryTime = item.canEntry, btn = btn, ticket = item)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.festago.festago.analytics.AnalyticsHelper
import com.festago.festago.analytics.logNetworkFailure
import com.festago.festago.model.Ticket
import com.festago.festago.presentation.mapper.toPresentation
import com.festago.festago.presentation.util.MutableSingleLiveData
import com.festago.festago.presentation.util.SingleLiveData
import com.festago.festago.repository.TicketRepository
import kotlinx.coroutines.launch
import java.time.LocalDateTime

class TicketListViewModel(
private val ticketRepository: TicketRepository,
Expand All @@ -29,7 +26,9 @@ class TicketListViewModel(
viewModelScope.launch {
ticketRepository.loadCurrentTickets()
.onSuccess { tickets ->
_uiState.value = TicketListUiState.Success(tickets.map { it.toUiState() })
_uiState.value = TicketListUiState.Success(
tickets.map { TicketListItemUiState.of(it, ::showTicketEntry) },
)
}.onFailure {
_uiState.value = TicketListUiState.Error
analyticsHelper.logNetworkFailure(KEY_LOAD_TICKETS_LOG, it.message.toString())
Expand All @@ -41,20 +40,6 @@ class TicketListViewModel(
_event.setValue(TicketListEvent.ShowTicketEntry(ticketId))
}

private fun Ticket.toUiState() = TicketListItemUiState(
id = id,
number = number,
entryTime = entryTime,
reserveAt = reserveAt,
condition = condition.toPresentation(),
stage = stage.toPresentation(),
festivalId = festivalTicket.id,
festivalName = festivalTicket.name,
festivalThumbnail = festivalTicket.thumbnail,
canEntry = LocalDateTime.now().isAfter(entryTime),
onTicketEntry = ::showTicketEntry,
)

companion object {
private const val KEY_LOAD_TICKETS_LOG = "load_tickets"
}
Expand Down
3 changes: 1 addition & 2 deletions android/festago/app/src/main/res/layout/item_ticket_list.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,10 @@
tools:text="100번" />

<TextView
android:id="@+id/tvTicketState"
android:id="@+id/tvTicketCondition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:text="@{ticket.condition.stateName}"
android:textColor="@color/md_theme_light_primary"
android:textSize="16sp"
android:textStyle="bold"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.arch.core.executor.testing.InstantTaskExecutorRule
import com.festago.festago.analytics.AnalyticsHelper
import com.festago.festago.model.Ticket
import com.festago.festago.presentation.fixture.TicketFixture
import com.festago.festago.presentation.mapper.toPresentation
import com.festago.festago.repository.TicketRepository
import io.mockk.coEvery
import io.mockk.mockk
Expand All @@ -20,7 +19,6 @@ import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import java.time.LocalDateTime

class TicketListViewModelTest {
private lateinit var vm: TicketListViewModel
Expand Down Expand Up @@ -67,7 +65,7 @@ class TicketListViewModelTest {

// and
val actual = (vm.uiState.value as TicketListUiState.Success).tickets
val expected = tickets.map { it.toUiState() }
val expected = tickets.map { TicketListItemUiState.of(it, vm::showTicketEntry) }
assertThat(actual).isEqualTo(expected)
}
softly.assertAll()
Expand All @@ -94,7 +92,8 @@ class TicketListViewModelTest {

// and
val actual = (vm.uiState.value as TicketListUiState.Success).tickets
val expected = fakeEmptyTickets.map { it.toUiState() }
val expected =
fakeEmptyTickets.map { TicketListItemUiState.of(it, vm::showTicketEntry) }
assertThat(actual).isEqualTo(expected)
}
softly.assertAll()
Expand Down Expand Up @@ -164,18 +163,4 @@ class TicketListViewModelTest {
val expected = 1L
assertThat(actual).isEqualTo(expected)
}

private fun Ticket.toUiState() = TicketListItemUiState(
id = id,
number = number,
entryTime = entryTime,
reserveAt = reserveAt,
condition = condition.toPresentation(),
stage = stage.toPresentation(),
festivalId = festivalTicket.id,
festivalName = festivalTicket.name,
festivalThumbnail = festivalTicket.thumbnail,
canEntry = LocalDateTime.now().isAfter(entryTime),
onTicketEntry = vm::showTicketEntry,
)
}

0 comments on commit 0b73941

Please sign in to comment.