Skip to content

Commit

Permalink
refactor: use helper method for flashlight button
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgerardojacinto committed Nov 14, 2023
1 parent 8ae91a8 commit c2f405c
Showing 1 changed file with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
Expand Down Expand Up @@ -203,32 +202,36 @@ class OSBARCScannerActivity : ComponentActivity() {
)

if (camera.cameraInfo.hasFlashUnit()) {
var isFlashlightOn by remember { mutableStateOf(false) }
val onIcon = painterResource(id = R.drawable.flash_on)
val offIcon = painterResource(id = R.drawable.flash_off)

Button(
onClick = {
toggleFlashlight(isFlashlightOn)
isFlashlightOn = !isFlashlightOn
},
modifier = Modifier.align(Alignment.BottomEnd),
colors = ButtonDefaults.buttonColors(
containerColor = if (isFlashlightOn) Color.White else Color.Black
),
shape = CircleShape
) {
val icon = if (isFlashlightOn) onIcon else offIcon
Image(
painter = icon,
contentDescription = null
)
}
TorchButton()
}
}

}

@Composable
fun TorchButton() {
var isFlashlightOn by remember { mutableStateOf(false) }
val onIcon = painterResource(id = R.drawable.flash_on)
val offIcon = painterResource(id = R.drawable.flash_off)

Button(
onClick = {
toggleFlashlight(isFlashlightOn)
isFlashlightOn = !isFlashlightOn
},
colors = ButtonDefaults.buttonColors(
containerColor = if (isFlashlightOn) Color.White else Color.Black
),
shape = CircleShape
) {
val icon = if (isFlashlightOn) onIcon else offIcon
Image(
painter = icon,
contentDescription = null
)
}
}

private fun hasCameraPermission(context: Context): Boolean {
return ContextCompat.checkSelfPermission(
context,
Expand Down

0 comments on commit c2f405c

Please sign in to comment.