From 66821397e9566747454cb20e420017a19ff67be8 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 7 Nov 2018 20:39:17 +0100 Subject: [PATCH] moving another function in Room --- .../notes/pro/activities/MainActivity.kt | 6 +- .../notes/pro/dialogs/ImportFolderDialog.kt | 14 ++- .../notes/pro/dialogs/NewNoteDialog.kt | 20 ++-- .../notes/pro/dialogs/RenameNoteDialog.kt | 91 ++++++++++--------- .../notes/pro/helpers/DBHelper.kt | 13 --- .../notes/pro/interfaces/NotesDao.kt | 3 + 6 files changed, 75 insertions(+), 72 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt index daa2a766..f2470041 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt @@ -300,7 +300,7 @@ class MainActivity : SimpleActivity() { } private fun displayNewNoteDialog(value: String = "") { - NewNoteDialog(this, dbHelper) { + NewNoteDialog(this) { val newNote = Note(null, it, value, TYPE_NOTE) addNewNote(newNote) } @@ -355,7 +355,7 @@ class MainActivity : SimpleActivity() { toast(R.string.invalid_file_format) } else if (file.length() > 10 * 1000 * 1000) { toast(R.string.file_too_large) - } else if (checkTitle && dbHelper.doesNoteTitleExist(path.getFilenameFromPath())) { + } else if (checkTitle && mNotes.any { it.title.equals(path.getFilenameFromPath(), true) }) { toast(R.string.title_taken) } else { onChecksPassed(file) @@ -386,7 +386,7 @@ class MainActivity : SimpleActivity() { private fun openPath(path: String) { openFile(path, false) { var title = path.getFilenameFromPath() - if (dbHelper.doesNoteTitleExist(title)) { + if (mNotes.any { it.title.equals(title, true) }) { title += " (file)" } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/ImportFolderDialog.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/ImportFolderDialog.kt index c01e7a43..ee505e81 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/ImportFolderDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/ImportFolderDialog.kt @@ -8,7 +8,7 @@ import com.simplemobiletools.commons.extensions.isMediaFile import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.activities.SimpleActivity -import com.simplemobiletools.notes.pro.extensions.dbHelper +import com.simplemobiletools.notes.pro.extensions.notesDB import com.simplemobiletools.notes.pro.helpers.NotesHelper import com.simplemobiletools.notes.pro.helpers.TYPE_NOTE import com.simplemobiletools.notes.pro.models.Note @@ -30,7 +30,9 @@ class ImportFolderDialog(val activity: SimpleActivity, val path: String, val cal activity.setupDialogStuff(view, this, R.string.import_folder) { getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { val updateFilesOnEdit = view.open_file_type.checkedRadioButtonId == R.id.open_file_update_file - saveFolder(updateFilesOnEdit) + Thread { + saveFolder(updateFilesOnEdit) + }.start() } } } @@ -44,7 +46,7 @@ class ImportFolderDialog(val activity: SimpleActivity, val path: String, val cal file.isDirectory -> false filename.isMediaFile() -> false file.length() > 10 * 1000 * 1000 -> false - activity.dbHelper.doesNoteTitleExist(filename) -> false + activity.notesDB.getNoteIdWithTitle(filename) != null -> false else -> true } }.forEach { @@ -61,8 +63,10 @@ class ImportFolderDialog(val activity: SimpleActivity, val path: String, val cal } } - callback() - dialog.dismiss() + activity.runOnUiThread { + callback() + dialog.dismiss() + } } private fun saveNote(title: String, value: String, path: String) { diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/NewNoteDialog.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/NewNoteDialog.kt index 7040084b..57796009 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/NewNoteDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/NewNoteDialog.kt @@ -8,10 +8,10 @@ import com.simplemobiletools.commons.extensions.showKeyboard import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.value import com.simplemobiletools.notes.pro.R -import com.simplemobiletools.notes.pro.helpers.DBHelper +import com.simplemobiletools.notes.pro.extensions.notesDB import kotlinx.android.synthetic.main.dialog_new_note.view.* -class NewNoteDialog(val activity: Activity, val db: DBHelper, callback: (title: String) -> Unit) { +class NewNoteDialog(val activity: Activity, callback: (title: String) -> Unit) { init { val view = activity.layoutInflater.inflate(R.layout.dialog_new_note, null) @@ -23,14 +23,16 @@ class NewNoteDialog(val activity: Activity, val db: DBHelper, callback: (title: showKeyboard(view.note_name) getButton(BUTTON_POSITIVE).setOnClickListener { val title = view.note_name.value - when { - title.isEmpty() -> activity.toast(R.string.no_title) - db.doesNoteTitleExist(title) -> activity.toast(R.string.title_taken) - else -> { - callback(title) - dismiss() + Thread { + when { + title.isEmpty() -> activity.toast(R.string.no_title) + activity.notesDB.getNoteIdWithTitle(title) != null -> activity.toast(R.string.title_taken) + else -> { + callback(title) + dismiss() + } } - } + }.start() } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/RenameNoteDialog.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/RenameNoteDialog.kt index 49c3620e..55d5d2cf 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/RenameNoteDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/RenameNoteDialog.kt @@ -5,13 +5,12 @@ import androidx.appcompat.app.AlertDialog import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.activities.SimpleActivity -import com.simplemobiletools.notes.pro.extensions.dbHelper -import com.simplemobiletools.notes.pro.helpers.NotesHelper +import com.simplemobiletools.notes.pro.extensions.notesDB import com.simplemobiletools.notes.pro.models.Note import kotlinx.android.synthetic.main.dialog_new_note.view.* import java.io.File -class RenameNoteDialog(val activity: SimpleActivity, val note: Note, callback: (note: Note) -> Unit) { +class RenameNoteDialog(val activity: SimpleActivity, val note: Note, val callback: (note: Note) -> Unit) { init { val view = activity.layoutInflater.inflate(R.layout.dialog_rename_note, null) @@ -25,47 +24,55 @@ class RenameNoteDialog(val activity: SimpleActivity, val note: Note, callback: ( showKeyboard(view.note_name) getButton(BUTTON_POSITIVE).setOnClickListener { val title = view.note_name.value - when { - title.isEmpty() -> activity.toast(R.string.no_title) - activity.dbHelper.doesNoteTitleExist(title) -> activity.toast(R.string.title_taken) - else -> { - note.title = title - val path = note.path - if (path.isEmpty()) { - NotesHelper(activity).insertOrUpdateNote(note) { - dismiss() - callback(note) - } - } else { - if (title.isEmpty()) { - activity.toast(R.string.filename_cannot_be_empty) - return@setOnClickListener - } - - val file = File(path) - val newFile = File(file.parent, title) - if (!newFile.name.isAValidFilename()) { - activity.toast(R.string.invalid_name) - return@setOnClickListener - } - - activity.renameFile(file.absolutePath, newFile.absolutePath) { - if (it) { - note.path = newFile.absolutePath - NotesHelper(activity).insertOrUpdateNote(note) { - dismiss() - callback(note) - } - } else { - activity.toast(R.string.rename_file_error) - return@renameFile - } - } - } - } - } + Thread { + newTitleConfirmed(title, this) + }.start() } } } } + + private fun newTitleConfirmed(title: String, dialog: AlertDialog) { + when { + title.isEmpty() -> activity.toast(R.string.no_title) + activity.notesDB.getNoteIdWithTitle(title) != null -> activity.toast(R.string.title_taken) + else -> { + note.title = title + val path = note.path + if (path.isEmpty()) { + activity.notesDB.insertOrUpdate(note) + activity.runOnUiThread { + dialog.dismiss() + callback(note) + } + } else { + if (title.isEmpty()) { + activity.toast(R.string.filename_cannot_be_empty) + return + } + + val file = File(path) + val newFile = File(file.parent, title) + if (!newFile.name.isAValidFilename()) { + activity.toast(R.string.invalid_name) + return + } + + activity.renameFile(file.absolutePath, newFile.absolutePath) { + if (it) { + note.path = newFile.absolutePath + activity.notesDB.insertOrUpdate(note) + activity.runOnUiThread { + dialog.dismiss() + callback(note) + } + } else { + activity.toast(R.string.rename_file_error) + return@renameFile + } + } + } + } + } + } } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/DBHelper.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/DBHelper.kt index 8ff5e55b..37b54f33 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/DBHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/DBHelper.kt @@ -78,19 +78,6 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe mDb.delete(WIDGETS_TABLE_NAME, "$COL_NOTE_ID = $id", null) } - fun doesNoteTitleExist(title: String): Boolean { - val cols = arrayOf(COL_ID) - val selection = "$COL_TITLE = ? COLLATE NOCASE" - val selectionArgs = arrayOf(title) - var cursor: Cursor? = null - try { - cursor = mDb.query(NOTES_TABLE_NAME, cols, selection, selectionArgs, null, null, null) - return cursor.count == 1 - } finally { - cursor?.close() - } - } - fun getWidgets(): ArrayList { val widgets = ArrayList() val cols = arrayOf(COL_WIDGET_ID, COL_NOTE_ID) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/interfaces/NotesDao.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/interfaces/NotesDao.kt index e1fc89ed..6165b89b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/interfaces/NotesDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/interfaces/NotesDao.kt @@ -14,6 +14,9 @@ interface NotesDao { @Query("SELECT id FROM notes WHERE path = :path") fun getNoteIdWithPath(path: String): Long? + @Query("SELECT id FROM notes WHERE title = :title COLLATE NOCASE") + fun getNoteIdWithTitle(title: String): Long? + @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertOrUpdate(note: Note): Long