diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt index 569bdc03..b5f74d2c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt @@ -38,7 +38,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener { private var mAdapter: NotesPagerAdapter? = null lateinit var mCurrentNote: Note - lateinit var mNotes: List + private var mNotes = ArrayList() 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) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/dialogs/ExportAsDialog.kt b/app/src/main/kotlin/com/simplemobiletools/notes/dialogs/ExportFileDialog.kt similarity index 92% rename from app/src/main/kotlin/com/simplemobiletools/notes/dialogs/ExportAsDialog.kt rename to app/src/main/kotlin/com/simplemobiletools/notes/dialogs/ExportFileDialog.kt index 585ed870..fe692f08 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/dialogs/ExportAsDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/dialogs/ExportFileDialog.kt @@ -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) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/dialogs/ExportFilesDialog.kt b/app/src/main/kotlin/com/simplemobiletools/notes/dialogs/ExportFilesDialog.kt new file mode 100644 index 00000000..a0b84683 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/notes/dialogs/ExportFilesDialog.kt @@ -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, 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() + } + } + } + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/fragments/NoteFragment.kt b/app/src/main/kotlin/com/simplemobiletools/notes/fragments/NoteFragment.kt index bfefb0e0..534aa514 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/fragments/NoteFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/fragments/NoteFragment.kt @@ -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) } } diff --git a/app/src/main/res/layout/dialog_export_as.xml b/app/src/main/res/layout/dialog_export_file.xml similarity index 93% rename from app/src/main/res/layout/dialog_export_as.xml rename to app/src/main/res/layout/dialog_export_file.xml index ae0f3fd7..de916f58 100644 --- a/app/src/main/res/layout/dialog_export_as.xml +++ b/app/src/main/res/layout/dialog_export_file.xml @@ -1,7 +1,7 @@ diff --git a/app/src/main/res/layout/dialog_export_files.xml b/app/src/main/res/layout/dialog_export_files.xml new file mode 100644 index 00000000..2de860e8 --- /dev/null +++ b/app/src/main/res/layout/dialog_export_files.xml @@ -0,0 +1,43 @@ + + + + + + + + + + + + diff --git a/app/src/main/res/menu/menu.xml b/app/src/main/res/menu/menu.xml index 70e3b276..2d9fa816 100644 --- a/app/src/main/res/menu/menu.xml +++ b/app/src/main/res/menu/menu.xml @@ -29,6 +29,10 @@ android:id="@+id/export_as_file" android:title="@string/export_as_file" app:showAsAction="never"/> +