Replace CompanionDeviceManager with Scanner library.

This commit is contained in:
Sylwester Zieliński
2021-10-12 14:39:23 +02:00
parent 90fa2db2ad
commit 5a84cc495b
52 changed files with 569 additions and 235 deletions

View File

@@ -20,16 +20,18 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavType
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
import androidx.navigation.compose.rememberNavController
import androidx.navigation.navArgument
import no.nordicsemi.android.csc.view.CSCScreen
import no.nordicsemi.android.gls.view.GLSScreen
import no.nordicsemi.android.hrs.view.HRSScreen
import no.nordicsemi.android.hts.view.HTSScreen
import no.nordicsemi.android.scanner.view.BluetoothNotAvailableScreen
import no.nordicsemi.android.scanner.view.BluetoothNotEnabledScreen
import no.nordicsemi.android.scanner.view.RequestPermissionScreen
import no.nordicsemi.android.permission.view.BluetoothNotAvailableScreen
import no.nordicsemi.android.permission.view.BluetoothNotEnabledScreen
import no.nordicsemi.android.permission.view.RequestPermissionScreen
import no.nordicsemi.android.scanner.view.ScanDeviceScreen
import no.nordicsemi.android.scanner.view.ScanDeviceScreenResult
import no.nordicsemi.android.theme.view.CloseIconAppBar
@@ -56,10 +58,13 @@ internal fun HomeScreen() {
composable(NavDestination.BLUETOOTH_NOT_ENABLED.id) {
BluetoothNotEnabledScreen(continueAction)
}
composable(NavDestination.DEVICE_NOT_CONNECTED.id) {
ScanDeviceScreen {
composable(
NavDestination.DEVICE_NOT_CONNECTED.id,
arguments = listOf(navArgument("args") { type = NavType.StringType })
) {
ScanDeviceScreen(it.arguments?.getString(ARGS_KEY)!!) {
when (it) {
ScanDeviceScreenResult.SUCCESS -> viewModel.finish()
ScanDeviceScreenResult.OK -> viewModel.finish()
ScanDeviceScreenResult.CANCEL -> viewModel.navigateUp()
}.exhaustive
}
@@ -67,7 +72,7 @@ internal fun HomeScreen() {
}
LaunchedEffect(state) {
navController.navigate(state.id)
navController.navigate(state.url)
}
}

View File

@@ -1,5 +1,7 @@
package no.nordicsemi.android.nrftoolbox
const val ARGS_KEY = "args"
enum class NavDestination(val id: String) {
HOME("home-screen"),
CSC("csc-screen"),
@@ -9,5 +11,5 @@ enum class NavDestination(val id: String) {
REQUEST_PERMISSION("request-permission"),
BLUETOOTH_NOT_AVAILABLE("bluetooth-not-available"),
BLUETOOTH_NOT_ENABLED("bluetooth-not-enabled"),
DEVICE_NOT_CONNECTED("device-not-connected"),
DEVICE_NOT_CONNECTED("device-not-connected/{$ARGS_KEY}");
}

View File

@@ -0,0 +1,6 @@
package no.nordicsemi.android.nrftoolbox
data class NavigationTarget(val destination: NavDestination, val args: String? = null) {
val url: String = args?.let { destination.id.replace("{$ARGS_KEY}", it) } ?: destination.id
}

View File

@@ -3,11 +3,15 @@ package no.nordicsemi.android.nrftoolbox
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import no.nordicsemi.android.scanner.tools.NordicBleScanner
import no.nordicsemi.android.scanner.tools.PermissionHelper
import no.nordicsemi.android.scanner.tools.ScannerStatus
import no.nordicsemi.android.csc.service.CYCLING_SPEED_AND_CADENCE_SERVICE_UUID
import no.nordicsemi.android.gls.repository.GLS_SERVICE_UUID
import no.nordicsemi.android.hrs.service.HR_SERVICE_UUID
import no.nordicsemi.android.hts.service.HT_SERVICE_UUID
import no.nordicsemi.android.permission.tools.NordicBleScanner
import no.nordicsemi.android.permission.tools.PermissionHelper
import no.nordicsemi.android.permission.tools.ScannerStatus
import no.nordicsemi.android.permission.viewmodel.BluetoothPermissionState
import no.nordicsemi.android.service.SelectedBluetoothDeviceHolder
import no.nordicsemi.android.scanner.viewmodel.BluetoothPermissionState
import javax.inject.Inject
@HiltViewModel
@@ -17,7 +21,7 @@ class NavigationViewModel @Inject constructor(
private val selectedDevice: SelectedBluetoothDeviceHolder
): ViewModel() {
val state= MutableStateFlow(NavDestination.HOME)
val state= MutableStateFlow(NavigationTarget(NavDestination.HOME))
private var targetDestination = NavDestination.HOME
fun navigate(destination: NavDestination) {
@@ -27,11 +31,11 @@ class NavigationViewModel @Inject constructor(
fun navigateUp() {
targetDestination = NavDestination.HOME
state.value = NavDestination.HOME
state.value = NavigationTarget(NavDestination.HOME)
}
fun finish() {
if (state.value != targetDestination) {
if (state.value.destination != targetDestination) {
navigateToNextScreen()
}
}
@@ -47,12 +51,33 @@ class NavigationViewModel @Inject constructor(
}
private fun navigateToNextScreen() {
state.value = when (getBluetoothState()) {
val destination = when (getBluetoothState()) {
BluetoothPermissionState.PERMISSION_REQUIRED -> NavDestination.REQUEST_PERMISSION
BluetoothPermissionState.BLUETOOTH_NOT_AVAILABLE -> NavDestination.BLUETOOTH_NOT_AVAILABLE
BluetoothPermissionState.BLUETOOTH_NOT_ENABLED -> NavDestination.BLUETOOTH_NOT_ENABLED
BluetoothPermissionState.DEVICE_NOT_CONNECTED -> NavDestination.DEVICE_NOT_CONNECTED
BluetoothPermissionState.READY -> targetDestination
}
val args = if (destination == NavDestination.DEVICE_NOT_CONNECTED) {
createServiceId(targetDestination)
} else {
null
}
state.tryEmit(NavigationTarget(destination, args))
}
private fun createServiceId(destination: NavDestination): String {
return when (destination) {
NavDestination.CSC -> CYCLING_SPEED_AND_CADENCE_SERVICE_UUID.toString()
NavDestination.HRS -> HR_SERVICE_UUID.toString()
NavDestination.HTS -> HT_SERVICE_UUID.toString()
NavDestination.GLS -> GLS_SERVICE_UUID.toString()
NavDestination.HOME,
NavDestination.REQUEST_PERMISSION,
NavDestination.BLUETOOTH_NOT_AVAILABLE,
NavDestination.BLUETOOTH_NOT_ENABLED,
NavDestination.DEVICE_NOT_CONNECTED -> throw IllegalArgumentException("There is no serivce related to the destination: $destination")
}
}
}