Chocolade19721309

Untitled

Aug 20th, 2024
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. package com.example.flashlightpro
  2.  
  3. import android.hardware.camera2.CameraManager
  4. import android.os.Bundle
  5. import androidx.activity.ComponentActivity
  6. import androidx.activity.OnBackPressedCallback
  7. import androidx.activity.compose.setContent
  8. import androidx.compose.foundation.layout.Arrangement
  9. import androidx.compose.foundation.layout.Column
  10. import androidx.compose.foundation.layout.fillMaxSize
  11. import androidx.compose.foundation.layout.fillMaxWidth
  12. import androidx.compose.foundation.layout.padding
  13. import androidx.compose.material3.Button
  14. import androidx.compose.material3.Slider
  15. import androidx.compose.material3.Text
  16. import androidx.compose.runtime.Composable
  17. import androidx.compose.runtime.getValue
  18. import androidx.compose.runtime.mutableStateOf
  19. import androidx.compose.runtime.remember
  20. import androidx.compose.runtime.setValue
  21. import androidx.compose.ui.Alignment
  22. import androidx.compose.ui.Modifier
  23. import androidx.compose.ui.res.stringResource
  24. import androidx.compose.ui.tooling.preview.Preview
  25. import androidx.compose.ui.unit.dp
  26. import com.example.flashlightpro.controller.FlashlightController
  27. import com.example.flashlightpro.features.StrobeLightFeature
  28.  
  29. class MainActivity : ComponentActivity() {
  30.  
  31. private lateinit var flashlightController: FlashlightController
  32. private var isBlinking = false
  33.  
  34. override fun onCreate(savedInstanceState: Bundle?) {
  35. super.onCreate(savedInstanceState)
  36.  
  37. val cameraManager = getSystemService(CAMERA_SERVICE) as CameraManager
  38. val cameraId = cameraManager.cameraIdList[0]
  39.  
  40. val strobeLightFeature = StrobeLightFeature(cameraManager, cameraId)
  41. flashlightController = FlashlightController(strobeLightFeature)
  42.  
  43. onBackPressedDispatcher.addCallback(this, object : OnBackPressedCallback(true) {
  44. override fun handleOnBackPressed() {
  45. if (isBlinking) {
  46. flashlightController.stopBlinking()
  47. }
  48. finish()
  49. }
  50. })
  51.  
  52. setContent {
  53. var buttonText by remember { mutableStateOf(stringResource(R.string.start_blinking)) }
  54. var sliderPosition by remember { mutableStateOf(0.5f) }
  55.  
  56. MyComposeUI(
  57. onBlinkStartStop = {
  58. isBlinking = !isBlinking
  59. if (isBlinking) {
  60. flashlightController.startBlinking(sliderPosition)
  61. buttonText = stringResource(R.string.stop_blinking)
  62. } else {
  63. flashlightController.stopBlinking()
  64. buttonText = stringResource(R.string.start_blinking)
  65. }
  66. },
  67. onSpeedChange = { speed: Float ->
  68. sliderPosition = speed
  69. if (isBlinking) {
  70. flashlightController.startBlinking(speed)
  71. }
  72. },
  73. buttonText = buttonText,
  74. sliderPosition = sliderPosition
  75. )
  76. }
  77. }
  78.  
  79. @Composable
  80. fun MyComposeUI(
  81. onBlinkStartStop: () -> Unit,
  82. onSpeedChange: (Float) -> Unit,
  83. buttonText: String,
  84. sliderPosition: Float
  85. ) {
  86. Column(
  87. modifier = Modifier
  88. .fillMaxSize()
  89. .padding(16.dp),
  90. verticalArrangement = Arrangement.SpaceBetween
  91. ) {
  92. Text(
  93. text = stringResource(R.string.app_name),
  94. modifier = Modifier.align(Alignment.CenterHorizontally)
  95. )
  96.  
  97. Button(
  98. onClick = onBlinkStartStop,
  99. modifier = Modifier.align(Alignment.CenterHorizontally)
  100. ) {
  101. Text(text = buttonText)
  102. }
  103.  
  104. Slider(
  105. value = sliderPosition,
  106. onValueChange = onSpeedChange,
  107. modifier = Modifier
  108. .fillMaxWidth()
  109. .padding(top = 16.dp)
  110. )
  111. }
  112. }
  113.  
  114. // This is the preview Composable with no parameters
  115. @Preview(showBackground = true)
  116. @Composable
  117. fun PreviewMyComposeUI() {
  118. MyComposeUI(
  119. onBlinkStartStop = {},
  120. onSpeedChange = {},
  121. buttonText = stringResource(R.string.start_blinking),
  122. sliderPosition = 0.5f
  123. )
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment