Skip to content

Commit 44d2be1

Browse files
committed
feat: 간단하게 막차 데이터와 경로 데이터 상세 경로 화면에 표시하기
1 parent a902afd commit 44d2be1

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import android.view.LayoutInflater
66
import android.view.View
77
import android.view.ViewGroup
88
import android.widget.Toast
9-
import androidx.fragment.app.viewModels
9+
import androidx.fragment.app.activityViewModels
10+
import androidx.navigation.findNavController
1011
import com.stop.R
1112
import com.stop.databinding.FragmentRouteBinding
1213
import com.stop.domain.model.route.tmap.custom.Itinerary
@@ -22,7 +23,8 @@ class RouteFragment : Fragment() {
2223
private val binding: FragmentRouteBinding
2324
get() = _binding!!
2425

25-
private val viewModel: RouteViewModel by viewModels()
26+
private val viewModel: RouteViewModel by activityViewModels()
27+
2628
private lateinit var adapter: RouteAdapter
2729

2830
override fun onCreateView(
@@ -78,6 +80,10 @@ class RouteFragment : Fragment() {
7880
Toast.makeText(requireContext(), message, Toast.LENGTH_SHORT).show()
7981
}
8082
}
83+
84+
viewModel.lastTimeResponse.observe(viewLifecycleOwner) {
85+
binding.root.findNavController().navigate(R.id.action_routeFragment_to_routeDetailFragment)
86+
}
8187
}
8288

8389
private fun setStartAndDestinationText() {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class RouteViewModel @Inject constructor(
2222
private val getLastTransportTimeUseCase: GetLastTransportTimeUseCase,
2323
) : ViewModel() {
2424

25+
var clickedItineraryIndex: Int = -1
26+
2527
private val _origin = MutableLiveData<Place>()
2628
val origin: LiveData<Place>
2729
get() = _origin
@@ -66,18 +68,32 @@ class RouteViewModel @Inject constructor(
6668
}
6769

6870
fun calculateLastTransportTime(itinerary: Itinerary) {
71+
checkClickedItinerary(itinerary)
6972
viewModelScope.launch(Dispatchers.IO) {
7073
val lastTimeInfo = getLastTransportTimeUseCase(itinerary)
7174

7275
_lastTimeResponse.postValue(lastTimeInfo)
7376
}
7477
}
7578

79+
private fun checkClickedItinerary(itinerary: Itinerary) {
80+
clickedItineraryIndex = _routeResponse.value?.indexOf(itinerary) ?: -1
81+
}
82+
7683
fun setOrigin(place: Place) {
7784
_origin.value = place
7885
}
7986

8087
fun setDestination(place: Place) {
8188
_destination.value = place
8289
}
90+
91+
fun getResult(): String {
92+
val clickedItinerary = _routeResponse.value?.get(clickedItineraryIndex) ?: return "함수를 잘못 호출했습니다."
93+
val lastTimes = _lastTimeResponse.value ?: return "이 함수를 호출한 시점에 막차 데이터가 null인 논리적 오류가 발생했습니다."
94+
95+
return clickedItinerary.routes.mapIndexed { index, route ->
96+
"${route.start.name}(${lastTimes[index]})"
97+
}.joinToString(" -> ")
98+
}
8399
}

presentation/src/main/java/com/stop/ui/routedetail/RouteDetailFragment.kt

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,19 @@ import androidx.fragment.app.Fragment
55
import android.view.LayoutInflater
66
import android.view.View
77
import android.view.ViewGroup
8+
import androidx.fragment.app.activityViewModels
89
import com.stop.databinding.FragmentRouteDetailBinding
10+
import com.stop.ui.route.RouteViewModel
11+
import dagger.hilt.android.AndroidEntryPoint
912

13+
@AndroidEntryPoint
1014
class RouteDetailFragment : Fragment() {
15+
1116
private var _binding: FragmentRouteDetailBinding? = null
1217
private val binding get() = _binding!!
1318

19+
private val parentViewModel: RouteViewModel by activityViewModels()
20+
1421
override fun onCreateView(
1522
inflater: LayoutInflater, container: ViewGroup?,
1623
savedInstanceState: Bundle?
@@ -28,7 +35,8 @@ class RouteDetailFragment : Fragment() {
2835
}
2936

3037
private fun initBinding() {
31-
38+
binding.lifecycleOwner = viewLifecycleOwner
39+
binding.parentViewModel = parentViewModel
3240
}
3341

3442
private fun initView() {
Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,26 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
xmlns:tools="http://schemas.android.com/tools"
4-
android:layout_width="match_parent"
5-
android:layout_height="match_parent">
2+
<layout xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:app="http://schemas.android.com/apk/res-auto">
64

5+
<data>
6+
<variable
7+
name="parentViewModel"
8+
type="com.stop.ui.route.RouteViewModel" />
9+
</data>
710

8-
</FrameLayout>
11+
<androidx.constraintlayout.widget.ConstraintLayout
12+
android:layout_width="match_parent"
13+
android:layout_height="match_parent">
14+
15+
16+
<TextView
17+
android:id="@+id/text_view_result"
18+
android:layout_width="wrap_content"
19+
android:layout_height="wrap_content"
20+
android:text="@{parentViewModel.getResult()}"
21+
app:layout_constraintBottom_toBottomOf="parent"
22+
app:layout_constraintEnd_toEndOf="parent"
23+
app:layout_constraintStart_toStartOf="parent"
24+
app:layout_constraintTop_toTopOf="parent" />
25+
</androidx.constraintlayout.widget.ConstraintLayout>
26+
</layout>

0 commit comments

Comments
 (0)