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 efbdbb64..daa2a766 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 @@ -236,16 +236,16 @@ class MainActivity : SimpleActivity() { } 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) { - updateSelectedNote(id) - return - } - - handlePermission(PERMISSION_WRITE_STORAGE) { - if (it) { - importFileWithSync(uri) + handlePermission(PERMISSION_WRITE_STORAGE) { + if (it) { + importFileWithSync(uri) + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/NoteFragment.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/NoteFragment.kt index aefbfd3c..9189f8b0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/NoteFragment.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/fragments/NoteFragment.kt @@ -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 } 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 c02d02a5..8ff5e55b 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 @@ -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 { val widgets = ArrayList() val cols = arrayOf(COL_WIDGET_ID, COL_NOTE_ID) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt index 43e00af2..34cbf7b0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt @@ -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) 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 7132bc9a..e1fc89ed 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 @@ -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