save note to file at pause, if its a file note

This commit is contained in:
tibbi 2017-03-08 22:05:21 +01:00
parent 6585cf0b2f
commit c5fc3d2bd5
2 changed files with 17 additions and 9 deletions

View File

@ -186,32 +186,39 @@ class MainActivity : SimpleActivity(), ViewPager.OnPageChangeListener {
private fun saveAsFile() {
SaveAsDialog(this, mCurrentNote.title) {
val file = File(it)
saveNoteValueToFile(it, getCurrentNoteText())
}
}
fun saveNoteValueToFile(path: String, content: String) {
try {
val file = File(path)
if (file.isDirectory) {
toast(R.string.directory_exists)
return@SaveAsDialog
return
}
val text = getCurrentNoteText()
if (needsStupidWritePermissions(it)) {
if (needsStupidWritePermissions(path)) {
if (isShowingPermDialog(file))
return@SaveAsDialog
return
var document = getFileDocument(it, config.treeUri) ?: return@SaveAsDialog
var document = getFileDocument(path, config.treeUri) ?: return
if (!file.exists()) {
document = document.createFile("", file.name)
}
contentResolver.openOutputStream(document.uri).apply {
write(text.toByteArray(Charset.forName("UTF-8")))
write(content.toByteArray(Charset.forName("UTF-8")))
flush()
close()
}
} else {
file.printWriter().use { out ->
out.write(text)
out.write(content)
}
}
toast(R.string.file_saved)
} catch (e: Exception) {
toast(R.string.unknown_error_occurred)
}
}

View File

@ -13,6 +13,7 @@ import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import com.simplemobiletools.notes.R
import com.simplemobiletools.notes.activities.MainActivity
import com.simplemobiletools.notes.extensions.config
import com.simplemobiletools.notes.extensions.getTextGravity
import com.simplemobiletools.notes.extensions.getTextSize
@ -76,7 +77,7 @@ class NoteFragment : Fragment() {
if (note.path.isEmpty()) {
mDb.updateNoteValue(note)
} else {
(activity as MainActivity).saveNoteValueToFile(note.path, getCurrentNoteViewText())
}
}