From 885170edfced75cd32c9dcc1c76b34f5282d1e45 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 7 Nov 2018 18:38:27 +0100 Subject: [PATCH] move some note fetchin related functions in Room --- .../notes/pro/activities/MainActivity.kt | 158 ++++++++++-------- .../notes/pro/activities/SettingsActivity.kt | 6 +- .../pro/activities/WidgetConfigureActivity.kt | 9 +- .../notes/pro/adapters/WidgetAdapter.kt | 4 +- .../notes/pro/dialogs/OpenNoteDialog.kt | 15 +- .../notes/pro/fragments/NoteFragment.kt | 101 ++++++----- .../notes/pro/helpers/DBHelper.kt | 63 ------- .../notes/pro/helpers/NotesHelper.kt | 44 +++++ .../notes/pro/interfaces/NotesDao.kt | 10 +- 9 files changed, 212 insertions(+), 198 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt 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 ec99e73e..c621c482 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 @@ -27,11 +27,9 @@ import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.adapters.NotesPagerAdapter import com.simplemobiletools.notes.pro.databases.NotesDatabase import com.simplemobiletools.notes.pro.dialogs.* -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.updateWidgets +import com.simplemobiletools.notes.pro.extensions.* 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.TYPE_NOTE import com.simplemobiletools.notes.pro.models.Note @@ -69,7 +67,7 @@ class MainActivity : SimpleActivity() { intent.apply { if (action == Intent.ACTION_SEND && type == MIME_TEXT_PLAIN) { getStringExtra(Intent.EXTRA_TEXT)?.let { - handleText(it) + handleTextIntent(it) intent.removeExtra(Intent.EXTRA_TEXT) } } @@ -213,21 +211,23 @@ class MainActivity : SimpleActivity() { } } - private fun handleText(text: String) { - val notes = dbHelper.getNotes() - val list = arrayListOf().apply { - add(RadioItem(0, getString(R.string.create_new_note))) - notes.forEachIndexed { index, note -> - add(RadioItem(index + 1, note.title)) + private fun handleTextIntent(text: String) { + NotesHelper(this).getNotes { + val notes = it + val list = arrayListOf().apply { + add(RadioItem(0, getString(R.string.create_new_note))) + notes.forEachIndexed { index, note -> + add(RadioItem(index + 1, note.title)) + } } - } - RadioGroupDialog(this, list, -1, R.string.add_to_note) { - if (it as Int == 0) { - displayNewNoteDialog(text) - } else { - updateSelectedNote(notes[it - 1].id!!) - addTextToCurrentNote(if (mCurrentNote.value.isEmpty()) text else "\n$text") + RadioGroupDialog(this, list, -1, R.string.add_to_note) { + if (it as Int == 0) { + displayNewNoteDialog(text) + } else { + updateSelectedNote(notes[it - 1].id!!) + addTextToCurrentNote(if (mCurrentNote.value.isEmpty()) text else "\n$text") + } } } } @@ -248,21 +248,23 @@ class MainActivity : SimpleActivity() { } private fun initViewPager() { - mNotes = dbHelper.getNotes() - mCurrentNote = mNotes[0] - mAdapter = NotesPagerAdapter(supportFragmentManager, mNotes, this) - view_pager.apply { - adapter = mAdapter - currentItem = getWantedNoteIndex() + NotesHelper(this).getNotes { + mNotes = it + mCurrentNote = mNotes[0] + mAdapter = NotesPagerAdapter(supportFragmentManager, mNotes, this) + view_pager.apply { + adapter = mAdapter + currentItem = getWantedNoteIndex() - onPageChangeListener { - mCurrentNote = mNotes[it] - config.currentNoteId = mCurrentNote.id!! + onPageChangeListener { + mCurrentNote = mNotes[it] + config.currentNoteId = mCurrentNote.id!! + } } - } - if (!config.showKeyboard) { - hideKeyboard() + if (!config.showKeyboard) { + hideKeyboard() + } } } @@ -302,15 +304,19 @@ class MainActivity : SimpleActivity() { } private fun addNewNote(note: Note) { - val id = dbHelper.insertNote(note) - mNotes = dbHelper.getNotes() - showSaveButton = false - invalidateOptionsMenu() - initViewPager() - updateSelectedNote(id) - view_pager.onGlobalLayout { - mAdapter?.focusEditText(getNoteIndexWithId(id)) - } + Thread { + val id = notesDB.insertOrUpdate(note).toInt() + mNotes = notesDB.getNotes().toMutableList() as ArrayList + showSaveButton = false + runOnUiThread { + invalidateOptionsMenu() + initViewPager() + updateSelectedNote(id) + view_pager.onGlobalLayout { + mAdapter?.focusEditText(getNoteIndexWithId(id)) + } + } + }.start() } private fun launchAbout() { @@ -400,13 +406,16 @@ class MainActivity : SimpleActivity() { FilePickerDialog(this, pickFile = false, canAddShowHiddenButton = true) { openFolder(it) { ImportFolderDialog(this, it.path) { - mNotes = dbHelper.getNotes() - showSaveButton = false - invalidateOptionsMenu() - initViewPager() - updateSelectedNote(it) - view_pager.onGlobalLayout { - mAdapter?.focusEditText(getNoteIndexWithId(it)) + val noteId = it + NotesHelper(this).getNotes { + mNotes = it + showSaveButton = false + invalidateOptionsMenu() + initViewPager() + updateSelectedNote(noteId) + view_pager.onGlobalLayout { + mAdapter?.focusEditText(getNoteIndexWithId(noteId)) + } } } } @@ -467,20 +476,22 @@ class MainActivity : SimpleActivity() { private fun exportAllNotes() { ExportFilesDialog(this, mNotes) { parent, extension -> var failCount = 0 - mNotes = dbHelper.getNotes() - mNotes.forEachIndexed { index, note -> - val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension" - val file = File(parent, filename) - if (!filename.isAValidFilename()) { - toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename))) - } else { - exportNoteValueToFile(file.absolutePath, note.value, false) { - if (!it) { - failCount++ - } + NotesHelper(this).getNotes { + mNotes = it + mNotes.forEachIndexed { index, note -> + val filename = if (extension.isEmpty()) note.title else "${note.title}.$extension" + val file = File(parent, filename) + if (!filename.isAValidFilename()) { + toast(String.format(getString(R.string.filename_invalid_characters_placeholder, filename))) + } else { + exportNoteValueToFile(file.absolutePath, note.value, false) { + if (!it) { + failCount++ + } - if (index == mNotes.size - 1) { - toast(if (failCount == 0) R.string.exporting_successful else R.string.exporting_some_entries_failed) + if (index == mNotes.size - 1) { + toast(if (failCount == 0) R.string.exporting_successful else R.string.exporting_some_entries_failed) + } } } } @@ -570,22 +581,23 @@ class MainActivity : SimpleActivity() { private fun doDeleteNote(note: Note, deleteFile: Boolean) { dbHelper.deleteNote(mCurrentNote.id!!) - mNotes = dbHelper.getNotes() + NotesHelper(this).getNotes { + mNotes = it + val firstNoteId = mNotes[0].id + updateSelectedNote(firstNoteId!!) + if (config.widgetNoteId == note.id) { + config.widgetNoteId = mCurrentNote.id!! + updateWidgets() + } - val firstNoteId = mNotes[0].id - updateSelectedNote(firstNoteId!!) - if (config.widgetNoteId == note.id) { - config.widgetNoteId = mCurrentNote.id!! - updateWidgets() - } + invalidateOptionsMenu() + initViewPager() - invalidateOptionsMenu() - initViewPager() - - if (deleteFile) { - deleteFile(FileDirItem(note.path, note.title)) { - if (!it) { - toast(R.string.unknown_error_occurred) + if (deleteFile) { + deleteFile(FileDirItem(note.path, note.title)) { + if (!it) { + toast(R.string.unknown_error_occurred) + } } } } diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/SettingsActivity.kt index 03d2caef..30375299 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/SettingsActivity.kt @@ -11,7 +11,6 @@ import com.simplemobiletools.commons.helpers.isOreoPlus import com.simplemobiletools.commons.models.RadioItem import com.simplemobiletools.notes.pro.R 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.helpers.* import kotlinx.android.synthetic.main.activity_settings.* @@ -109,7 +108,10 @@ class SettingsActivity : SimpleActivity() { } 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_holder.setOnClickListener { settings_show_note_picker.toggle() diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/WidgetConfigureActivity.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/WidgetConfigureActivity.kt index 6c8c471d..9b1cd5fc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/WidgetConfigureActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/activities/WidgetConfigureActivity.kt @@ -18,6 +18,7 @@ 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.helpers.MyWidgetProvider +import com.simplemobiletools.notes.pro.helpers.NotesHelper import com.simplemobiletools.notes.pro.models.Note import com.simplemobiletools.notes.pro.models.Widget import kotlinx.android.synthetic.main.widget_config.* @@ -80,10 +81,12 @@ class WidgetConfigureActivity : SimpleActivity() { mTextColor = config.widgetTextColor updateTextColor() - mNotes = dbHelper.getNotes() mIsCustomizingColors = intent.extras?.getBoolean(IS_CUSTOMIZING_COLORS) ?: false - notes_picker_holder.beVisibleIf(mNotes.size > 1 && !mIsCustomizingColors) - updateCurrentNote(mNotes.first()) + NotesHelper(this).getNotes { + mNotes = it + notes_picker_holder.beVisibleIf(mNotes.size > 1 && !mIsCustomizingColors) + updateCurrentNote(mNotes.first()) + } } private fun showNoteSelector() { diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/WidgetAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/WidgetAdapter.kt index 0e612df3..da4a80df 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/WidgetAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/WidgetAdapter.kt @@ -10,8 +10,8 @@ import com.simplemobiletools.commons.extensions.setTextSize import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.R.id.widget_text_holder 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.notesDB import com.simplemobiletools.notes.pro.helpers.GRAVITY_CENTER import com.simplemobiletools.notes.pro.helpers.GRAVITY_RIGHT 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 { val noteId = intent.getIntExtra(NOTE_ID, 1) 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) { val noteText = note.getNoteStoredValue() ?: "" val textSize = context.getTextSize() / context.resources.displayMetrics.density diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/OpenNoteDialog.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/OpenNoteDialog.kt index 17da43c4..3aa98486 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/OpenNoteDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/dialogs/OpenNoteDialog.kt @@ -1,6 +1,7 @@ package com.simplemobiletools.notes.pro.dialogs import android.app.Activity +import android.view.View import android.view.ViewGroup import android.widget.RadioGroup import androidx.appcompat.app.AlertDialog @@ -10,17 +11,23 @@ import com.simplemobiletools.commons.extensions.setupDialogStuff import com.simplemobiletools.commons.extensions.toast import com.simplemobiletools.notes.pro.R 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.open_note_item.view.* class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Int) -> Unit) { - lateinit var dialog: AlertDialog + private var dialog: AlertDialog? = null init { val view = activity.layoutInflater.inflate(R.layout.dialog_open_note, null) + NotesHelper(activity).getNotes { + initDialog(it, view) + } + } + + private fun initDialog(notes: ArrayList, view: View) { val textColor = activity.config.textColor - val notes = activity.dbHelper.getNotes() notes.forEach { activity.layoutInflater.inflate(R.layout.open_note_item, null).apply { val note = it @@ -31,7 +38,7 @@ class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Int) -> U setOnClickListener { callback(id) - dialog.dismiss() + dialog?.dismiss() } } open_note_item_icon.apply { 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 da7fab81..bb511907 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 @@ -40,7 +40,8 @@ class NoteFragment : androidx.fragment.app.Fragment() { private var isUndoOrRedo = false private var skipTextUpdating = false private var noteId = 0 - lateinit var note: Note + private var note: Note? = null + lateinit var view: ViewGroup 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 noteId = arguments!!.getInt(NOTE_ID) db = context!!.dbHelper - note = db.getNoteWithId(noteId) ?: return view retainInstance = true 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() { super.onResume() - 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) + NotesHelper(activity!!).getNoteWithId(noteId) { + if (it != null) { + note = it + setupFragment() } } - - 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() { @@ -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 saveText(force: Boolean) { - if (note.path.isNotEmpty() && !File(note.path).exists()) { + if (note!!.path.isNotEmpty() && !File(note!!.path).exists()) { return } @@ -163,15 +172,15 @@ class NoteFragment : androidx.fragment.app.Fragment() { } val newText = getCurrentNoteViewText() - val oldText = note.getNoteStoredValue() + val oldText = note!!.getNoteStoredValue() if (newText != null && (newText != oldText || force)) { - note.value = newText - saveNoteValue(note) + note!!.value = newText + saveNoteValue(note!!) context!!.updateWidgets() } } - fun hasUnsavedChanges() = getCurrentNoteViewText() != note.getNoteStoredValue() + fun hasUnsavedChanges() = getCurrentNoteViewText() != note!!.getNoteStoredValue() fun focusEditText() { view.notes_view.requestFocus() 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 98c4788c..b4161367 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,11 +7,9 @@ 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.getStringValue import com.simplemobiletools.notes.pro.R import com.simplemobiletools.notes.pro.models.Note import com.simplemobiletools.notes.pro.models.Widget -import java.io.File import java.util.* 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 { - val notes = ArrayList() - 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 { val cols = arrayOf(COL_ID) val selection = "$COL_PATH = ?" 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 new file mode 100644 index 00000000..073d3006 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/helpers/NotesHelper.kt @@ -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) -> Unit) { + Thread { + val notes = activity.notesDB.getNotes() as ArrayList + val notesToDelete = ArrayList(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() + } +} 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 3cfbb554..7132bc9a 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 @@ -1,14 +1,11 @@ package com.simplemobiletools.notes.pro.interfaces -import androidx.room.Dao -import androidx.room.Insert -import androidx.room.OnConflictStrategy -import androidx.room.Query +import androidx.room.* import com.simplemobiletools.notes.pro.models.Note @Dao interface NotesDao { - @Query("SELECT * FROM notes") + @Query("SELECT * FROM notes ORDER BY title COLLATE NOCASE ASC") fun getNotes(): List @Query("SELECT * FROM notes WHERE id = :id") @@ -16,4 +13,7 @@ interface NotesDao { @Insert(onConflict = OnConflictStrategy.REPLACE) fun insertOrUpdate(note: Note): Long + + @Delete + fun deleteNote(note: Note) }