store save folder locaiton persistently

This commit is contained in:
tibbi 2018-02-12 15:55:30 +01:00
parent 13d703fa52
commit a81e1db859
4 changed files with 43 additions and 32 deletions

View File

@ -37,11 +37,13 @@ class MainActivity : SimpleActivity(), CanvasListener {
private val FOLDER_NAME = "images"
private val FILE_NAME = "simple-draw.png"
private var curPath = ""
private var defaultPath = ""
private var defaultFilename = ""
private var defaultExtension = PNG
private var intentUri: Uri? = null
private var color = 0
private var strokeWidth = 0f
private var suggestedFileExtension = PNG
private var isEraserOn = false
private var isImageCaptureIntent = false
@ -56,6 +58,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
setBackgroundColor(config.canvasBackgroundColor)
setColor(config.brushColor)
defaultPath = config.lastSaveFolder
strokeWidth = config.brushSize
my_canvas.setStrokeWidth(strokeWidth)
@ -141,7 +144,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
}
private fun openFile() {
val path = if (isImageCaptureIntent) "" else curPath
val path = if (isImageCaptureIntent) "" else defaultPath
FilePickerDialog(this, path) {
openPath(it)
}
@ -174,7 +177,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
if (output != null && output is Uri) {
isImageCaptureIntent = true
intentUri = output
curPath = output.path
defaultPath = output.path
invalidateOptionsMenu()
}
}
@ -200,12 +203,12 @@ class MainActivity : SimpleActivity(), CanvasListener {
path.endsWith(".svg") -> {
my_canvas.mBackgroundBitmap = null
Svg.loadSvg(this, File(path), my_canvas)
suggestedFileExtension = SVG
defaultExtension = SVG
true
}
File(path).isImageSlow() -> {
my_canvas.drawBitmap(this, path)
suggestedFileExtension = JPG
defaultExtension = JPG
true
}
else -> {
@ -221,12 +224,12 @@ class MainActivity : SimpleActivity(), CanvasListener {
"svg", "image/svg+xml" -> {
my_canvas.mBackgroundBitmap = null
Svg.loadSvg(this, uri, my_canvas)
suggestedFileExtension = SVG
defaultExtension = SVG
true
}
"jpg", "jpeg", "png" -> {
my_canvas.drawBitmap(this, uri)
suggestedFileExtension = JPG
defaultExtension = JPG
true
}
else -> {
@ -255,7 +258,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
}
private fun confirmImage() {
val file = File(curPath)
val file = File(defaultPath)
if (intentUri?.scheme == "content") {
val outputStream = contentResolver.openOutputStream(intentUri)
saveToOutputStream(outputStream, file.getCompressionFormat())
@ -288,9 +291,12 @@ class MainActivity : SimpleActivity(), CanvasListener {
}
private fun saveImage() {
SaveImageDialog(this, suggestedFileExtension, curPath, my_canvas) { path, extension ->
curPath = path
suggestedFileExtension = extension
SaveImageDialog(this, defaultExtension, defaultPath, defaultFilename, my_canvas) { path, extension ->
defaultPath = File(path).parent
defaultFilename = path.getFilenameFromPath()
defaultFilename = defaultFilename.substring(0, defaultFilename.lastIndexOf("."))
defaultExtension = extension
config.lastSaveFolder = defaultPath
}
}
@ -329,8 +335,8 @@ class MainActivity : SimpleActivity(), CanvasListener {
private fun clearCanvas() {
my_canvas.clearCanvas()
suggestedFileExtension = PNG
curPath = ""
defaultExtension = PNG
defaultPath = ""
}
private fun pickColor() {
@ -345,7 +351,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
eraser.applyColorFilter(contrastColor)
redo.applyColorFilter(contrastColor)
my_canvas.updateBackgroundColor(pickedColor)
suggestedFileExtension = PNG
defaultExtension = PNG
}
private fun setColor(pickedColor: Int) {

View File

@ -15,26 +15,26 @@ import kotlinx.android.synthetic.main.dialog_save_image.view.*
import java.io.File
import java.io.OutputStream
class SaveImageDialog(val activity: SimpleActivity, val suggestedExtension: String, val curPath: String, val canvas: MyCanvas,
callback: (path: String, extension: String) -> Unit) {
class SaveImageDialog(val activity: SimpleActivity, val defaultExtension: String, val defaultPath: String, val defaultFilename: String,
val canvas: MyCanvas, callback: (path: String, extension: String) -> Unit) {
private val SIMPLE_DRAW = "Simple Draw"
init {
val initialFilename = getInitialFilename()
var realPath = if (curPath.isEmpty()) "${activity.internalStoragePath}/$SIMPLE_DRAW" else File(curPath).parent.trimEnd('/')
var folder = if (defaultPath.isEmpty()) "${activity.internalStoragePath}/$SIMPLE_DRAW" else defaultPath
val view = activity.layoutInflater.inflate(R.layout.dialog_save_image, null).apply {
save_image_filename.setText(initialFilename)
save_image_radio_group.check(when (suggestedExtension) {
save_image_radio_group.check(when (defaultExtension) {
JPG -> R.id.save_image_radio_jpg
SVG -> R.id.save_image_radio_svg
else -> R.id.save_image_radio_png
})
save_image_path.text = activity.humanizePath(realPath)
save_image_path.text = activity.humanizePath(folder)
save_image_path.setOnClickListener {
FilePickerDialog(activity, realPath, false, showFAB = true) {
FilePickerDialog(activity, folder, false, showFAB = true) {
save_image_path.text = activity.humanizePath(it)
realPath = it
folder = it
}
}
}
@ -58,7 +58,7 @@ class SaveImageDialog(val activity: SimpleActivity, val suggestedExtension: Stri
else -> JPG
}
val newFile = File(realPath, "$filename.$extension")
val newFile = File(folder, "$filename.$extension")
if (!newFile.name.isAValidFilename()) {
activity.toast(R.string.filename_invalid_characters)
return@setOnClickListener
@ -104,7 +104,7 @@ class SaveImageDialog(val activity: SimpleActivity, val suggestedExtension: Stri
}
private fun getInitialFilename(): String {
val defaultFilename = "image_${activity.getCurrentFormattedDateTime()}"
return if (curPath.isEmpty()) defaultFilename else curPath.getFilenameFromPath().substring(0, curPath.getFilenameFromPath().lastIndexOf("."))
val newFilename = "image_${activity.getCurrentFormattedDateTime()}"
return if (defaultFilename.isEmpty()) newFilename else defaultFilename
}
}

View File

@ -24,4 +24,8 @@ class Config(context: Context) : BaseConfig(context) {
var canvasBackgroundColor: Int
get() = prefs.getInt(CANVAS_BACKGROUND_COLOR, Color.WHITE)
set(canvasBackgroundColor) = prefs.edit().putInt(CANVAS_BACKGROUND_COLOR, canvasBackgroundColor).apply()
var lastSaveFolder: String
get() = prefs.getString(LAST_SAVE_FOLDER, "")
set(lastSaveFolder) = prefs.edit().putString(LAST_SAVE_FOLDER, lastSaveFolder).apply()
}

View File

@ -1,10 +1,11 @@
package com.simplemobiletools.draw.helpers
val BRUSH_COLOR = "brush_color"
val CANVAS_BACKGROUND_COLOR = "canvas_background_color"
val SHOW_BRUSH_SIZE = "show_brush_size"
val BRUSH_SIZE = "brush_size"
const val BRUSH_COLOR = "brush_color"
const val CANVAS_BACKGROUND_COLOR = "canvas_background_color"
const val SHOW_BRUSH_SIZE = "show_brush_size"
const val BRUSH_SIZE = "brush_size"
const val LAST_SAVE_FOLDER = "last_save_folder"
val PNG = "png"
val SVG = "svg"
val JPG = "jpg"
const val PNG = "png"
const val SVG = "svg"
const val JPG = "jpg"