Skip to content

Commit

Permalink
Added ability to disable tool exit confirmation by #1594
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Jan 16, 2025
1 parent c59a086 commit 6d5f04a
Show file tree
Hide file tree
Showing 20 changed files with 215 additions and 56 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package ru.tech.imageresizershrinker.core.resources.icons

import androidx.compose.material.icons.Icons
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.graphics.vector.path
import androidx.compose.ui.unit.dp

val Icons.Outlined.SaveConfirm: ImageVector by lazy {
ImageVector.Builder(
name = "Outlined.SaveConfirm",
defaultWidth = 24.dp,
defaultHeight = 24.dp,
viewportWidth = 24f,
viewportHeight = 24f
).apply {
path(fill = SolidColor(Color(0xFF000000))) {
moveTo(14f, 12.8f)
curveTo(13.5f, 12.31f, 12.78f, 12f, 12f, 12f)
curveTo(10.34f, 12f, 9f, 13.34f, 9f, 15f)
curveTo(9f, 16.31f, 9.84f, 17.41f, 11f, 17.82f)
curveTo(11.07f, 15.67f, 12.27f, 13.8f, 14f, 12.8f)
moveTo(11.09f, 19f)
horizontalLineTo(5f)
verticalLineTo(5f)
horizontalLineTo(16.17f)
lineTo(19f, 7.83f)
verticalLineTo(12.35f)
curveTo(19.75f, 12.61f, 20.42f, 13f, 21f, 13.54f)
verticalLineTo(7f)
lineTo(17f, 3f)
horizontalLineTo(5f)
curveTo(3.89f, 3f, 3f, 3.9f, 3f, 5f)
verticalLineTo(19f)
curveTo(3f, 20.1f, 3.89f, 21f, 5f, 21f)
horizontalLineTo(11.81f)
curveTo(11.46f, 20.39f, 11.21f, 19.72f, 11.09f, 19f)
moveTo(6f, 10f)
horizontalLineTo(15f)
verticalLineTo(6f)
horizontalLineTo(6f)
verticalLineTo(10f)
moveTo(15.75f, 21f)
lineTo(13f, 18f)
lineTo(14.16f, 16.84f)
lineTo(15.75f, 18.43f)
lineTo(19.34f, 14.84f)
lineTo(20.5f, 16.25f)
lineTo(15.75f, 21f)
}
}.build()
}
2 changes: 2 additions & 0 deletions core/resources/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1558,4 +1558,6 @@
<string name="none">None</string>
<string name="custom_pages">Custom Pages</string>
<string name="pages_selection">Pages Selection</string>
<string name="tool_exit_confirmation">Tool Exit Confirmation</string>
<string name="tool_exit_confirmation_sub">If you have unsaved changes while using particular tools and try to close it, then confirm dialog will be shown</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ interface SettingsInteractor : SimpleSettingsInteractor {
suspend fun removeCustomFont(font: DomainFontFamily.Custom)

suspend fun createCustomFontsExport(): ByteArray

suspend fun toggleEnableToolExitConfirmation()
}

fun SettingsInteractor.toSimpleSettingsInteractor(): SimpleSettingsInteractor =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ data class SettingsState(
val fastSettingsSide: FastSettingsSide,
val settingGroupsInitialVisibility: Map<Int, Boolean>,
val hashingTypeForFilename: HashingType?,
val customFonts: List<DomainFontFamily.Custom>
val customFonts: List<DomainFontFamily.Custom>,
val enableToolExitConfirmation: Boolean
) {

companion object {
Expand Down Expand Up @@ -207,7 +208,8 @@ data class SettingsState(
fastSettingsSide = FastSettingsSide.CenterStart,
settingGroupsInitialVisibility = emptyMap(),
hashingTypeForFilename = null,
customFonts = emptyList()
customFonts = emptyList(),
enableToolExitConfirmation = true
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,4 +482,9 @@ sealed class Setting(
title = R.string.checksum_as_filename,
subtitle = R.string.checksum_as_filename_sub
)

data object EnableToolExitConfirmation : Setting(
title = R.string.tool_exit_confirmation,
subtitle = R.string.tool_exit_confirmation_sub
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ sealed class SettingsGroup(
titleId = R.string.behavior,
settingsList = listOf(
Setting.SkipFilePicking,
Setting.EnableToolExitConfirmation,
Setting.ShowSettingsInLandscape,
Setting.UseFullscreenSettings,
Setting.FastSettingsSide,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ data class UiSettingsState(
val fastSettingsSide: FastSettingsSide,
val settingGroupsInitialVisibility: Map<Int, Boolean>,
val hashingTypeForFilename: HashingType?,
val customFonts: List<UiFontFamily.Custom>
val customFonts: List<UiFontFamily.Custom>,
val enableToolExitConfirmation: Boolean
)

fun UiSettingsState.isFirstLaunch(
Expand Down Expand Up @@ -372,7 +373,8 @@ fun SettingsState.toUiState(
fastSettingsSide = fastSettingsSide,
settingGroupsInitialVisibility = settingGroupsInitialVisibility,
hashingTypeForFilename = hashingTypeForFilename,
customFonts = customFonts
customFonts = customFonts,
enableToolExitConfirmation = enableToolExitConfirmation
)
}
}.value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package ru.tech.imageresizershrinker.core.ui.widget

import androidx.activity.compose.BackHandler
import androidx.compose.animation.AnimatedContent
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Box
Expand Down Expand Up @@ -67,6 +66,7 @@ import ru.tech.imageresizershrinker.core.resources.R
import ru.tech.imageresizershrinker.core.settings.presentation.provider.LocalSettingsState
import ru.tech.imageresizershrinker.core.ui.utils.animation.fancySlideTransition
import ru.tech.imageresizershrinker.core.ui.utils.provider.ProvideContainerDefaults
import ru.tech.imageresizershrinker.core.ui.widget.dialogs.ExitBackHandler
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedBottomSheetDefaults
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedIconButton
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedTopAppBar
Expand Down Expand Up @@ -300,7 +300,7 @@ fun AdaptiveBottomScaffoldLayoutScreen(
}
}

BackHandler(
ExitBackHandler(
enabled = !shouldDisableBackHandler,
onBack = onGoBack
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package ru.tech.imageresizershrinker.core.ui.widget

import androidx.activity.compose.BackHandler
import androidx.compose.animation.AnimatedContent
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -75,6 +74,7 @@ import kotlinx.coroutines.launch
import ru.tech.imageresizershrinker.core.resources.R
import ru.tech.imageresizershrinker.core.settings.presentation.provider.LocalSettingsState
import ru.tech.imageresizershrinker.core.ui.utils.animation.fancySlideTransition
import ru.tech.imageresizershrinker.core.ui.widget.dialogs.ExitBackHandler
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedIconButton
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedTopAppBar
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedTopAppBarType
Expand Down Expand Up @@ -300,7 +300,7 @@ fun AdaptiveLayoutScreen(
}
}

BackHandler(
ExitBackHandler(
enabled = !shouldDisableBackHandler,
onBack = onGoBack
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@

package ru.tech.imageresizershrinker.core.ui.widget.dialogs

import androidx.activity.compose.BackHandler
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.Save
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.style.TextAlign
import ru.tech.imageresizershrinker.core.resources.R
import ru.tech.imageresizershrinker.core.settings.presentation.provider.LocalSettingsState
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedAlertDialog
import ru.tech.imageresizershrinker.core.ui.widget.enhanced.EnhancedButton

Expand All @@ -39,39 +42,62 @@ fun ExitWithoutSavingDialog(
title: String = stringResource(R.string.image_not_saved),
icon: ImageVector = Icons.Outlined.Save
) {
EnhancedAlertDialog(
visible = visible,
onDismissRequest = onDismiss,
dismissButton = {
EnhancedButton(
containerColor = MaterialTheme.colorScheme.secondaryContainer,
onClick = {
onDismiss()
onExit()
val settingsState = LocalSettingsState.current

if (!settingsState.enableToolExitConfirmation) {
LaunchedEffect(visible) {
if (visible) onExit()
}
} else {
EnhancedAlertDialog(
visible = visible,
onDismissRequest = onDismiss,
dismissButton = {
EnhancedButton(
containerColor = MaterialTheme.colorScheme.secondaryContainer,
onClick = {
onDismiss()
onExit()
}
) {
Text(stringResource(R.string.exit))
}
) {
Text(stringResource(R.string.exit))
}
},
confirmButton = {
EnhancedButton(
onClick = onDismiss
) {
Text(stringResource(R.string.stay))
},
confirmButton = {
EnhancedButton(
onClick = onDismiss
) {
Text(stringResource(R.string.stay))
}
},
title = { Text(text = title) },
text = {
Text(
text = text,
textAlign = TextAlign.Center
)
},
icon = {
Icon(
imageVector = icon,
contentDescription = null
)
}
},
title = { Text(text = title) },
text = {
Text(
text = text,
textAlign = TextAlign.Center
)
},
icon = {
Icon(
imageVector = icon,
contentDescription = null
)
}
)
)
}
}

@Composable
fun ExitBackHandler(
enabled: Boolean = true,
onBack: () -> Unit
) {
val settingsState = LocalSettingsState.current

if (settingsState.enableToolExitConfirmation) {
BackHandler(
enabled = enabled,
onBack = onBack
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package ru.tech.imageresizershrinker.feature.crop.presentation


import android.net.Uri
import androidx.activity.compose.BackHandler
import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
Expand Down Expand Up @@ -385,9 +384,4 @@ fun CropContent(
onCancelLoading = component::cancelSaving,
canCancel = component.isSaving
)

BackHandler(
enabled = component.bitmap != null,
onBack = onBack
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package ru.tech.imageresizershrinker.feature.image_preview.presentation

import android.net.Uri
import androidx.activity.compose.BackHandler
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.animation.AnimatedContent
Expand Down Expand Up @@ -84,6 +83,7 @@ import ru.tech.imageresizershrinker.core.ui.utils.content_pickers.rememberImageP
import ru.tech.imageresizershrinker.core.ui.utils.helper.listFilesInDirectory
import ru.tech.imageresizershrinker.core.ui.utils.provider.LocalComponentActivity
import ru.tech.imageresizershrinker.core.ui.utils.provider.rememberLocalEssentials
import ru.tech.imageresizershrinker.core.ui.widget.dialogs.ExitBackHandler
import ru.tech.imageresizershrinker.core.ui.widget.dialogs.ExitWithoutSavingDialog
import ru.tech.imageresizershrinker.core.ui.widget.dialogs.LoadingDialog
import ru.tech.imageresizershrinker.core.ui.widget.dialogs.OneTimeImagePickingDialog
Expand Down Expand Up @@ -400,7 +400,10 @@ fun ImagePreviewContent(
onNavigate = component.onNavigate
)

BackHandler(enabled = !component.uris.isNullOrEmpty(), onBack = onBack)
ExitBackHandler(
enabled = !component.uris.isNullOrEmpty(),
onBack = onBack
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
package ru.tech.imageresizershrinker.feature.pdf_tools.presentation

import android.net.Uri
import androidx.activity.compose.BackHandler
import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.animation.AnimatedVisibility
Expand Down Expand Up @@ -84,6 +83,7 @@ import ru.tech.imageresizershrinker.core.ui.widget.controls.ScaleSmallImagesToLa
import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.ImageFormatSelector
import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.PresetSelector
import ru.tech.imageresizershrinker.core.ui.widget.controls.selection.QualitySelector
import ru.tech.imageresizershrinker.core.ui.widget.dialogs.ExitBackHandler
import ru.tech.imageresizershrinker.core.ui.widget.dialogs.ExitWithoutSavingDialog
import ru.tech.imageresizershrinker.core.ui.widget.dialogs.LoadingDialog
import ru.tech.imageresizershrinker.core.ui.widget.dialogs.OneTimeSaveLocationSelectionDialog
Expand Down Expand Up @@ -493,7 +493,7 @@ fun PdfToolsContent(
)
}

BackHandler(
ExitBackHandler(
enabled = component.haveChanges,
onBack = onBack
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,11 @@ internal class AndroidSettingsManager @Inject constructor(
out.toByteArray()
}

override suspend fun toggleEnableToolExitConfirmation() = toggle(
key = ENABLE_TOOL_EXIT_CONFIRMATION,
defaultValue = default.enableToolExitConfirmation
)

private fun MutablePreferences.toggle(
key: Preferences.Key<Boolean>,
defaultValue: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,9 @@ internal fun Preferences.toSettingsState(
default
),
hashingTypeForFilename = HashingType.fromString(this[CHECKSUM_TYPE_FOR_FILENAME]),
customFonts = this[CUSTOM_FONTS].toCustomFonts()
customFonts = this[CUSTOM_FONTS].toCustomFonts(),
enableToolExitConfirmation = this[ENABLE_TOOL_EXIT_CONFIRMATION]
?: default.enableToolExitConfirmation
)

private fun Set<String>?.toSettingGroupsInitialVisibility(
Expand Down
Loading

0 comments on commit 6d5f04a

Please sign in to comment.