From 8ba998d3ae393b5d18c259c037bfd56347517448 Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 12 Dec 2019 11:59:56 +0100 Subject: [PATCH] return both the resolution and path from the new dialog --- .../pro/activities/ViewPagerActivity.kt | 2 +- .../pro/dialogs/ResizeWithPathDialog.kt | 37 ++++++++++++++++--- 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt index 9e9b0cf5b..5de7555d3 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/ViewPagerActivity.kt @@ -921,7 +921,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View private fun resizeImage() { val currentPath = getCurrentPath() val originalSize = currentPath.getImageResolution() ?: return - ResizeWithPathDialog(this, originalSize, currentPath) { + ResizeWithPathDialog(this, originalSize, currentPath) { newSize, newPath -> } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/dialogs/ResizeWithPathDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/dialogs/ResizeWithPathDialog.kt index f3863943d..b377f6801 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/dialogs/ResizeWithPathDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/dialogs/ResizeWithPathDialog.kt @@ -4,15 +4,14 @@ import android.graphics.Point import android.widget.EditText import androidx.appcompat.app.AlertDialog import com.simplemobiletools.commons.activities.BaseSimpleActivity +import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.FilePickerDialog import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.gallery.pro.R import com.simplemobiletools.gallery.pro.extensions.config -import kotlinx.android.synthetic.main.dialog_resize_image.view.image_height -import kotlinx.android.synthetic.main.dialog_resize_image.view.image_width import kotlinx.android.synthetic.main.dialog_resize_image_with_path.view.* -class ResizeWithPathDialog(val activity: BaseSimpleActivity, val size: Point, val path: String, val callback: (newSize: Point) -> Unit) { +class ResizeWithPathDialog(val activity: BaseSimpleActivity, val size: Point, val path: String, val callback: (newSize: Point, newPath: String) -> Unit) { init { var realPath = path.getParentPath() val view = activity.layoutInflater.inflate(R.layout.dialog_resize_image_with_path, null).apply { @@ -84,8 +83,36 @@ class ResizeWithPathDialog(val activity: BaseSimpleActivity, val size: Point, va } val newSize = Point(getViewValue(widthView), getViewValue(heightView)) - callback(newSize) - dismiss() + + val filename = view.image_name.value + val extension = view.image_extension.value + if (filename.isEmpty()) { + activity.toast(R.string.filename_cannot_be_empty) + return@setOnClickListener + } + + if (extension.isEmpty()) { + activity.toast(R.string.extension_cannot_be_empty) + return@setOnClickListener + } + + val newFilename = "$filename.$extension" + val newPath = "${realPath.trimEnd('/')}/$newFilename" + if (!newFilename.isAValidFilename()) { + activity.toast(R.string.filename_invalid_characters) + return@setOnClickListener + } + + if (activity.getDoesFilePathExist(newPath)) { + val title = String.format(activity.getString(R.string.file_already_exists_overwrite), newFilename) + ConfirmationDialog(activity, title) { + callback(newSize, newPath) + dismiss() + } + } else { + callback(newSize, newPath) + dismiss() + } } } }