DocumentScreen: add a spinner when changing color mode
This commit is contained in:
@@ -42,7 +42,6 @@ import org.fairscan.app.ui.screens.document.DocumentUiState
|
|||||||
import org.fairscan.app.ui.state.DocumentUiModel
|
import org.fairscan.app.ui.state.DocumentUiModel
|
||||||
import org.fairscan.app.ui.state.PageThumbnail
|
import org.fairscan.app.ui.state.PageThumbnail
|
||||||
import org.fairscan.imageprocessing.ColorMode
|
import org.fairscan.imageprocessing.ColorMode
|
||||||
import java.lang.IllegalStateException
|
|
||||||
import kotlin.math.min
|
import kotlin.math.min
|
||||||
|
|
||||||
@OptIn(ExperimentalCoroutinesApi::class)
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
@@ -76,16 +75,17 @@ class MainViewModel(val imageRepository: ImageRepository, launchMode: LaunchMode
|
|||||||
)
|
)
|
||||||
|
|
||||||
private val _currentPageIndex = MutableStateFlow(0)
|
private val _currentPageIndex = MutableStateFlow(0)
|
||||||
|
private val _loadingPageId = MutableStateFlow<String?>(null)
|
||||||
private val currentPageUiState: Flow<CurrentPageUiState?> =
|
private val currentPageUiState: Flow<CurrentPageUiState?> =
|
||||||
combine(_currentPageIndex, _pages) { index, pages -> pages.getOrNull(index) }
|
combine(_currentPageIndex, _pages, _loadingPageId) { index, pages, loadingId ->
|
||||||
.mapLatest { page ->
|
val page = pages.getOrNull(index)
|
||||||
|
Pair(page, loadingId)
|
||||||
|
}
|
||||||
|
.mapLatest { (page,loadingId) ->
|
||||||
page?.let {
|
page?.let {
|
||||||
CurrentPageUiState(
|
val isLoading = (it.id == loadingId)
|
||||||
page.id,
|
val bitmap = imageRepository.jpegBytes(it.key())?.toBitmap()
|
||||||
imageRepository.jpegBytes(it.key())?.toBitmap(),
|
CurrentPageUiState(it.id, bitmap, it.colorMode, isLoading)
|
||||||
page.colorMode
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.flowOn(Dispatchers.IO)
|
.flowOn(Dispatchers.IO)
|
||||||
@@ -158,6 +158,7 @@ class MainViewModel(val imageRepository: ImageRepository, launchMode: LaunchMode
|
|||||||
viewModelScope.launch {
|
viewModelScope.launch {
|
||||||
val currentPage = currentPage()
|
val currentPage = currentPage()
|
||||||
currentPage.colorMode?.let {
|
currentPage.colorMode?.let {
|
||||||
|
_loadingPageId.value = currentPage.id
|
||||||
val newColorMode =
|
val newColorMode =
|
||||||
if (it == ColorMode.COLOR) ColorMode.GRAYSCALE else ColorMode.COLOR
|
if (it == ColorMode.COLOR) ColorMode.GRAYSCALE else ColorMode.COLOR
|
||||||
val pages = withContext(Dispatchers.IO) {
|
val pages = withContext(Dispatchers.IO) {
|
||||||
@@ -165,6 +166,7 @@ class MainViewModel(val imageRepository: ImageRepository, launchMode: LaunchMode
|
|||||||
imageRepository.pages()
|
imageRepository.pages()
|
||||||
}
|
}
|
||||||
_pages.value = pages
|
_pages.value = pages
|
||||||
|
_loadingPageId.value = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ import androidx.compose.material.icons.filled.RotateRight
|
|||||||
import androidx.compose.material.icons.outlined.Add
|
import androidx.compose.material.icons.outlined.Add
|
||||||
import androidx.compose.material.icons.outlined.Delete
|
import androidx.compose.material.icons.outlined.Delete
|
||||||
import androidx.compose.material3.ButtonDefaults
|
import androidx.compose.material3.ButtonDefaults
|
||||||
|
import androidx.compose.material3.CircularProgressIndicator
|
||||||
import androidx.compose.material3.DropdownMenu
|
import androidx.compose.material3.DropdownMenu
|
||||||
import androidx.compose.material3.DropdownMenuItem
|
import androidx.compose.material3.DropdownMenuItem
|
||||||
import androidx.compose.material3.ExperimentalMaterial3Api
|
import androidx.compose.material3.ExperimentalMaterial3Api
|
||||||
@@ -181,6 +182,16 @@ private fun DocumentPreview(
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (uiState.currentPage?.isLoading ?: false) {
|
||||||
|
Box(
|
||||||
|
modifier = Modifier
|
||||||
|
.fillMaxSize()
|
||||||
|
.background(MaterialTheme.colorScheme.surface.copy(alpha = 0.4f)),
|
||||||
|
contentAlignment = Alignment.Center
|
||||||
|
) {
|
||||||
|
CircularProgressIndicator()
|
||||||
|
}
|
||||||
|
}
|
||||||
uiState.currentPage?.colorMode?.let {
|
uiState.currentPage?.colorMode?.let {
|
||||||
ColorModeButton(
|
ColorModeButton(
|
||||||
currentColorMode = it,
|
currentColorMode = it,
|
||||||
|
|||||||
@@ -28,4 +28,5 @@ data class CurrentPageUiState(
|
|||||||
val id: String,
|
val id: String,
|
||||||
val bitmap: Bitmap?,
|
val bitmap: Bitmap?,
|
||||||
val colorMode: ColorMode?,
|
val colorMode: ColorMode?,
|
||||||
|
val isLoading: Boolean = false,
|
||||||
)
|
)
|
||||||
|
|||||||
Reference in New Issue
Block a user