Append to file but before the extension

This commit is contained in:
Benoit Marty 2020-11-05 15:40:12 +01:00
parent 15ad351579
commit b7a1f96294
3 changed files with 22 additions and 2 deletions

View File

@ -23,6 +23,7 @@ import androidx.core.net.toUri
import androidx.fragment.app.Fragment
import com.yalantis.ucrop.UCrop
import im.vector.app.R
import im.vector.app.core.extensions.insertBeforeLast
import im.vector.app.core.extensions.registerStartForActivityResult
import im.vector.app.core.resources.ColorProvider
import im.vector.app.core.utils.PERMISSIONS_FOR_TAKING_PHOTO
@ -86,7 +87,7 @@ class GalleryOrCameraDialogHelper(
}
private fun startUCrop(image: MultiPickerImageType) {
val destinationFile = File(activity.cacheDir, "${image.displayName}_e_${System.currentTimeMillis()}")
val destinationFile = File(activity.cacheDir, image.displayName.insertBeforeLast("_e_${System.currentTimeMillis()}"))
val uri = image.contentUri
createUCropWithDefaultSettings(colorProvider, uri, destinationFile.toUri(), fragment.getString(R.string.rotate_and_crop_screen_title))
.withAspectRatio(1f, 1f)

View File

@ -48,3 +48,21 @@ fun CharSequence.isMsisdn(): Boolean {
false
}
}
/**
* Useful to append a String at the end of a filename but before the extension if any
* Ex:
* - "file.txt".insertBeforeLast("_foo") will return "file_foo.txt"
* - "file".insertBeforeLast("_foo") will return "file_foo"
* - "fi.le.txt".insertBeforeLast("_foo") will return "fi.le_foo.txt"
* - null.insertBeforeLast("_foo") will return "_foo"
*/
fun String?.insertBeforeLast(insert: String, delimiter: String = ".") : String {
if (this == null) return insert
val idx = lastIndexOf(delimiter)
return if (idx == -1) {
this + insert
} else {
replaceRange(idx, idx, insert)
}
}

View File

@ -38,6 +38,7 @@ import com.airbnb.mvrx.withState
import com.yalantis.ucrop.UCrop
import im.vector.app.R
import im.vector.app.core.extensions.cleanup
import im.vector.app.core.extensions.insertBeforeLast
import im.vector.app.core.extensions.registerStartForActivityResult
import im.vector.app.core.platform.VectorBaseFragment
import im.vector.app.core.resources.ColorProvider
@ -170,7 +171,7 @@ class AttachmentsPreviewFragment @Inject constructor(
private fun handleEditAction() = withState(viewModel) {
val currentAttachment = it.attachments.getOrNull(it.currentAttachmentIndex) ?: return@withState
val destinationFile = File(requireContext().cacheDir, "${currentAttachment.name}_edited_image_${System.currentTimeMillis()}")
val destinationFile = File(requireContext().cacheDir, currentAttachment.name.insertBeforeLast("_edited_image_${System.currentTimeMillis()}"))
val uri = currentAttachment.queryUri
createUCropWithDefaultSettings(colorProvider, uri, destinationFile.toUri(), currentAttachment.name)
.getIntent(requireContext())