Skip to content

Commit

Permalink
Define arrivals interface
Browse files Browse the repository at this point in the history
  • Loading branch information
jdamcd committed Dec 16, 2023
1 parent 20a6ace commit 1afecf0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
4 changes: 2 additions & 2 deletions macOS/Arrivals/ArrivalsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ class ArrivalsViewModel: ObservableObject {
@Published var state: ArrivalsState = .idle
@Published var loading = false

private let fetcher = Arrivals()
private let fetcher = ArrivalsBuilder().tflArrivals()

func load() {
if !loading {
loading = true
Task {
do {
let result = try await fetcher.fetchArrivals()
let result = try await fetcher.latest()
state = .data(result)
} catch {
state = .error
Expand Down
2 changes: 1 addition & 1 deletion macOS/Arrivals/SettingsViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import TflArrivals
class SettingsViewModel: ObservableObject {
@Published var state: SettingsState = .idle

private let fetcher = Arrivals()
private let fetcher = ArrivalsBuilder().tflArrivals()
private let settings = Settings()

func performSearch(_ query: String) {
Expand Down
8 changes: 4 additions & 4 deletions shared/src/commonMain/kotlin/com/jdamcd/tflarrivals/TflApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ internal class TflApi {
}

@Serializable
data class ApiArrival(
internal data class ApiArrival(
val id: Int,
val stationName: String,
val platformName: String,
Expand All @@ -66,18 +66,18 @@ data class ApiArrival(
)

@Serializable
data class ApiSearchResult(
internal data class ApiSearchResult(
val matches: List<ApiMatchedStop>
)

@Serializable
data class ApiMatchedStop(
internal data class ApiMatchedStop(
val id: String,
val name: String
)

@Serializable
data class ApiStopPoint(
internal data class ApiStopPoint(
val commonName: String,
val naptanId: String,
val stopType: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,27 @@ package com.jdamcd.tflarrivals
import kotlin.coroutines.cancellation.CancellationException
import kotlin.math.roundToInt

class Arrivals {
private val api = TflApi()
private val settings = Settings()
object ArrivalsBuilder {
fun tflArrivals(): Arrivals = TflArrivals(TflApi(), Settings())
}

interface Arrivals {
@Throws(NoDataException::class, CancellationException::class)
suspend fun latest(): ArrivalsInfo

@Throws(CancellationException::class)
suspend fun searchStops(query: String): List<StopResult>

@Throws(CancellationException::class)
suspend fun stopDetails(id: String): StopDetails
}

internal class TflArrivals(
private val api: TflApi,
private val settings: Settings
) : Arrivals {
@Throws(NoDataException::class, CancellationException::class)
suspend fun fetchArrivals(): ArrivalsInfo {
override suspend fun latest(): ArrivalsInfo {
try {
val model = formatArrivals(api.fetchArrivals(settings.selectedStopId))
if (model.arrivals.isNotEmpty()) {
Expand All @@ -22,12 +37,13 @@ class Arrivals {
}

@Throws(CancellationException::class)
suspend fun searchStops(query: String): List<StopResult> {
override suspend fun searchStops(query: String): List<StopResult> {
return api.searchStations(query).matches
.map { StopResult(it.id, it.name) }
}

suspend fun stopDetails(id: String): StopDetails {
@Throws(CancellationException::class)
override suspend fun stopDetails(id: String): StopDetails {
val stopPoint = api.stopDetails(id)
return StopDetails(
stopPoint.naptanId,
Expand All @@ -43,18 +59,12 @@ class Arrivals {
apiArrivals
.sortedBy { it.timeToStation }
.filter {
if (settings.platformFilter.isEmpty()) {
true
} else {
settings.platformFilter.isEmpty() ||
it.platformName.contains(settings.platformFilter, ignoreCase = true)
}
}
.filter { arrival ->
if (settings.directionFilter == SettingsConfig.DIRECTION_FILTER_DEFAULT) {
true
} else {
settings.directionFilter == SettingsConfig.DIRECTION_FILTER_DEFAULT ||
arrival.direction.contains(settings.directionFilter)
}
}
.take(3)
.map {
Expand Down Expand Up @@ -123,4 +133,7 @@ private fun formatStation(name: String) =
.replace("DLR Station", "")
.trim()

private fun formatDirection(direction: String) = direction.replaceFirstChar { if (it.isLowerCase()) it.titlecase() else it.toString() }
private fun formatDirection(direction: String) =
direction.replaceFirstChar {
if (it.isLowerCase()) it.titlecase() else it.toString()
}

0 comments on commit 1afecf0

Please sign in to comment.