Skip to content

Commit 255a5ad

Browse files
committed
handle the SAF dialog when opening a note from SD card with sync
1 parent a25f826 commit 255a5ad

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed
Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.simplemobiletools.notes.dialogs
22

3-
import android.app.Activity
43
import android.support.v7.app.AlertDialog
54
import android.view.LayoutInflater
65
import android.view.ViewGroup
@@ -9,18 +8,21 @@ import com.simplemobiletools.commons.extensions.humanizePath
98
import com.simplemobiletools.commons.extensions.setupDialogStuff
109
import com.simplemobiletools.notes.R
1110
import com.simplemobiletools.notes.R.id.open_file_update_file
11+
import com.simplemobiletools.notes.activities.SimpleActivity
1212
import com.simplemobiletools.notes.helpers.TYPE_NOTE
1313
import com.simplemobiletools.notes.models.Note
1414
import kotlinx.android.synthetic.main.dialog_open_file.view.*
1515
import java.io.File
1616

17-
class OpenFileDialog(val activity: Activity, val path: String, val callback: (note: Note) -> Unit) : AlertDialog.Builder(activity) {
17+
class OpenFileDialog(val activity: SimpleActivity, val path: String, val callback: (note: Note) -> Unit) : AlertDialog.Builder(activity) {
18+
private var dialog: AlertDialog
19+
1820
init {
1921
val view = (LayoutInflater.from(activity).inflate(R.layout.dialog_open_file, null) as ViewGroup).apply {
2022
open_file_filename.text = activity.humanizePath(path)
2123
}
2224

23-
AlertDialog.Builder(activity)
25+
dialog = AlertDialog.Builder(activity)
2426
.setPositiveButton(R.string.ok, null)
2527
.setNegativeButton(R.string.cancel, null)
2628
.create().apply {
@@ -30,11 +32,21 @@ class OpenFileDialog(val activity: Activity, val path: String, val callback: (no
3032
val storePath = if (updateFileOnEdit) path else ""
3133
val storeContent = if (updateFileOnEdit) "" else File(path).readText()
3234

33-
val filename = path.getFilenameFromPath()
34-
val note = Note(0, filename, storeContent, TYPE_NOTE, storePath)
35-
callback.invoke(note)
36-
dismiss()
35+
if (updateFileOnEdit) {
36+
activity.handleSAFDialog(File(path)) {
37+
saveNote(storeContent, storePath)
38+
}
39+
} else {
40+
saveNote(storeContent, storePath)
41+
}
3742
})
3843
}
3944
}
45+
46+
private fun saveNote(storeContent: String, storePath: String) {
47+
val filename = path.getFilenameFromPath()
48+
val note = Note(0, filename, storeContent, TYPE_NOTE, storePath)
49+
callback.invoke(note)
50+
dialog.dismiss()
51+
}
4052
}

0 commit comments

Comments
 (0)