Skip to content

Commit 4cb2727

Browse files
committed
fix: Fragment 재생성 시 LiveData 중복 observe 문제 해결
1 parent 5534469 commit 4cb2727

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

presentation/src/main/java/com/stop/ui/route/RouteFragment.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ class RouteFragment : Fragment() {
8282
}
8383

8484
viewModel.lastTimeResponse.observe(viewLifecycleOwner) {
85-
binding.root.findNavController().navigate(R.id.action_routeFragment_to_routeDetailFragment)
85+
it.getContentIfNotHandled()?.let {
86+
binding.root.findNavController().navigate(R.id.action_routeFragment_to_routeDetailFragment)
87+
}
8688
}
8789
}
8890

presentation/src/main/java/com/stop/ui/route/RouteViewModel.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ class RouteViewModel @Inject constructor(
3535
val routeResponse: LiveData<List<Itinerary>>
3636
get() = _routeResponse
3737

38-
private val _lastTimeResponse = MutableLiveData<List<String?>>()
39-
val lastTimeResponse: LiveData<List<String?>>
38+
private val _lastTimeResponse = MutableLiveData<Event<List<String?>>>()
39+
val lastTimeResponse: LiveData<Event<List<String?>>>
4040
get() = _lastTimeResponse
4141

4242
private val _errorMessage = MutableLiveData<Event<ErrorType>>()
@@ -69,7 +69,7 @@ class RouteViewModel @Inject constructor(
6969
fun calculateLastTransportTime(itinerary: Itinerary) {
7070
checkClickedItinerary(itinerary)
7171
viewModelScope.launch {
72-
this@RouteViewModel._lastTimeResponse.value = getLastTransportTimeUseCase(itinerary)
72+
this@RouteViewModel._lastTimeResponse.value = Event(getLastTransportTimeUseCase(itinerary))
7373
}
7474
}
7575

@@ -90,7 +90,7 @@ class RouteViewModel @Inject constructor(
9090
val lastTimes = _lastTimeResponse.value ?: return "이 함수를 호출한 시점에 막차 데이터가 null인 논리적 오류가 발생했습니다."
9191

9292
return clickedItinerary.routes.mapIndexed { index, route ->
93-
"${route.start.name}(${lastTimes[index]})"
93+
"${route.start.name}(${lastTimes.peekContent()[index]})"
9494
}.joinToString(" -> ")
9595
}
9696
}

0 commit comments

Comments
 (0)