Modify UART layout

This commit is contained in:
Sylwester Zieliński
2022-05-03 12:44:36 +02:00
parent 1550df6ccb
commit 51a9a739c2
4 changed files with 49 additions and 18 deletions

View File

@@ -1,14 +1,19 @@
package no.nordicsemi.android.uart.data package no.nordicsemi.android.uart.data
internal data class UARTData( internal data class UARTData(
val messages: List<UARTOutputRecord> = emptyList(), val messages: List<UARTRecord> = emptyList(),
val batteryLevel: Int? = null, val batteryLevel: Int? = null,
) { ) {
val displayMessages = messages.reversed().take(10) val displayMessages = messages.reversed().take(10)
} }
internal data class UARTOutputRecord( internal data class UARTRecord(
val text: String, val text: String,
val type: UARTRecordType,
val timestamp: Long = System.currentTimeMillis() val timestamp: Long = System.currentTimeMillis()
) )
enum class UARTRecordType {
INPUT, OUTPUT
}

View File

@@ -90,7 +90,7 @@ internal class UARTManager(
setNotificationCallback(txCharacteristic).asFlow().onEach { setNotificationCallback(txCharacteristic).asFlow().onEach {
val text: String = it.getStringValue(0) ?: String.EMPTY val text: String = it.getStringValue(0) ?: String.EMPTY
log(10, "\"$text\" received") log(10, "\"$text\" received")
data.value = data.value.copy(messages = data.value.messages + UARTOutputRecord(text)) data.value = data.value.copy(messages = data.value.messages + UARTRecord(text, UARTRecordType.OUTPUT))
}.launchIn(scope) }.launchIn(scope)
requestMtu(517).enqueue() requestMtu(517).enqueue()
@@ -153,6 +153,7 @@ internal class UARTManager(
request.split() request.split()
} }
request.suspend() request.suspend()
data.value = data.value.copy(messages = data.value.messages + UARTRecord(text, UARTRecordType.INPUT))
log(10, "\"$text\" sent") log(10, "\"$text\" sent")
} }
} }

View File

@@ -1,6 +1,7 @@
package no.nordicsemi.android.uart.view package no.nordicsemi.android.uart.view
import androidx.compose.foundation.layout.* import androidx.compose.foundation.layout.*
import androidx.compose.foundation.layout.RowScopeInstance.weight
import androidx.compose.material.icons.Icons import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Add import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.Delete import androidx.compose.material.icons.filled.Delete
@@ -11,6 +12,8 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.painterResource import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
@@ -19,7 +22,8 @@ import no.nordicsemi.android.theme.view.SectionTitle
import no.nordicsemi.android.uart.R import no.nordicsemi.android.uart.R
import no.nordicsemi.android.uart.data.MacroEol import no.nordicsemi.android.uart.data.MacroEol
import no.nordicsemi.android.uart.data.UARTData import no.nordicsemi.android.uart.data.UARTData
import no.nordicsemi.android.uart.data.UARTOutputRecord import no.nordicsemi.android.uart.data.UARTRecord
import no.nordicsemi.android.uart.data.UARTRecordType
import no.nordicsemi.android.utils.EMPTY import no.nordicsemi.android.utils.EMPTY
import java.text.SimpleDateFormat import java.text.SimpleDateFormat
import java.util.* import java.util.*
@@ -34,18 +38,19 @@ internal fun UARTContentView(
horizontalAlignment = Alignment.CenterHorizontally, horizontalAlignment = Alignment.CenterHorizontally,
modifier = Modifier.padding(16.dp) modifier = Modifier.padding(16.dp)
) { ) {
InputSection(onEvent = onEvent)
Spacer(modifier = Modifier.size(16.dp))
MacroSection(viewState, onEvent)
Spacer(modifier = Modifier.size(16.dp))
OutputSection(state.displayMessages, onEvent) OutputSection(state.displayMessages, onEvent)
Spacer(modifier = Modifier.size(16.dp)) Spacer(modifier = Modifier.size(16.dp))
// MacroSection(viewState, onEvent)
//
// Spacer(modifier = Modifier.size(16.dp))
InputSection(onEvent = onEvent)
Spacer(modifier = Modifier.size(16.dp))
Button( Button(
onClick = { onEvent(DisconnectEvent) } onClick = { onEvent(DisconnectEvent) }
) { ) {
@@ -201,8 +206,8 @@ private fun DeleteConfigurationDialog(onEvent: (UARTViewEvent) -> Unit, onDismis
} }
@Composable @Composable
private fun OutputSection(records: List<UARTOutputRecord>, onEvent: (UARTViewEvent) -> Unit) { private fun OutputSection(records: List<UARTRecord>, onEvent: (UARTViewEvent) -> Unit) {
ScreenSection { ScreenSection(modifier = Modifier.weight(1f)) {
Column( Column(
horizontalAlignment = Alignment.CenterHorizontally horizontalAlignment = Alignment.CenterHorizontally
) { ) {
@@ -243,7 +248,7 @@ private fun OutputSection(records: List<UARTOutputRecord>, onEvent: (UARTViewEve
} }
@Composable @Composable
private fun MessageItem(record: UARTOutputRecord) { private fun MessageItem(record: UARTRecord) {
Column { Column {
Text( Text(
text = record.timeToString(), text = record.timeToString(),
@@ -252,14 +257,31 @@ private fun MessageItem(record: UARTOutputRecord) {
) )
Spacer(modifier = Modifier.height(4.dp)) Spacer(modifier = Modifier.height(4.dp))
Text( Text(
text = record.text, text = createRecordText(record = record),
style = MaterialTheme.typography.bodyMedium style = MaterialTheme.typography.bodyMedium,
color = createRecordColor(record = record)
) )
} }
} }
@Composable
private fun createRecordText(record: UARTRecord): String {
return when (record.type) {
UARTRecordType.INPUT -> stringResource(id = R.string.uart_input_log, record.text)
UARTRecordType.OUTPUT -> stringResource(id = R.string.uart_output_log, record.text)
}
}
@Composable
private fun createRecordColor(record: UARTRecord): Color {
return when (record.type) {
UARTRecordType.INPUT -> colorResource(id = R.color.nordicGrass)
UARTRecordType.OUTPUT -> MaterialTheme.colorScheme.onBackground
}
}
private val datFormatter = SimpleDateFormat("dd MMMM yyyy, HH:mm:ss", Locale.ENGLISH) private val datFormatter = SimpleDateFormat("dd MMMM yyyy, HH:mm:ss", Locale.ENGLISH)
private fun UARTOutputRecord.timeToString(): String { private fun UARTRecord.timeToString(): String {
return datFormatter.format(timestamp) return datFormatter.format(timestamp)
} }

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources xmlns:tools="http://schemas.android.com/tools">
<string name="uart_title">UART</string> <string name="uart_title">UART</string>
<string name="uart_no_macros_info">Please define a macro to send command to the device.</string> <string name="uart_no_macros_info">Please define a macro to send command to the device.</string>
@@ -52,4 +52,7 @@
<string name="uart_delete_dialog_info">Are you sure that you want to delete this configuration? Your data will be irretrievably lost.</string> <string name="uart_delete_dialog_info">Are you sure that you want to delete this configuration? Your data will be irretrievably lost.</string>
<string name="uart_delete_dialog_confirm">Confirm</string> <string name="uart_delete_dialog_confirm">Confirm</string>
<string name="uart_delete_dialog_cancel">Cancel</string> <string name="uart_delete_dialog_cancel">Cancel</string>
<string name="uart_input_log">--&gt; %s</string>
<string name="uart_output_log" tools:ignore="TypographyDashes">&lt;-- %s</string>
</resources> </resources>