Skip to content

Commit

Permalink
Implement Save Trip functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ksharma-xyz committed Oct 19, 2024
1 parent 2b3c038 commit 59a10a5
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import xyz.ksharma.krail.trip_planner.ui.state.TransportModeLine

data class TimeTableState(
val isLoading: Boolean = false,
val isTripSaved: Boolean = false,
val journeyList: ImmutableList<JourneyCardInfo> = persistentListOf(),
) {
data class JourneyCardInfo(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
package xyz.ksharma.krail.trip_planner.ui.state.timetable

import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json

@Serializable
data class Trip(
val fromStopId: String,
val fromStopName: String,
val toStopId: String,
val toStopName: String,
)
) {
fun toJsonString() = Json.encodeToString(serializer(), this)

companion object {
fun fromJsonString(json: String) =
kotlin.runCatching { Json.decodeFromString(serializer(), json) }.getOrNull()
}
}
1 change: 1 addition & 0 deletions feature/trip-planner/ui/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ dependencies {
implementation(projects.core.designSystem)
implementation(projects.feature.tripPlanner.network.api)
implementation(projects.feature.tripPlanner.state)
implementation(projects.sandook.api)

implementation(libs.compose.foundation)
implementation(libs.compose.navigation)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.shrinkOut
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
Expand Down Expand Up @@ -50,6 +52,16 @@ fun TimeTableScreen(
Text("Loading...", modifier = Modifier.padding(horizontal = 16.dp))
}
} else if (timeTableState.journeyList.isNotEmpty()) {
item {
Text(
text = if (timeTableState.isTripSaved) "Trip Saved" else "Save Trip Button",
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 16.dp, vertical = 16.dp)
.clickable(enabled = !timeTableState.isTripSaved) { onEvent(TimeTableUiEvent.SaveTripButtonClicked) }
)
}

items(timeTableState.journeyList) { journey ->

AnimatedVisibility(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import kotlinx.coroutines.launch
import timber.log.Timber
import xyz.ksharma.krail.core.date_time.DateTimeHelper.calculateTimeDifferenceFromNow
import xyz.ksharma.krail.core.date_time.DateTimeHelper.toGenericFormattedTimeString
import xyz.ksharma.krail.sandook.Sandook
import xyz.ksharma.krail.trip_planner.network.api.repository.TripPlanningRepository
import xyz.ksharma.krail.trip_planner.ui.state.timetable.TimeTableState
import xyz.ksharma.krail.trip_planner.ui.state.timetable.TimeTableUiEvent
Expand All @@ -28,6 +29,7 @@ import kotlin.time.Duration.Companion.seconds
@HiltViewModel
class TimeTableViewModel @Inject constructor(
private val tripRepository: TripPlanningRepository,
private val sandook: Sandook,
) : ViewModel() {

private val _uiState: MutableStateFlow<TimeTableState> = MutableStateFlow(TimeTableState())
Expand All @@ -52,7 +54,7 @@ class TimeTableViewModel @Inject constructor(
private val _expandedJourneyId: MutableStateFlow<String?> = MutableStateFlow(null)
val expandedJourneyId: StateFlow<String?> = _expandedJourneyId

var trip: Trip? = null
private var tripInfo: Trip? = null

fun onEvent(event: TimeTableUiEvent) {
when (event) {
Expand All @@ -64,11 +66,13 @@ class TimeTableViewModel @Inject constructor(

private fun onSaveTripButtonClicked() {
Timber.d("Save Trip Button Clicked")
// TODO: Implement Save Trip Functionality
trip?.let {
Timber.d("Save Trip: $it")
// TODO save trip to cache
// TODO update ui with confirmation / error
tripInfo?.let { trip ->
Timber.d("Save Trip: $trip")
sandook.putString(key = trip.fromStopId + trip.toStopId, trip.toJsonString())
sandook.getString(key = trip.fromStopId + trip.toStopId)?.let { savedTrip ->
Timber.d("Saved Trip (Pref): ${Trip.fromJsonString(savedTrip)}")
}
updateUiState { copy(isTripSaved = true) }
}
}

Expand All @@ -79,7 +83,7 @@ class TimeTableViewModel @Inject constructor(

private fun onLoadTimeTable(tripInfo: Trip) = with(tripInfo) {
Timber.d("loadTimeTable API Call- fromStopItem: ${fromStopId}, toStopItem: ${toStopId}")
trip = this
this@TimeTableViewModel.tripInfo = this
updateUiState { copy(isLoading = true) }

viewModelScope.launch {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package xyz.ksharma.krail.sandook

import kotlin.reflect.KClass

interface Sandook {

fun clear()
Expand Down

0 comments on commit 59a10a5

Please sign in to comment.