Skip to content

Commit 43896a9

Browse files
committed
moving more db related functions to Room
1 parent 95ab889 commit 43896a9

File tree

5 files changed

+26
-27
lines changed

5 files changed

+26
-27
lines changed

app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/MainActivity.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,16 +236,16 @@ class MainActivity : SimpleActivity() {
236236
}
237237

238238
private fun handleUri(uri: Uri) {
239-
val id = dbHelper.getNoteId(uri.path)
240-
241-
if (id > 0) {
242-
updateSelectedNote(id)
243-
return
244-
}
239+
NotesHelper(this).getNoteIdWithPath(uri.path) {
240+
if (it != null && it > 0L) {
241+
updateSelectedNote(it)
242+
return@getNoteIdWithPath
243+
}
245244

246-
handlePermission(PERMISSION_WRITE_STORAGE) {
247-
if (it) {
248-
importFileWithSync(uri)
245+
handlePermission(PERMISSION_WRITE_STORAGE) {
246+
if (it) {
247+
importFileWithSync(uri)
248+
}
249249
}
250250
}
251251
}

app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/NoteFragment.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ class NoteFragment : androidx.fragment.app.Fragment() {
117117
}
118118

119119
private fun setupFragment() {
120-
val config = config!!
120+
val config = config ?: return
121121
view.notes_view.apply {
122122
typeface = if (config.monospacedFont) Typeface.MONOSPACE else Typeface.DEFAULT
123123

@@ -163,6 +163,10 @@ class NoteFragment : androidx.fragment.app.Fragment() {
163163
fun getNotesView() = view.notes_view
164164

165165
fun saveText(force: Boolean) {
166+
if (note == null) {
167+
return
168+
}
169+
166170
if (note!!.path.isNotEmpty() && !File(note!!.path).exists()) {
167171
return
168172
}

app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/DBHelper.kt

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import android.database.sqlite.SQLiteDatabase
77
import android.database.sqlite.SQLiteDatabase.CONFLICT_IGNORE
88
import android.database.sqlite.SQLiteOpenHelper
99
import com.simplemobiletools.commons.extensions.getIntValue
10-
import com.simplemobiletools.commons.extensions.getLongValue
1110
import com.simplemobiletools.notes.pro.R
1211
import com.simplemobiletools.notes.pro.models.Note
1312
import com.simplemobiletools.notes.pro.models.Widget
@@ -92,22 +91,6 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
9291
}
9392
}
9493

95-
fun getNoteId(path: String): Long {
96-
val cols = arrayOf(COL_ID)
97-
val selection = "$COL_PATH = ?"
98-
val selectionArgs = arrayOf(path)
99-
var cursor: Cursor? = null
100-
try {
101-
cursor = mDb.query(NOTES_TABLE_NAME, cols, selection, selectionArgs, null, null, null)
102-
if (cursor?.moveToFirst() == true) {
103-
return cursor.getLongValue(COL_ID)
104-
}
105-
} finally {
106-
cursor?.close()
107-
}
108-
return 0
109-
}
110-
11194
fun getWidgets(): ArrayList<Widget> {
11295
val widgets = ArrayList<Widget>()
11396
val cols = arrayOf(COL_WIDGET_ID, COL_NOTE_ID)

app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ class NotesHelper(val activity: Activity) {
4242
}.start()
4343
}
4444

45+
fun getNoteIdWithPath(path: String, callback: (id: Long?) -> Unit) {
46+
Thread {
47+
val id = activity.notesDB.getNoteIdWithPath(path)
48+
activity.runOnUiThread {
49+
callback(id)
50+
}
51+
}.start()
52+
}
53+
4554
fun insertOrUpdateNote(note: Note, callback: ((newNoteId: Long) -> Unit)? = null) {
4655
Thread {
4756
val noteId = activity.notesDB.insertOrUpdate(note)

app/src/main/kotlin/com/simplemobiletools/notes/pro/interfaces/NotesDao.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ interface NotesDao {
1111
@Query("SELECT * FROM notes WHERE id = :id")
1212
fun getNoteWithId(id: Int): Note?
1313

14+
@Query("SELECT id FROM notes WHERE path = :path")
15+
fun getNoteIdWithPath(path: String): Long?
16+
1417
@Insert(onConflict = OnConflictStrategy.REPLACE)
1518
fun insertOrUpdate(note: Note): Long
1619

0 commit comments

Comments
 (0)