Skip to content

Instantly share code, notes, and snippets.

@FireZenk
Created April 22, 2022 09:50
Show Gist options
  • Save FireZenk/e02c04e48b6ffbb6ced5ba7e58871937 to your computer and use it in GitHub Desktop.
Save FireZenk/e02c04e48b6ffbb6ced5ba7e58871937 to your computer and use it in GitHub Desktop.
Bluetooth activation request for Compose
@Composable
fun ActivateBluetooth(launcher: (() -> Unit) -> Unit, isRequestingActivation: (Boolean) -> Unit,
onActivated: () -> Unit) {
val bluetoothActivationLauncher =
rememberLauncherForActivityResult(RequestBluetoothActivation()) {
if (it) {
isRequestingActivation(false)
onActivated()
}
}
launcher {
isRequestingActivation(true)
bluetoothActivationLauncher.launch()
}
}
var bluetoothLauncher: (() -> Unit)? = null
var isRequestingBluetoothActivation = false
// Composable
ActivateBluetooth(
launcher = { bluetoothLauncher = it },
isRequestingActivation = { isRequestingBluetoothActivation = it },
onActivated = { vm reduce Action.SearchAgain },
)
// Launch bluetooth activation
if (!isRequestingBluetoothActivation)
bluetoothLauncher?.invoke()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment