Skip to content

Commit be4d06f

Browse files
committed
fix #109, allow exporting all notes at once
1 parent 98a3798 commit be4d06f

File tree

7 files changed

+150
-15
lines changed

7 files changed

+150
-15
lines changed

app/src/main/kotlin/com/simplemobiletools/notes/activities/MainActivity.kt

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
3838
private var mAdapter: NotesPagerAdapter? = null
3939

4040
lateinit var mCurrentNote: Note
41-
lateinit var mNotes: List<Note>
41+
private var mNotes = ArrayList<Note>()
4242

4343
private var noteViewWithTextSelected: MyEditText? = null
4444
private var wasInit = false
@@ -110,6 +110,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
110110
findItem(R.id.rename_note).isVisible = shouldBeVisible
111111
findItem(R.id.open_note).isVisible = shouldBeVisible
112112
findItem(R.id.delete_note).isVisible = shouldBeVisible
113+
findItem(R.id.export_all_notes).isVisible = shouldBeVisible
113114
}
114115

115116
pager_title_strip.beVisibleIf(shouldBeVisible)
@@ -125,6 +126,7 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
125126
R.id.share -> shareText()
126127
R.id.open_file -> tryOpenFile()
127128
R.id.export_as_file -> tryExportAsFile()
129+
R.id.export_all_notes -> tryExportAllNotes()
128130
R.id.delete_note -> displayDeleteNotePrompt()
129131
R.id.settings -> startActivity(Intent(applicationContext, SettingsActivity::class.java))
130132
R.id.about -> launchAbout()
@@ -319,14 +321,42 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
319321
}
320322

321323
private fun exportAsFile() {
322-
ExportAsDialog(this, mCurrentNote) {
324+
ExportFileDialog(this, mCurrentNote) {
323325
if (getCurrentNoteText()?.isNotEmpty() == true) {
324-
exportNoteValueToFile(it, getCurrentNoteText()!!)
326+
exportNoteValueToFile(it, getCurrentNoteText()!!, true)
325327
}
326328
}
327329
}
328330

329-
fun exportNoteValueToFile(path: String, content: String) {
331+
private fun tryExportAllNotes() {
332+
handlePermission(PERMISSION_WRITE_STORAGE) {
333+
if (it) {
334+
exportAllNotes()
335+
}
336+
}
337+
}
338+
339+
private fun exportAllNotes() {
340+
ExportFilesDialog(this, mNotes) { parent, extension ->
341+
var failCount = 0
342+
mNotes = dbHelper.getNotes()
343+
mNotes.forEachIndexed { index, note ->
344+
val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension"
345+
val file = File(parent, filename)
346+
exportNoteValueToFile(file.absolutePath, note.value, false) {
347+
if (!it) {
348+
failCount++
349+
}
350+
351+
if (index == mNotes.size - 1) {
352+
toast(if (failCount == 0) R.string.exporting_successful else R.string.exporting_some_entries_failed)
353+
}
354+
}
355+
}
356+
}
357+
}
358+
359+
fun exportNoteValueToFile(path: String, content: String, showSuccessToasts: Boolean, callback: ((success: Boolean) -> Unit)? = null) {
330360
try {
331361
val file = File(path)
332362
if (file.isDirectory) {
@@ -345,16 +375,23 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
345375
flush()
346376
close()
347377
}
348-
noteExportedSuccessfully(path.getFilenameFromPath())
378+
if (showSuccessToasts) {
379+
noteExportedSuccessfully(path.getFilenameFromPath())
380+
}
381+
callback?.invoke(true)
349382
}
350383
} else {
351384
file.printWriter().use { out ->
352385
out.write(content)
353386
}
354-
noteExportedSuccessfully(path.getFilenameFromPath())
387+
if (showSuccessToasts) {
388+
noteExportedSuccessfully(path.getFilenameFromPath())
389+
}
390+
callback?.invoke(true)
355391
}
356392
} catch (e: Exception) {
357393
showErrorToast(e)
394+
callback?.invoke(false)
358395
}
359396
}
360397

app/src/main/kotlin/com/simplemobiletools/notes/dialogs/ExportAsDialog.kt renamed to app/src/main/kotlin/com/simplemobiletools/notes/dialogs/ExportFileDialog.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import com.simplemobiletools.notes.R
88
import com.simplemobiletools.notes.activities.SimpleActivity
99
import com.simplemobiletools.notes.extensions.config
1010
import com.simplemobiletools.notes.models.Note
11-
import kotlinx.android.synthetic.main.dialog_export_as.view.*
11+
import kotlinx.android.synthetic.main.dialog_export_file.view.*
1212
import java.io.File
1313

14-
class ExportAsDialog(val activity: SimpleActivity, val note: Note, val callback: (exportPath: String) -> Unit) {
14+
class ExportFileDialog(val activity: SimpleActivity, val note: Note, val callback: (exportPath: String) -> Unit) {
1515

1616
init {
1717
var realPath = File(note.path).parent ?: activity.config.lastUsedSavePath
18-
val view = activity.layoutInflater.inflate(R.layout.dialog_export_as, null).apply {
18+
val view = activity.layoutInflater.inflate(R.layout.dialog_export_file, null).apply {
1919
file_path.text = activity.humanizePath(realPath)
2020

2121
file_name.setText(note.title)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.simplemobiletools.notes.dialogs
2+
3+
import android.support.v7.app.AlertDialog
4+
import android.view.WindowManager
5+
import com.simplemobiletools.commons.dialogs.FilePickerDialog
6+
import com.simplemobiletools.commons.extensions.humanizePath
7+
import com.simplemobiletools.commons.extensions.setupDialogStuff
8+
import com.simplemobiletools.commons.extensions.value
9+
import com.simplemobiletools.notes.R
10+
import com.simplemobiletools.notes.activities.SimpleActivity
11+
import com.simplemobiletools.notes.extensions.config
12+
import com.simplemobiletools.notes.models.Note
13+
import kotlinx.android.synthetic.main.dialog_export_files.view.*
14+
import java.io.File
15+
16+
class ExportFilesDialog(val activity: SimpleActivity, val notes: ArrayList<Note>, val callback: (parent: String, extension: String) -> Unit) {
17+
init {
18+
var realPath = activity.config.lastUsedSavePath
19+
val view = activity.layoutInflater.inflate(R.layout.dialog_export_files, null).apply {
20+
folder_path.text = activity.humanizePath(realPath)
21+
22+
file_extension.setText(activity.config.lastUsedExtension)
23+
folder_path.setOnClickListener {
24+
FilePickerDialog(activity, realPath, false, false, true) {
25+
folder_path.text = activity.humanizePath(it)
26+
realPath = it
27+
}
28+
}
29+
}
30+
31+
AlertDialog.Builder(activity)
32+
.setPositiveButton(R.string.ok, null)
33+
.setNegativeButton(R.string.cancel, null)
34+
.create().apply {
35+
window!!.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE)
36+
activity.setupDialogStuff(view, this, R.string.export_as_file) {
37+
getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
38+
activity.handleSAFDialog(File(realPath)) {
39+
val extension = view.file_extension.value
40+
activity.config.lastUsedExtension = extension
41+
activity.config.lastUsedSavePath = realPath
42+
callback(realPath, extension)
43+
dismiss()
44+
}
45+
}
46+
}
47+
}
48+
}
49+
}

app/src/main/kotlin/com/simplemobiletools/notes/fragments/NoteFragment.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,13 @@ class NoteFragment : Fragment() {
6060
fun getNotesView() = view.notes_view
6161

6262
fun saveText() {
63-
if (note.path.isNotEmpty() && !File(note.path).exists())
63+
if (note.path.isNotEmpty() && !File(note.path).exists()) {
6464
return
65+
}
6566

66-
if (context == null || activity == null)
67+
if (context == null || activity == null) {
6768
return
69+
}
6870

6971
val newText = getCurrentNoteViewText()
7072
val oldText = context!!.getNoteStoredValue(note)
@@ -84,7 +86,7 @@ class NoteFragment : Fragment() {
8486
mDb.updateNoteValue(note)
8587
(activity as MainActivity).noteSavedSuccessfully(note.title)
8688
} else {
87-
(activity as MainActivity).exportNoteValueToFile(note.path, getCurrentNoteViewText())
89+
(activity as MainActivity).exportNoteValueToFile(note.path, getCurrentNoteViewText(), true)
8890
}
8991
}
9092

app/src/main/res/layout/dialog_export_as.xml renamed to app/src/main/res/layout/dialog_export_file.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout
33
xmlns:android="http://schemas.android.com/apk/res/android"
4-
android:id="@+id/export_as_holder"
4+
android:id="@+id/export_file_holder"
55
android:layout_width="match_parent"
66
android:layout_height="match_parent"
77
android:orientation="vertical"
@@ -18,8 +18,8 @@
1818
android:id="@+id/file_path"
1919
android:layout_width="match_parent"
2020
android:layout_height="wrap_content"
21-
android:layout_marginBottom="@dimen/activity_margin"
22-
android:layout_marginLeft="@dimen/activity_margin"
21+
android:paddingBottom="@dimen/activity_margin"
22+
android:paddingLeft="@dimen/activity_margin"
2323
android:paddingRight="@dimen/small_margin"
2424
android:paddingTop="@dimen/small_margin"/>
2525

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<LinearLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
android:id="@+id/export_files_holder"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
android:orientation="vertical"
8+
android:padding="@dimen/activity_margin">
9+
10+
<com.simplemobiletools.commons.views.MyTextView
11+
android:id="@+id/folder_path_label"
12+
android:layout_width="wrap_content"
13+
android:layout_height="wrap_content"
14+
android:text="@string/path"
15+
android:textSize="@dimen/smaller_text_size"/>
16+
17+
<com.simplemobiletools.commons.views.MyTextView
18+
android:id="@+id/folder_path"
19+
android:layout_width="match_parent"
20+
android:layout_height="wrap_content"
21+
android:paddingBottom="@dimen/activity_margin"
22+
android:paddingLeft="@dimen/activity_margin"
23+
android:paddingRight="@dimen/small_margin"
24+
android:paddingTop="@dimen/small_margin"/>
25+
26+
<com.simplemobiletools.commons.views.MyTextView
27+
android:id="@+id/file_extension_label"
28+
android:layout_width="wrap_content"
29+
android:layout_height="wrap_content"
30+
android:text="@string/extension"
31+
android:textSize="@dimen/smaller_text_size"/>
32+
33+
<com.simplemobiletools.commons.views.MyEditText
34+
android:id="@+id/file_extension"
35+
android:layout_width="match_parent"
36+
android:layout_height="wrap_content"
37+
android:layout_marginBottom="@dimen/activity_margin"
38+
android:inputType="text"
39+
android:singleLine="true"
40+
android:textCursorDrawable="@null"
41+
android:textSize="@dimen/normal_text_size"/>
42+
43+
</LinearLayout>

app/src/main/res/menu/menu.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@
2929
android:id="@+id/export_as_file"
3030
android:title="@string/export_as_file"
3131
app:showAsAction="never"/>
32+
<item
33+
android:id="@+id/export_all_notes"
34+
android:title="@string/export_all_notes"
35+
app:showAsAction="never"/>
3236
<item
3337
android:id="@+id/delete_note"
3438
android:icon="@drawable/ic_delete"

0 commit comments

Comments
 (0)