-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UI: Refactor DateTime formatting and selection handling (#403)
### TL;DR Refactored date-time handling and selection functionality to improve code organization and reusability. ### What changed? - Renamed date/time formatting functions to be more descriptive (`formatDate` -> `toReadableDate`, `formatTime` -> `to12HourTimeString`) - Moved `DateTimeSelectionItem` and `JourneyTimeOptions` to the state module - Added documentation to time formatting functions - Added date-time selection handling to TimeTableViewModel - Added comma separator in date format for better readability ### How to test? 1. Open the date-time selector screen 2. Verify date formats display correctly (Today, Tomorrow, Mon, 1 Jan) 3. Verify time formats display in 12-hour format (1:30 PM) 4. Test journey time options (Leave/Arrive) work correctly 5. Confirm date-time selection updates properly in the time table view ### Why make this change? - Improves code organization by moving date-time related logic to appropriate modules - Makes the code more maintainable with better-named functions and documentation - Enhances readability of displayed dates with consistent formatting - Centralizes date-time selection handling in the ViewModel for better state management
- Loading branch information
1 parent
1b1ef36
commit ad9ff93
Showing
10 changed files
with
103 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
.../kotlin/xyz/ksharma/krail/trip/planner/ui/state/datetimeselector/DateTimeSelectionItem.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package xyz.ksharma.krail.trip.planner.ui.state.datetimeselector | ||
|
||
import kotlinx.datetime.LocalDate | ||
import kotlinx.serialization.Serializable | ||
import kotlinx.serialization.json.Json | ||
import xyz.ksharma.krail.core.datetime.toReadableDate | ||
import xyz.ksharma.krail.core.datetime.to12HourTimeString | ||
|
||
@Serializable | ||
data class DateTimeSelectionItem( | ||
val option: JourneyTimeOptions, | ||
val hour: Int, | ||
val minute: Int, | ||
val date: LocalDate, | ||
) { | ||
fun toDateTimeText(): String = when (option) { | ||
JourneyTimeOptions.LEAVE -> { | ||
"Leave: ${toReadableDate(date)} ${to12HourTimeString(hour, minute)}" | ||
} | ||
|
||
JourneyTimeOptions.ARRIVE -> { | ||
"Arrive: ${toReadableDate(date)} ${to12HourTimeString(hour, minute)}" | ||
} | ||
} | ||
|
||
fun toJsonString() = Json.encodeToString(serializer(), this) | ||
|
||
fun toHHMM(): String { | ||
val hh = hour.toString().padStart(2, '0') | ||
val mm = minute.toString().padStart(2, '0') | ||
return "$hh$mm" | ||
} | ||
|
||
fun toYYYYMMDD(): String { | ||
val yyyy = date.year.toString() | ||
val mm = date.monthNumber.toString().padStart(2, '0') | ||
val dd = date.dayOfMonth.toString().padStart(2, '0') | ||
return "$yyyy$mm$dd" | ||
} | ||
|
||
@Suppress("ConstPropertyName") | ||
companion object { | ||
private const val serialVersionUID: Long = 1L | ||
|
||
fun fromJsonString(json: String) = | ||
kotlin.runCatching { Json.decodeFromString(serializer(), json) }.getOrNull() | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...ain/kotlin/xyz/ksharma/krail/trip/planner/ui/state/datetimeselector/JourneyTimeOptions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package xyz.ksharma.krail.trip.planner.ui.state.datetimeselector | ||
|
||
enum class JourneyTimeOptions(val text: String) { | ||
LEAVE("Leave"), | ||
ARRIVE("Arrive") | ||
} |
5 changes: 5 additions & 0 deletions
5
...c/commonMain/kotlin/xyz/ksharma/krail/trip/planner/ui/state/timetable/TimeTableUiEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
package xyz.ksharma.krail.trip.planner.ui.state.timetable | ||
|
||
import xyz.ksharma.krail.trip.planner.ui.state.datetimeselector.DateTimeSelectionItem | ||
|
||
sealed interface TimeTableUiEvent { | ||
data object SaveTripButtonClicked : TimeTableUiEvent | ||
data class LoadTimeTable(val trip: Trip) : TimeTableUiEvent | ||
data class JourneyCardClicked(val journeyId: String) : TimeTableUiEvent | ||
data class DateTimeSelectionChanged(val dateTimeSelectionItem: DateTimeSelectionItem?) : | ||
TimeTableUiEvent | ||
|
||
data object ReverseTripButtonClicked : TimeTableUiEvent | ||
data object RetryButtonClicked : TimeTableUiEvent | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters