1+ package ch.opentransportdata.presentation.tir.filter
2+
3+ import androidx.compose.foundation.layout.Arrangement
4+ import androidx.compose.foundation.layout.Column
5+ import androidx.compose.foundation.layout.Row
6+ import androidx.compose.foundation.layout.fillMaxWidth
7+ import androidx.compose.foundation.layout.padding
8+ import androidx.compose.foundation.layout.size
9+ import androidx.compose.foundation.rememberScrollState
10+ import androidx.compose.foundation.text.KeyboardOptions
11+ import androidx.compose.foundation.verticalScroll
12+ import androidx.compose.material3.ListItem
13+ import androidx.compose.material3.MaterialTheme
14+ import androidx.compose.material3.RadioButton
15+ import androidx.compose.material3.Switch
16+ import androidx.compose.material3.Text
17+ import androidx.compose.material3.TextField
18+ import androidx.compose.runtime.Composable
19+ import androidx.compose.runtime.mutableIntStateOf
20+ import androidx.compose.runtime.remember
21+ import androidx.compose.ui.Alignment
22+ import androidx.compose.ui.Modifier
23+ import androidx.compose.ui.text.input.KeyboardType
24+ import androidx.compose.ui.text.style.TextAlign
25+ import androidx.compose.ui.unit.dp
26+ import ch.opentransportdata.domain.VehicleOption
27+
28+ @Composable
29+ fun FilterScreen (
30+ currentWalkingSpeed : Int ,
31+ onSelectWalkingSpeed : (Int ) -> Unit ,
32+ isDirectConnection : Boolean ,
33+ onCheckDirectConnection : () -> Unit ,
34+ isFewerTransfers : Boolean ,
35+ onCheckFewerTransfers : () -> Unit ,
36+ vehicleOptions : List <VehicleOption >,
37+ onToggleVehicle : (String ) -> Unit ,
38+ vehicleSubOptions : List <VehicleOption >,
39+ onToggleSubVehicle : (String ) -> Unit ,
40+ minDistance : String ,
41+ onMinDistanceChange : (String ) -> Unit ,
42+ maxDistance : String ,
43+ onMaxDistanceChange : (String ) -> Unit ,
44+ minDuration : String ,
45+ onMinDurationChange : (String ) -> Unit ,
46+ maxDuration : String ,
47+ onMaxDurationChange : (String ) -> Unit ,
48+ isBikeTransport : Boolean ,
49+ onCheckBikeTransport : () -> Unit ,
50+ ) {
51+ val selectedWalkingSpeed = remember { mutableIntStateOf(currentWalkingSpeed) }
52+ val walkingSpeedOptions = listOf (50 , 75 , 100 , 150 , 200 , 400 )
53+
54+ Column (
55+ modifier = Modifier
56+ .verticalScroll(rememberScrollState())
57+ .padding(horizontal = 12 .dp)
58+ ) {
59+ Text (text = " Deviation from average walking speed in percent. (100% == average)" , textAlign = TextAlign .Center )
60+ Row (modifier = Modifier .fillMaxWidth(), horizontalArrangement = Arrangement .SpaceEvenly ) {
61+ walkingSpeedOptions.forEach { walkingSpeed ->
62+ Column (
63+ horizontalAlignment = Alignment .CenterHorizontally
64+ ) {
65+ RadioButton (
66+ modifier = Modifier .size(40 .dp),
67+ selected = selectedWalkingSpeed.intValue == walkingSpeed,
68+ onClick = {
69+ onSelectWalkingSpeed(walkingSpeed)
70+ selectedWalkingSpeed.value = walkingSpeed
71+ },
72+ )
73+ Text (" ${walkingSpeed} %" )
74+ }
75+ }
76+ }
77+ OptionItem (
78+ text = " Only direct connections" ,
79+ isSelected = isDirectConnection,
80+ onClick = {
81+ onCheckDirectConnection()
82+ }
83+ )
84+ OptionItem (
85+ text = " Fewer transfers" ,
86+ enabled = ! isDirectConnection,
87+ isSelected = isFewerTransfers && ! isDirectConnection,
88+ onClick = {
89+ onCheckFewerTransfers()
90+ },
91+ )
92+ OptionItem (
93+ text = " Bike Transport" ,
94+ isSelected = isBikeTransport,
95+ onClick = {
96+ onCheckBikeTransport()
97+ }
98+ )
99+ Text (" Select your travel modes" )
100+ vehicleOptions.forEach { option ->
101+ OptionItem (
102+ text = option.vehicleType,
103+ isSelected = option.isSelected,
104+ onClick = { onToggleVehicle(option.vehicleType) }
105+ )
106+ }
107+ Text (" Select your rail sub mode" )
108+ vehicleSubOptions.forEach { option ->
109+ OptionItem (
110+ text = option.vehicleType,
111+ isSelected = if (vehicleOptions.first().isSelected) option.isSelected else false ,
112+ enabled = vehicleOptions.first().isSelected,
113+ onClick = { onToggleSubVehicle(option.vehicleType) }
114+ )
115+ }
116+ Text (" Select your distance" )
117+ Column {
118+ DistanceItem (
119+ text = " Min Distance (meter)" ,
120+ distance = minDistance,
121+ onValueChange = { onMinDistanceChange(it) }
122+ )
123+ DistanceItem (
124+ text = " Max Distance (meter)" ,
125+ distance = maxDistance,
126+ onValueChange = { onMaxDistanceChange(it) }
127+ )
128+ }
129+ Text (" Select your duration" )
130+ Column {
131+ DistanceItem (
132+ text = " Min Duration (min)" ,
133+ distance = minDuration,
134+ onValueChange = { onMinDurationChange(it) }
135+ )
136+ DistanceItem (
137+ text = " Max Duration (min)" ,
138+ distance = maxDuration,
139+ onValueChange = { onMaxDurationChange(it) }
140+ )
141+ }
142+ }
143+ }
144+
145+ @Composable
146+ private fun OptionItem (
147+ text : String ,
148+ isSelected : Boolean ,
149+ enabled : Boolean = true,
150+ onClick : () -> Unit ,
151+ ) {
152+ ListItem (
153+ headlineContent = {
154+ Text (
155+ text = text,
156+ style = MaterialTheme .typography.titleSmall,
157+ color = MaterialTheme .colorScheme.onSurface
158+ )
159+ },
160+ trailingContent = {
161+ Switch (
162+ checked = isSelected,
163+ onCheckedChange = { onClick() },
164+ enabled = enabled
165+ )
166+ }
167+ )
168+ }
169+
170+ @Composable
171+ private fun DistanceItem (
172+ text : String ,
173+ distance : String ,
174+ onValueChange : (String ) -> Unit ,
175+ ) {
176+ ListItem (
177+ headlineContent = {
178+ Text (
179+ text = text,
180+ style = MaterialTheme .typography.titleSmall,
181+ color = MaterialTheme .colorScheme.onSurface
182+ )
183+ },
184+ trailingContent = {
185+ TextField (
186+ modifier = Modifier .fillMaxWidth(0.5f ),
187+ value = distance,
188+ onValueChange = { onValueChange(it) },
189+ keyboardOptions = KeyboardOptions .Default .copy(keyboardType = KeyboardType .Number )
190+ )
191+ }
192+ )
193+ }
0 commit comments