move saving dialog in a separate class and allow changing the folder

This commit is contained in:
tibbi 2017-04-08 23:34:25 +02:00
parent 8969abd442
commit fcac79b308
3 changed files with 128 additions and 105 deletions

View File

@ -5,44 +5,35 @@ import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.drawable.ColorDrawable
import android.media.MediaScannerConnection
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.support.v4.app.ActivityCompat
import android.support.v4.content.FileProvider
import android.support.v7.app.AlertDialog
import android.view.Menu
import android.view.MenuItem
import android.widget.EditText
import android.widget.RadioGroup
import android.widget.SeekBar
import com.simplemobiletools.commons.dialogs.ColorPickerDialog
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.hasWriteStoragePermission
import com.simplemobiletools.commons.extensions.storeStoragePaths
import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.commons.extensions.value
import com.simplemobiletools.commons.helpers.LICENSE_KOTLIN
import com.simplemobiletools.draw.BuildConfig
import com.simplemobiletools.draw.MyCanvas
import com.simplemobiletools.draw.R
import com.simplemobiletools.draw.Svg
import com.simplemobiletools.draw.dialogs.SaveImageDialog
import com.simplemobiletools.draw.extensions.config
import kotlinx.android.synthetic.main.activity_main.*
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
private val FOLDER_NAME = "images"
private val FILE_NAME = "simple-draw.png"
private val SAVE_FOLDER_NAME = "Simple Draw"
private val STORAGE_PERMISSION = 1
private var curFileName: String? = null
private var curExtensionId = 0
private var curPath = ""
private var color = 0
private var strokeWidth = 0f
@ -61,6 +52,7 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
color_picker.setOnClickListener { pickColor() }
undo.setOnClickListener { my_canvas.undo() }
storeStoragePaths()
}
override fun onResume() {
@ -131,88 +123,9 @@ class MainActivity : SimpleActivity(), MyCanvas.PathsChangedListener {
}
private fun saveImage() {
val saveFileView = layoutInflater.inflate(R.layout.dialog_save_file, null)
val builder = AlertDialog.Builder(this)
builder.setTitle(resources.getString(R.string.save_file))
val fileNameET = saveFileView.findViewById(R.id.file_name) as EditText
fileNameET.setText(curFileName)
val fileExtensionRG = saveFileView.findViewById(R.id.extension_radio_group) as RadioGroup
if (curExtensionId != 0) {
fileExtensionRG.check(curExtensionId)
SaveImageDialog(this, curPath, my_canvas) {
curPath = it
}
builder.setView(saveFileView)
builder.setPositiveButton(R.string.ok, null)
builder.setNegativeButton(R.string.cancel, null)
builder.create().apply {
show()
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val fileName = fileNameET.value
if (!fileName.isEmpty()) {
val extension = when (fileExtensionRG.checkedRadioButtonId) {
R.id.extension_radio_svg -> ".svg"
else -> ".png"
}
if (saveFile(fileName, extension)) {
curFileName = fileName
curExtensionId = fileExtensionRG.checkedRadioButtonId
toast(R.string.saving_ok)
dismiss()
} else {
toast(R.string.saving_error)
}
} else {
toast(R.string.enter_file_name)
}
}
}
}
private fun saveFile(fileName: String, extension: String): Boolean {
val path = Environment.getExternalStorageDirectory().toString()
val directory = File(path, SAVE_FOLDER_NAME)
if (!directory.exists()) {
if (!directory.mkdir()) {
return false
}
}
val file = File(directory, fileName + extension)
when (extension) {
".png" -> {
var out: FileOutputStream? = null
try {
out = FileOutputStream(file)
my_canvas.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out)
MediaScannerConnection.scanFile(applicationContext, arrayOf(file.absolutePath), null, null)
} catch (e: Exception) {
return false
} finally {
try {
out?.close()
} catch (e: IOException) {
}
}
}
".svg" -> {
try {
Svg.saveSvg(file, my_canvas)
} catch (e: Exception) {
return false
}
}
else -> return false
}
return true
}
private fun shareImage() {

View File

@ -0,0 +1,94 @@
package com.simplemobiletools.draw.dialogs
import android.graphics.Bitmap
import android.support.v7.app.AlertDialog
import android.view.WindowManager
import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.draw.MyCanvas
import com.simplemobiletools.draw.R
import com.simplemobiletools.draw.Svg
import com.simplemobiletools.draw.activities.SimpleActivity
import kotlinx.android.synthetic.main.dialog_save_image.view.*
import java.io.File
import java.io.FileOutputStream
class SaveImageDialog(val activity: SimpleActivity, val curPath: String, val canvas: MyCanvas, callback: (path: String) -> Unit) {
private val PNG = "png"
private val SVG = "svg"
private val SIMPLE_DRAW = "Simple Draw"
init {
val defaultFilename = "image_${System.currentTimeMillis() / 1000}"
val initialFilename = if (curPath.isEmpty()) defaultFilename else curPath.getFilenameFromPath().substring(0, curPath.getFilenameFromPath().lastIndexOf("."))
var realPath = if (curPath.isEmpty()) "${activity.internalStoragePath}/$SIMPLE_DRAW" else File(curPath).parent.trimEnd('/')
val view = activity.layoutInflater.inflate(R.layout.dialog_save_image, null).apply {
save_image_filename.setText(initialFilename)
save_image_radio_group.check(if (curPath.endsWith(SVG)) R.id.save_image_radio_svg else R.id.save_image_radio_png)
save_image_path.text = activity.humanizePath(realPath)
save_image_path.setOnClickListener {
FilePickerDialog(activity, realPath, false, showFAB = true) {
save_image_path.text = activity.humanizePath(it)
realPath = it
}
}
}
AlertDialog.Builder(activity)
.setPositiveButton(R.string.ok, null)
.setNegativeButton(R.string.cancel, null)
.create().apply {
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
activity.setupDialogStuff(view, this, R.string.save_as)
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener({
val filename = view.save_image_filename.value
if (filename.isEmpty()) {
activity.toast(R.string.filename_cannot_be_empty)
return@setOnClickListener
}
val extension = if (view.save_image_radio_group.checkedRadioButtonId == R.id.save_image_radio_svg) SVG else PNG
val newFile = File(realPath, "$filename.$extension")
if (!newFile.name.isAValidFilename()) {
activity.toast(R.string.filename_invalid_characters)
return@setOnClickListener
}
if (saveFile(newFile)) {
activity.toast(R.string.saving_ok)
callback(newFile.absolutePath)
dismiss()
} else {
activity.toast(R.string.saving_error)
}
})
}
}
private fun saveFile(file: File): Boolean {
if (!file.parentFile.exists()) {
if (!file.parentFile.mkdir()) {
return false
}
}
when (file.extension) {
PNG -> {
var out: FileOutputStream? = null
try {
out = FileOutputStream(file)
canvas.getBitmap().compress(Bitmap.CompressFormat.PNG, 100, out)
activity.scanFile(file) {}
} catch (e: Exception) {
return false
} finally {
out?.close()
}
}
SVG -> Svg.saveSvg(file, canvas)
}
return true
}
}

View File

@ -1,44 +1,60 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/save_image_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/file_name_label"
android:id="@+id/save_image_path_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/file_name"/>
android:text="@string/path"
android:textSize="@dimen/smaller_text_size"/>
<com.simplemobiletools.commons.views.MyEditText
android:id="@+id/file_name"
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/save_image_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
android:layout_marginLeft="@dimen/activity_margin"
android:paddingRight="@dimen/small_margin"
android:paddingTop="@dimen/small_margin"/>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/save_image_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/filename"
android:textSize="@dimen/smaller_text_size"/>
<com.simplemobiletools.commons.views.MyEditText
android:id="@+id/save_image_filename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/file_name_label"
android:layout_marginBottom="@dimen/activity_margin"
android:singleLine="true"
android:textCursorDrawable="@null"/>
<RadioGroup
android:id="@+id/extension_radio_group"
android:id="@+id/save_image_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/file_name"
android:checkedButton="@+id/extension_radio_png">
android:checkedButton="@+id/save_image_radio_png">
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/extension_radio_png"
android:id="@+id/save_image_radio_png"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=".png"/>
<com.simplemobiletools.commons.views.MyCompatRadioButton
android:id="@+id/extension_radio_svg"
android:id="@+id/save_image_radio_svg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=".svg"/>
</RadioGroup>
</RelativeLayout>
</LinearLayout>