mirror of
https://github.com/aljazceru/Android-nRF-Toolbox.git
synced 2025-12-23 17:34:28 +01:00
Add device name to Toolbar
This commit is contained in:
@@ -51,7 +51,7 @@ fun TitleAppBar(text: String) {
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun BackIconAppBar(text: String, onClick: () -> Unit) {
|
||||
fun LoggerBackIconAppBar(text: String, onClick: () -> Unit) {
|
||||
SmallTopAppBar(
|
||||
title = { Text(text) },
|
||||
colors = TopAppBarDefaults.smallTopAppBarColors(
|
||||
@@ -83,6 +83,29 @@ fun BackIconAppBar(text: String, onClick: () -> Unit) {
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun BackIconAppBar(text: String, onClick: () -> Unit) {
|
||||
SmallTopAppBar(
|
||||
title = { Text(text) },
|
||||
colors = TopAppBarDefaults.smallTopAppBarColors(
|
||||
scrolledContainerColor = MaterialTheme.colorScheme.primary,
|
||||
containerColor = colorResource(id = R.color.appBarColor),
|
||||
titleContentColor = MaterialTheme.colorScheme.onPrimary,
|
||||
actionIconContentColor = MaterialTheme.colorScheme.onPrimary,
|
||||
navigationIconContentColor = MaterialTheme.colorScheme.onPrimary,
|
||||
),
|
||||
navigationIcon = {
|
||||
IconButton(onClick = { onClick() }) {
|
||||
Icon(
|
||||
Icons.Default.ArrowBack,
|
||||
tint = MaterialTheme.colorScheme.onPrimary,
|
||||
contentDescription = stringResource(id = R.string.back_screen),
|
||||
)
|
||||
}
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun LoggerIconAppBar(text: String, onClick: () -> Unit, onDisconnectClick: () -> Unit, onLoggerClick: () -> Unit) {
|
||||
SmallTopAppBar(
|
||||
|
||||
@@ -6,7 +6,7 @@ import androidx.compose.ui.res.stringResource
|
||||
import androidx.hilt.navigation.compose.hiltViewModel
|
||||
import no.nordicsemi.android.gls.R
|
||||
import no.nordicsemi.android.gls.details.viewmodel.GLSDetailsViewModel
|
||||
import no.nordicsemi.android.theme.view.BackIconAppBar
|
||||
import no.nordicsemi.android.theme.view.LoggerBackIconAppBar
|
||||
|
||||
@Composable
|
||||
internal fun GLSDetailsScreen() {
|
||||
@@ -14,7 +14,7 @@ internal fun GLSDetailsScreen() {
|
||||
val record = viewModel.record
|
||||
|
||||
Column {
|
||||
BackIconAppBar(stringResource(id = R.string.gls_title)) {
|
||||
LoggerBackIconAppBar(stringResource(id = R.string.gls_title)) {
|
||||
viewModel.navigateBack()
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,6 @@ class UARTRepository @Inject internal constructor(
|
||||
private val serviceManager: ServiceManager,
|
||||
private val configurationDataSource: ConfigurationDataSource,
|
||||
private val toolboxLoggerFactory: ToolboxLoggerFactory,
|
||||
private val sp: UARTSharedPrefs
|
||||
) {
|
||||
private var manager: UARTManager? = null
|
||||
private var logger: ToolboxLogger? = null
|
||||
@@ -31,11 +30,7 @@ class UARTRepository @Inject internal constructor(
|
||||
private val _data = MutableStateFlow<BleManagerResult<UARTData>>(IdleResult())
|
||||
internal val data = _data.asStateFlow()
|
||||
|
||||
var showTutorial: Boolean
|
||||
get() = sp.showTutorial
|
||||
set(value) {
|
||||
sp.showTutorial = value
|
||||
}
|
||||
var device: BluetoothDevice? = null
|
||||
|
||||
val isRunning = data.map { it.isRunning() }
|
||||
val hasBeenDisconnected = data.map { it.hasBeenDisconnected() }
|
||||
@@ -47,6 +42,7 @@ class UARTRepository @Inject internal constructor(
|
||||
}
|
||||
|
||||
fun start(device: BluetoothDevice, scope: CoroutineScope) {
|
||||
this.device = device
|
||||
val createdLogger = toolboxLoggerFactory.create("UART", device.address).also {
|
||||
logger = it
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
package no.nordicsemi.android.uart.repository
|
||||
|
||||
import android.content.Context
|
||||
import dagger.hilt.android.qualifiers.ApplicationContext
|
||||
import javax.inject.Inject
|
||||
|
||||
private const val FILE_NAME = "uart-sp"
|
||||
private const val SHOW_TUTORIAL_FIELD = "show-tutorial"
|
||||
|
||||
class UARTSharedPrefs @Inject constructor(
|
||||
@ApplicationContext
|
||||
private val context: Context
|
||||
) {
|
||||
|
||||
private val sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)
|
||||
|
||||
var showTutorial: Boolean
|
||||
get() = sp.getBoolean(SHOW_TUTORIAL_FIELD, true)
|
||||
set(value) = sp.edit().putBoolean(SHOW_TUTORIAL_FIELD, value).apply()
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
package no.nordicsemi.android.uart.view
|
||||
|
||||
import androidx.compose.foundation.Image
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.layout.ContentScale
|
||||
import androidx.compose.ui.res.painterResource
|
||||
import no.nordicsemi.android.material.you.Tutorial
|
||||
import no.nordicsemi.android.uart.R
|
||||
import no.nordicsemi.android.uart.viewmodel.UARTViewModel
|
||||
|
||||
@Composable
|
||||
internal fun TutorialScreen(viewModel: UARTViewModel) {
|
||||
|
||||
val page1: @Composable () -> Unit = {
|
||||
Image(
|
||||
painter = painterResource(id = R.drawable.tutorial_1),
|
||||
contentDescription = "",
|
||||
contentScale = ContentScale.Crop,
|
||||
modifier = Modifier.fillMaxSize()
|
||||
)
|
||||
}
|
||||
val page2: @Composable () -> Unit = {
|
||||
Image(
|
||||
painter = painterResource(id = R.drawable.tutorial_2),
|
||||
contentDescription = "",
|
||||
contentScale = ContentScale.Crop,
|
||||
modifier = Modifier.fillMaxSize()
|
||||
)
|
||||
}
|
||||
|
||||
Tutorial(listOf(page1, page2)) {
|
||||
viewModel.onTutorialClose()
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package no.nordicsemi.android.uart.view
|
||||
|
||||
import android.annotation.SuppressLint
|
||||
import androidx.compose.foundation.layout.Column
|
||||
import androidx.compose.foundation.rememberScrollState
|
||||
import androidx.compose.foundation.verticalScroll
|
||||
@@ -12,6 +13,7 @@ import no.nordicsemi.android.material.you.PagerView
|
||||
import no.nordicsemi.android.material.you.PagerViewEntity
|
||||
import no.nordicsemi.android.material.you.PagerViewItem
|
||||
import no.nordicsemi.android.service.*
|
||||
import no.nordicsemi.android.theme.view.BackIconAppBar
|
||||
import no.nordicsemi.android.theme.view.LoggerIconAppBar
|
||||
import no.nordicsemi.android.uart.R
|
||||
import no.nordicsemi.android.uart.data.UARTData
|
||||
@@ -22,6 +24,7 @@ import no.nordicsemi.ui.scanner.ui.DeviceDisconnectedView
|
||||
import no.nordicsemi.ui.scanner.ui.NoDeviceView
|
||||
import no.nordicsemi.ui.scanner.ui.Reason
|
||||
|
||||
@SuppressLint("MissingPermission")
|
||||
@Composable
|
||||
fun UARTScreen() {
|
||||
val viewModel: UARTViewModel = hiltViewModel()
|
||||
@@ -30,8 +33,24 @@ fun UARTScreen() {
|
||||
Column {
|
||||
val navigateUp = { viewModel.onEvent(NavigateUp) }
|
||||
|
||||
LoggerIconAppBar(stringResource(id = R.string.uart_title), navigateUp, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLogger)
|
||||
Column(modifier = Modifier) {
|
||||
when (state.uartManagerState) {
|
||||
NoDeviceState -> BackIconAppBar(stringResource(id = R.string.uart_title), navigateUp)
|
||||
is WorkingState -> when (state.uartManagerState.result) {
|
||||
is IdleResult,
|
||||
is DisconnectedResult,
|
||||
is LinkLossResult,
|
||||
is MissingServiceResult,
|
||||
is UnknownErrorResult -> BackIconAppBar(stringResource(id = R.string.uart_title), navigateUp)
|
||||
is ConnectingResult,
|
||||
is SuccessResult -> {
|
||||
val text = state.uartManagerState.device.name ?: state.uartManagerState.device.address ?: stringResource(id = R.string.uart_title)
|
||||
LoggerIconAppBar(text, navigateUp, { viewModel.onEvent(DisconnectEvent) }) {
|
||||
viewModel.onEvent(OpenLogger)
|
||||
}
|
||||
}
|
||||
}
|
||||
}.exhaustive
|
||||
}
|
||||
|
||||
Column(modifier = Modifier) {
|
||||
@@ -46,7 +65,6 @@ fun UARTScreen() {
|
||||
is UnknownErrorResult -> Scroll { DeviceDisconnectedView(Reason.UNKNOWN, navigateUp) }
|
||||
is SuccessResult -> SuccessScreen(state.uartManagerState.result.data, state, viewModel)
|
||||
}
|
||||
TutorialState -> TutorialScreen(viewModel)
|
||||
}.exhaustive
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package no.nordicsemi.android.uart.view
|
||||
|
||||
import android.bluetooth.BluetoothDevice
|
||||
import no.nordicsemi.android.service.BleManagerResult
|
||||
import no.nordicsemi.android.uart.data.UARTConfiguration
|
||||
import no.nordicsemi.android.uart.data.UARTData
|
||||
@@ -26,8 +27,9 @@ internal data class UARTViewState(
|
||||
|
||||
internal sealed class HTSManagerState
|
||||
|
||||
internal data class WorkingState(val result: BleManagerResult<UARTData>) : HTSManagerState()
|
||||
internal data class WorkingState(
|
||||
val device: BluetoothDevice,
|
||||
val result: BleManagerResult<UARTData>
|
||||
) : HTSManagerState()
|
||||
|
||||
internal object NoDeviceState : HTSManagerState()
|
||||
|
||||
internal object TutorialState : HTSManagerState()
|
||||
|
||||
@@ -31,9 +31,7 @@ internal class UARTViewModel @Inject constructor(
|
||||
|
||||
init {
|
||||
viewModelScope.launch {
|
||||
if (repository.showTutorial) {
|
||||
_state.value = _state.value.copy(uartManagerState = TutorialState)
|
||||
} else if (repository.isRunning.firstOrNull() == false) {
|
||||
if (repository.isRunning.firstOrNull() == false) {
|
||||
requestBluetoothDevice()
|
||||
}
|
||||
}
|
||||
@@ -42,7 +40,7 @@ internal class UARTViewModel @Inject constructor(
|
||||
if (it is IdleResult) {
|
||||
return@onEach
|
||||
}
|
||||
_state.value = _state.value.copy(uartManagerState = WorkingState(it))
|
||||
_state.value = _state.value.copy(uartManagerState = WorkingState(repository.device!!, it))
|
||||
}.launchIn(viewModelScope)
|
||||
|
||||
dataSource.getConfigurations().onEach {
|
||||
@@ -93,16 +91,6 @@ internal class UARTViewModel @Inject constructor(
|
||||
}.exhaustive
|
||||
}
|
||||
|
||||
fun onTutorialClose() {
|
||||
repository.showTutorial = false
|
||||
_state.value = _state.value.copy(uartManagerState = NoDeviceState)
|
||||
viewModelScope.launch {
|
||||
if (repository.isRunning.firstOrNull() == false) {
|
||||
requestBluetoothDevice()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun onMacroInputSwitch() {
|
||||
_state.value = _state.value.copy(isInputVisible = !state.value.isInputVisible)
|
||||
}
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 153 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 291 KiB |
Reference in New Issue
Block a user