Skip to content

Commit 108ddfd

Browse files
committed
auto scroll received data
1 parent f430948 commit 108ddfd

6 files changed

Lines changed: 22 additions & 8 deletions

File tree

.github/20241217095029.png

98 KB
Loading

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@
1313
.externalNativeBuild
1414
.cxx
1515
local.properties
16+
/app/release

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22

33
Another simple serial tester for Android, use `jSerialComm`, support HEX send and receive mode, very easy to use and convinient for Modbus debugging.
44

5-
![Phone](.github/20241216211119.png)
5+
![Phone](.github/20241216211119.png)
6+
7+
![Tablet RK3288](.github/20241217095029.png)

app/build.gradle.kts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ android {
5151

5252
dependencies {
5353
implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
54-
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
55-
implementation("androidx.activity:activity-compose:1.7.0")
54+
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.8.7")
55+
implementation("androidx.activity:activity-compose:1.9.3")
5656
implementation(platform("androidx.compose:compose-bom:2023.10.01"))
5757
implementation("androidx.compose.ui:ui")
5858
implementation("androidx.compose.ui:ui-graphics")

app/src/main/java/com/zhaoxinsoft/serialtester/MainActivity.kt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import androidx.compose.foundation.layout.width
2222
import androidx.compose.foundation.lazy.grid.GridCells
2323
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
2424
import androidx.compose.foundation.lazy.grid.items
25+
import androidx.compose.foundation.lazy.grid.rememberLazyGridState
2526
import androidx.compose.foundation.text.KeyboardOptions
2627
import androidx.compose.material3.Button
2728
import androidx.compose.material3.Checkbox
@@ -40,6 +41,7 @@ import androidx.compose.material3.TopAppBarDefaults.topAppBarColors
4041
import androidx.compose.runtime.Composable
4142
import androidx.compose.runtime.getValue
4243
import androidx.compose.runtime.mutableStateOf
44+
import androidx.compose.runtime.rememberCoroutineScope
4345
import androidx.compose.runtime.saveable.rememberSaveable
4446
import androidx.compose.runtime.setValue
4547
import androidx.compose.ui.Alignment
@@ -53,6 +55,7 @@ import com.fazecast.jSerialComm.SerialPort
5355
import com.fazecast.jSerialComm.SerialPortDataListener
5456
import com.fazecast.jSerialComm.SerialPortEvent
5557
import com.zhaoxinsoft.serialtester.ui.theme.SerialTesterTheme
58+
import kotlinx.coroutines.launch
5659
import java.text.SimpleDateFormat
5760
import java.util.Date
5861
import java.util.Locale
@@ -100,7 +103,7 @@ class MainActivity : ComponentActivity() {
100103
val str = if (viewModel.hexMode.value) {
101104
newData.joinToString(" ") { String.format("%02X", it) }
102105
} else {
103-
newData.toString(Charsets.UTF_8)
106+
newData.toString(Charsets.UTF_8).trimEnd()
104107
}
105108
val timestamp = SimpleDateFormat("HH:mm:ss.SSS", Locale.getDefault()).format(Date())
106109
viewModel.receivedData.add("[$timestamp] RX: $str")
@@ -139,12 +142,10 @@ class MainActivity : ComponentActivity() {
139142
private fun disconnect() {
140143
serialPort?.closePort()
141144
serialPort?.removeDataListener()
142-
viewModel.receivedData.clear()
143145
viewModel.isConnected.value = false
144146
}
145147

146148
private fun clear() {
147-
// viewModel.data.value = null
148149
viewModel.receivedData.clear()
149150
}
150151

@@ -285,16 +286,26 @@ class MainActivity : ComponentActivity() {
285286
Text("Send")
286287
}
287288
}
289+
} else if (viewModel.receivedData.isNotEmpty()) {
290+
OutlinedButton(onClick = { clear() }) {
291+
Text("Clear")
292+
}
288293
}
294+
val lazyGridState = rememberLazyGridState()
295+
val coroutineScope = rememberCoroutineScope()
289296
LazyVerticalGrid(
290-
columns = GridCells.Fixed(1), modifier = Modifier.fillMaxWidth(),
297+
columns = GridCells.Fixed(1), modifier = Modifier.fillMaxWidth(), state = lazyGridState
291298
) {
292299
item {
293300
// put static item here
294301
}
295302
items(viewModel.receivedData) { data ->
296303
Text(data)
297304
}
305+
coroutineScope.launch {
306+
if (viewModel.receivedData.isNotEmpty())
307+
lazyGridState.scrollToItem(viewModel.receivedData.size - 1)
308+
}
298309
}
299310
}
300311
}

app/src/main/java/com/zhaoxinsoft/serialtester/MainViewModel.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import androidx.lifecycle.ViewModel
66

77
class MainViewModel : ViewModel() {
88
val availablePorts = mutableListOf<String?>(null)
9-
val isConnected = mutableStateOf(true)
9+
val isConnected = mutableStateOf(false)
1010
val selectedDevice = mutableStateOf<String?>(null)
1111
val baudRate = mutableStateOf<Number?>(9600)
1212
val dataBits = mutableStateOf<Number?>(8)

0 commit comments

Comments
 (0)