fix #109, allow exporting all notes at once

This commit is contained in:
tibbi 2018-01-26 18:15:19 +01:00
parent 98a37986a9
commit be4d06f04c
7 changed files with 150 additions and 15 deletions

View File

@ -38,7 +38,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
private var mAdapter: NotesPagerAdapter? = null
lateinit var mCurrentNote: Note
lateinit var mNotes: List<Note>
private var mNotes = ArrayList<Note>()
private var noteViewWithTextSelected: MyEditText? = null
private var wasInit = false
@ -110,6 +110,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
findItem(R.id.rename_note).isVisible = shouldBeVisible
findItem(R.id.open_note).isVisible = shouldBeVisible
findItem(R.id.delete_note).isVisible = shouldBeVisible
findItem(R.id.export_all_notes).isVisible = shouldBeVisible
}
pager_title_strip.beVisibleIf(shouldBeVisible)
@ -125,6 +126,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
R.id.share -> shareText()
R.id.open_file -> tryOpenFile()
R.id.export_as_file -> tryExportAsFile()
R.id.export_all_notes -> tryExportAllNotes()
R.id.delete_note -> displayDeleteNotePrompt()
R.id.settings -> startActivity(Intent(applicationContext, SettingsActivity::class.java))
R.id.about -> launchAbout()
@ -319,14 +321,42 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
}
private fun exportAsFile() {
ExportAsDialog(this, mCurrentNote) {
ExportFileDialog(this, mCurrentNote) {
if (getCurrentNoteText()?.isNotEmpty() == true) {
exportNoteValueToFile(it, getCurrentNoteText()!!)
exportNoteValueToFile(it, getCurrentNoteText()!!, true)
}
}
}
fun exportNoteValueToFile(path: String, content: String) {
private fun tryExportAllNotes() {
handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) {
exportAllNotes()
}
}
}
private fun exportAllNotes() {
ExportFilesDialog(this, mNotes) { parent, extension ->
var failCount = 0
mNotes = dbHelper.getNotes()
mNotes.forEachIndexed { index, note ->
val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension"
val file = File(parent, filename)
exportNoteValueToFile(file.absolutePath, note.value, false) {
if (!it) {
failCount++
}
if (index == mNotes.size - 1) {
toast(if (failCount == 0) R.string.exporting_successful else R.string.exporting_some_entries_failed)
}
}
}
}
}
fun exportNoteValueToFile(path: String, content: String, showSuccessToasts: Boolean, callback: ((success: Boolean) -> Unit)? = null) {
try {
val file = File(path)
if (file.isDirectory) {
@ -345,16 +375,23 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
flush()
close()
}
noteExportedSuccessfully(path.getFilenameFromPath())
if (showSuccessToasts) {
noteExportedSuccessfully(path.getFilenameFromPath())
}
callback?.invoke(true)
}
} else {
file.printWriter().use { out ->
out.write(content)
}
noteExportedSuccessfully(path.getFilenameFromPath())
if (showSuccessToasts) {
noteExportedSuccessfully(path.getFilenameFromPath())
}
callback?.invoke(true)
}
} catch (e: Exception) {
showErrorToast(e)
callback?.invoke(false)
}
}

View File

@ -8,14 +8,14 @@ import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.activities.SimpleActivity
import com.simplemobiletools.notes.extensions.config
import com.simplemobiletools.notes.models.Note
import kotlinx.android.synthetic.main.dialog_export_as.view.*
import kotlinx.android.synthetic.main.dialog_export_file.view.*
import java.io.File
class ExportAsDialog(val activity: SimpleActivity, val note: Note, val callback: (exportPath: String) -> Unit) {
class ExportFileDialog(val activity: SimpleActivity, val note: Note, val callback: (exportPath: String) -> Unit) {
init {
var realPath = File(note.path).parent ?: activity.config.lastUsedSavePath
val view = activity.layoutInflater.inflate(R.layout.dialog_export_as, null).apply {
val view = activity.layoutInflater.inflate(R.layout.dialog_export_file, null).apply {
file_path.text = activity.humanizePath(realPath)
file_name.setText(note.title)

View File

@ -0,0 +1,49 @@
package com.simplemobiletools.notes.dialogs
import android.support.v7.app.AlertDialog
import android.view.WindowManager
import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.extensions.humanizePath
import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.value
import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.activities.SimpleActivity
import com.simplemobiletools.notes.extensions.config
import com.simplemobiletools.notes.models.Note
import kotlinx.android.synthetic.main.dialog_export_files.view.*
import java.io.File
class ExportFilesDialog(val activity: SimpleActivity, val notes: ArrayList<Note>, val callback: (parent: String, extension: String) -> Unit) {
init {
var realPath = activity.config.lastUsedSavePath
val view = activity.layoutInflater.inflate(R.layout.dialog_export_files, null).apply {
folder_path.text = activity.humanizePath(realPath)
file_extension.setText(activity.config.lastUsedExtension)
folder_path.setOnClickListener {
FilePickerDialog(activity, realPath, false, false, true) {
folder_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.export_as_file) {
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
activity.handleSAFDialog(File(realPath)) {
val extension = view.file_extension.value
activity.config.lastUsedExtension = extension
activity.config.lastUsedSavePath = realPath
callback(realPath, extension)
dismiss()
}
}
}
}
}
}

View File

@ -60,11 +60,13 @@ class NoteFragment : Fragment() {
fun getNotesView() = view.notes_view
fun saveText() {
if (note.path.isNotEmpty() && !File(note.path).exists())
if (note.path.isNotEmpty() && !File(note.path).exists()) {
return
}
if (context == null || activity == null)
if (context == null || activity == null) {
return
}
val newText = getCurrentNoteViewText()
val oldText = context!!.getNoteStoredValue(note)
@ -84,7 +86,7 @@ class NoteFragment : Fragment() {
mDb.updateNoteValue(note)
(activity as MainActivity).noteSavedSuccessfully(note.title)
} else {
(activity as MainActivity).exportNoteValueToFile(note.path, getCurrentNoteViewText())
(activity as MainActivity).exportNoteValueToFile(note.path, getCurrentNoteViewText(), true)
}
}

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/export_as_holder"
android:id="@+id/export_file_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
@ -18,8 +18,8 @@
android:id="@+id/file_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
android:layout_marginLeft="@dimen/activity_margin"
android:paddingBottom="@dimen/activity_margin"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/small_margin"
android:paddingTop="@dimen/small_margin"/>

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/export_files_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/folder_path_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/path"
android:textSize="@dimen/smaller_text_size"/>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/folder_path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_margin"
android:paddingLeft="@dimen/activity_margin"
android:paddingRight="@dimen/small_margin"
android:paddingTop="@dimen/small_margin"/>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/file_extension_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/extension"
android:textSize="@dimen/smaller_text_size"/>
<com.simplemobiletools.commons.views.MyEditText
android:id="@+id/file_extension"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_margin"
android:inputType="text"
android:singleLine="true"
android:textCursorDrawable="@null"
android:textSize="@dimen/normal_text_size"/>
</LinearLayout>

View File

@ -29,6 +29,10 @@
android:id="@+id/export_as_file"
android:title="@string/export_as_file"
app:showAsAction="never"/>
<item
android:id="@+id/export_all_notes"
android:title="@string/export_all_notes"
app:showAsAction="never"/>
<item
android:id="@+id/delete_note"
android:icon="@drawable/ic_delete"