move some note fetchin related functions in Room

This commit is contained in:
tibbi 2018-11-07 18:38:27 +01:00
parent 32837ebb38
commit 885170edfc
9 changed files with 212 additions and 198 deletions

View File

@ -27,11 +27,9 @@ import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.adapters.NotesPagerAdapter import com.simplemobiletools.notes.pro.adapters.NotesPagerAdapter
import com.simplemobiletools.notes.pro.databases.NotesDatabase import com.simplemobiletools.notes.pro.databases.NotesDatabase
import com.simplemobiletools.notes.pro.dialogs.* import com.simplemobiletools.notes.pro.dialogs.*
import com.simplemobiletools.notes.pro.extensions.config import com.simplemobiletools.notes.pro.extensions.*
import com.simplemobiletools.notes.pro.extensions.dbHelper
import com.simplemobiletools.notes.pro.extensions.getTextSize
import com.simplemobiletools.notes.pro.extensions.updateWidgets
import com.simplemobiletools.notes.pro.helpers.MIME_TEXT_PLAIN import com.simplemobiletools.notes.pro.helpers.MIME_TEXT_PLAIN
import com.simplemobiletools.notes.pro.helpers.NotesHelper
import com.simplemobiletools.notes.pro.helpers.OPEN_NOTE_ID import com.simplemobiletools.notes.pro.helpers.OPEN_NOTE_ID
import com.simplemobiletools.notes.pro.helpers.TYPE_NOTE import com.simplemobiletools.notes.pro.helpers.TYPE_NOTE
import com.simplemobiletools.notes.pro.models.Note import com.simplemobiletools.notes.pro.models.Note
@ -69,7 +67,7 @@ class MainActivity : SimpleActivity() {
intent.apply { intent.apply {
if (action == Intent.ACTION_SEND && type == MIME_TEXT_PLAIN) { if (action == Intent.ACTION_SEND && type == MIME_TEXT_PLAIN) {
getStringExtra(Intent.EXTRA_TEXT)?.let { getStringExtra(Intent.EXTRA_TEXT)?.let {
handleText(it) handleTextIntent(it)
intent.removeExtra(Intent.EXTRA_TEXT) intent.removeExtra(Intent.EXTRA_TEXT)
} }
} }
@ -213,8 +211,9 @@ class MainActivity : SimpleActivity() {
} }
} }
private fun handleText(text: String) { private fun handleTextIntent(text: String) {
val notes = dbHelper.getNotes() NotesHelper(this).getNotes {
val notes = it
val list = arrayListOf<RadioItem>().apply { val list = arrayListOf<RadioItem>().apply {
add(RadioItem(0, getString(R.string.create_new_note))) add(RadioItem(0, getString(R.string.create_new_note)))
notes.forEachIndexed { index, note -> notes.forEachIndexed { index, note ->
@ -231,6 +230,7 @@ class MainActivity : SimpleActivity() {
} }
} }
} }
}
private fun handleUri(uri: Uri) { private fun handleUri(uri: Uri) {
val id = dbHelper.getNoteId(uri.path) val id = dbHelper.getNoteId(uri.path)
@ -248,7 +248,8 @@ class MainActivity : SimpleActivity() {
} }
private fun initViewPager() { private fun initViewPager() {
mNotes = dbHelper.getNotes() NotesHelper(this).getNotes {
mNotes = it
mCurrentNote = mNotes[0] mCurrentNote = mNotes[0]
mAdapter = NotesPagerAdapter(supportFragmentManager, mNotes, this) mAdapter = NotesPagerAdapter(supportFragmentManager, mNotes, this)
view_pager.apply { view_pager.apply {
@ -265,6 +266,7 @@ class MainActivity : SimpleActivity() {
hideKeyboard() hideKeyboard()
} }
} }
}
private fun getWantedNoteIndex(): Int { private fun getWantedNoteIndex(): Int {
var wantedNoteId = intent.getIntExtra(OPEN_NOTE_ID, -1) var wantedNoteId = intent.getIntExtra(OPEN_NOTE_ID, -1)
@ -302,9 +304,11 @@ class MainActivity : SimpleActivity() {
} }
private fun addNewNote(note: Note) { private fun addNewNote(note: Note) {
val id = dbHelper.insertNote(note) Thread {
mNotes = dbHelper.getNotes() val id = notesDB.insertOrUpdate(note).toInt()
mNotes = notesDB.getNotes().toMutableList() as ArrayList<Note>
showSaveButton = false showSaveButton = false
runOnUiThread {
invalidateOptionsMenu() invalidateOptionsMenu()
initViewPager() initViewPager()
updateSelectedNote(id) updateSelectedNote(id)
@ -312,6 +316,8 @@ class MainActivity : SimpleActivity() {
mAdapter?.focusEditText(getNoteIndexWithId(id)) mAdapter?.focusEditText(getNoteIndexWithId(id))
} }
} }
}.start()
}
private fun launchAbout() { private fun launchAbout() {
val licenses = LICENSE_RTL val licenses = LICENSE_RTL
@ -400,13 +406,16 @@ class MainActivity : SimpleActivity() {
FilePickerDialog(this, pickFile = false, canAddShowHiddenButton = true) { FilePickerDialog(this, pickFile = false, canAddShowHiddenButton = true) {
openFolder(it) { openFolder(it) {
ImportFolderDialog(this, it.path) { ImportFolderDialog(this, it.path) {
mNotes = dbHelper.getNotes() val noteId = it
NotesHelper(this).getNotes {
mNotes = it
showSaveButton = false showSaveButton = false
invalidateOptionsMenu() invalidateOptionsMenu()
initViewPager() initViewPager()
updateSelectedNote(it) updateSelectedNote(noteId)
view_pager.onGlobalLayout { view_pager.onGlobalLayout {
mAdapter?.focusEditText(getNoteIndexWithId(it)) mAdapter?.focusEditText(getNoteIndexWithId(noteId))
}
} }
} }
} }
@ -467,7 +476,8 @@ class MainActivity : SimpleActivity() {
private fun exportAllNotes() { private fun exportAllNotes() {
ExportFilesDialog(this, mNotes) { parent, extension -> ExportFilesDialog(this, mNotes) { parent, extension ->
var failCount = 0 var failCount = 0
mNotes = dbHelper.getNotes() NotesHelper(this).getNotes {
mNotes = it
mNotes.forEachIndexed { index, note -> mNotes.forEachIndexed { index, note ->
val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension" val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension"
val file = File(parent, filename) val file = File(parent, filename)
@ -487,6 +497,7 @@ class MainActivity : SimpleActivity() {
} }
} }
} }
}
fun exportNoteValueToFile(path: String, content: String, showSuccessToasts: Boolean, callback: ((success: Boolean) -> Unit)? = null) { fun exportNoteValueToFile(path: String, content: String, showSuccessToasts: Boolean, callback: ((success: Boolean) -> Unit)? = null) {
try { try {
@ -570,8 +581,8 @@ class MainActivity : SimpleActivity() {
private fun doDeleteNote(note: Note, deleteFile: Boolean) { private fun doDeleteNote(note: Note, deleteFile: Boolean) {
dbHelper.deleteNote(mCurrentNote.id!!) dbHelper.deleteNote(mCurrentNote.id!!)
mNotes = dbHelper.getNotes() NotesHelper(this).getNotes {
mNotes = it
val firstNoteId = mNotes[0].id val firstNoteId = mNotes[0].id
updateSelectedNote(firstNoteId!!) updateSelectedNote(firstNoteId!!)
if (config.widgetNoteId == note.id) { if (config.widgetNoteId == note.id) {
@ -590,6 +601,7 @@ class MainActivity : SimpleActivity() {
} }
} }
} }
}
private fun displayOpenNoteDialog() { private fun displayOpenNoteDialog() {
OpenNoteDialog(this) { OpenNoteDialog(this) {

View File

@ -11,7 +11,6 @@ import com.simplemobiletools.commons.helpers.isOreoPlus
import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.commons.models.RadioItem
import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.extensions.config import com.simplemobiletools.notes.pro.extensions.config
import com.simplemobiletools.notes.pro.extensions.dbHelper
import com.simplemobiletools.notes.pro.extensions.updateWidgets import com.simplemobiletools.notes.pro.extensions.updateWidgets
import com.simplemobiletools.notes.pro.helpers.* import com.simplemobiletools.notes.pro.helpers.*
import kotlinx.android.synthetic.main.activity_settings.* import kotlinx.android.synthetic.main.activity_settings.*
@ -109,7 +108,10 @@ class SettingsActivity : SimpleActivity() {
} }
private fun setupShowNotePicker() { private fun setupShowNotePicker() {
settings_show_note_picker_holder.beVisibleIf(dbHelper.getNotes().size > 1) NotesHelper(this).getNotes {
settings_show_note_picker_holder.beVisibleIf(it.size > 1)
}
settings_show_note_picker.isChecked = config.showNotePicker settings_show_note_picker.isChecked = config.showNotePicker
settings_show_note_picker_holder.setOnClickListener { settings_show_note_picker_holder.setOnClickListener {
settings_show_note_picker.toggle() settings_show_note_picker.toggle()

View File

@ -18,6 +18,7 @@ import com.simplemobiletools.notes.pro.extensions.config
import com.simplemobiletools.notes.pro.extensions.dbHelper import com.simplemobiletools.notes.pro.extensions.dbHelper
import com.simplemobiletools.notes.pro.extensions.getTextSize import com.simplemobiletools.notes.pro.extensions.getTextSize
import com.simplemobiletools.notes.pro.helpers.MyWidgetProvider import com.simplemobiletools.notes.pro.helpers.MyWidgetProvider
import com.simplemobiletools.notes.pro.helpers.NotesHelper
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
import kotlinx.android.synthetic.main.widget_config.* import kotlinx.android.synthetic.main.widget_config.*
@ -80,11 +81,13 @@ class WidgetConfigureActivity : SimpleActivity() {
mTextColor = config.widgetTextColor mTextColor = config.widgetTextColor
updateTextColor() updateTextColor()
mNotes = dbHelper.getNotes()
mIsCustomizingColors = intent.extras?.getBoolean(IS_CUSTOMIZING_COLORS) ?: false mIsCustomizingColors = intent.extras?.getBoolean(IS_CUSTOMIZING_COLORS) ?: false
NotesHelper(this).getNotes {
mNotes = it
notes_picker_holder.beVisibleIf(mNotes.size > 1 && !mIsCustomizingColors) notes_picker_holder.beVisibleIf(mNotes.size > 1 && !mIsCustomizingColors)
updateCurrentNote(mNotes.first()) updateCurrentNote(mNotes.first())
} }
}
private fun showNoteSelector() { private fun showNoteSelector() {
val items = ArrayList<RadioItem>() val items = ArrayList<RadioItem>()

View File

@ -10,8 +10,8 @@ import com.simplemobiletools.commons.extensions.setTextSize
import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.R.id.widget_text_holder import com.simplemobiletools.notes.pro.R.id.widget_text_holder
import com.simplemobiletools.notes.pro.extensions.config import com.simplemobiletools.notes.pro.extensions.config
import com.simplemobiletools.notes.pro.extensions.dbHelper
import com.simplemobiletools.notes.pro.extensions.getTextSize import com.simplemobiletools.notes.pro.extensions.getTextSize
import com.simplemobiletools.notes.pro.extensions.notesDB
import com.simplemobiletools.notes.pro.helpers.GRAVITY_CENTER import com.simplemobiletools.notes.pro.helpers.GRAVITY_CENTER
import com.simplemobiletools.notes.pro.helpers.GRAVITY_RIGHT import com.simplemobiletools.notes.pro.helpers.GRAVITY_RIGHT
import com.simplemobiletools.notes.pro.helpers.NOTE_ID import com.simplemobiletools.notes.pro.helpers.NOTE_ID
@ -24,7 +24,7 @@ class WidgetAdapter(val context: Context, val intent: Intent) : RemoteViewsServi
override fun getViewAt(position: Int): RemoteViews { override fun getViewAt(position: Int): RemoteViews {
val noteId = intent.getIntExtra(NOTE_ID, 1) val noteId = intent.getIntExtra(NOTE_ID, 1)
val views = RemoteViews(context.packageName, R.layout.widget_text_layout).apply { val views = RemoteViews(context.packageName, R.layout.widget_text_layout).apply {
val note = context.dbHelper.getNoteWithId(noteId) val note = context.notesDB.getNoteWithId(noteId)
if (note != null) { if (note != null) {
val noteText = note.getNoteStoredValue() ?: "" val noteText = note.getNoteStoredValue() ?: ""
val textSize = context.getTextSize() / context.resources.displayMetrics.density val textSize = context.getTextSize() / context.resources.displayMetrics.density

View File

@ -1,6 +1,7 @@
package com.simplemobiletools.notes.pro.dialogs package com.simplemobiletools.notes.pro.dialogs
import android.app.Activity import android.app.Activity
import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.widget.RadioGroup import android.widget.RadioGroup
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
@ -10,17 +11,23 @@ import com.simplemobiletools.commons.extensions.setupDialogStuff
import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.commons.extensions.toast
import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.extensions.config import com.simplemobiletools.notes.pro.extensions.config
import com.simplemobiletools.notes.pro.extensions.dbHelper import com.simplemobiletools.notes.pro.helpers.NotesHelper
import com.simplemobiletools.notes.pro.models.Note
import kotlinx.android.synthetic.main.dialog_open_note.view.* import kotlinx.android.synthetic.main.dialog_open_note.view.*
import kotlinx.android.synthetic.main.open_note_item.view.* import kotlinx.android.synthetic.main.open_note_item.view.*
class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Int) -> Unit) { class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Int) -> Unit) {
lateinit var dialog: AlertDialog private var dialog: AlertDialog? = null
init { init {
val view = activity.layoutInflater.inflate(R.layout.dialog_open_note, null) val view = activity.layoutInflater.inflate(R.layout.dialog_open_note, null)
NotesHelper(activity).getNotes {
initDialog(it, view)
}
}
private fun initDialog(notes: ArrayList<Note>, view: View) {
val textColor = activity.config.textColor val textColor = activity.config.textColor
val notes = activity.dbHelper.getNotes()
notes.forEach { notes.forEach {
activity.layoutInflater.inflate(R.layout.open_note_item, null).apply { activity.layoutInflater.inflate(R.layout.open_note_item, null).apply {
val note = it val note = it
@ -31,7 +38,7 @@ class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Int) -> U
setOnClickListener { setOnClickListener {
callback(id) callback(id)
dialog.dismiss() dialog?.dismiss()
} }
} }
open_note_item_icon.apply { open_note_item_icon.apply {

View File

@ -40,7 +40,8 @@ class NoteFragment : androidx.fragment.app.Fragment() {
private var isUndoOrRedo = false private var isUndoOrRedo = false
private var skipTextUpdating = false private var skipTextUpdating = false
private var noteId = 0 private var noteId = 0
lateinit var note: Note private var note: Note? = null
lateinit var view: ViewGroup lateinit var view: ViewGroup
private lateinit var db: DBHelper private lateinit var db: DBHelper
@ -48,7 +49,6 @@ class NoteFragment : androidx.fragment.app.Fragment() {
view = inflater.inflate(R.layout.fragment_note, container, false) as ViewGroup view = inflater.inflate(R.layout.fragment_note, container, false) as ViewGroup
noteId = arguments!!.getInt(NOTE_ID) noteId = arguments!!.getInt(NOTE_ID)
db = context!!.dbHelper db = context!!.dbHelper
note = db.getNoteWithId(noteId) ?: return view
retainInstance = true retainInstance = true
val layoutToInflate = if (config!!.enableLineWrap) R.layout.note_view_static else R.layout.note_view_horiz_scrollable val layoutToInflate = if (config!!.enableLineWrap) R.layout.note_view_static else R.layout.note_view_horiz_scrollable
@ -72,47 +72,12 @@ class NoteFragment : androidx.fragment.app.Fragment() {
override fun onResume() { override fun onResume() {
super.onResume() super.onResume()
val config = config!! NotesHelper(activity!!).getNoteWithId(noteId) {
view.notes_view.apply { if (it != null) {
typeface = if (config.monospacedFont) Typeface.MONOSPACE else Typeface.DEFAULT note = it
setupFragment()
val fileContents = note.getNoteStoredValue()
if (fileContents == null) {
(activity as MainActivity).deleteNote(false)
return
}
setColors(config.textColor, config.primaryColor, config.backgroundColor)
setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getTextSize())
gravity = getTextGravity()
if (text.toString() != fileContents) {
if (!skipTextUpdating) {
setText(fileContents)
}
skipTextUpdating = false
setSelection(if (config.placeCursorToEnd) text.length else 0)
}
if (config.showKeyboard) {
requestFocus()
}
imeOptions = if (config.useIncognitoMode) {
imeOptions or EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING
} else {
imeOptions.removeBit(EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING)
} }
} }
if (config.showWordCount) {
view.notes_counter.beVisible()
view.notes_counter.setTextColor(config.textColor)
setWordCounter(view.notes_view.text.toString())
} else {
view.notes_counter.beGone()
}
view.notes_view.addTextChangedListener(textWatcher)
} }
override fun onPause() { override fun onPause() {
@ -151,10 +116,54 @@ class NoteFragment : androidx.fragment.app.Fragment() {
} }
} }
private fun setupFragment() {
val config = config!!
view.notes_view.apply {
typeface = if (config.monospacedFont) Typeface.MONOSPACE else Typeface.DEFAULT
val fileContents = note!!.getNoteStoredValue()
if (fileContents == null) {
(activity as MainActivity).deleteNote(false)
return
}
setColors(config.textColor, config.primaryColor, config.backgroundColor)
setTextSize(TypedValue.COMPLEX_UNIT_PX, context.getTextSize())
gravity = getTextGravity()
if (text.toString() != fileContents) {
if (!skipTextUpdating) {
setText(fileContents)
}
skipTextUpdating = false
setSelection(if (config.placeCursorToEnd) text.length else 0)
}
if (config.showKeyboard) {
requestFocus()
}
imeOptions = if (config.useIncognitoMode) {
imeOptions or EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING
} else {
imeOptions.removeBit(EditorInfo.IME_FLAG_NO_PERSONALIZED_LEARNING)
}
}
if (config.showWordCount) {
view.notes_counter.beVisible()
view.notes_counter.setTextColor(config.textColor)
setWordCounter(view.notes_view.text.toString())
} else {
view.notes_counter.beGone()
}
view.notes_view.addTextChangedListener(textWatcher)
}
fun getNotesView() = view.notes_view fun getNotesView() = view.notes_view
fun saveText(force: Boolean) { fun saveText(force: Boolean) {
if (note.path.isNotEmpty() && !File(note.path).exists()) { if (note!!.path.isNotEmpty() && !File(note!!.path).exists()) {
return return
} }
@ -163,15 +172,15 @@ class NoteFragment : androidx.fragment.app.Fragment() {
} }
val newText = getCurrentNoteViewText() val newText = getCurrentNoteViewText()
val oldText = note.getNoteStoredValue() val oldText = note!!.getNoteStoredValue()
if (newText != null && (newText != oldText || force)) { if (newText != null && (newText != oldText || force)) {
note.value = newText note!!.value = newText
saveNoteValue(note) saveNoteValue(note!!)
context!!.updateWidgets() context!!.updateWidgets()
} }
} }
fun hasUnsavedChanges() = getCurrentNoteViewText() != note.getNoteStoredValue() fun hasUnsavedChanges() = getCurrentNoteViewText() != note!!.getNoteStoredValue()
fun focusEditText() { fun focusEditText() {
view.notes_view.requestFocus() view.notes_view.requestFocus()

View File

@ -7,11 +7,9 @@ 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.getStringValue
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
import java.io.File
import java.util.* import java.util.*
class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHelper(mContext, DB_NAME, null, DB_VERSION) { class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHelper(mContext, DB_NAME, null, DB_VERSION) {
@ -98,67 +96,6 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe
} }
} }
fun getNotes(): ArrayList<Note> {
val notes = ArrayList<Note>()
val cols = arrayOf(COL_ID, COL_TITLE, COL_VALUE, COL_TYPE, COL_PATH)
var cursor: Cursor? = null
try {
cursor = mDb.query(NOTES_TABLE_NAME, cols, null, null, null, null, "$COL_TITLE COLLATE NOCASE ASC")
if (cursor?.moveToFirst() == true) {
do {
try {
val id = cursor.getIntValue(COL_ID)
val title = cursor.getStringValue(COL_TITLE)
val value = cursor.getStringValue(COL_VALUE)
val type = cursor.getIntValue(COL_TYPE)
val path = cursor.getStringValue(COL_PATH) ?: ""
if (path.isNotEmpty() && !File(path).exists()) {
deleteNote(id)
continue
}
val note = Note(id, title, value, type, path)
notes.add(note)
} catch (e: Exception) {
continue
}
} while (cursor.moveToNext())
}
} finally {
cursor?.close()
}
if (notes.isEmpty()) {
val generalNote = mContext.resources.getString(R.string.general_note)
val note = Note(1, generalNote, "", TYPE_NOTE)
insertNote(note)
return arrayListOf(note)
}
return notes
}
fun getNoteWithId(id: Int): Note? {
val cols = arrayOf(COL_TITLE, COL_VALUE, COL_TYPE, COL_PATH)
val selection = "$COL_ID = ?"
val selectionArgs = arrayOf(id.toString())
var note: Note? = null
var cursor: Cursor? = null
try {
cursor = mDb.query(NOTES_TABLE_NAME, cols, selection, selectionArgs, null, null, null)
if (cursor?.moveToFirst() == true) {
val title = cursor.getStringValue(COL_TITLE)
val value = cursor.getStringValue(COL_VALUE)
val type = cursor.getIntValue(COL_TYPE)
val path = cursor.getStringValue(COL_PATH) ?: ""
note = Note(id, title, value, type, path)
}
} finally {
cursor?.close()
}
return note
}
fun getNoteId(path: String): Int { fun getNoteId(path: String): Int {
val cols = arrayOf(COL_ID) val cols = arrayOf(COL_ID)
val selection = "$COL_PATH = ?" val selection = "$COL_PATH = ?"

View File

@ -0,0 +1,44 @@
package com.simplemobiletools.notes.pro.helpers
import android.app.Activity
import com.simplemobiletools.notes.pro.R
import com.simplemobiletools.notes.pro.extensions.notesDB
import com.simplemobiletools.notes.pro.models.Note
import java.io.File
class NotesHelper(val activity: Activity) {
fun getNotes(callback: (notes: ArrayList<Note>) -> Unit) {
Thread {
val notes = activity.notesDB.getNotes() as ArrayList<Note>
val notesToDelete = ArrayList<Note>(notes.size)
notes.forEach {
if (it.path.isNotEmpty() && !File(it.path).exists()) {
activity.notesDB.deleteNote(it)
notesToDelete.add(it)
}
}
notes.removeAll(notesToDelete)
if (notes.isEmpty()) {
val generalNote = activity.resources.getString(R.string.general_note)
val note = Note(null, generalNote, "", TYPE_NOTE)
activity.notesDB.insertOrUpdate(note)
notes.add(note)
}
activity.runOnUiThread {
callback(notes)
}
}.start()
}
fun getNoteWithId(id: Int, callback: (note: Note?) -> Unit) {
Thread {
val note = activity.notesDB.getNoteWithId(id)
activity.runOnUiThread {
callback(note)
}
}.start()
}
}

View File

@ -1,14 +1,11 @@
package com.simplemobiletools.notes.pro.interfaces package com.simplemobiletools.notes.pro.interfaces
import androidx.room.Dao import androidx.room.*
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.simplemobiletools.notes.pro.models.Note import com.simplemobiletools.notes.pro.models.Note
@Dao @Dao
interface NotesDao { interface NotesDao {
@Query("SELECT * FROM notes") @Query("SELECT * FROM notes ORDER BY title COLLATE NOCASE ASC")
fun getNotes(): List<Note> fun getNotes(): List<Note>
@Query("SELECT * FROM notes WHERE id = :id") @Query("SELECT * FROM notes WHERE id = :id")
@ -16,4 +13,7 @@ interface NotesDao {
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertOrUpdate(note: Note): Long fun insertOrUpdate(note: Note): Long
@Delete
fun deleteNote(note: Note)
} }