moving more db related functions to Room

This commit is contained in:
tibbi 2018-11-07 20:12:07 +01:00
parent 95ab889045
commit 43896a964a
5 changed files with 26 additions and 27 deletions

View File

@ -236,16 +236,16 @@ class MainActivity : SimpleActivity() {
} }
private fun handleUri(uri: Uri) { private fun handleUri(uri: Uri) {
val id = dbHelper.getNoteId(uri.path) NotesHelper(this).getNoteIdWithPath(uri.path) {
if (it != null && it > 0L) {
updateSelectedNote(it)
return@getNoteIdWithPath
}
if (id > 0) { handlePermission(PERMISSION_WRITE_STORAGE) {
updateSelectedNote(id) if (it) {
return importFileWithSync(uri)
} }
handlePermission(PERMISSION_WRITE_STORAGE) {
if (it) {
importFileWithSync(uri)
} }
} }
} }

View File

@ -117,7 +117,7 @@ class NoteFragment : androidx.fragment.app.Fragment() {
} }
private fun setupFragment() { private fun setupFragment() {
val config = config!! val config = config ?: return
view.notes_view.apply { view.notes_view.apply {
typeface = if (config.monospacedFont) Typeface.MONOSPACE else Typeface.DEFAULT typeface = if (config.monospacedFont) Typeface.MONOSPACE else Typeface.DEFAULT
@ -163,6 +163,10 @@ class NoteFragment : androidx.fragment.app.Fragment() {
fun getNotesView() = view.notes_view fun getNotesView() = view.notes_view
fun saveText(force: Boolean) { fun saveText(force: Boolean) {
if (note == null) {
return
}
if (note!!.path.isNotEmpty() && !File(note!!.path).exists()) { if (note!!.path.isNotEmpty() && !File(note!!.path).exists()) {
return return
} }

View File

@ -7,7 +7,6 @@ import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteDatabase.CONFLICT_IGNORE import android.database.sqlite.SQLiteDatabase.CONFLICT_IGNORE
import android.database.sqlite.SQLiteOpenHelper import android.database.sqlite.SQLiteOpenHelper
import com.simplemobiletools.commons.extensions.getIntValue import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getLongValue
import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.models.Note import com.simplemobiletools.notes.pro.models.Note
import com.simplemobiletools.notes.pro.models.Widget import com.simplemobiletools.notes.pro.models.Widget
@ -92,22 +91,6 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
} }
} }
fun getNoteId(path: String): Long {
val cols = arrayOf(COL_ID)
val selection = "$COL_PATH = ?"
val selectionArgs = arrayOf(path)
var cursor: Cursor? = null
try {
cursor = mDb.query(NOTES_TABLE_NAME, cols, selection, selectionArgs, null, null, null)
if (cursor?.moveToFirst() == true) {
return cursor.getLongValue(COL_ID)
}
} finally {
cursor?.close()
}
return 0
}
fun getWidgets(): ArrayList<Widget> { fun getWidgets(): ArrayList<Widget> {
val widgets = ArrayList<Widget>() val widgets = ArrayList<Widget>()
val cols = arrayOf(COL_WIDGET_ID, COL_NOTE_ID) val cols = arrayOf(COL_WIDGET_ID, COL_NOTE_ID)

View File

@ -42,6 +42,15 @@ class NotesHelper(val activity: Activity) {
}.start() }.start()
} }
fun getNoteIdWithPath(path: String, callback: (id: Long?) -> Unit) {
Thread {
val id = activity.notesDB.getNoteIdWithPath(path)
activity.runOnUiThread {
callback(id)
}
}.start()
}
fun insertOrUpdateNote(note: Note, callback: ((newNoteId: Long) -> Unit)? = null) { fun insertOrUpdateNote(note: Note, callback: ((newNoteId: Long) -> Unit)? = null) {
Thread { Thread {
val noteId = activity.notesDB.insertOrUpdate(note) val noteId = activity.notesDB.insertOrUpdate(note)

View File

@ -11,6 +11,9 @@ interface NotesDao {
@Query("SELECT * FROM notes WHERE id = :id") @Query("SELECT * FROM notes WHERE id = :id")
fun getNoteWithId(id: Int): Note? fun getNoteWithId(id: Int): Note?
@Query("SELECT id FROM notes WHERE path = :path")
fun getNoteIdWithPath(path: String): Long?
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertOrUpdate(note: Note): Long fun insertOrUpdate(note: Note): Long