use one shared function for updating note title, value, path

This commit is contained in:
tibbi 2018-11-07 12:58:35 +01:00
parent 2fc3d8cba9
commit f26ea25a1e
4 changed files with 12 additions and 24 deletions

View File

@ -445,12 +445,12 @@ class MainActivity : SimpleActivity() {
exportNoteValueToFile(exportPath, currentNoteText, true) { exportNoteValueToFile(exportPath, currentNoteText, true) {
if (syncFile) { if (syncFile) {
mCurrentNote.path = exportPath mCurrentNote.path = exportPath
dbHelper.updateNotePath(mCurrentNote)
if (mCurrentNote.getNoteStoredValue() == currentNoteText) { if (mCurrentNote.getNoteStoredValue() == currentNoteText) {
mCurrentNote.value = "" mCurrentNote.value = ""
dbHelper.updateNoteValue(mCurrentNote)
} }
dbHelper.updateNote(mCurrentNote)
} }
} }
} }

View File

@ -30,7 +30,9 @@ class RenameNoteDialog(val activity: SimpleActivity, val note: Note, callback: (
else -> { else -> {
note.title = title note.title = title
val path = note.path val path = note.path
if (path.isNotEmpty()) { if (path.isEmpty()) {
activity.dbHelper.updateNote(note)
} else {
if (title.isEmpty()) { if (title.isEmpty()) {
activity.toast(R.string.filename_cannot_be_empty) activity.toast(R.string.filename_cannot_be_empty)
return@setOnClickListener return@setOnClickListener
@ -46,14 +48,14 @@ class RenameNoteDialog(val activity: SimpleActivity, val note: Note, callback: (
activity.renameFile(file.absolutePath, newFile.absolutePath) { activity.renameFile(file.absolutePath, newFile.absolutePath) {
if (it) { if (it) {
note.path = newFile.absolutePath note.path = newFile.absolutePath
activity.dbHelper.updateNotePath(note) activity.dbHelper.updateNote(note)
} else { } else {
activity.toast(R.string.rename_file_error) activity.toast(R.string.rename_file_error)
return@renameFile return@renameFile
} }
} }
} }
activity.dbHelper.updateNoteTitle(note)
dismiss() dismiss()
callback(note) callback(note)
} }

View File

@ -179,7 +179,7 @@ class NoteFragment : androidx.fragment.app.Fragment() {
private fun saveNoteValue(note: Note) { private fun saveNoteValue(note: Note) {
if (note.path.isEmpty()) { if (note.path.isEmpty()) {
db.updateNoteValue(note) db.updateNote(note)
(activity as MainActivity).noteSavedSuccessfully(note.title) (activity as MainActivity).noteSavedSuccessfully(note.title)
} else { } else {
val currentText = getCurrentNoteViewText() val currentText = getCurrentNoteViewText()

View File

@ -69,7 +69,7 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
put(COL_TITLE, note.title) put(COL_TITLE, note.title)
put(COL_VALUE, note.value) put(COL_VALUE, note.value)
put(COL_PATH, note.path) put(COL_PATH, note.path)
put(COL_TYPE, 0) put(COL_TYPE, TYPE_NOTE)
} }
} }
@ -168,24 +168,10 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
return 0 return 0
} }
fun updateNoteValue(note: Note) { fun updateNote(note: Note) {
val values = ContentValues().apply { put(COL_VALUE, note.value) } val values = fillNoteContentValues(note)
updateNote(note.id!!, values)
}
fun updateNoteTitle(note: Note) {
val values = ContentValues().apply { put(COL_TITLE, note.title) }
updateNote(note.id!!, values)
}
fun updateNotePath(note: Note) {
val values = ContentValues().apply { put(COL_PATH, note.path) }
updateNote(note.id!!, values)
}
private fun updateNote(id: Int, values: ContentValues) {
val selection = "$COL_ID = ?" val selection = "$COL_ID = ?"
val selectionArgs = arrayOf(id.toString()) val selectionArgs = arrayOf(note.id.toString())
mDb.update(NOTES_TABLE_NAME, values, selection, selectionArgs) mDb.update(NOTES_TABLE_NAME, values, selection, selectionArgs)
} }