From 24ae1241ca64a1209770bc6d27e1397763550945 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 23 Oct 2017 12:47:45 +0200 Subject: [PATCH] allow saving files in the file editor --- app/build.gradle | 2 +- .../activities/ReadTextActivity.kt | 21 +++++ .../filemanager/dialogs/SaveAsDialog.kt | 83 +++++++++++++++++++ app/src/main/res/layout/dialog_save_as.xml | 48 +++++++++++ 4 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/SaveAsDialog.kt create mode 100644 app/src/main/res/layout/dialog_save_as.xml diff --git a/app/build.gradle b/app/build.gradle index b6af4f09..92e14680 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -37,7 +37,7 @@ android { } dependencies { - compile 'com.simplemobiletools:commons:2.31.0' + compile 'com.simplemobiletools:commons:2.31.1' compile 'com.bignerdranch.android:recyclerview-multiselect:0.2' compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/ReadTextActivity.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/ReadTextActivity.kt index e88d6352..4a532272 100644 --- a/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/ReadTextActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/activities/ReadTextActivity.kt @@ -3,14 +3,20 @@ package com.simplemobiletools.filemanager.activities import android.os.Bundle import android.view.Menu import android.view.MenuItem +import com.simplemobiletools.commons.extensions.getFileOutputStream +import com.simplemobiletools.commons.extensions.getRealPathFromURI +import com.simplemobiletools.commons.extensions.hideKeyboard import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE import com.simplemobiletools.filemanager.R +import com.simplemobiletools.filemanager.dialogs.SaveAsDialog import com.simplemobiletools.filemanager.extensions.config import kotlinx.android.synthetic.main.activity_read_text.* import java.io.File class ReadTextActivity : SimpleActivity() { + var filePath = "" + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_read_text) @@ -39,13 +45,28 @@ class ReadTextActivity : SimpleActivity() { } private fun saveText() { + if (filePath.isEmpty()) { + filePath = getRealPathFromURI(intent.data) ?: "" + } + SaveAsDialog(this, filePath) { + getFileOutputStream(File(it)) { + if (it != null) { + it.bufferedWriter().use { it.write(read_text_view.text.toString()) } + toast(R.string.file_saved) + hideKeyboard() + } else { + toast(R.string.unknown_error_occurred) + } + } + } } private fun checkIntent() { read_text_view.setTextColor(config.textColor) val uri = intent.data val text = if (uri.scheme == "file") { + filePath = uri.path File(uri.path).readText() } else { contentResolver.openInputStream(uri).bufferedReader().use { it.readText() } diff --git a/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/SaveAsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/SaveAsDialog.kt new file mode 100644 index 00000000..346bd923 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/filemanager/dialogs/SaveAsDialog.kt @@ -0,0 +1,83 @@ +package com.simplemobiletools.filemanager.dialogs + +import android.support.v7.app.AlertDialog +import android.view.LayoutInflater +import android.view.WindowManager +import com.simplemobiletools.commons.dialogs.ConfirmationDialog +import com.simplemobiletools.commons.dialogs.FilePickerDialog +import com.simplemobiletools.commons.extensions.* +import com.simplemobiletools.filemanager.R +import com.simplemobiletools.filemanager.activities.SimpleActivity +import kotlinx.android.synthetic.main.dialog_save_as.view.* +import java.io.File + +class SaveAsDialog(val activity: SimpleActivity, var path: String, val callback: (savePath: String) -> Unit) { + + init { + if (path.isEmpty()) { + path = "${activity.internalStoragePath}/${System.currentTimeMillis()}.txt" + } + + var realPath = File(path).parent.trimEnd('/') + val view = LayoutInflater.from(activity).inflate(R.layout.dialog_save_as, null).apply { + save_as_path.text = activity.humanizePath(realPath) + + val fullName = path.getFilenameFromPath() + val dotAt = fullName.lastIndexOf(".") + var name = fullName + + if (dotAt > 0) { + name = fullName.substring(0, dotAt) + val extension = fullName.substring(dotAt + 1) + save_as_extension.setText(extension) + } + + save_as_name.setText(name) + save_as_path.setOnClickListener { + FilePickerDialog(activity, realPath, false, false, true) { + save_as_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_as_name.value + val extension = view.save_as_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 newFile = File(realPath, "$filename.$extension") + if (!newFile.name.isAValidFilename()) { + activity.toast(R.string.filename_invalid_characters) + return@setOnClickListener + } + + if (newFile.exists()) { + val title = String.format(activity.getString(R.string.file_already_exists_overwrite), newFile.name) + ConfirmationDialog(activity, title) { + callback(newFile.absolutePath) + dismiss() + } + } else { + callback(newFile.absolutePath) + dismiss() + } + }) + } + } +} diff --git a/app/src/main/res/layout/dialog_save_as.xml b/app/src/main/res/layout/dialog_save_as.xml new file mode 100644 index 00000000..c40bd92a --- /dev/null +++ b/app/src/main/res/layout/dialog_save_as.xml @@ -0,0 +1,48 @@ + + + + + + + + + + + + + +