Skip to content

Commit

Permalink
Added ability To set name while saving single image by #1308
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Sep 15, 2024
1 parent cfc4714 commit 73a847d
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,21 @@ internal class AndroidFileController @Inject constructor(
)
}
} else {
(oneTimeSaveLocationUri ?: settingsState.saveFolderUri).takeIf {
val documentFile: DocumentFile?
val treeUri = (oneTimeSaveLocationUri ?: settingsState.saveFolderUri).takeIf {
!it.isNullOrEmpty()
}?.let { treeUri ->
val hasDir: Boolean = runCatching {
}

if (treeUri != null) {
documentFile = runCatching {
treeUri.toUri().let {
DocumentFile.fromTreeUri(context, it)
}?.exists()
}.getOrNull() == true
if (DocumentFile.isDocumentUri(context, it)) {
DocumentFile.fromSingleUri(context, it)
} else DocumentFile.fromTreeUri(context, it)
}
}.getOrNull()

if (!hasDir) {
if (documentFile?.exists() == false && documentFile.isDirectory || documentFile == null) {
if (oneTimeSaveLocationUri == null) {
settingsManager.setSaveFolderUri(null)
} else {
Expand All @@ -255,6 +260,8 @@ internal class AndroidFileController @Inject constructor(
)
)
}
} else {
documentFile = null
}

var initialExif: ExifInterface? = null
Expand All @@ -270,10 +277,8 @@ internal class AndroidFileController @Inject constructor(
)
} else saveTarget

val savingFolder = context.getSavingFolder(
treeUri = (oneTimeSaveLocationUri ?: settingsState.saveFolderUri)?.takeIf {
it.isNotEmpty()
}?.toUri(),
val savingFolder = getSavingFolder(
treeUri = treeUri?.toUri(),
saveTarget = newSaveTarget
)

Expand All @@ -293,35 +298,39 @@ internal class AndroidFileController @Inject constructor(
val filename = newSaveTarget.filename ?: ""

oneTimeSaveLocationUri?.let {
val currentLocation =
settingsState.oneTimeSaveLocations.find { it.uri == oneTimeSaveLocationUri }

settingsManager.setOneTimeSaveLocations(
currentLocation?.let {
settingsState.oneTimeSaveLocations.toMutableList().apply {
remove(currentLocation)
add(
currentLocation.copy(
uri = oneTimeSaveLocationUri,
date = System.currentTimeMillis(),
count = currentLocation.count + 1
if (documentFile?.isDirectory == true) {
val currentLocation =
settingsState.oneTimeSaveLocations.find { it.uri == oneTimeSaveLocationUri }

settingsManager.setOneTimeSaveLocations(
currentLocation?.let {
settingsState.oneTimeSaveLocations.toMutableList().apply {
remove(currentLocation)
add(
currentLocation.copy(
uri = oneTimeSaveLocationUri,
date = System.currentTimeMillis(),
count = currentLocation.count + 1
)
)
}
} ?: settingsState.oneTimeSaveLocations.plus(
OneTimeSaveLocation(
uri = oneTimeSaveLocationUri,
date = System.currentTimeMillis(),
count = 1
)
}
} ?: settingsState.oneTimeSaveLocations.plus(
OneTimeSaveLocation(
uri = oneTimeSaveLocationUri,
date = System.currentTimeMillis(),
count = 1
)
)
)
}
}


return@withContext SaveResult.Success(
message = if (savingPath.isNotEmpty()) {
if (filename.isNotEmpty()) {
val isFile = documentFile?.isDirectory != true
if (isFile) {
context.getString(R.string.saved_to_custom)
} else if (filename.isNotEmpty()) {
context.getString(
R.string.saved_to,
savingPath,
Expand Down Expand Up @@ -387,7 +396,6 @@ internal class AndroidFileController @Inject constructor(

private data class SavingFolder(
val outputStream: OutputStream? = null,
val file: File? = null,
val fileUri: Uri? = null
)

Expand Down Expand Up @@ -593,11 +601,11 @@ internal class AndroidFileController @Inject constructor(
}.getOrNull()
}

private suspend fun Context.getSavingFolder(
private suspend fun getSavingFolder(
treeUri: Uri?,
saveTarget: SaveTarget
): SavingFolder = withContext(defaultDispatcher) {
if (treeUri == null) {
if (treeUri == null || DocumentFile.isDocumentUri(context, treeUri)) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
val type = saveTarget.mimeType
val path = "${Environment.DIRECTORY_DOCUMENTS}/ResizedImages"
Expand All @@ -612,13 +620,13 @@ internal class AndroidFileController @Inject constructor(
path
)
}
val imageUri = contentResolver.insert(
val imageUri = context.contentResolver.insert(
MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY),
contentValues
)

SavingFolder(
outputStream = imageUri?.let { contentResolver.openOutputStream(it) },
outputStream = imageUri?.let { context.contentResolver.openOutputStream(it) },
fileUri = imageUri
)
} else {
Expand All @@ -636,19 +644,23 @@ internal class AndroidFileController @Inject constructor(
)
}
} else {
val documentFile = DocumentFile.fromTreeUri(this@getSavingFolder, treeUri)
val documentFile = if (DocumentFile.isDocumentUri(context, treeUri)) {
DocumentFile.fromSingleUri(context, treeUri)
} else DocumentFile.fromTreeUri(context, treeUri)

if (documentFile?.exists() == false || documentFile == null) {
throw NoSuchFileException(File(treeUri.toString()))
}

val file =
documentFile.createFile(saveTarget.mimeType, saveTarget.filename!!)
val fileUri = try {
documentFile.createFile(saveTarget.mimeType, saveTarget.filename!!)!!.uri
} catch (_: UnsupportedOperationException) {
documentFile.uri
}

val imageUri = file!!.uri
SavingFolder(
outputStream = contentResolver.openOutputStream(imageUri),
fileUri = imageUri
outputStream = context.contentResolver.openOutputStream(fileUri),
fileUri = fileUri
)
}
}
Expand Down
3 changes: 3 additions & 0 deletions core/resources/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1403,4 +1403,7 @@
<string name="jitter">Jitter</string>
<string name="domain_warp">Domain Warp</string>
<string name="alignment">Alignment</string>
<string name="select_filename">Select Filename</string>
<string name="select_filename_sub">Select location and filename which are will be used to save current image</string>
<string name="saved_to_custom">Saved to folder with custom name</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import androidx.compose.foundation.verticalScroll
import androidx.compose.material.ExperimentalMaterialApi
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.CreateNewFolder
import androidx.compose.material.icons.outlined.DriveFileRenameOutline
import androidx.compose.material.icons.outlined.SaveAs
import androidx.compose.material.icons.rounded.DeleteOutline
import androidx.compose.material.icons.rounded.Folder
Expand Down Expand Up @@ -69,6 +70,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.net.toUri
import kotlinx.coroutines.launch
import ru.tech.imageresizershrinker.core.domain.image.model.ImageFormat
import ru.tech.imageresizershrinker.core.resources.R
import ru.tech.imageresizershrinker.core.settings.domain.model.OneTimeSaveLocation
import ru.tech.imageresizershrinker.core.settings.presentation.provider.LocalSettingsState
Expand All @@ -93,7 +95,8 @@ import java.util.Locale
@Composable
fun OneTimeSaveLocationSelectionDialog(
onDismiss: () -> Unit,
onSaveRequest: ((String?) -> Unit)?
onSaveRequest: ((String?) -> Unit)?,
formatForFilenameSelection: ImageFormat? = null
) {
val settingsState = LocalSettingsState.current
var tempSelectedSaveFolderUri by rememberSaveable {
Expand Down Expand Up @@ -336,6 +339,38 @@ fun OneTimeSaveLocationSelectionDialog(
.padding(horizontal = 4.dp, vertical = 2.dp),
color = MaterialTheme.colorScheme.surfaceContainer
)

if (formatForFilenameSelection != null) {
val createLauncher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.CreateDocument(formatForFilenameSelection.mimeType),
onResult = { uri ->
uri?.let {
onSaveRequest?.invoke(it.toString())
onDismiss()
}
}
)
val imageString = stringResource(R.string.image)
PreferenceItem(
title = stringResource(id = R.string.select_filename),
subtitle = stringResource(id = R.string.select_filename_sub),
startIcon = Icons.Outlined.DriveFileRenameOutline,
shape = ContainerShapeDefaults.defaultShape,
titleFontStyle = LocalTextStyle.current.copy(
fontSize = 14.sp,
fontWeight = FontWeight.Medium,
lineHeight = 16.sp,
textAlign = TextAlign.Start
),
onClick = {
createLauncher.launch("$imageString.${formatForFilenameSelection.extension}")
},
modifier = Modifier
.fillMaxWidth()
.padding(horizontal = 4.dp, vertical = 2.dp),
color = MaterialTheme.colorScheme.surfaceContainer
)
}
}
},
modifier = Modifier.alertDialogBorder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,8 @@ fun SingleEditContent(
if (showFolderSelectionDialog) {
OneTimeSaveLocationSelectionDialog(
onDismiss = { showFolderSelectionDialog = false },
onSaveRequest = saveBitmap
onSaveRequest = saveBitmap,
formatForFilenameSelection = imageInfo.imageFormat
)
}
},
Expand Down

0 comments on commit 73a847d

Please sign in to comment.