11package com.simplemobiletools.notes.dialogs
22
3- import android.app.Activity
43import android.support.v7.app.AlertDialog
54import android.view.LayoutInflater
65import android.view.ViewGroup
@@ -9,18 +8,21 @@ import com.simplemobiletools.commons.extensions.humanizePath
98import com.simplemobiletools.commons.extensions.setupDialogStuff
109import com.simplemobiletools.notes.R
1110import com.simplemobiletools.notes.R.id.open_file_update_file
11+ import com.simplemobiletools.notes.activities.SimpleActivity
1212import com.simplemobiletools.notes.helpers.TYPE_NOTE
1313import com.simplemobiletools.notes.models.Note
1414import kotlinx.android.synthetic.main.dialog_open_file.view.*
1515import 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