Skip to content

Commit bb009a7

Browse files
committed
Change bluetooth to state (as opposed to boolean)
1 parent 5b1ca40 commit bb009a7

File tree

9 files changed

+89
-27
lines changed

9 files changed

+89
-27
lines changed

DittoHealth/src/main/java/live/ditto/health/HealthScreenActionHandler.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import android.provider.Settings
77

88
class HealthScreenActionHandler {
99
fun handle(actionType: HealthUiActionType, context: Context) = when (actionType) {
10-
HealthUiActionType.NoAction -> {}
10+
HealthUiActionType.NoAction,
11+
HealthUiActionType.BluetoothUnsupported -> {
12+
}
13+
1114
HealthUiActionType.EnableWifi -> {
1215
context.startActivity(Intent(Settings.ACTION_WIFI_SETTINGS))
1316
}
@@ -23,5 +26,6 @@ class HealthScreenActionHandler {
2326
HealthUiActionType.EnableBluetooth -> {
2427
context.startActivity(Intent(Settings.ACTION_BLUETOOTH_SETTINGS))
2528
}
29+
2630
}
2731
}

DittoHealth/src/main/java/live/ditto/health/HealthUiActionType.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ sealed class HealthUiActionType {
55
object RequestPermissions : HealthUiActionType()
66
object EnableWifi : HealthUiActionType()
77
object EnableBluetooth : HealthUiActionType()
8+
object BluetoothUnsupported : HealthUiActionType()
89
}

DittoHealth/src/main/java/live/ditto/health/HealthUiStateCause.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ data class HealthUiStateCause(
1515
HealthUiActionType.EnableWifi -> false
1616
HealthUiActionType.RequestPermissions -> false
1717
HealthUiActionType.EnableBluetooth -> false
18+
HealthUiActionType.BluetoothUnsupported -> false
1819
}
1920
}
2021

@@ -23,5 +24,6 @@ data class HealthUiStateCause(
2324
HealthUiActionType.EnableWifi -> context.getString(R.string.enable_wifi)
2425
HealthUiActionType.RequestPermissions -> context.getString(R.string.request_permissions)
2526
HealthUiActionType.EnableBluetooth -> context.getString(R.string.enable_bluetooth)
27+
HealthUiActionType.BluetoothUnsupported -> ""
2628
}
2729
}

DittoHealth/src/main/java/live/ditto/health/data/HealthUiState.kt

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import kotlinx.coroutines.withContext
77
import live.ditto.health.HealthUiActionType
88
import live.ditto.health.HealthUiStateCause
99
import live.ditto.health.R
10+
import live.ditto.health.usecase.BluetoothState
1011
import live.ditto.health.usecase.WifiAwareState
1112

1213
data class HealthUiState(
1314
val defaultDispatcher: CoroutineDispatcher = Dispatchers.Default,
1415
val missingPermissions: List<String> = emptyList(),
1516
val wifiEnabled: Boolean = true,
16-
val bluetoothEnabled: Boolean = true,
17+
val bluetoothState: BluetoothState = BluetoothState.UNSUPPORTED,
1718
val wifiAwareState: WifiAwareState = WifiAwareState.UNSUPPORTED,
1819
val deviceDetails: DeviceDetails
1920
) {
@@ -81,22 +82,36 @@ data class HealthUiState(
8182
withActions: MutableList<HealthUiStateCause>,
8283
context: Context
8384
) {
84-
if (bluetoothEnabled) {
85-
noActions.add(
86-
HealthUiStateCause(
87-
reason = context.getString(R.string.bluetooth_status),
88-
details = listOf(context.getString(R.string.bluetooth_enabled)),
89-
actionType = HealthUiActionType.NoAction
85+
when (bluetoothState) {
86+
BluetoothState.ENABLED -> {
87+
noActions.add(
88+
HealthUiStateCause(
89+
reason = context.getString(R.string.bluetooth_status),
90+
details = listOf(context.getString(R.string.bluetooth_enabled)),
91+
actionType = HealthUiActionType.NoAction
92+
)
9093
)
91-
)
92-
} else {
93-
withActions.add(
94-
HealthUiStateCause(
95-
reason = context.getString(R.string.bluetooth_status),
96-
details = listOf(context.getString(R.string.bluetooth_not_enabled)),
97-
actionType = HealthUiActionType.EnableBluetooth
94+
}
95+
96+
BluetoothState.DISABLED -> {
97+
withActions.add(
98+
HealthUiStateCause(
99+
reason = context.getString(R.string.bluetooth_status),
100+
details = listOf(context.getString(R.string.bluetooth_not_enabled)),
101+
actionType = HealthUiActionType.EnableBluetooth
102+
)
98103
)
99-
)
104+
}
105+
106+
BluetoothState.UNSUPPORTED -> {
107+
noActions.add(
108+
HealthUiStateCause(
109+
reason = context.getString(R.string.bluetooth_status),
110+
details = listOf(context.getString(R.string.bluetooth_unsupported)),
111+
actionType = HealthUiActionType.BluetoothUnsupported
112+
)
113+
)
114+
}
100115
}
101116
}
102117
}

DittoHealth/src/main/java/live/ditto/health/ui/composables/TransportHealthInformation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private fun TransportHealthInformation(
6868
healthUiStateCauses.forEach { healthUiStateCause ->
6969
val actionType = healthUiStateCause.actionType
7070

71-
if (actionType == HealthUiActionType.NoAction) {
71+
if (actionType == HealthUiActionType.NoAction || actionType == HealthUiActionType.BluetoothUnsupported) {
7272
HealthCheckWithNoAction(
7373
header = healthUiStateCause.reason,
7474
isHealthy = healthUiStateCause.isHealthy,

DittoHealth/src/main/java/live/ditto/health/ui/viewmodel/HealthViewModel.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import kotlinx.coroutines.flow.onEach
1414
import kotlinx.coroutines.flow.update
1515
import live.ditto.health.data.DeviceDetails
1616
import live.ditto.health.data.HealthUiState
17+
import live.ditto.health.usecase.BluetoothState
1718
import live.ditto.health.usecase.GetBluetoothStatusFlow
1819
import live.ditto.health.usecase.GetDittoMissingPermissionsFlow
1920
import live.ditto.health.usecase.GetPermissionsMetricsUseCase
@@ -109,9 +110,9 @@ class HealthViewModel(
109110
}
110111
}
111112

112-
private fun onBluetoothStatus(status: Boolean?) {
113+
private fun onBluetoothStatus(bluetoothState: BluetoothState) {
113114
_state.update {
114-
it.copy(bluetoothEnabled = status ?: false)
115+
it.copy(bluetoothState = bluetoothState)
115116
}
116117
}
117118

DittoHealth/src/main/java/live/ditto/health/usecase/GetBluetoothStatusFlow.kt

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,44 @@ class GetBluetoothStatusFlow(context: Context) {
1818
private val state = callbackFlow {
1919
val receiver = object : BroadcastReceiver() {
2020
override fun onReceive(context: Context, intent: Intent) {
21-
launch { send(bluetoothManager.adapter.isEnabled) }
21+
launch { send(determineBluetoothState(bluetoothManager = bluetoothManager)) }
2222
}
2323
}
2424

2525
context.registerReceiver(receiver, IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED))
2626

27-
send(bluetoothManager.adapter?.isEnabled)
27+
if (bluetoothManager.adapter == null) {
28+
send(BluetoothState.UNSUPPORTED)
29+
} else {
30+
if (bluetoothManager.adapter.isEnabled) {
31+
send(BluetoothState.ENABLED)
32+
} else {
33+
send(BluetoothState.DISABLED)
34+
}
35+
}
2836

2937
awaitClose {
3038
context.unregisterReceiver(receiver)
3139
}
3240
}
3341

3442
operator fun invoke() = state
43+
44+
private fun determineBluetoothState(bluetoothManager: BluetoothManager): BluetoothState {
45+
return if (bluetoothManager.adapter == null) {
46+
BluetoothState.UNSUPPORTED
47+
} else {
48+
if (bluetoothManager.adapter.isEnabled) {
49+
BluetoothState.ENABLED
50+
} else {
51+
BluetoothState.DISABLED
52+
}
53+
}
54+
}
55+
}
56+
57+
enum class BluetoothState {
58+
ENABLED,
59+
DISABLED,
60+
UNSUPPORTED
3561
}

DittoHealth/src/main/java/live/ditto/health/usecase/GetPermissionsMetricsUseCase.kt

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,19 @@ class GetPermissionsMetricsUseCase {
88
operator fun invoke(healthUiState: HealthUiState): HealthMetric {
99
val missingPermissions = healthUiState.missingPermissions
1010
val wifiEnabled = healthUiState.wifiEnabled
11-
val bluetoothEnabled = healthUiState.bluetoothEnabled
11+
val bluetoothState = healthUiState.bluetoothState
1212
val wifiAwareState = healthUiState.wifiAwareState
1313

14-
val isHealthy = missingPermissions.isEmpty() && wifiEnabled && bluetoothEnabled
14+
val isHealthy by lazy {
15+
missingPermissions.isEmpty()
16+
&& wifiEnabled
17+
&& bluetoothState == BluetoothState.ENABLED
18+
}
1519

1620
val details = createDetailsMap(
1721
missingPermissions = missingPermissions,
1822
wifiEnabled = wifiEnabled,
19-
bluetoothEnabled = bluetoothEnabled,
23+
bluetoothState = bluetoothState,
2024
wifiAwareState = wifiAwareState
2125
)
2226

@@ -29,15 +33,15 @@ class GetPermissionsMetricsUseCase {
2933
private fun createDetailsMap(
3034
missingPermissions: List<String>,
3135
wifiEnabled: Boolean,
32-
bluetoothEnabled: Boolean,
36+
bluetoothState: BluetoothState,
3337
wifiAwareState: WifiAwareState
3438
): MutableMap<String, String> {
3539
return mutableMapOf<String, String>().apply {
3640
if (missingPermissions.isNotEmpty()) {
3741
this[KEY_MISSING_PERMISSIONS] = missingPermissions.joinToString()
3842
}
3943
this[KEY_WIFI_ENABLED] = wifiEnabled.toString()
40-
this[KEY_BLUETOOTH_ENABLED] = bluetoothEnabled.toString()
44+
this[KEY_BLUETOOTH_STATE] = getBluetoothStateDescription(bluetoothState)
4145
this[KEY_WIFI_AWARE_STATE] = getWifiAwareStateDescription(wifiAwareState)
4246
}
4347
}
@@ -50,10 +54,18 @@ class GetPermissionsMetricsUseCase {
5054
}
5155
}
5256

57+
private fun getBluetoothStateDescription(bluetoothState: BluetoothState): String {
58+
return when (bluetoothState) {
59+
BluetoothState.ENABLED -> "Bluetooth is enabled"
60+
BluetoothState.DISABLED -> "Bluetooth is disabled"
61+
BluetoothState.UNSUPPORTED -> "Bluetooth is not supported by this device"
62+
}
63+
}
64+
5365
companion object {
5466
const val KEY_MISSING_PERMISSIONS = "Missing Permissions"
5567
const val KEY_WIFI_ENABLED = "WiFi Enabled"
56-
const val KEY_BLUETOOTH_ENABLED = "Bluetooth Enabled"
68+
const val KEY_BLUETOOTH_STATE = "Bluetooth State"
5769
const val KEY_WIFI_AWARE_STATE = "Wifi Aware State"
5870
}
5971
}

DittoHealth/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<string name="bluetooth_enabled">Bluetooth Enabled</string>
1313
<string name="bluetooth_not_enabled">Bluetooth Not Enabled</string>
1414
<string name="enable_bluetooth">Enable Bluetooth</string>
15+
<string name="bluetooth_unsupported">Bluetooth unsupported</string>
1516
<string name="wifi_aware_supported">WiFi Aware is supported by this device</string>
1617
<string name="wifi_aware_unsupported">Wifi Aware is unavailable for this device</string>
1718
<string name="wifi_aware_unsupported_android_version">Wifi Aware is not supported by this device as it not running Android 8.0 (API 26) or later</string>

0 commit comments

Comments
 (0)