Skip to content

Commit

Permalink
👽 Migrate to Träwelling station IDs (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
jheubuch authored May 22, 2024
1 parent cdd34ea commit ccff669
Show file tree
Hide file tree
Showing 16 changed files with 87 additions and 138 deletions.
16 changes: 8 additions & 8 deletions app/src/main/kotlin/de/hbch/traewelling/api/ApiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ interface AuthService {
@GET("auth/user")
fun getLoggedInUser(): Call<Data<User>>

@PUT("trains/station/{stationName}/home")
fun setUserHomelandStation(
@Path("stationName") stationName: String
): Call<Data<Station>>
@PUT("station/{id}/home")
suspend fun setUserHomelandStation(
@Path("id") stationId: Int
): Data<Station>

@GET("trains/station/history")
fun getLastVisitedStations(): Call<Data<List<Station>>>
Expand Down Expand Up @@ -189,12 +189,12 @@ interface TravelService {
@Query("longitude") longitude: Double
): Data<Station>

@GET("trains/station/{station}/departures")
fun getDeparturesAtStation(
@Path("station", encoded = false) station: String,
@GET("station/{id}/departures")
suspend fun getDeparturesAtStation(
@Path("id") stationId: Int,
@Query("when") time: ZonedDateTime,
@Query("travelType") filter: String
): Call<HafasTripPage>
): HafasTripPage

@GET("trains/station/autocomplete/{station}")
suspend fun autoCompleteStationSearch(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ package de.hbch.traewelling.api.models.station

import com.google.gson.annotations.SerializedName

data class StationData(
@SerializedName("data") val data: Station
)

data class Station(
@SerializedName("id") val id: Int,
@SerializedName("name") val name: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ object SearchConnection : ArgumentDestination, DeepLinkedDestination {
override val route = "search-connection/?station={station}&date={date}"
override val arguments = listOf(
navArgument("station") {
type = NavType.StringType
type = NavType.IntType
},
navArgument("date") {
type = NavType.LongType
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/de/hbch/traewelling/navigation/NavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fun TraewelldroidNavHost(
val context = LocalContext.current
val secureStorage = SecureStorage(context)

val navToSearchConnections: (String, ZonedDateTime?) -> Unit = { station, date ->
val navToSearchConnections: (Int, ZonedDateTime?) -> Unit = { station, date ->
val formattedDate =
if (date == null)
""
Expand Down Expand Up @@ -366,7 +366,7 @@ fun TraewelldroidNavHost(

SearchConnection(
loggedInUserViewModel = loggedInUserViewModel,
station = it.arguments?.getString("station") ?: "",
station = it.arguments?.getString("station")?.toIntOrNull() ?: 5167,
currentSearchDate = zonedDateTime,
checkInViewModel = checkInViewModel,
onTripSelected = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import de.hbch.traewelling.api.models.station.Station
import de.hbch.traewelling.api.models.status.Status
import de.hbch.traewelling.shared.LoggedInUserViewModel
import de.hbch.traewelling.ui.composables.NotificationsAvailableHint
Expand All @@ -32,7 +33,7 @@ import java.time.ZonedDateTime
@Composable
fun Dashboard(
loggedInUserViewModel: LoggedInUserViewModel,
searchConnectionsAction: (String, ZonedDateTime?) -> Unit = { _, _ -> },
searchConnectionsAction: (Int, ZonedDateTime?) -> Unit = { _, _ -> },
userSelectedAction: (String) -> Unit = { },
statusSelectedAction: (Int) -> Unit = { },
statusDeletedAction: () -> Unit = { },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fun CardSearch(
modifier: Modifier = Modifier,
homelandStationData: LiveData<Station?>,
recentStationsData: LiveData<List<Station>?>,
onStationSelected: (String) -> Unit = { },
onStationSelected: (Int) -> Unit = { },
onUserSelected: (User) -> Unit = { },
queryStations: Boolean = true,
queryUsers: Boolean = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.constraintlayout.compose.Dimension
import de.hbch.traewelling.R
import de.hbch.traewelling.api.models.station.Station
import de.hbch.traewelling.api.models.status.Status
import de.hbch.traewelling.api.models.status.StatusBusiness
import de.hbch.traewelling.api.models.trip.HafasTrainTripStation
import de.hbch.traewelling.api.models.trip.ProductType
import de.hbch.traewelling.shared.LoggedInUserViewModel
import de.hbch.traewelling.theme.AppTypography
Expand All @@ -73,7 +75,7 @@ fun CheckInCard(
status: Status?,
loggedInUserViewModel: LoggedInUserViewModel? = null,
displayLongDate: Boolean = false,
stationSelected: (String, ZonedDateTime?) -> Unit = { _, _ -> },
stationSelected: (Int, ZonedDateTime?) -> Unit = { _, _ -> },
userSelected: (String) -> Unit = { },
statusSelected: (Int) -> Unit = { },
handleEditClicked: (Status) -> Unit = { },
Expand Down Expand Up @@ -150,7 +152,7 @@ fun CheckInCard(
top.linkTo(parent.top)
width = Dimension.fillToConstraints
},
stationName = status.journey.origin.name,
station = status.journey.origin,
timePlanned = status.journey.origin.departurePlanned,
timeReal = status.journey.departureManual ?: status.journey.origin.departureReal,
stationSelected = stationSelected
Expand All @@ -165,7 +167,7 @@ fun CheckInCard(
bottom.linkTo(parent.bottom)
width = Dimension.fillToConstraints
},
stationName = status.journey.destination.name,
station = status.journey.destination,
timePlanned = status.journey.destination.arrivalPlanned,
timeReal = status.journey.arrivalManual ?: status.journey.destination.arrivalReal,
verticalAlignment = Alignment.Bottom,
Expand Down Expand Up @@ -256,11 +258,11 @@ private fun calculateProgress(
@Composable
private fun StationRow(
modifier: Modifier = Modifier,
stationName: String,
station: HafasTrainTripStation,
timePlanned: ZonedDateTime,
timeReal: ZonedDateTime?,
verticalAlignment: Alignment.Vertical = Alignment.Top,
stationSelected: (String, ZonedDateTime?) -> Unit = { _, _ -> }
stationSelected: (Int, ZonedDateTime?) -> Unit = { _, _ -> }
) {
val primaryColor = LocalColorScheme.current.primary

Expand All @@ -274,8 +276,8 @@ private fun StationRow(
) {
Text(
modifier = Modifier
.clickable { stationSelected(stationName, null) },
text = stationName,
.clickable { stationSelected(station.id, null) },
text = station.name,
style = AppTypography.titleLarge,
overflow = TextOverflow.Ellipsis,
maxLines = 2,
Expand All @@ -292,7 +294,7 @@ private fun StationRow(
else
timePlanned
Text(
modifier = Modifier.clickable { stationSelected(stationName, displayedDate) },
modifier = Modifier.clickable { stationSelected(station.id, displayedDate) },
text = getLocalTimeString(
date = displayedDate
),
Expand Down
9 changes: 3 additions & 6 deletions app/src/main/kotlin/de/hbch/traewelling/ui/search/Search.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fun Search(
queryUsers: Boolean = true,
homelandStation: Station? = null,
recentStations: List<Station>? = null,
onStationSelected: (String) -> Unit = { },
onStationSelected: (Int) -> Unit = { },
onUserSelected: (User) -> Unit = { }
) {
val searchInstruction =
Expand Down Expand Up @@ -99,7 +99,7 @@ fun Search(

val stationSelected: (Station) -> Unit = {
active = false
onStationSelected(it.name)
onStationSelected(it.id)
}
val userSelected: (User) -> Unit = {
active = false
Expand Down Expand Up @@ -158,10 +158,7 @@ fun Search(
DockedSearchBar(
query = query,
onQueryChange = { query = it },
onSearch = {
active = false
onStationSelected(it)
},
onSearch = { query = it },
active = active,
onActiveChange = { active = it },
modifier = modifier,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SearchViewModel : ViewModel() {
): List<Station>? {
return try {
val stations = TraewellingApi.travelService.autoCompleteStationSearch(query).data
stations.sortedWith(compareBy(nullsLast()) { it.ds100 }).take(5)
stations.sortedWith(compareBy(nullsLast()) { it.ds100 })
} catch (_: Exception) {
null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,15 @@ import androidx.compose.material3.rememberDatePickerState
import androidx.compose.material3.rememberTimePickerState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -44,6 +48,7 @@ import de.hbch.traewelling.R
import de.hbch.traewelling.api.models.station.Station
import de.hbch.traewelling.api.models.trip.HafasLine
import de.hbch.traewelling.api.models.trip.HafasTrip
import de.hbch.traewelling.api.models.trip.HafasTripPage
import de.hbch.traewelling.api.models.trip.ProductType
import de.hbch.traewelling.shared.CheckInViewModel
import de.hbch.traewelling.shared.LoggedInUserViewModel
Expand All @@ -59,6 +64,7 @@ import de.hbch.traewelling.ui.include.cardSearchStation.CardSearch
import de.hbch.traewelling.util.getDelayColor
import de.hbch.traewelling.util.getLastDestination
import de.hbch.traewelling.util.getLocalTimeString
import kotlinx.coroutines.launch
import java.time.Instant
import java.time.ZonedDateTime
import java.time.ZoneId
Expand All @@ -67,37 +73,32 @@ import java.time.ZoneId
fun SearchConnection(
loggedInUserViewModel: LoggedInUserViewModel,
checkInViewModel: CheckInViewModel,
station: String,
station: Int,
currentSearchDate: ZonedDateTime,
onTripSelected: () -> Unit = { },
onHomelandSelected: (Station) -> Unit = { }
) {
val viewModel: SearchConnectionViewModel = viewModel()
var stationName by remember { mutableStateOf(station) }
val coroutineScope = rememberCoroutineScope()

var hafasTripPage by remember { mutableStateOf<HafasTripPage?>(null) }
var stationId by rememberSaveable { mutableIntStateOf(station) }
val stationName by remember { derivedStateOf { hafasTripPage?.meta?.station?.name ?: "" } }
val trips by remember { derivedStateOf { hafasTripPage?.data ?: listOf() } }
val times by remember { derivedStateOf { hafasTripPage?.meta?.times } }

val scrollState = rememberScrollState()
val trips = remember { mutableStateListOf<HafasTrip>() }
val times by viewModel.pageTimes.observeAsState()
var searchDate by remember { mutableStateOf(currentSearchDate) }
var loading by remember { mutableStateOf(false) }
var searchConnections by remember { mutableStateOf(true) }
var selectedFilter by remember { mutableStateOf<FilterType?>(null) }

LaunchedEffect(searchConnections, selectedFilter) {
if (searchConnections) {
loading = true
viewModel.searchConnections(
stationName,
searchDate,
selectedFilter,
{
loading = false
searchConnections = false
trips.clear()
trips.addAll(it.data)
stationName = it.meta.station.name
},
{ }
)
LaunchedEffect(stationId, searchDate, selectedFilter) {
loading = true

coroutineScope.launch {
val tripPage = viewModel.searchConnections(stationId, searchDate, selectedFilter)
loading = false
hafasTripPage = tripPage
}
}

Expand All @@ -109,8 +110,7 @@ fun SearchConnection(
) {
CardSearch(
onStationSelected = { station ->
stationName = station
searchConnections = true
stationId = station
},
homelandStationData = loggedInUserViewModel.home,
recentStationsData = loggedInUserViewModel.lastVisitedStations,
Expand Down Expand Up @@ -138,14 +138,12 @@ fun SearchConnection(
val time = times?.previous
time?.let {
searchDate = it
searchConnections = true
}
},
onNextTime = {
val time = times?.next
time?.let {
searchDate = it
searchConnections = true
}
},
onTripSelection = { trip ->
Expand All @@ -163,22 +161,19 @@ fun SearchConnection(
},
onTimeSelection = {
searchDate = it
searchConnections = true
},
onHomelandStationSelection = {
viewModel.setUserHomelandStation(
station,
{ s ->
coroutineScope.launch {
val s = viewModel.setUserHomelandStation(stationId)
if (s != null) {
loggedInUserViewModel.setHomelandStation(s)
onHomelandSelected(s)
},
{}
)
}
}
},
appliedFilter = selectedFilter,
onFilter = {
selectedFilter = it
searchConnections = true
}
)
}
Expand Down
Loading

0 comments on commit ccff669

Please sign in to comment.