Skip to content

Commit

Permalink
Fix PDF to images closing
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Jan 16, 2025
1 parent 0322629 commit c59a086
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package ru.tech.imageresizershrinker.core.ui.utils

import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import com.arkivanov.decompose.ComponentContext
import kotlinx.coroutines.CoroutineExceptionHandler
Expand Down Expand Up @@ -52,16 +53,14 @@ abstract class BaseComponent(
}

protected open val _isImageLoading: MutableState<Boolean> = mutableStateOf(false)
open val isImageLoading: Boolean
get() = _isImageLoading.value
open val isImageLoading: Boolean by _isImageLoading

private var imageCalculationJob: Job? by smartJob {
_isImageLoading.update { false }
}

protected open val _haveChanges: MutableState<Boolean> = mutableStateOf(false)
open val haveChanges: Boolean
get() = _haveChanges.value
open val haveChanges: Boolean by _haveChanges

protected fun registerSave() {
_haveChanges.update { false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,10 @@ fun AdaptiveLayoutScreen(
}
}

BackHandler(!shouldDisableBackHandler) { onGoBack() }
BackHandler(
enabled = !shouldDisableBackHandler,
onBack = onGoBack
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,8 @@ fun PdfToolsContent(
val isPortrait by isPortraitOrientationAsState()

val onBack = {
if (component.pdfType is Screen.PdfTools.Type.Preview) component.onGoBack()
else {
if (component.haveChanges) showExitDialog = true
else if (component.pdfType != null) {
component.clearType()
} else {
component.onGoBack()
}
}
if (component.haveChanges) showExitDialog = true
else component.onGoBack()
}

val savePdfLauncher = rememberLauncherForActivityResult(
Expand Down Expand Up @@ -500,20 +493,14 @@ fun PdfToolsContent(
)
}

BackHandler(
enabled = component.haveChanges,
onBack = onBack
)

ExitWithoutSavingDialog(
onExit = {
if (component.pdfType != null) {
component.clearType()
} else {
component.onGoBack()
}
},
onExit = component::clearAll,
onDismiss = { showExitDialog = false },
visible = showExitDialog
)

BackHandler(
enabled = (component.pdfType !is Screen.PdfTools.Type.Preview && component.pdfType != null) || component.haveChanges,
onBack = onBack
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ class PdfToolsComponent @AssistedInject internal constructor(
_isSaving.value = false
}

override val haveChanges: Boolean
get() = super.haveChanges || _byteArray.value != null

fun setType(type: Screen.PdfTools.Type) {
when (type) {
is Screen.PdfTools.Type.ImagesToPdf -> setImagesToPdf(type.imageUris)
Expand Down Expand Up @@ -192,7 +189,7 @@ class PdfToolsComponent @AssistedInject internal constructor(
resetCalculatedData()
}

fun clearType() {
fun clearAll() {
_pdfType.update { null }
_pdfPreviewUri.update { null }
_imagesToPdfState.update { null }
Expand Down Expand Up @@ -222,30 +219,28 @@ class PdfToolsComponent @AssistedInject internal constructor(
pages = _pdfToImageState.value?.selectedPages,
preset = presetSelected,
onProgressChange = { _, bitmap ->
imageInfo.let {
imageTransformer.applyPresetBy(
image = bitmap,
preset = _presetSelected.value,
currentInfo = it
)
}.apply {
results.add(
fileController.save(
saveTarget = ImageSaveTarget(
imageInfo = this,
metadata = null,
originalUri = _pdfToImageState.value?.uri.toString(),
sequenceNumber = _done.value + 1,
data = imageCompressor.compressAndTransform(
image = bitmap,
imageInfo = this
)
),
keepOriginalMetadata = false,
oneTimeSaveLocationUri = oneTimeSaveLocationUri
)
val imageInfo = imageTransformer.applyPresetBy(
image = bitmap,
preset = _presetSelected.value,
currentInfo = imageInfo
)

results.add(
fileController.save(
saveTarget = ImageSaveTarget(
imageInfo = imageInfo,
metadata = null,
originalUri = _pdfToImageState.value?.uri.toString(),
sequenceNumber = _done.value + 1,
data = imageCompressor.compressAndTransform(
image = bitmap,
imageInfo = imageInfo
)
),
keepOriginalMetadata = false,
oneTimeSaveLocationUri = oneTimeSaveLocationUri
)
}
)
_done.value += 1
},
onGetPagesCount = { size ->
Expand Down Expand Up @@ -273,6 +268,7 @@ class PdfToolsComponent @AssistedInject internal constructor(
scaleSmallImagesToLarge = _scaleSmallImagesToLarge.value,
preset = _presetSelected.value
)
registerChanges()
onComplete()
_isSaving.value = false
}
Expand Down Expand Up @@ -432,9 +428,9 @@ class PdfToolsComponent @AssistedInject internal constructor(

fun updatePdfToImageSelection(ints: List<Int>) {
_pdfToImageState.update { state ->
if (state != null) checkForOOM()
state?.copy(selectedPages = ints.filter { it < state.pagesCount }.sorted())
}
checkForOOM()
}

fun updateImageFormat(imageFormat: ImageFormat) {
Expand Down

0 comments on commit c59a086

Please sign in to comment.