Introduce BluetoothAddress typealias

This commit introduces a typealias `BluetoothAddress` for `String` to improve code clarity and type safety when dealing with Bluetooth MAC addresses.

The following changes were made:
- Created `BluetoothAddress.kt` defining the typealias.
- Updated various classes to use `BluetoothAddress` instead of `String` for Bluetooth addresses:
    - `RPAChecker`
    - `PopUpReaction`
    - `DeviceSelectionDialogFactory`
    - `MonitorWorker`
    - `GeneralSettings`
    - `BluetoothDevice2`
This commit is contained in:
darken
2025-06-06 06:03:41 +02:00
committed by Matthias Urhahn
parent 11a19da0b8
commit 877c5d9013
7 changed files with 27 additions and 24 deletions
@@ -1,37 +1,33 @@
package eu.darken.capod.main.ui.settings.general
import android.content.Context
import android.content.DialogInterface
import androidx.appcompat.app.AlertDialog
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import eu.darken.capod.R
import eu.darken.capod.common.bluetooth.BluetoothDevice2
class DeviceSelectionDialogFactory constructor(private val context: Context) {
class DeviceSelectionDialogFactory(private val context: Context) {
fun create(
devices: List<BluetoothDevice2>,
current: BluetoothDevice2?,
callback: (BluetoothDevice2?) -> Unit
): AlertDialog {
return MaterialAlertDialogBuilder(context).apply {
setTitle(R.string.settings_maindevice_address_label)
): AlertDialog = MaterialAlertDialogBuilder(context).apply {
setTitle(R.string.settings_maindevice_address_label)
val pairing = devices
.map { (it.name ?: "?") to it.address }
.plus(context.getString(R.string.settings_maindevice_address_none) to "")
val pairing = devices
.map { (it.name ?: "?") to it.address }
.plus(context.getString(R.string.settings_maindevice_address_none) to "")
setSingleChoiceItems(
pairing.map { it.first }.toTypedArray(),
pairing.indexOfFirst { it.second == current?.address ?: "" },
DialogInterface.OnClickListener { dialog, which ->
val selected = devices.firstOrNull { it.address == pairing[which].second }
callback(selected)
dialog.dismiss()
}
)
setSingleChoiceItems(
pairing.map { it.first }.toTypedArray(),
pairing.indexOfFirst { it.second == current?.address }
) { dialog, which ->
val selected = devices.firstOrNull { it.address == pairing[which].second }
callback(selected)
dialog.dismiss()
}
}.create()
}
}.create()
}
@@ -8,6 +8,7 @@ import androidx.work.ForegroundInfo
import androidx.work.WorkerParameters
import dagger.assisted.Assisted
import dagger.assisted.AssistedInject
import eu.darken.capod.common.bluetooth.BluetoothAddress
import eu.darken.capod.common.bluetooth.BluetoothDevice2
import eu.darken.capod.common.bluetooth.BluetoothManager2
import eu.darken.capod.common.coroutine.DispatcherProvider
@@ -146,7 +147,7 @@ class MonitorWorker @AssistedInject constructor(
.setupCommonEventHandlers(TAG) { "MonitorMode" }
.flatMapLatest { arguments ->
val monitorMode = arguments[0] as MonitorMode
val mainAddress = arguments[1] as String?
val mainAddress = arguments[1] as BluetoothAddress?
@Suppress("UNCHECKED_CAST")
val devices = arguments[2] as Collection<BluetoothDevice2>