Skip to content

Commit 288e877

Browse files
committed
Load desktop config file
1 parent f201c4a commit 288e877

File tree

6 files changed

+37
-5
lines changed

6 files changed

+37
-5
lines changed

desktop/build.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dependencies {
1313
implementation(libs.compose.viewmodel)
1414
implementation(libs.coroutines.swing)
1515
implementation(libs.koin)
16+
implementation(libs.snakeyaml)
1617
}
1718

1819
compose.desktop {

desktop/src/main/kotlin/com/jdamcd/arrivals/desktop/Main.kt

+29
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ import androidx.compose.ui.window.WindowPosition
1212
import androidx.compose.ui.window.application
1313
import androidx.compose.ui.window.rememberWindowState
1414
import com.jdamcd.arrivals.Arrivals
15+
import com.jdamcd.arrivals.Settings
16+
import com.jdamcd.arrivals.SettingsConfig
1517
import com.jdamcd.arrivals.initKoin
18+
import org.yaml.snakeyaml.Yaml
19+
import java.io.File
20+
import java.io.FileInputStream
1621
import kotlin.system.exitProcess
1722

1823
private val koin = initKoin().koin
@@ -27,6 +32,8 @@ fun main(args: Array<String>) = application {
2732
size = DpSize(width.dp, height.dp)
2833
)
2934

35+
loadConfig(koin.get<Settings>())
36+
3037
val viewModel = ArrivalsViewModel(koin.get<Arrivals>())
3138
val state: ArrivalsState by viewModel.uiState.collectAsState(ArrivalsState.Loading)
3239

@@ -48,3 +55,25 @@ fun main(args: Array<String>) = application {
4855
}
4956

5057
private fun dimenFromArg(args: Array<String>, index: Int): Int? = if (args.size > index) args[index].toIntOrNull() else null
58+
59+
fun loadConfig(settings: Settings) {
60+
val path = "${System.getProperty("user.home")}/.arrivals.yml"
61+
val configFile = File(path)
62+
63+
if (configFile.exists()) {
64+
try {
65+
FileInputStream(configFile).use { inputStream ->
66+
val data: Map<String, String> = Yaml().load(inputStream)
67+
data[SettingsConfig.MODE]?.let { settings.mode = it }
68+
data[SettingsConfig.GTFS_REALTIME]?.let { settings.gtfsRealtime = it }
69+
data[SettingsConfig.GTFS_SCHEDULE]?.let { settings.gtfsSchedule = it }
70+
data[SettingsConfig.GTFS_STOP]?.let { settings.gtfsStop = it }
71+
data[SettingsConfig.TFL_STOP]?.let { settings.tflStopId = it }
72+
data[SettingsConfig.TFL_PLATFORM]?.let { settings.tflPlatform = it }
73+
data[SettingsConfig.TFL_DIRECTION]?.let { settings.tflDirection = it }
74+
}
75+
} catch (e: Exception) {
76+
println("Error reading config file: ${e.message}")
77+
}
78+
}
79+
}

gradle/libs.versions.toml

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ logging = "2.0.16"
1515
clikt = "5.0.2"
1616
composeMP = "1.7.3"
1717
viewModel = "2.8.2"
18+
snakeYaml = "2.3"
1819

1920
[libraries]
2021
ktor-client-core = { group = "io.ktor", name = "ktor-client-core", version.ref = "ktor" }
@@ -32,6 +33,7 @@ logging-nop = { group = "org.slf4j", name = "slf4j-nop", version.ref = "logging"
3233
clikt = { group = "com.github.ajalt.clikt", name = "clikt", version.ref = "clikt" }
3334
compose-viewmodel = { group = "org.jetbrains.androidx.lifecycle", name = "lifecycle-viewmodel-compose", version.ref = "viewModel" }
3435
coroutines-swing = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-swing", version.ref = "coroutines" }
36+
snakeyaml = { group = "org.yaml", name = "snakeyaml", version.ref = "snakeYaml" }
3537

3638
coroutines-test = { group = "org.jetbrains.kotlinx", name = "kotlinx-coroutines-test", version.ref = "coroutines" }
3739
mockk = { group = "io.mockk", name = "mockk", version.ref = "mockk" }

shared/src/commonMain/kotlin/com/jdamcd/arrivals/Settings.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ object SettingsConfig {
1818
const val MODE_TFL = "tfl"
1919
const val MODE_GTFS = "gtfs"
2020

21-
const val TFL_STOP_ID = "selected_stop_id"
22-
const val TFL_STOP_ID_DEFAULT = "910GSHRDHST"
21+
const val TFL_STOP = "tfl_stop"
22+
const val TFL_STOP_DEFAULT = "910GSHRDHST"
2323
const val TFL_PLATFORM = "tfl_platform"
2424
const val TFL_PLATFORM_DEFAULT = "Platform 2"
2525
const val TFL_DIRECTION = "tfl_direction"

shared/src/jvmMain/kotlin/com/jdamcd/arrivals/Settings.jvm.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.jdamcd.arrivals
22

33
actual class Settings actual constructor() {
44
actual var mode = SettingsConfig.MODE_GTFS
5-
actual var tflStopId = SettingsConfig.TFL_STOP_ID_DEFAULT
5+
actual var tflStopId = SettingsConfig.TFL_STOP_DEFAULT
66
actual var tflPlatform = SettingsConfig.TFL_PLATFORM_DEFAULT
77
actual var tflDirection = SettingsConfig.TFL_DIRECTION_DEFAULT
88
actual var gtfsStopsUpdated = 0L

shared/src/macosMain/kotlin/com/jdamcd/arrivals/Settings.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ actual class Settings actual constructor() {
1212
}
1313

1414
actual var tflStopId: String
15-
get() = defaults.stringForKey(SettingsConfig.TFL_STOP_ID) ?: SettingsConfig.TFL_STOP_ID_DEFAULT
15+
get() = defaults.stringForKey(SettingsConfig.TFL_STOP) ?: SettingsConfig.TFL_STOP_DEFAULT
1616
set(value) {
17-
defaults.setObject(value, SettingsConfig.TFL_STOP_ID)
17+
defaults.setObject(value, SettingsConfig.TFL_STOP)
1818
}
1919

2020
actual var tflPlatform: String

0 commit comments

Comments
 (0)