Rename UiState to CameraScreenState
This commit is contained in:
@@ -4,7 +4,7 @@ import android.graphics.Bitmap
|
||||
import androidx.compose.runtime.Immutable
|
||||
|
||||
@Immutable
|
||||
data class UiState(
|
||||
data class CameraScreenState(
|
||||
val detectionMessage: String? = null,
|
||||
val inferenceTime: Long = 0L,
|
||||
val binaryMask: Bitmap? = null,
|
||||
@@ -17,7 +17,6 @@ import androidx.compose.foundation.layout.padding
|
||||
import androidx.compose.material3.Scaffold
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.compose.ui.graphics.Color
|
||||
@@ -40,19 +39,17 @@ class MainActivity : ComponentActivity() {
|
||||
val viewModel: MainViewModel by viewModels { MainViewModel.getFactory(this) }
|
||||
enableEdgeToEdge()
|
||||
setContent {
|
||||
// TODO or collectAsStateWithLifecycle()?
|
||||
val currentScreen by viewModel.currentScreen.collectAsState()
|
||||
// TODO should uiState own currentScreen?
|
||||
val uiState by viewModel.uiState.collectAsStateWithLifecycle()
|
||||
val currentScreen by viewModel.currentScreen.collectAsStateWithLifecycle()
|
||||
val cameraScreenState by viewModel.cameraScreenState.collectAsStateWithLifecycle()
|
||||
val context = LocalContext.current
|
||||
MyScanTheme {
|
||||
Scaffold { innerPadding ->
|
||||
Column {
|
||||
Greeting(modifier = Modifier.padding(innerPadding))
|
||||
MyMessageBox(uiState.detectionMessage, uiState.inferenceTime)
|
||||
MyMessageBox(cameraScreenState.detectionMessage, cameraScreenState.inferenceTime)
|
||||
when (val screen = currentScreen) {
|
||||
is Screen.Camera -> {
|
||||
CameraScreen(viewModel, uiState,
|
||||
CameraScreen(viewModel, cameraScreenState,
|
||||
onImageAnalyzed = { image -> viewModel.segment(image) } )
|
||||
}
|
||||
is Screen.PagePreview -> {
|
||||
|
||||
@@ -28,8 +28,8 @@ class MainViewModel(private val imageSegmentationService: ImageSegmentationServi
|
||||
}
|
||||
}
|
||||
|
||||
private var _uiState = MutableStateFlow(UiState("just started"))
|
||||
val uiState: StateFlow<UiState> = _uiState.asStateFlow()
|
||||
private var _cameraScreenState = MutableStateFlow(CameraScreenState("just started"))
|
||||
val cameraScreenState: StateFlow<CameraScreenState> = _cameraScreenState.asStateFlow()
|
||||
|
||||
private val _currentScreen = MutableStateFlow<Screen>(Screen.Camera)
|
||||
val currentScreen: StateFlow<Screen> = _currentScreen.asStateFlow()
|
||||
@@ -41,7 +41,7 @@ class MainViewModel(private val imageSegmentationService: ImageSegmentationServi
|
||||
.filterNotNull()
|
||||
.map {
|
||||
val binaryMask = it.segmentation.toBinaryMask()
|
||||
UiState(
|
||||
CameraScreenState(
|
||||
detectionMessage = "Inference done",
|
||||
inferenceTime = it.inferenceTime,
|
||||
binaryMask = binaryMask,
|
||||
@@ -49,7 +49,7 @@ class MainViewModel(private val imageSegmentationService: ImageSegmentationServi
|
||||
)
|
||||
}
|
||||
.collect {
|
||||
_uiState.value = it
|
||||
_cameraScreenState.value = it
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ import androidx.lifecycle.compose.LocalLifecycleOwner
|
||||
import com.google.common.util.concurrent.ListenableFuture
|
||||
import org.mydomain.myscan.MainViewModel
|
||||
import org.mydomain.myscan.Point
|
||||
import org.mydomain.myscan.UiState
|
||||
import org.mydomain.myscan.CameraScreenState
|
||||
import org.mydomain.myscan.scaledTo
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Executors
|
||||
@@ -59,7 +59,7 @@ import java.util.concurrent.Executors
|
||||
@Composable
|
||||
fun CameraScreen(
|
||||
viewModel: MainViewModel,
|
||||
uiState: UiState,
|
||||
uiState: CameraScreenState,
|
||||
onImageAnalyzed: (ImageProxy) -> Unit,
|
||||
) {
|
||||
// TODO Check the errors in the logs before the user gives the required authorization
|
||||
@@ -182,20 +182,21 @@ fun bindCameraUseCases(
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun AnalysisOverlay(uiState: UiState) {
|
||||
if (uiState.binaryMask == null) {
|
||||
private fun AnalysisOverlay(cameraScreenState: CameraScreenState) {
|
||||
val binaryMask = cameraScreenState.binaryMask
|
||||
if (binaryMask == null) {
|
||||
return
|
||||
}
|
||||
val maskOverlay = replaceColor(uiState.binaryMask, Color.Black, Color.Transparent)
|
||||
val maskOverlay = replaceColor(binaryMask, Color.Black, Color.Transparent)
|
||||
Canvas(modifier = Modifier.fillMaxSize()) {
|
||||
drawImage(
|
||||
maskOverlay.scale(size.width.toInt(), size.height.toInt()).asImageBitmap(),
|
||||
colorFilter = ColorFilter.tint(Color(0x8000FF00), BlendMode.SrcIn)
|
||||
)
|
||||
if (uiState.documentQuad != null) {
|
||||
val scaledQuad = uiState.documentQuad.scaledTo(
|
||||
fromWidth = uiState.binaryMask.width,
|
||||
fromHeight = uiState.binaryMask.height,
|
||||
if (cameraScreenState.documentQuad != null) {
|
||||
val scaledQuad = cameraScreenState.documentQuad.scaledTo(
|
||||
fromWidth = binaryMask.width,
|
||||
fromHeight = binaryMask.height,
|
||||
toWidth = size.width.toInt(),
|
||||
toHeight = size.height.toInt()
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user