fix #82, notify the user when overwriting an existing file

This commit is contained in:
tibbi 2018-02-26 23:35:23 +01:00
parent c66256fbae
commit d2cec078d3
2 changed files with 37 additions and 43 deletions

View File

@ -305,15 +305,38 @@ class MainActivity : SimpleActivity(), CanvasListener {
}
private fun saveImage() {
SaveImageDialog(this, defaultExtension, defaultPath, defaultFilename, my_canvas) { path, extension ->
defaultPath = File(path).parent
defaultFilename = path.getFilenameFromPath()
SaveImageDialog(this, defaultExtension, defaultPath, defaultFilename) {
saveFile(it)
defaultPath = it.getParentPath()
defaultFilename = it.getFilenameFromPath()
defaultFilename = defaultFilename.substring(0, defaultFilename.lastIndexOf("."))
defaultExtension = extension
defaultExtension = it.getFilenameExtension()
config.lastSaveFolder = defaultPath
}
}
private fun saveFile(path: String) {
when (path.getFilenameExtension()) {
SVG -> Svg.saveSvg(this, path, my_canvas)
else -> saveImageFile(path)
}
scanPath(path) {}
}
private fun saveImageFile(path: String) {
val fileDirItem = FileDirItem(path, path.getFilenameFromPath())
getFileOutputStream(fileDirItem, true) {
writeToOutputStream(path, it!!)
toast(R.string.file_saved)
}
}
private fun writeToOutputStream(path: String, out: OutputStream) {
out.use {
my_canvas.getBitmap().compress(path.getCompressionFormat(), 70, out)
}
}
private fun shareImage() {
getImagePath(my_canvas.getBitmap()) {
if (it != null) {

View File

@ -2,22 +2,18 @@ package com.simplemobiletools.draw.dialogs
import android.support.v7.app.AlertDialog
import android.view.WindowManager
import com.simplemobiletools.commons.dialogs.ConfirmationDialog
import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.models.FileDirItem
import com.simplemobiletools.draw.R
import com.simplemobiletools.draw.activities.SimpleActivity
import com.simplemobiletools.draw.helpers.JPG
import com.simplemobiletools.draw.helpers.PNG
import com.simplemobiletools.draw.helpers.SVG
import com.simplemobiletools.draw.models.Svg
import com.simplemobiletools.draw.views.MyCanvas
import kotlinx.android.synthetic.main.dialog_save_image.view.*
import java.io.File
import java.io.OutputStream
class SaveImageDialog(val activity: SimpleActivity, val defaultExtension: String, val defaultPath: String, val defaultFilename: String,
val canvas: MyCanvas, callback: (path: String, extension: String) -> Unit) {
callback: (savePath: String) -> Unit) {
private val SIMPLE_DRAW = "Simple Draw"
init {
@ -65,46 +61,21 @@ class SaveImageDialog(val activity: SimpleActivity, val defaultExtension: String
return@setOnClickListener
}
if (saveFile(newPath)) {
callback(newPath, extension)
if (activity.getDoesFilePathExist(newPath)) {
val title = String.format(activity.getString(R.string.file_already_exists_overwrite), newPath.getFilenameFromPath())
ConfirmationDialog(activity, title) {
callback(newPath)
dismiss()
}
} else {
activity.toast(R.string.unknown_error_occurred)
callback(newPath)
dismiss()
}
}
}
}
}
private fun saveFile(path: String): Boolean {
if (!activity.getDoesFilePathExist(path.getParentPath())) {
if (!File(path).parentFile.mkdir()) {
return false
}
}
when (path.getFilenameExtension()) {
SVG -> Svg.saveSvg(activity, path, canvas)
else -> saveImageFile(path)
}
activity.scanPath(path) {}
return true
}
private fun saveImageFile(path: String) {
val fileDirItem = FileDirItem(path, path.getFilenameFromPath())
activity.getFileOutputStream(fileDirItem, true) {
writeToOutputStream(path, it!!)
activity.toast(R.string.file_saved)
}
}
private fun writeToOutputStream(path: String, out: OutputStream) {
out.use {
canvas.getBitmap().compress(path.getCompressionFormat(), 70, out)
}
}
private fun getInitialFilename(): String {
val newFilename = "image_${activity.getCurrentFormattedDateTime()}"
return if (defaultFilename.isEmpty()) newFilename else defaultFilename