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 FOLDER_NAME = "images"
private val FILE_NAME = "simple-draw.png" 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 intentUri: Uri? = null
private var color = 0 private var color = 0
private var strokeWidth = 0f private var strokeWidth = 0f
private var suggestedFileExtension = PNG
private var isEraserOn = false private var isEraserOn = false
private var isImageCaptureIntent = false private var isImageCaptureIntent = false
@ -56,6 +58,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
setBackgroundColor(config.canvasBackgroundColor) setBackgroundColor(config.canvasBackgroundColor)
setColor(config.brushColor) setColor(config.brushColor)
defaultPath = config.lastSaveFolder
strokeWidth = config.brushSize strokeWidth = config.brushSize
my_canvas.setStrokeWidth(strokeWidth) my_canvas.setStrokeWidth(strokeWidth)
@ -141,7 +144,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
} }
private fun openFile() { private fun openFile() {
val path = if (isImageCaptureIntent) "" else curPath val path = if (isImageCaptureIntent) "" else defaultPath
FilePickerDialog(this, path) { FilePickerDialog(this, path) {
openPath(it) openPath(it)
} }
@ -174,7 +177,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
if (output != null && output is Uri) { if (output != null && output is Uri) {
isImageCaptureIntent = true isImageCaptureIntent = true
intentUri = output intentUri = output
curPath = output.path defaultPath = output.path
invalidateOptionsMenu() invalidateOptionsMenu()
} }
} }
@ -200,12 +203,12 @@ class MainActivity : SimpleActivity(), CanvasListener {
path.endsWith(".svg") -> { path.endsWith(".svg") -> {
my_canvas.mBackgroundBitmap = null my_canvas.mBackgroundBitmap = null
Svg.loadSvg(this, File(path), my_canvas) Svg.loadSvg(this, File(path), my_canvas)
suggestedFileExtension = SVG defaultExtension = SVG
true true
} }
File(path).isImageSlow() -> { File(path).isImageSlow() -> {
my_canvas.drawBitmap(this, path) my_canvas.drawBitmap(this, path)
suggestedFileExtension = JPG defaultExtension = JPG
true true
} }
else -> { else -> {
@ -221,12 +224,12 @@ class MainActivity : SimpleActivity(), CanvasListener {
"svg", "image/svg+xml" -> { "svg", "image/svg+xml" -> {
my_canvas.mBackgroundBitmap = null my_canvas.mBackgroundBitmap = null
Svg.loadSvg(this, uri, my_canvas) Svg.loadSvg(this, uri, my_canvas)
suggestedFileExtension = SVG defaultExtension = SVG
true true
} }
"jpg", "jpeg", "png" -> { "jpg", "jpeg", "png" -> {
my_canvas.drawBitmap(this, uri) my_canvas.drawBitmap(this, uri)
suggestedFileExtension = JPG defaultExtension = JPG
true true
} }
else -> { else -> {
@ -255,7 +258,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
} }
private fun confirmImage() { private fun confirmImage() {
val file = File(curPath) val file = File(defaultPath)
if (intentUri?.scheme == "content") { if (intentUri?.scheme == "content") {
val outputStream = contentResolver.openOutputStream(intentUri) val outputStream = contentResolver.openOutputStream(intentUri)
saveToOutputStream(outputStream, file.getCompressionFormat()) saveToOutputStream(outputStream, file.getCompressionFormat())
@ -288,9 +291,12 @@ class MainActivity : SimpleActivity(), CanvasListener {
} }
private fun saveImage() { private fun saveImage() {
SaveImageDialog(this, suggestedFileExtension, curPath, my_canvas) { path, extension -> SaveImageDialog(this, defaultExtension, defaultPath, defaultFilename, my_canvas) { path, extension ->
curPath = path defaultPath = File(path).parent
suggestedFileExtension = extension 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() { private fun clearCanvas() {
my_canvas.clearCanvas() my_canvas.clearCanvas()
suggestedFileExtension = PNG defaultExtension = PNG
curPath = "" defaultPath = ""
} }
private fun pickColor() { private fun pickColor() {
@ -345,7 +351,7 @@ class MainActivity : SimpleActivity(), CanvasListener {
eraser.applyColorFilter(contrastColor) eraser.applyColorFilter(contrastColor)
redo.applyColorFilter(contrastColor) redo.applyColorFilter(contrastColor)
my_canvas.updateBackgroundColor(pickedColor) my_canvas.updateBackgroundColor(pickedColor)
suggestedFileExtension = PNG defaultExtension = PNG
} }
private fun setColor(pickedColor: Int) { 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.File
import java.io.OutputStream import java.io.OutputStream
class SaveImageDialog(val activity: SimpleActivity, val suggestedExtension: String, val curPath: String, val canvas: MyCanvas, class SaveImageDialog(val activity: SimpleActivity, val defaultExtension: String, val defaultPath: String, val defaultFilename: String,
callback: (path: String, extension: String) -> Unit) { val canvas: MyCanvas, callback: (path: String, extension: String) -> Unit) {
private val SIMPLE_DRAW = "Simple Draw" private val SIMPLE_DRAW = "Simple Draw"
init { init {
val initialFilename = getInitialFilename() 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 { val view = activity.layoutInflater.inflate(R.layout.dialog_save_image, null).apply {
save_image_filename.setText(initialFilename) 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 JPG -> R.id.save_image_radio_jpg
SVG -> R.id.save_image_radio_svg SVG -> R.id.save_image_radio_svg
else -> R.id.save_image_radio_png 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 { save_image_path.setOnClickListener {
FilePickerDialog(activity, realPath, false, showFAB = true) { FilePickerDialog(activity, folder, false, showFAB = true) {
save_image_path.text = activity.humanizePath(it) 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 else -> JPG
} }
val newFile = File(realPath, "$filename.$extension") val newFile = File(folder, "$filename.$extension")
if (!newFile.name.isAValidFilename()) { if (!newFile.name.isAValidFilename()) {
activity.toast(R.string.filename_invalid_characters) activity.toast(R.string.filename_invalid_characters)
return@setOnClickListener return@setOnClickListener
@ -104,7 +104,7 @@ class SaveImageDialog(val activity: SimpleActivity, val suggestedExtension: Stri
} }
private fun getInitialFilename(): String { private fun getInitialFilename(): String {
val defaultFilename = "image_${activity.getCurrentFormattedDateTime()}" val newFilename = "image_${activity.getCurrentFormattedDateTime()}"
return if (curPath.isEmpty()) defaultFilename else curPath.getFilenameFromPath().substring(0, curPath.getFilenameFromPath().lastIndexOf(".")) return if (defaultFilename.isEmpty()) newFilename else defaultFilename
} }
} }

View File

@ -24,4 +24,8 @@ class Config(context: Context) : BaseConfig(context) {
var canvasBackgroundColor: Int var canvasBackgroundColor: Int
get() = prefs.getInt(CANVAS_BACKGROUND_COLOR, Color.WHITE) get() = prefs.getInt(CANVAS_BACKGROUND_COLOR, Color.WHITE)
set(canvasBackgroundColor) = prefs.edit().putInt(CANVAS_BACKGROUND_COLOR, canvasBackgroundColor).apply() 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 package com.simplemobiletools.draw.helpers
val BRUSH_COLOR = "brush_color" const val BRUSH_COLOR = "brush_color"
val CANVAS_BACKGROUND_COLOR = "canvas_background_color" const val CANVAS_BACKGROUND_COLOR = "canvas_background_color"
val SHOW_BRUSH_SIZE = "show_brush_size" const val SHOW_BRUSH_SIZE = "show_brush_size"
val BRUSH_SIZE = "brush_size" const val BRUSH_SIZE = "brush_size"
const val LAST_SAVE_FOLDER = "last_save_folder"
val PNG = "png" const val PNG = "png"
val SVG = "svg" const val SVG = "svg"
val JPG = "jpg" const val JPG = "jpg"