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,11 +236,10 @@ class MainActivity : SimpleActivity() {
}
private fun handleUri(uri: Uri) {
val id = dbHelper.getNoteId(uri.path)
if (id > 0) {
updateSelectedNote(id)
return
NotesHelper(this).getNoteIdWithPath(uri.path) {
if (it != null && it > 0L) {
updateSelectedNote(it)
return@getNoteIdWithPath
}
handlePermission(PERMISSION_WRITE_STORAGE) {
@ -249,6 +248,7 @@ class MainActivity : SimpleActivity() {
}
}
}
}
private fun initViewPager() {
NotesHelper(this).getNotes {

View File

@ -117,7 +117,7 @@ class NoteFragment : androidx.fragment.app.Fragment() {
}
private fun setupFragment() {
val config = config!!
val config = config ?: return
view.notes_view.apply {
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 saveText(force: Boolean) {
if (note == null) {
return
}
if (note!!.path.isNotEmpty() && !File(note!!.path).exists()) {
return
}

View File

@ -7,7 +7,6 @@ import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteDatabase.CONFLICT_IGNORE
import android.database.sqlite.SQLiteOpenHelper
import com.simplemobiletools.commons.extensions.getIntValue
import com.simplemobiletools.commons.extensions.getLongValue
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.models.Note
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> {
val widgets = ArrayList<Widget>()
val cols = arrayOf(COL_WIDGET_ID, COL_NOTE_ID)

View File

@ -42,6 +42,15 @@ class NotesHelper(val activity: Activity) {
}.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) {
Thread {
val noteId = activity.notesDB.insertOrUpdate(note)

View File

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