feat: add train type recognition and restructure settings storage

This commit is contained in:
Nedifinita
2025-08-01 20:06:57 +08:00
parent 4e97dcafd7
commit 59e9987d7f
16 changed files with 567 additions and 133 deletions

View File

@@ -13,8 +13,8 @@ android {
applicationId = "org.noxylva.lbjconsole"
minSdk = 29
targetSdk = 35
versionCode = 8
versionName = "0.0.8"
versionCode = 9
versionName = "0.1.0"
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
@@ -90,4 +90,5 @@ dependencies {
implementation(libs.androidx.room.runtime)
implementation(libs.androidx.room.ktx)
ksp(libs.androidx.room.compiler)
implementation(libs.androidx.startup.runtime)
}

View File

@@ -0,0 +1,82 @@
"^[Gg](4000|[1-3]\d{3}|[1-9]\d{0,2})$","直通图定高速动车组"
"^[Gg](400[1-9]|40[1-9]\d|4[1-8]\d{2}|49[0-8]\d|499[0-8])$","直通临客高速动车组"
"^[Gg](9000|[6-8]\d{3}|500[1-9]|50[1-9]\d|5[1-9]\d{2})$","管内图定高速动车组"
"^[Gg](900[1-9]|90[1-9]\d|9[1-8]\d{2}|99[0-8]\d|999[0-8])$","管内临客高速动车组"
"^[Cc]([1-8]\d{3}|9000)$","图定城际动车组"
"^[Cc](900[1-9]|90[1-9]\d|9[1-8]\d{2}|99[0-8]\d|999[0-8])$","临客城际动车组"
"^[Cc][1-9]\d{2}$","动力集中城际动车组"
"^[IDid](4000|[1-3]\d{3}|[1-9]\d{0,2})$","直通图定动车组"
"^[IDid](400[1-9]|40[1-9]\d|4[1-8]\d{2}|49[0-8]\d|499[0-8])$","直通临客动车组"
"^[IDid](9000|[6-8]\d{3}|500[1-9]|50[1-9]\d|5[1-9]\d{2})$","管内图定动车组"
"^[IDid](900[1-9]|90[1-9]\d|9[1-8]\d{2}|99[0-8]\d|999[0-8])$","管内临客动车组"
"^[IDid](8([0-8]\d|9[0-8])|7(0[1-9]|[1-9]\d))$","动力集中动车组"
"^[IDid](300|[12]\d{2}|[1-9]\d?)$","跨局动力集中动车组"
"^[PZpz](4000|[1-3]\d{3}|[1-9]\d{0,2})$","直通图定直达特快旅客列车"
"^[PZpz](400[1-9]|40[1-9]\d|4[1-8]\d{2}|49[0-8]\d|499[0-8])$","直通临客直达特快旅客列车"
"^[PZpz](9000|[6-8]\d{3}|500[1-9]|50[1-9]\d|5[1-9]\d{2})$","管内图定直达特快旅客列车"
"^[PZpz](900[1-9]|90[1-9]\d|9[1-8]\d{2}|99[0-8]\d|999[0-8])$","管内临客直达特快旅客列车"
"^[QTqt](3000|[12]\d{3}|[1-9]\d{0,2})$","直通图定特快旅客列车"
"^[QTqt](300[1-9]|30[1-9]\d|3[1-8]\d{2}|39[0-8]\d|399[0-8])$","直通临客特快旅客列车"
"^[QTqt](4(00[1-9]|0[1-9]\d|[1-8]\d{2}|9[0-8]\d|99[0-8]))$","管内临客特快旅客列车"
"^[QTqt]([5-8]\d{3}|9([0-8]\d{2}|9[0-8]\d|99[0-8])|500[1-9]|50[1-9]\d|5[1-9]\d{2})$","管内图定特快旅客列车"
"^[WKwk](4000|[1-3]\d{3}|[1-9]\d{0,2})$","直通图定快速旅客列车"
"^[WKwk](400[1-9]|40[1-9]\d|4[1-8]\d{2}|49[0-8]\d|499[0-8])$","直通临客快速旅客列车"
"^[WKwk](6([0-8]\d{2}|9[0-8]\d|99[0-8])|500[1-9]|50[1-9]\d|5[1-9]\d{2})$","管内临客快速旅客列车"
"^[WKwk](8\d{3}|9([0-8]\d{2}|9[0-8]\d|99[0-8])|7(00[1-9]|0[1-9]\d|[1-9]\d{2}))$","管内图定快速旅客列车"
"^[Vv1](00[1-9]|0[1-9]\d|[1-9]\d{2})$","跨三局及以上图定普通旅客快车"
"^[Bb2](00[1-9]|0[1-9]\d|[1-9]\d{2})$","跨两局图定普通旅客快车"
"^3(00[1-9]|0[1-9]\d|[1-9]\d{2})$","跨局临时普通旅客快车"
"^[Uu4](00[1-9]|0[1-9]\d|[1-9]\d{2})$","管内图定普通旅客快车四字头"
"^[Xx5]([0-8]\d{2}|9[0-8]\d|99[0-8])$","管内图定普通旅客快车五字头"
"^6(19[0-8]|1[0-8]\d|0[1-9]\d|00[1-9])$","直通普通旅客慢车"
"^(6(20[1-9]|2[1-9]\d|[3-9]\d{2})|7([0-4]\d{2}|5([0-8]\d|9[0-8])))$","管内普通旅客慢车"
"^(8([0-8]\d{2}|9[0-8]\d|99[0-8])|7(60[1-9]|6[1-9]\d|[7-9]\d{2}))$","通勤列车"
"^[Yy](500|[1-4]\d{2}|[1-9]\d?)$","跨局旅游列车"
"^[Yy](50[1-9]|5[1-9]\d|[6-9]\d{2})$","管内旅游列车"
"^[Ss][1-9]\d{0,3}$","市郊旅客列车"
"^[Ll](6([0-8]\d{2}|9[0-8]\d|99[0-8])|[1-5]\d{3}|[1-9]\d{0,2})$","直通临时旅客列车"
"^[Ll]([7-9]\d{3})$","管内临时旅客列车"
"^[Xx](19[0-8]|1[0-8]\d|[1-9]\d?)$","特快货物班列"
"^[Xx](39[0-8]|3[0-8]\d|2[1-9]\d|20[1-9])$","快速货物班列"
"^[Xx]2(40[1-9]|4[1-9]\d|[5-9]\d{2})$","直通货物快运列车"
"^[Xx]([4-9]\d{2}|4[1-9]\d|40[1-9])$","管内货物快运列车"
"^[Xx]8\d{3}$","中欧中亚集装箱班列"
"^[Xx]9([0-4]\d{2}|500)$","中亚集装箱班列"
"^[Xx]9(50[1-9]|5[1-9]\d|[6-9]\d{2})$","水铁联运班列"
"^[Xx][1-4]\d{4}$","加挂零散快运车辆货物列车"
"^1(000[1-9]|00[1-9]\d|0[1-9]\d{2}|[1-9]\d{3})$","技术直达列车"
"^2\d{4}$","直通货物列车"
"^3\d{4}$","区段摘挂列车"
"^4([0-3]\d{3}|4([0-8]\d{2}|9[0-8]\d|99[0-8]))$","摘挂列车"
"^4(500[1-9]|50[1-9]\d|5[1-9]\d{2}|[6-9]\d{3})$","小运转列车"
"^6\d{4}$","自备列车"
"^70\d{3}$","超限货物列车"
"^7([1-6]\d{3}|7([0-8]\d{2}|9[0-8]\d|99[0-8]))$","重载货物列车"
"^78\d{3}$","保温列车"
"^8(0\d{3}|1([0-8]\d{2}|9[0-8]\d|99[0-8]))$","普快货物班列"
"^8(200[1-9]|20[1-9]\d|2[1-9]\d{2}|[34]\d{3})$","煤炭直达列车"
"^85\d{3}$","石油直达列车"
"^86\d{3}$","始发直达列车"
"^87\d{3}$","空车直达列车"
"^(90\d{3}|91([0-8]\d{2}|9[0-8]\d|99[0-8]))$","军用列车"
"^50\d{3}$","客车单机"
"^51\d{3}$","货车单机"
"^52\d{3}$","小运转单机"
"^5(3\d{3}|4([0-8]\d{2}|9[0-8]\d|99[0-8]))$","补机列车"
"^55(300|[0-2]\d{2})$","普通客货试运转列车"
"^55(500|30[1-9]|3[1-9]\d|4\d{2})$","高速动车组试运转列车"
"^55(50[1-9]|5[1-9]\d|[6-9]\d{2})$","普通动车组试运转列车"
"^56\d{3}$","轻油动车与轨道车"
"^57\d{3}$","路用列车"
"^58(10[1-9]|1[1-9]\d|[2-8]\d{2}|9([0-8]\d|9[0-8]))$","救援列车"
"^DJ(400|[1-3]\d{2}|[1-9]\d?)$","动车组检测列车300直通"
"^DJ([4-9]\d{2}|40[1-9]|4[1-9]\d)$","动车组检测列车300管内"
"^DJ1(400|[0-3]\d{2})$","动车组检测列车250直通"
"^DJ1(40[1-9]|4[1-9]\d|[5-9]\d{2})$","动车组检测列车250管内"
"^DJ[56]\d{3}$","动车组确认列车直通"
"^DJ[78]\d{3}$","动车组确认列车管内"
"^[Ff][GDCZTKgdcztk]?\d{1,4}$","因故折返旅客列车"
"^0[GDCZTKgdcztk]\d{1,4}$","回送图定客车底"
"^00(100|[1-9]\d?)$","有火回送动车组车底"
"^00(10[1-9]|1[1-9]\d|2([0-8]\d|9[0-8]))$","无火回送动车组车底"
"^00(30[1-9]|3[1-9]\d|4([0-8]\d|9[0-8]))$","无火回送普速客车底"
1 ^[Gg](4000|[1-3]\d{3}|[1-9]\d{0,2})$ 直通图定高速动车组
2 ^[Gg](400[1-9]|40[1-9]\d|4[1-8]\d{2}|49[0-8]\d|499[0-8])$ 直通临客高速动车组
3 ^[Gg](9000|[6-8]\d{3}|500[1-9]|50[1-9]\d|5[1-9]\d{2})$ 管内图定高速动车组
4 ^[Gg](900[1-9]|90[1-9]\d|9[1-8]\d{2}|99[0-8]\d|999[0-8])$ 管内临客高速动车组
5 ^[Cc]([1-8]\d{3}|9000)$ 图定城际动车组
6 ^[Cc](900[1-9]|90[1-9]\d|9[1-8]\d{2}|99[0-8]\d|999[0-8])$ 临客城际动车组
7 ^[Cc][1-9]\d{2}$ 动力集中城际动车组
8 ^[IDid](4000|[1-3]\d{3}|[1-9]\d{0,2})$ 直通图定动车组
9 ^[IDid](400[1-9]|40[1-9]\d|4[1-8]\d{2}|49[0-8]\d|499[0-8])$ 直通临客动车组
10 ^[IDid](9000|[6-8]\d{3}|500[1-9]|50[1-9]\d|5[1-9]\d{2})$ 管内图定动车组
11 ^[IDid](900[1-9]|90[1-9]\d|9[1-8]\d{2}|99[0-8]\d|999[0-8])$ 管内临客动车组
12 ^[IDid](8([0-8]\d|9[0-8])|7(0[1-9]|[1-9]\d))$ 动力集中动车组
13 ^[IDid](300|[12]\d{2}|[1-9]\d?)$ 跨局动力集中动车组
14 ^[PZpz](4000|[1-3]\d{3}|[1-9]\d{0,2})$ 直通图定直达特快旅客列车
15 ^[PZpz](400[1-9]|40[1-9]\d|4[1-8]\d{2}|49[0-8]\d|499[0-8])$ 直通临客直达特快旅客列车
16 ^[PZpz](9000|[6-8]\d{3}|500[1-9]|50[1-9]\d|5[1-9]\d{2})$ 管内图定直达特快旅客列车
17 ^[PZpz](900[1-9]|90[1-9]\d|9[1-8]\d{2}|99[0-8]\d|999[0-8])$ 管内临客直达特快旅客列车
18 ^[QTqt](3000|[12]\d{3}|[1-9]\d{0,2})$ 直通图定特快旅客列车
19 ^[QTqt](300[1-9]|30[1-9]\d|3[1-8]\d{2}|39[0-8]\d|399[0-8])$ 直通临客特快旅客列车
20 ^[QTqt](4(00[1-9]|0[1-9]\d|[1-8]\d{2}|9[0-8]\d|99[0-8]))$ 管内临客特快旅客列车
21 ^[QTqt]([5-8]\d{3}|9([0-8]\d{2}|9[0-8]\d|99[0-8])|500[1-9]|50[1-9]\d|5[1-9]\d{2})$ 管内图定特快旅客列车
22 ^[WKwk](4000|[1-3]\d{3}|[1-9]\d{0,2})$ 直通图定快速旅客列车
23 ^[WKwk](400[1-9]|40[1-9]\d|4[1-8]\d{2}|49[0-8]\d|499[0-8])$ 直通临客快速旅客列车
24 ^[WKwk](6([0-8]\d{2}|9[0-8]\d|99[0-8])|500[1-9]|50[1-9]\d|5[1-9]\d{2})$ 管内临客快速旅客列车
25 ^[WKwk](8\d{3}|9([0-8]\d{2}|9[0-8]\d|99[0-8])|7(00[1-9]|0[1-9]\d|[1-9]\d{2}))$ 管内图定快速旅客列车
26 ^[Vv1](00[1-9]|0[1-9]\d|[1-9]\d{2})$ 跨三局及以上图定普通旅客快车
27 ^[Bb2](00[1-9]|0[1-9]\d|[1-9]\d{2})$ 跨两局图定普通旅客快车
28 ^3(00[1-9]|0[1-9]\d|[1-9]\d{2})$ 跨局临时普通旅客快车
29 ^[Uu4](00[1-9]|0[1-9]\d|[1-9]\d{2})$ 管内图定普通旅客快车四字头
30 ^[Xx5]([0-8]\d{2}|9[0-8]\d|99[0-8])$ 管内图定普通旅客快车五字头
31 ^6(19[0-8]|1[0-8]\d|0[1-9]\d|00[1-9])$ 直通普通旅客慢车
32 ^(6(20[1-9]|2[1-9]\d|[3-9]\d{2})|7([0-4]\d{2}|5([0-8]\d|9[0-8])))$ 管内普通旅客慢车
33 ^(8([0-8]\d{2}|9[0-8]\d|99[0-8])|7(60[1-9]|6[1-9]\d|[7-9]\d{2}))$ 通勤列车
34 ^[Yy](500|[1-4]\d{2}|[1-9]\d?)$ 跨局旅游列车
35 ^[Yy](50[1-9]|5[1-9]\d|[6-9]\d{2})$ 管内旅游列车
36 ^[Ss][1-9]\d{0,3}$ 市郊旅客列车
37 ^[Ll](6([0-8]\d{2}|9[0-8]\d|99[0-8])|[1-5]\d{3}|[1-9]\d{0,2})$ 直通临时旅客列车
38 ^[Ll]([7-9]\d{3})$ 管内临时旅客列车
39 ^[Xx](19[0-8]|1[0-8]\d|[1-9]\d?)$ 特快货物班列
40 ^[Xx](39[0-8]|3[0-8]\d|2[1-9]\d|20[1-9])$ 快速货物班列
41 ^[Xx]2(40[1-9]|4[1-9]\d|[5-9]\d{2})$ 直通货物快运列车
42 ^[Xx]([4-9]\d{2}|4[1-9]\d|40[1-9])$ 管内货物快运列车
43 ^[Xx]8\d{3}$ 中欧中亚集装箱班列
44 ^[Xx]9([0-4]\d{2}|500)$ 中亚集装箱班列
45 ^[Xx]9(50[1-9]|5[1-9]\d|[6-9]\d{2})$ 水铁联运班列
46 ^[Xx][1-4]\d{4}$ 加挂零散快运车辆货物列车
47 ^1(000[1-9]|00[1-9]\d|0[1-9]\d{2}|[1-9]\d{3})$ 技术直达列车
48 ^2\d{4}$ 直通货物列车
49 ^3\d{4}$ 区段摘挂列车
50 ^4([0-3]\d{3}|4([0-8]\d{2}|9[0-8]\d|99[0-8]))$ 摘挂列车
51 ^4(500[1-9]|50[1-9]\d|5[1-9]\d{2}|[6-9]\d{3})$ 小运转列车
52 ^6\d{4}$ 自备列车
53 ^70\d{3}$ 超限货物列车
54 ^7([1-6]\d{3}|7([0-8]\d{2}|9[0-8]\d|99[0-8]))$ 重载货物列车
55 ^78\d{3}$ 保温列车
56 ^8(0\d{3}|1([0-8]\d{2}|9[0-8]\d|99[0-8]))$ 普快货物班列
57 ^8(200[1-9]|20[1-9]\d|2[1-9]\d{2}|[34]\d{3})$ 煤炭直达列车
58 ^85\d{3}$ 石油直达列车
59 ^86\d{3}$ 始发直达列车
60 ^87\d{3}$ 空车直达列车
61 ^(90\d{3}|91([0-8]\d{2}|9[0-8]\d|99[0-8]))$ 军用列车
62 ^50\d{3}$ 客车单机
63 ^51\d{3}$ 货车单机
64 ^52\d{3}$ 小运转单机
65 ^5(3\d{3}|4([0-8]\d{2}|9[0-8]\d|99[0-8]))$ 补机列车
66 ^55(300|[0-2]\d{2})$ 普通客货试运转列车
67 ^55(500|30[1-9]|3[1-9]\d|4\d{2})$ 高速动车组试运转列车
68 ^55(50[1-9]|5[1-9]\d|[6-9]\d{2})$ 普通动车组试运转列车
69 ^56\d{3}$ 轻油动车与轨道车
70 ^57\d{3}$ 路用列车
71 ^58(10[1-9]|1[1-9]\d|[2-8]\d{2}|9([0-8]\d|9[0-8]))$ 救援列车
72 ^DJ(400|[1-3]\d{2}|[1-9]\d?)$ 动车组检测列车300直通
73 ^DJ([4-9]\d{2}|40[1-9]|4[1-9]\d)$ 动车组检测列车300管内
74 ^DJ1(400|[0-3]\d{2})$ 动车组检测列车250直通
75 ^DJ1(40[1-9]|4[1-9]\d|[5-9]\d{2})$ 动车组检测列车250管内
76 ^DJ[56]\d{3}$ 动车组确认列车直通
77 ^DJ[78]\d{3}$ 动车组确认列车管内
78 ^[Ff][GDCZTKgdcztk]?\d{1,4}$ 因故折返旅客列车
79 ^0[GDCZTKgdcztk]\d{1,4}$ 回送图定客车底
80 ^00(100|[1-9]\d?)$ 有火回送动车组车底
81 ^00(10[1-9]|1[1-9]\d|2([0-8]\d|9[0-8]))$ 无火回送动车组车底
82 ^00(30[1-9]|3[1-9]\d|4([0-8]\d|9[0-8]))$ 无火回送普速客车底

View File

@@ -534,7 +534,7 @@ class BLEClient(private val context: Context) : BluetoothGattCallback() {
String(it, StandardCharsets.UTF_8)
} ?: return
Log.d(TAG, "Received data len=${newData.length} preview=${newData.take(50)}")
Log.d(TAG, "Received data len=${newData.length} preview=${newData}")
dataBuffer.append(newData)
@@ -640,7 +640,7 @@ class BLEClient(private val context: Context) : BluetoothGattCallback() {
private fun processJsonString(jsonStr: String): Boolean {
try {
val jsonObject = JSONObject(jsonStr)
Log.d(TAG, "Parsed JSON len=${jsonStr.length} preview=${jsonStr.take(50)}")
Log.d(TAG, "Parsed JSON len=${jsonStr.length} preview=${jsonStr}")
handler.post {

View File

@@ -65,6 +65,8 @@ import org.noxylva.lbjconsole.ui.screens.SettingsScreen
import org.noxylva.lbjconsole.ui.theme.LBJConsoleTheme
import org.noxylva.lbjconsole.util.LocoInfoUtil
import org.noxylva.lbjconsole.util.TrainTypeUtil
import org.noxylva.lbjconsole.database.AppSettingsRepository
import java.util.*
import androidx.lifecycle.lifecycleScope
import android.bluetooth.le.ScanCallback
@@ -75,7 +77,9 @@ class MainActivity : ComponentActivity() {
private val bleClient by lazy { BLEClient(this) }
private val trainRecordManager by lazy { TrainRecordManager(this) }
private val locoInfoUtil by lazy { LocoInfoUtil(this) }
private val trainTypeUtil by lazy { TrainTypeUtil(this) }
private val notificationService by lazy { NotificationService(this) }
private val appSettingsRepository by lazy { AppSettingsRepository(this) }
private var deviceStatus by mutableStateOf("未连接")
@@ -120,9 +124,6 @@ class MainActivity : ComponentActivity() {
private var showDisconnectButton by mutableStateOf(false)
private var autoConnectEnabled by mutableStateOf(true)
private val settingsPrefs by lazy { getSharedPreferences("app_settings", Context.MODE_PRIVATE) }
private fun getAppVersion(): String {
return try {
val packageInfo = packageManager.getPackageInfo(packageName, 0)
@@ -246,8 +247,12 @@ class MainActivity : ComponentActivity() {
} catch (e: Exception) {
Log.e(TAG, "Load locomotive data failed", e)
}
}
try {
@@ -274,8 +279,10 @@ class MainActivity : ComponentActivity() {
saveSettings()
if (SettingsActivity.isBackgroundServiceEnabled(this)) {
BackgroundService.startService(this)
lifecycleScope.launch {
if (SettingsActivity.isBackgroundServiceEnabled(this@MainActivity)) {
BackgroundService.startService(this@MainActivity)
}
}
enableEdgeToEdge()
@@ -294,6 +301,9 @@ class MainActivity : ComponentActivity() {
isScanning = isScanning,
currentTab = currentTab,
onTabChange = { tab ->
if (currentTab == 2 && tab != 2) {
saveSettings()
}
currentTab = tab
saveSettings()
},
@@ -369,6 +379,7 @@ class MainActivity : ComponentActivity() {
settingsScrollPosition = settingsScrollPosition,
onSettingsScrollPositionChange = { position ->
android.util.Log.d(TAG, "Settings scroll position changed: $position")
settingsScrollPosition = position
saveSettings()
},
@@ -427,6 +438,7 @@ class MainActivity : ComponentActivity() {
},
appVersion = getAppVersion(),
locoInfoUtil = locoInfoUtil,
trainTypeUtil = trainTypeUtil,
onOpenSettings = {
val intent = Intent(this@MainActivity, SettingsActivity::class.java)
startActivity(intent)
@@ -741,86 +753,91 @@ class MainActivity : ComponentActivity() {
private fun loadSettings() {
settingsDeviceName = settingsPrefs.getString("device_name", "LBJReceiver") ?: "LBJReceiver"
targetDeviceName = settingsDeviceName
currentTab = settingsPrefs.getInt("current_tab", 0)
historyEditMode = settingsPrefs.getBoolean("history_edit_mode", false)
val selectedRecordsStr = settingsPrefs.getString("history_selected_records", "")
historySelectedRecords = if (selectedRecordsStr.isNullOrEmpty()) {
emptySet()
} else {
selectedRecordsStr.split(",").toSet()
lifecycleScope.launch {
try {
val settings = appSettingsRepository.getSettings()
settingsDeviceName = settings.deviceName
targetDeviceName = settingsDeviceName
currentTab = settings.currentTab
historyEditMode = settings.historyEditMode
historySelectedRecords = if (settings.historySelectedRecords.isEmpty()) {
emptySet()
} else {
settings.historySelectedRecords.split(",").toSet()
}
historyExpandedStates = if (settings.historyExpandedStates.isEmpty()) {
emptyMap()
} else {
settings.historyExpandedStates.split(";").mapNotNull { pair ->
val parts = pair.split(":")
if (parts.size == 2) parts[0] to (parts[1] == "true") else null
}.toMap()
}
historyScrollPosition = settings.historyScrollPosition
historyScrollOffset = settings.historyScrollOffset
settingsScrollPosition = settings.settingsScrollPosition
android.util.Log.d(TAG, "Loaded settings scroll position: $settingsScrollPosition")
mapCenterPosition = if (settings.mapCenterLat != null && settings.mapCenterLon != null) {
settings.mapCenterLat.toDouble() to settings.mapCenterLon.toDouble()
} else null
mapZoomLevel = settings.mapZoomLevel.toDouble()
mapRailwayLayerVisible = settings.mapRailwayLayerVisible
mergeSettings = trainRecordManager.mergeSettings
specifiedDeviceAddress = settings.specifiedDeviceAddress
searchOrderList = if (settings.searchOrderList.isEmpty()) {
emptyList()
} else {
settings.searchOrderList.split(",").filter { it.isNotBlank() }
}
autoConnectEnabled = settings.autoConnectEnabled
bleClient.setSpecifiedDeviceAddress(specifiedDeviceAddress)
Log.d(TAG, "Loaded settings from Room: deviceName=${settingsDeviceName} tab=${currentTab} specifiedDevice=${specifiedDeviceAddress} searchOrder=${searchOrderList.size} autoConnect=${autoConnectEnabled}")
} catch (e: Exception) {
Log.e(TAG, "Error loading settings from Room", e)
}
}
val expandedStatesStr = settingsPrefs.getString("history_expanded_states", "")
historyExpandedStates = if (expandedStatesStr.isNullOrEmpty()) {
emptyMap()
} else {
expandedStatesStr.split(";").mapNotNull { pair ->
val parts = pair.split(":")
if (parts.size == 2) parts[0] to (parts[1] == "true") else null
}.toMap()
}
historyScrollPosition = settingsPrefs.getInt("history_scroll_position", 0)
historyScrollOffset = settingsPrefs.getInt("history_scroll_offset", 0)
settingsScrollPosition = settingsPrefs.getInt("settings_scroll_position", 0)
val centerLat = settingsPrefs.getFloat("map_center_lat", Float.NaN)
val centerLon = settingsPrefs.getFloat("map_center_lon", Float.NaN)
mapCenterPosition = if (!centerLat.isNaN() && !centerLon.isNaN()) {
centerLat.toDouble() to centerLon.toDouble()
} else null
mapZoomLevel = settingsPrefs.getFloat("map_zoom_level", 10.0f).toDouble()
mapRailwayLayerVisible = settingsPrefs.getBoolean("map_railway_visible", true)
mergeSettings = trainRecordManager.mergeSettings
specifiedDeviceAddress = settingsPrefs.getString("specified_device_address", null)
val searchOrderStr = settingsPrefs.getString("search_order_list", "")
searchOrderList = if (searchOrderStr.isNullOrEmpty()) {
emptyList()
} else {
searchOrderStr.split(",").filter { it.isNotBlank() }
}
autoConnectEnabled = settingsPrefs.getBoolean("auto_connect_enabled", true)
bleClient.setSpecifiedDeviceAddress(specifiedDeviceAddress)
Log.d(TAG, "Loaded settings deviceName=${settingsDeviceName} tab=${currentTab} specifiedDevice=${specifiedDeviceAddress} searchOrder=${searchOrderList.size} autoConnect=${autoConnectEnabled}")
}
private fun saveSettings() {
lifecycleScope.launch(Dispatchers.IO) {
val editor = settingsPrefs.edit()
.putString("device_name", settingsDeviceName)
.putInt("current_tab", currentTab)
.putBoolean("history_edit_mode", historyEditMode)
.putString("history_selected_records", historySelectedRecords.joinToString(","))
.putString("history_expanded_states", historyExpandedStates.map { "${it.key}:${it.value}" }.joinToString(";"))
.putInt("history_scroll_position", historyScrollPosition)
.putInt("history_scroll_offset", historyScrollOffset)
.putInt("settings_scroll_position", settingsScrollPosition)
.putFloat("map_zoom_level", mapZoomLevel.toFloat())
.putBoolean("map_railway_visible", mapRailwayLayerVisible)
.putString("specified_device_address", specifiedDeviceAddress)
.putString("search_order_list", searchOrderList.joinToString(","))
.putBoolean("auto_connect_enabled", autoConnectEnabled)
try {
val currentSettings = appSettingsRepository.getSettings()
val updatedSettings = currentSettings.copy(
deviceName = settingsDeviceName,
currentTab = currentTab,
historyEditMode = historyEditMode,
historySelectedRecords = historySelectedRecords.joinToString(","),
historyExpandedStates = historyExpandedStates.map { "${it.key}:${it.value}" }.joinToString(";"),
historyScrollPosition = historyScrollPosition,
historyScrollOffset = historyScrollOffset,
settingsScrollPosition = settingsScrollPosition,
mapCenterLat = mapCenterPosition?.first?.toFloat(),
mapCenterLon = mapCenterPosition?.second?.toFloat(),
mapZoomLevel = mapZoomLevel.toFloat(),
mapRailwayLayerVisible = mapRailwayLayerVisible,
specifiedDeviceAddress = specifiedDeviceAddress,
searchOrderList = searchOrderList.joinToString(","),
autoConnectEnabled = autoConnectEnabled
)
mapCenterPosition?.let { (lat, lon) ->
editor.putFloat("map_center_lat", lat.toFloat())
editor.putFloat("map_center_lon", lon.toFloat())
appSettingsRepository.saveSettings(updatedSettings)
Log.d(TAG, "Saved settings to Room: deviceName=${settingsDeviceName} tab=${currentTab} settingsScrollPosition=${settingsScrollPosition} mapCenter=${mapCenterPosition} zoom=${mapZoomLevel}")
} catch (e: Exception) {
Log.e(TAG, "Error saving settings to Room", e)
}
editor.apply()
Log.d(TAG, "Saved settings deviceName=${settingsDeviceName} tab=${currentTab} mapCenter=${mapCenterPosition} zoom=${mapZoomLevel}")
}
}
@@ -909,6 +926,7 @@ fun MainContent(
locoInfoUtil: LocoInfoUtil,
trainTypeUtil: TrainTypeUtil,
historyEditMode: Boolean,
@@ -1124,6 +1142,7 @@ fun MainContent(
lastUpdateTime = lastUpdateTime,
temporaryStatusMessage = temporaryStatusMessage,
locoInfoUtil = locoInfoUtil,
trainTypeUtil = trainTypeUtil,
mergeSettings = mergeSettings,
onClearRecords = onClearRecords,
onRecordClick = onRecordClick,

View File

@@ -5,7 +5,9 @@ import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import kotlinx.coroutines.runBlocking
import org.noxylva.lbjconsole.database.AppSettingsRepository
import org.noxylva.lbjconsole.database.TrainDatabase
import android.os.Build
import android.util.Log
import android.view.View
@@ -27,7 +29,7 @@ class NotificationService(private val context: Context) {
}
private val notificationManager = NotificationManagerCompat.from(context)
private val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
private val appSettingsRepository = AppSettingsRepository(context)
private var notificationIdCounter = NOTIFICATION_ID_BASE
init {
@@ -53,11 +55,15 @@ class NotificationService(private val context: Context) {
}
fun isNotificationEnabled(): Boolean {
return prefs.getBoolean(KEY_ENABLED, false)
return runBlocking {
appSettingsRepository.getSettings().notificationEnabled
}
}
fun setNotificationEnabled(enabled: Boolean) {
prefs.edit().putBoolean(KEY_ENABLED, enabled).apply()
runBlocking {
appSettingsRepository.updateNotificationEnabled(enabled)
}
Log.d(TAG, "Notification enabled set to: $enabled")
}

View File

@@ -1,30 +1,29 @@
package org.noxylva.lbjconsole
import android.content.Context
import android.content.SharedPreferences
import android.os.Bundle
import android.widget.Switch
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.launch
import org.noxylva.lbjconsole.database.AppSettingsRepository
class SettingsActivity : AppCompatActivity() {
companion object {
private const val PREFS_NAME = "lbj_console_settings"
private const val KEY_BACKGROUND_SERVICE = "background_service_enabled"
fun isBackgroundServiceEnabled(context: Context): Boolean {
val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
return prefs.getBoolean(KEY_BACKGROUND_SERVICE, false)
suspend fun isBackgroundServiceEnabled(context: Context): Boolean {
val repository = AppSettingsRepository(context)
return repository.getSettings().backgroundServiceEnabled
}
fun setBackgroundServiceEnabled(context: Context, enabled: Boolean) {
val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
prefs.edit().putBoolean(KEY_BACKGROUND_SERVICE, enabled).apply()
suspend fun setBackgroundServiceEnabled(context: Context, enabled: Boolean) {
val repository = AppSettingsRepository(context)
repository.updateBackgroundServiceEnabled(enabled)
}
}
private lateinit var backgroundServiceSwitch: Switch
private lateinit var prefs: SharedPreferences
private lateinit var appSettingsRepository: AppSettingsRepository
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@@ -33,7 +32,7 @@ class SettingsActivity : AppCompatActivity() {
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.title = "Settings"
prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
appSettingsRepository = AppSettingsRepository(this)
initViews()
setupListeners()
@@ -41,12 +40,16 @@ class SettingsActivity : AppCompatActivity() {
private fun initViews() {
backgroundServiceSwitch = findViewById(R.id.switch_background_service)
backgroundServiceSwitch.isChecked = isBackgroundServiceEnabled(this)
lifecycleScope.launch {
backgroundServiceSwitch.isChecked = isBackgroundServiceEnabled(this@SettingsActivity)
}
}
private fun setupListeners() {
backgroundServiceSwitch.setOnCheckedChangeListener { _, isChecked ->
setBackgroundServiceEnabled(this, isChecked)
lifecycleScope.launch {
setBackgroundServiceEnabled(this@SettingsActivity, isChecked)
}
if (isChecked) {
BackgroundService.startService(this)

View File

@@ -0,0 +1,31 @@
package org.noxylva.lbjconsole.database
import androidx.room.*
import kotlinx.coroutines.flow.Flow
@Dao
interface AppSettingsDao {
@Query("SELECT * FROM app_settings WHERE id = 1")
suspend fun getSettings(): AppSettingsEntity?
@Query("SELECT * FROM app_settings WHERE id = 1")
fun getSettingsFlow(): Flow<AppSettingsEntity?>
@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertSettings(settings: AppSettingsEntity)
@Update
suspend fun updateSettings(settings: AppSettingsEntity)
@Query("DELETE FROM app_settings")
suspend fun deleteAllSettings()
@Query("UPDATE app_settings SET notificationEnabled = :enabled WHERE id = 1")
suspend fun updateNotificationEnabled(enabled: Boolean)
@Transaction
suspend fun saveSettings(settings: AppSettingsEntity) {
insertSettings(settings)
}
}

View File

@@ -0,0 +1,26 @@
package org.noxylva.lbjconsole.database
import androidx.room.Entity
import androidx.room.PrimaryKey
@Entity(tableName = "app_settings")
data class AppSettingsEntity(
@PrimaryKey val id: Int = 1,
val deviceName: String = "LBJReceiver",
val currentTab: Int = 0,
val historyEditMode: Boolean = false,
val historySelectedRecords: String = "",
val historyExpandedStates: String = "",
val historyScrollPosition: Int = 0,
val historyScrollOffset: Int = 0,
val settingsScrollPosition: Int = 0,
val mapCenterLat: Float? = null,
val mapCenterLon: Float? = null,
val mapZoomLevel: Float = 10.0f,
val mapRailwayLayerVisible: Boolean = true,
val specifiedDeviceAddress: String? = null,
val searchOrderList: String = "",
val autoConnectEnabled: Boolean = true,
val backgroundServiceEnabled: Boolean = false,
val notificationEnabled: Boolean = false
)

View File

@@ -0,0 +1,127 @@
package org.noxylva.lbjconsole.database
import android.content.Context
import android.content.SharedPreferences
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.first
class AppSettingsRepository(private val context: Context) {
private val dao = TrainDatabase.getDatabase(context).appSettingsDao()
private val sharedPrefs: SharedPreferences = context.getSharedPreferences("settings", Context.MODE_PRIVATE)
suspend fun getSettings(): AppSettingsEntity {
var settings = dao.getSettings()
if (settings == null) {
settings = migrateFromSharedPreferences()
dao.saveSettings(settings)
}
return settings
}
fun getSettingsFlow(): Flow<AppSettingsEntity?> {
return dao.getSettingsFlow()
}
suspend fun saveSettings(settings: AppSettingsEntity) {
dao.saveSettings(settings)
}
suspend fun updateDeviceName(deviceName: String) {
val current = getSettings()
saveSettings(current.copy(deviceName = deviceName))
}
suspend fun updateCurrentTab(tab: Int) {
val current = getSettings()
saveSettings(current.copy(currentTab = tab))
}
suspend fun updateHistoryEditMode(editMode: Boolean) {
val current = getSettings()
saveSettings(current.copy(historyEditMode = editMode))
}
suspend fun updateHistorySelectedRecords(selectedRecords: String) {
val current = getSettings()
saveSettings(current.copy(historySelectedRecords = selectedRecords))
}
suspend fun updateHistoryExpandedStates(expandedStates: String) {
val current = getSettings()
saveSettings(current.copy(historyExpandedStates = expandedStates))
}
suspend fun updateHistoryScrollPosition(position: Int, offset: Int = 0) {
val current = getSettings()
saveSettings(current.copy(historyScrollPosition = position, historyScrollOffset = offset))
}
suspend fun updateSettingsScrollPosition(position: Int) {
val current = getSettings()
saveSettings(current.copy(settingsScrollPosition = position))
}
suspend fun updateMapSettings(centerLat: Float?, centerLon: Float?, zoomLevel: Float, railwayLayerVisible: Boolean) {
val current = getSettings()
saveSettings(current.copy(
mapCenterLat = centerLat,
mapCenterLon = centerLon,
mapZoomLevel = zoomLevel,
mapRailwayLayerVisible = railwayLayerVisible
))
}
suspend fun updateSpecifiedDeviceAddress(address: String?) {
val current = getSettings()
saveSettings(current.copy(specifiedDeviceAddress = address))
}
suspend fun updateSearchOrderList(orderList: String) {
val current = getSettings()
saveSettings(current.copy(searchOrderList = orderList))
}
suspend fun updateAutoConnectEnabled(enabled: Boolean) {
val current = getSettings()
saveSettings(current.copy(autoConnectEnabled = enabled))
}
suspend fun updateBackgroundServiceEnabled(enabled: Boolean) {
val current = getSettings()
saveSettings(current.copy(backgroundServiceEnabled = enabled))
}
suspend fun updateNotificationEnabled(enabled: Boolean) {
val current = getSettings()
saveSettings(current.copy(notificationEnabled = enabled))
}
private fun migrateFromSharedPreferences(): AppSettingsEntity {
return AppSettingsEntity(
deviceName = sharedPrefs.getString("device_name", "LBJReceiver") ?: "LBJReceiver",
currentTab = sharedPrefs.getInt("current_tab", 0),
historyEditMode = sharedPrefs.getBoolean("history_edit_mode", false),
historySelectedRecords = sharedPrefs.getString("history_selected_records", "") ?: "",
historyExpandedStates = sharedPrefs.getString("history_expanded_states", "") ?: "",
historyScrollPosition = sharedPrefs.getInt("history_scroll_position", 0),
historyScrollOffset = sharedPrefs.getInt("history_scroll_offset", 0),
settingsScrollPosition = sharedPrefs.getInt("settings_scroll_position", 0),
mapCenterLat = if (sharedPrefs.contains("map_center_lat")) sharedPrefs.getFloat("map_center_lat", 0f) else null,
mapCenterLon = if (sharedPrefs.contains("map_center_lon")) sharedPrefs.getFloat("map_center_lon", 0f) else null,
mapZoomLevel = sharedPrefs.getFloat("map_zoom_level", 10.0f),
mapRailwayLayerVisible = sharedPrefs.getBoolean("map_railway_layer_visible", true),
specifiedDeviceAddress = sharedPrefs.getString("specified_device_address", null),
searchOrderList = sharedPrefs.getString("search_order_list", "") ?: "",
autoConnectEnabled = sharedPrefs.getBoolean("auto_connect_enabled", true),
backgroundServiceEnabled = sharedPrefs.getBoolean("background_service_enabled", false),
notificationEnabled = context.getSharedPreferences("notification_settings", Context.MODE_PRIVATE)
.getBoolean("notifications_enabled", false)
)
}
suspend fun clearSharedPreferences() {
sharedPrefs.edit().clear().apply()
}
}

View File

@@ -8,25 +8,59 @@ import androidx.room.migration.Migration
import androidx.sqlite.db.SupportSQLiteDatabase
@Database(
entities = [TrainRecordEntity::class],
version = 1,
entities = [TrainRecordEntity::class, AppSettingsEntity::class],
version = 3,
exportSchema = false
)
abstract class TrainDatabase : RoomDatabase() {
abstract fun trainRecordDao(): TrainRecordDao
abstract fun appSettingsDao(): AppSettingsDao
companion object {
@Volatile
private var INSTANCE: TrainDatabase? = null
private val MIGRATION_1_2 = object : Migration(1, 2) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("""
CREATE TABLE IF NOT EXISTS `app_settings` (
`id` INTEGER NOT NULL,
`deviceName` TEXT NOT NULL,
`currentTab` INTEGER NOT NULL,
`historyEditMode` INTEGER NOT NULL,
`historySelectedRecords` TEXT NOT NULL,
`historyExpandedStates` TEXT NOT NULL,
`historyScrollPosition` INTEGER NOT NULL,
`historyScrollOffset` INTEGER NOT NULL,
`settingsScrollPosition` INTEGER NOT NULL,
`mapCenterLat` REAL,
`mapCenterLon` REAL,
`mapZoomLevel` REAL NOT NULL,
`mapRailwayLayerVisible` INTEGER NOT NULL,
`specifiedDeviceAddress` TEXT,
`searchOrderList` TEXT NOT NULL,
`autoConnectEnabled` INTEGER NOT NULL,
`backgroundServiceEnabled` INTEGER NOT NULL,
PRIMARY KEY(`id`)
)
""")
}
}
val MIGRATION_2_3 = object : Migration(2, 3) {
override fun migrate(database: SupportSQLiteDatabase) {
database.execSQL("ALTER TABLE app_settings ADD COLUMN notificationEnabled INTEGER NOT NULL DEFAULT 0")
}
}
fun getDatabase(context: Context): TrainDatabase {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
TrainDatabase::class.java,
"train_database"
).build()
).addMigrations(MIGRATION_1_2, MIGRATION_2_3).build()
INSTANCE = instance
instance
}

View File

@@ -84,7 +84,7 @@ class TrainRecord(jsonData: JSONObject? = null) {
_coordinates = null
Log.d(TAG, "Successfully parsed: train=$train, dir=$direction, speed=$speed")
Log.d(TAG, "Successfully parsed: train=$train, dir=$direction, speed=$speed, lbjClass='$lbjClass'")
} catch (e: Exception) {
Log.e(TAG, "JSON parse error: ${e.message}", e)

View File

@@ -42,6 +42,7 @@ import org.noxylva.lbjconsole.model.MergedTrainRecord
import org.noxylva.lbjconsole.model.MergeSettings
import org.noxylva.lbjconsole.model.GroupBy
import org.noxylva.lbjconsole.util.LocoInfoUtil
import org.noxylva.lbjconsole.util.TrainTypeUtil
import java.text.SimpleDateFormat
import java.util.*
@@ -54,6 +55,7 @@ fun TrainRecordItem(
expandedStatesMap: MutableMap<String, Boolean>,
latestRecord: TrainRecord?,
locoInfoUtil: LocoInfoUtil?,
trainTypeUtil: TrainTypeUtil?,
onRecordClick: (TrainRecord) -> Unit,
onToggleSelection: (TrainRecord) -> Unit,
onLongClick: (TrainRecord) -> Unit,
@@ -62,6 +64,8 @@ fun TrainRecordItem(
val recordId = record.uniqueId
val isExpanded = expandedStatesMap[recordId] == true
val cardColor = when {
isSelected -> MaterialTheme.colorScheme.primaryContainer
else -> MaterialTheme.colorScheme.surface
@@ -130,11 +134,19 @@ fun TrainRecordItem(
}
}
Text(
text = "${record.rssi} dBm",
fontSize = 10.sp,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
val trainType = if (record.train?.trim().isNullOrEmpty()) {
null
} else {
val lbjClassValue = record.lbjClass?.trim() ?: "NA"
trainTypeUtil?.getTrainType(lbjClassValue, record.train!!.trim())
}
if (!trainType.isNullOrEmpty()) {
Text(
text = trainType,
fontSize = 10.sp,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
Spacer(modifier = Modifier.height(2.dp))
@@ -393,6 +405,7 @@ fun MergedTrainRecordItem(
mergedRecord: MergedTrainRecord,
expandedStatesMap: MutableMap<String, Boolean>,
locoInfoUtil: LocoInfoUtil?,
trainTypeUtil: TrainTypeUtil?,
mergeSettings: MergeSettings? = null,
isInEditMode: Boolean = false,
selectedRecords: List<TrainRecord> = emptyList(),
@@ -484,11 +497,19 @@ fun MergedTrainRecordItem(
}
}
Text(
text = "${latestRecord.rssi} dBm",
fontSize = 10.sp,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
val trainType = if (latestRecord.train?.trim().isNullOrEmpty()) {
null
} else {
val lbjClassValue = latestRecord.lbjClass?.trim() ?: "NA"
trainTypeUtil?.getTrainType(lbjClassValue, latestRecord.train!!.trim())
}
if (!trainType.isNullOrEmpty()) {
Text(
text = trainType,
fontSize = 10.sp,
color = MaterialTheme.colorScheme.onSurfaceVariant
)
}
}
Spacer(modifier = Modifier.height(2.dp))
@@ -873,6 +894,7 @@ fun HistoryScreen(
lastUpdateTime: Date?,
temporaryStatusMessage: String? = null,
locoInfoUtil: LocoInfoUtil? = null,
trainTypeUtil: TrainTypeUtil? = null,
mergeSettings: MergeSettings? = null,
onClearRecords: () -> Unit = {},
onRecordClick: (TrainRecord) -> Unit = {},
@@ -1042,6 +1064,7 @@ fun HistoryScreen(
expandedStatesMap = expandedStatesMap,
latestRecord = latestRecord,
locoInfoUtil = locoInfoUtil,
trainTypeUtil = trainTypeUtil,
onRecordClick = onRecordClick,
onToggleSelection = { record ->
if (selectedRecordsList.contains(record)) {
@@ -1065,6 +1088,7 @@ fun HistoryScreen(
mergedRecord = item,
expandedStatesMap = expandedStatesMap,
locoInfoUtil = locoInfoUtil,
trainTypeUtil = trainTypeUtil,
mergeSettings = mergeSettings,
isInEditMode = isInEditMode,
selectedRecords = selectedRecordsList,

View File

@@ -25,6 +25,7 @@ import org.noxylva.lbjconsole.NotificationService
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.DisposableEffect
@OptIn(ExperimentalMaterial3Api::class)
@Composable
@@ -44,18 +45,14 @@ fun SettingsScreen(
onAutoConnectEnabledChange: (Boolean) -> Unit = {}
) {
val uriHandler = LocalUriHandler.current
val scrollState = rememberScrollState()
val scrollState = rememberScrollState(initial = scrollPosition)
LaunchedEffect(scrollPosition) {
if (scrollPosition > 0 && scrollState.value != scrollPosition) {
scrollState.animateScrollTo(scrollPosition)
DisposableEffect(Unit) {
onDispose {
onScrollPositionChange(scrollState.value)
}
}
LaunchedEffect(scrollState.value) {
onScrollPositionChange(scrollState.value)
}
Column(
modifier = Modifier
.fillMaxSize()
@@ -199,8 +196,20 @@ fun SettingsScreen(
val context = LocalContext.current
val notificationService = remember(context) { NotificationService(context) }
var backgroundServiceEnabled by remember(context) {
mutableStateOf(SettingsActivity.isBackgroundServiceEnabled(context))
var backgroundServiceEnabled by remember { mutableStateOf(false) }
LaunchedEffect(context) {
backgroundServiceEnabled = SettingsActivity.isBackgroundServiceEnabled(context)
}
LaunchedEffect(backgroundServiceEnabled) {
SettingsActivity.setBackgroundServiceEnabled(context, backgroundServiceEnabled)
if (backgroundServiceEnabled) {
BackgroundService.startService(context)
} else {
BackgroundService.stopService(context)
}
}
var notificationEnabled by remember(context, notificationService) {
@@ -228,13 +237,6 @@ fun SettingsScreen(
checked = backgroundServiceEnabled,
onCheckedChange = { enabled ->
backgroundServiceEnabled = enabled
SettingsActivity.setBackgroundServiceEnabled(context, enabled)
if (enabled) {
BackgroundService.startService(context)
} else {
BackgroundService.stopService(context)
}
}
)
}

View File

@@ -0,0 +1,66 @@
package org.noxylva.lbjconsole.util
import android.content.Context
import java.io.BufferedReader
import java.io.InputStreamReader
import java.util.regex.Pattern
class TrainTypeUtil(private val context: Context) {
private val trainTypePatterns = mutableListOf<Pair<Pattern, String>>()
init {
loadTrainTypePatterns()
}
private fun loadTrainTypePatterns() {
try {
val inputStream = context.assets.open("train_number_info.csv")
val reader = BufferedReader(InputStreamReader(inputStream))
reader.useLines { lines ->
lines.forEach { line ->
if (line.isNotBlank()) {
val firstQuoteEnd = line.indexOf('"', 1)
if (firstQuoteEnd > 0 && firstQuoteEnd < line.length - 1) {
val regex = line.substring(1, firstQuoteEnd)
val remainingPart = line.substring(firstQuoteEnd + 1).trim()
if (remainingPart.startsWith(",\"") && remainingPart.endsWith("\"")) {
val type = remainingPart.substring(2, remainingPart.length - 1)
try {
val pattern = Pattern.compile(regex)
trainTypePatterns.add(Pair(pattern, type))
} catch (e: Exception) {
e.printStackTrace()
}
}
}
}
}
}
} catch (e: Exception) {
e.printStackTrace()
}
}
fun getTrainType(locoType: String, train: String): String? {
if (train.isEmpty()) {
return null
}
val actualTrain = if (locoType == "NA") {
train
} else {
locoType + train
}
for ((pattern, type) in trainTypePatterns) {
if (pattern.matcher(actualTrain).matches()) {
return type
}
}
return null
}
}