Skip to content

Commit 7daeab9

Browse files
committed
refactor: MissionViewModel에서 버스 지하철 로직 분기처리
1 parent 752db07 commit 7daeab9

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

presentation/src/main/java/com/stop/ui/mission/MissionFragment.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,13 @@ class MissionFragment : Fragment(), MissionHandler {
142142
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
143143
}
144144
}
145+
viewModel.transportIsArrived.observe(viewLifecycleOwner) {
146+
it.getContentIfNotHandled()?.let { isArrived ->
147+
if (isArrived) {
148+
Toast.makeText(requireContext(), "도착했습니다.", Toast.LENGTH_LONG).show()
149+
}
150+
}
151+
}
145152
}
146153

147154
private fun drawBusLocationLine() {

presentation/src/main/java/com/stop/ui/mission/MissionViewModel.kt

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class MissionViewModel @Inject constructor(
5555
val errorMessage: LiveData<Event<ErrorType>>
5656
get() = _errorMessage
5757

58+
private val _transportIsArrived = MutableLiveData<Event<Boolean>>()
59+
val transportIsArrived: LiveData<Event<Boolean>>
60+
get() = _transportIsArrived
61+
5862
val leftMinute: LiveData<String> = Transformations.switchMap(estimatedTimeRemaining) {
5963
MutableLiveData<String>().apply {
6064
value = (it / TIME_UNIT).toString().padStart(TIME_DIGIT, '0')
@@ -80,8 +84,15 @@ class MissionViewModel @Inject constructor(
8084
lateinit var startSubwayStation: String
8185

8286
init {
83-
getBusNowLocation()
84-
getSubwayRoute()
87+
startMission()
88+
}
89+
90+
fun startMission() {
91+
val transportLastTime = _transportLastTime.value ?: return
92+
when (transportLastTime.transportMoveType) {
93+
TransportMoveType.BUS -> getBusNowLocation(transportLastTime)
94+
TransportMoveType.SUBWAY -> getSubwayRoute()
95+
}
8596
}
8697

8798
fun setDestination(inputDestination: String) {
@@ -121,20 +132,14 @@ class MissionViewModel @Inject constructor(
121132
}
122133
}
123134

124-
private fun getBusNowLocation() {
125-
val lastTimeValue = _transportLastTime.value
126-
if (lastTimeValue == null) {
127-
_errorMessage.value = Event(ErrorType.TRANSPORT_LAST_TIME_IS_NOT_RECEIVED_YET)
128-
throw AlreadyHandledException()
129-
}
130-
135+
private fun getBusNowLocation(transportLastTime: TransportLastTime) {
131136
viewModelScope.launch {
132137
/**
133138
* 이 작업은 알람 화면에서 진행되고,
134139
* 탑승해야 하는 버스 아이디 1개만 전해줍니다.
135140
* 여기서는 임의로 중간에 있는 버스를 선택했습니다.
136141
*/
137-
var busVehicleIds = getBusesOnRouteUseCase(lastTimeValue)
142+
var busVehicleIds = getBusesOnRouteUseCase(transportLastTime)
138143
if (busVehicleIds.isEmpty()) {
139144
_errorMessage.value = Event(ErrorType.AVAILABLE_BUS_NO_EXIST_YET)
140145
throw AlreadyHandledException()
@@ -162,12 +167,17 @@ class MissionViewModel @Inject constructor(
162167
}
163168
delay(5000)
164169
}
165-
_errorMessage.value = Event(ErrorType.MISSION_SOMETHING_WRONG)
170+
val transportIsArrived = _transportIsArrived.value?.peekContent()
171+
if (transportIsArrived != true) {
172+
_errorMessage.value = Event(ErrorType.MISSION_SOMETHING_WRONG)
173+
return@launch
174+
}
166175
}
167176
}
168177

169178
// TODO: 버스가 도착했을 때 처리하기
170179
private fun busArrivedAtDestination() {
180+
_transportIsArrived.value = Event(true)
171181
}
172182

173183
private suspend fun getSubwayTrainNowLocation(): TrainLocationInfoDomain {
@@ -226,9 +236,6 @@ class MissionViewModel @Inject constructor(
226236
private const val RANDOM_LIMIT = 5
227237
private const val ZERO = 0
228238

229-
private const val TEST_BUS_540_ROUTE_ID = "100100083"
230-
private var TIME_TEST = 0
231-
232239
private const val TEST_SUBWAY_LINE_NUMBER = 4
233240
private const val LINE = "호선" //임시로.. 종성님이 어떻게 넘겨주시느냐에 따라 달림
234241

0 commit comments

Comments
 (0)