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