File tree Expand file tree Collapse file tree 4 files changed +56
-8
lines changed Expand file tree Collapse file tree 4 files changed +56
-8
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,8 @@ import android.view.LayoutInflater
6
6
import android.view.View
7
7
import android.view.ViewGroup
8
8
import android.widget.Toast
9
- import androidx.fragment.app.viewModels
9
+ import androidx.fragment.app.activityViewModels
10
+ import androidx.navigation.findNavController
10
11
import com.stop.R
11
12
import com.stop.databinding.FragmentRouteBinding
12
13
import com.stop.domain.model.route.tmap.custom.Itinerary
@@ -22,7 +23,8 @@ class RouteFragment : Fragment() {
22
23
private val binding: FragmentRouteBinding
23
24
get() = _binding !!
24
25
25
- private val viewModel: RouteViewModel by viewModels()
26
+ private val viewModel: RouteViewModel by activityViewModels()
27
+
26
28
private lateinit var adapter: RouteAdapter
27
29
28
30
override fun onCreateView (
@@ -78,6 +80,10 @@ class RouteFragment : Fragment() {
78
80
Toast .makeText(requireContext(), message, Toast .LENGTH_SHORT ).show()
79
81
}
80
82
}
83
+
84
+ viewModel.lastTimeResponse.observe(viewLifecycleOwner) {
85
+ binding.root.findNavController().navigate(R .id.action_routeFragment_to_routeDetailFragment)
86
+ }
81
87
}
82
88
83
89
private fun setStartAndDestinationText () {
Original file line number Diff line number Diff line change @@ -22,6 +22,8 @@ class RouteViewModel @Inject constructor(
22
22
private val getLastTransportTimeUseCase : GetLastTransportTimeUseCase ,
23
23
) : ViewModel() {
24
24
25
+ var clickedItineraryIndex: Int = - 1
26
+
25
27
private val _origin = MutableLiveData <Place >()
26
28
val origin: LiveData <Place >
27
29
get() = _origin
@@ -66,18 +68,32 @@ class RouteViewModel @Inject constructor(
66
68
}
67
69
68
70
fun calculateLastTransportTime (itinerary : Itinerary ) {
71
+ checkClickedItinerary(itinerary)
69
72
viewModelScope.launch(Dispatchers .IO ) {
70
73
val lastTimeInfo = getLastTransportTimeUseCase(itinerary)
71
74
72
75
_lastTimeResponse .postValue(lastTimeInfo)
73
76
}
74
77
}
75
78
79
+ private fun checkClickedItinerary (itinerary : Itinerary ) {
80
+ clickedItineraryIndex = _routeResponse .value?.indexOf(itinerary) ? : - 1
81
+ }
82
+
76
83
fun setOrigin (place : Place ) {
77
84
_origin .value = place
78
85
}
79
86
80
87
fun setDestination (place : Place ) {
81
88
_destination .value = place
82
89
}
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
+ }
83
99
}
Original file line number Diff line number Diff line change @@ -5,12 +5,19 @@ import androidx.fragment.app.Fragment
5
5
import android.view.LayoutInflater
6
6
import android.view.View
7
7
import android.view.ViewGroup
8
+ import androidx.fragment.app.activityViewModels
8
9
import com.stop.databinding.FragmentRouteDetailBinding
10
+ import com.stop.ui.route.RouteViewModel
11
+ import dagger.hilt.android.AndroidEntryPoint
9
12
13
+ @AndroidEntryPoint
10
14
class RouteDetailFragment : Fragment () {
15
+
11
16
private var _binding : FragmentRouteDetailBinding ? = null
12
17
private val binding get() = _binding !!
13
18
19
+ private val parentViewModel: RouteViewModel by activityViewModels()
20
+
14
21
override fun onCreateView (
15
22
inflater : LayoutInflater , container : ViewGroup ? ,
16
23
savedInstanceState : Bundle ?
@@ -28,7 +35,8 @@ class RouteDetailFragment : Fragment() {
28
35
}
29
36
30
37
private fun initBinding () {
31
-
38
+ binding.lifecycleOwner = viewLifecycleOwner
39
+ binding.parentViewModel = parentViewModel
32
40
}
33
41
34
42
private fun initView () {
Original file line number Diff line number Diff line change 1
1
<?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" >
6
4
5
+ <data >
6
+ <variable
7
+ name =" parentViewModel"
8
+ type =" com.stop.ui.route.RouteViewModel" />
9
+ </data >
7
10
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 >
You can’t perform that action at this time.
0 commit comments