Skip to content

Commit

Permalink
Add Ktlint
Browse files Browse the repository at this point in the history
  • Loading branch information
jdamcd committed Dec 11, 2023
1 parent a6b685a commit 9e35588
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 61 deletions.
5 changes: 5 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
root = true

[*.kt]
ij_kotlin_allow_trailing_comma = false
ij_kotlin_allow_trailing_comma_on_call_site = false
23 changes: 18 additions & 5 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
plugins {
kotlin("multiplatform").version("1.9.20").apply(false)
kotlin("multiplatform").version("1.9.21").apply(false)
id("com.diffplug.spotless") version "6.23.3"
}

buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.jetbrains.kotlin:kotlin-serialization:1.9.20")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.20")
classpath("org.jetbrains.kotlin:kotlin-serialization:1.9.21")
classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.9.21")
classpath("com.codingfeline.buildkonfig:buildkonfig-gradle-plugin:0.15.0")
}
}

tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
allprojects {
repositories {
mavenCentral()
gradlePluginPortal()
}

apply(plugin = "com.diffplug.spotless")

spotless {
kotlin {
target("**/*.kt")
ktlint("1.0.1")
}
}
}
4 changes: 2 additions & 2 deletions macOS/Arrivals.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 0.2;
MARKETING_VERSION = 0.3;
OTHER_LDFLAGS = (
"$(inherited)",
"-framework",
Expand Down Expand Up @@ -505,7 +505,7 @@
"$(inherited)",
"@executable_path/../Frameworks",
);
MARKETING_VERSION = 0.2;
MARKETING_VERSION = 0.3;
OTHER_LDFLAGS = (
"$(inherited)",
"-framework",
Expand Down
4 changes: 2 additions & 2 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ kotlin {
}

sourceSets {
val ktorVersion = "2.2.1"
val ktorVersion = "2.3.7"

commonMain.dependencies {
implementation("io.ktor:ktor-client-core:${ktorVersion}")
implementation("io.ktor:ktor-client-content-negotiation:${ktorVersion}")
implementation("io.ktor:ktor-serialization-kotlinx-json:${ktorVersion}")
implementation("io.ktor:ktor-client-logging:${ktorVersion}")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4-native-mt")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.7.3")
}

commonTest.dependencies {
Expand Down
75 changes: 45 additions & 30 deletions shared/src/commonMain/kotlin/com/jdamcd/tflarrivals/Arrivals.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import kotlin.coroutines.cancellation.CancellationException
import kotlin.math.roundToInt

class Arrivals {

private val api = TflApi()
private val settings = Settings()

Expand All @@ -14,7 +13,9 @@ class Arrivals {
val model = formatArrivals(api.fetchArrivals(settings.selectedStopId))
if (model.arrivals.isNotEmpty()) {
return model
} else throw NoDataException("No arrivals found")
} else {
throw NoDataException("No arrivals found")
}
} catch (e: Exception) {
throw NoDataException(e.message.orEmpty())
}
Expand All @@ -28,31 +29,42 @@ class Arrivals {

suspend fun stopDetails(id: String): StopDetails {
val stopPoint = api.stopDetails(id)
return StopDetails(stopPoint.naptanId, stopPoint.commonName,
return StopDetails(
stopPoint.naptanId,
stopPoint.commonName,
stopPoint.children
.filter { it.stopType == "NaptanMetroStation" || it.stopType == "NaptanRailStation" }
.map { StopResult(it.naptanId, it.commonName) }
)
}

private fun formatArrivals(apiArrivals: List<ApiArrival>): ArrivalsInfo {
val arrivals = apiArrivals
.sortedBy { it.timeToStation }
.filter {
if (settings.platformFilter.isEmpty()) true
else it.platformName.contains(settings.platformFilter, ignoreCase = true)
}
.filter { arrival ->
if (settings.directionFilter == SettingsConfig.DIRECTION_FILTER_DEFAULT) true
else arrival.direction.contains(settings.directionFilter) }
.take(3)
.map {
Arrival(
it.hashCode(), // DLR arrivals all have the same ID?!
formatStation(it.destinationName),
formatTime(it.timeToStation)
)
}
val arrivals =
apiArrivals
.sortedBy { it.timeToStation }
.filter {
if (settings.platformFilter.isEmpty()) {
true
} else {
it.platformName.contains(settings.platformFilter, ignoreCase = true)
}
}
.filter { arrival ->
if (settings.directionFilter == SettingsConfig.DIRECTION_FILTER_DEFAULT) {
true
} else {
arrival.direction.contains(settings.directionFilter)
}
}
.take(3)
.map {
Arrival(
// DLR arrivals all have the same ID, so use hash
it.hashCode(),
formatStation(it.destinationName),
formatTime(it.timeToStation)
)
}
return ArrivalsInfo(
station = stationInfo(),
arrivals = arrivals
Expand Down Expand Up @@ -95,17 +107,20 @@ data class StopDetails(
val children: List<StopResult>
)

class NoDataException(message: String): Throwable(message = message)
class NoDataException(message: String) : Throwable(message = message)

private fun formatTime(seconds: Int) =
if (seconds < 60) "Due"
else "${(seconds / 60f).roundToInt()} min"
if (seconds < 60) {
"Due"
} else {
"${(seconds / 60f).roundToInt()} min"
}

private fun formatStation(name: String) = name
.replace("Rail Station", "")
.replace("Underground Station", "")
.replace("DLR Station", "")
.trim()
private fun formatStation(name: String) =
name
.replace("Rail Station", "")
.replace("Underground Station", "")
.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() }
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ object SettingsConfig {
const val PLATFORM_FILTER_DEFAULT = "Platform 2"
const val DIRECTION_FILTER = "direction_filter"
const val DIRECTION_FILTER_DEFAULT = "all"
}
}
32 changes: 16 additions & 16 deletions shared/src/commonMain/kotlin/com/jdamcd/tflarrivals/TflApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ import kotlinx.serialization.Serializable
import kotlinx.serialization.json.Json

internal class TflApi {

private val client = HttpClient {
install(HttpTimeout) {
requestTimeoutMillis = 10_000 // 10 seconds
}
install(ContentNegotiation) {
json(
Json {
isLenient = true
ignoreUnknownKeys = true
private val client =
HttpClient {
install(HttpTimeout) {
requestTimeoutMillis = 10_000 // 10 seconds
}
install(ContentNegotiation) {
json(
Json {
isLenient = true
ignoreUnknownKeys = true
}
)
}
if (DEBUG) {
install(Logging) {
level = LogLevel.ALL
}
)
}
if (DEBUG) {
install(Logging) {
level = LogLevel.ALL
}
}
}

suspend fun fetchArrivals(station: String): List<ApiArrival> {
return client.get("$BASE_URL/StopPoint/$station/Arrivals") {
Expand Down
17 changes: 12 additions & 5 deletions shared/src/macOSMain/kotlin/com/jdamcd/tflarrivals/Settings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,29 @@ import com.jdamcd.tflarrivals.SettingsConfig.STORE_NAME
import platform.Foundation.NSUserDefaults

actual class Settings actual constructor() {

private val defaults: NSUserDefaults = NSUserDefaults(suiteName = STORE_NAME)

actual var selectedStopName: String
get() = defaults.stringForKey(SELECTED_STOP_NAME) ?: SELECTED_STOP_NAME_DEFAULT
set(value) { defaults.setObject(value, SELECTED_STOP_NAME) }
set(value) {
defaults.setObject(value, SELECTED_STOP_NAME)
}

actual var selectedStopId: String
get() = defaults.stringForKey(SELECTED_STOP_ID) ?: SELECTED_STOP_ID_DEFAULT
set(value) { defaults.setObject(value, SELECTED_STOP_ID) }
set(value) {
defaults.setObject(value, SELECTED_STOP_ID)
}

actual var platformFilter: String
get() = defaults.stringForKey(PLATFORM_FILTER) ?: PLATFORM_FILTER_DEFAULT
set(value) { defaults.setObject(value, PLATFORM_FILTER) }
set(value) {
defaults.setObject(value, PLATFORM_FILTER)
}

actual var directionFilter: String
get() = defaults.stringForKey(DIRECTION_FILTER) ?: DIRECTION_FILTER_DEFAULT
set(value) { defaults.setObject(value, DIRECTION_FILTER) }
set(value) {
defaults.setObject(value, DIRECTION_FILTER)
}
}

0 comments on commit 9e35588

Please sign in to comment.