Created
April 22, 2022 09:50
-
-
Save FireZenk/e02c04e48b6ffbb6ced5ba7e58871937 to your computer and use it in GitHub Desktop.
Bluetooth activation request for Compose
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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