From e7ff0c6e1517d4a44f341ed4b868237e5b4182a2 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 7 Nov 2018 12:18:52 +0100 Subject: [PATCH] preparing the migration to Room database --- app/build.gradle | 5 ++ .../notes/pro/activities/MainActivity.kt | 18 +++++-- .../pro/activities/WidgetConfigureActivity.kt | 4 +- .../notes/pro/adapters/NotesPagerAdapter.kt | 2 +- .../notes/pro/databases/NotesDatabase.kt | 51 +++++++++++++++++++ .../notes/pro/dialogs/OpenNoteDialog.kt | 2 +- .../notes/pro/extensions/Context.kt | 4 ++ .../notes/pro/helpers/DBHelper.kt | 6 +-- .../notes/pro/interfaces/NotesDao.kt | 19 +++++++ .../notes/pro/models/Note.kt | 13 ++++- .../notes/pro/objects/MyExecutor.kt | 7 +++ 11 files changed, 118 insertions(+), 13 deletions(-) create mode 100644 app/src/main/kotlin/com/simplemobiletools/notes/pro/databases/NotesDatabase.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/notes/pro/interfaces/NotesDao.kt create mode 100644 app/src/main/kotlin/com/simplemobiletools/notes/pro/objects/MyExecutor.kt diff --git a/app/build.gradle b/app/build.gradle index 334dacc2..c43cfe25 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,6 +1,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' +apply plugin: 'kotlin-kapt' def keystorePropertiesFile = rootProject.file("keystore.properties") def keystoreProperties = new Properties() @@ -51,4 +52,8 @@ android { dependencies { implementation 'com.simplemobiletools:commons:5.3.13' + + kapt 'androidx.room:room-compiler:2.0.0' + implementation 'androidx.room:room-runtime:2.0.0' + annotationProcessor 'androidx.room:room-compiler:2.0.0' } 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 4f95f644..28efba3a 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 @@ -25,6 +25,7 @@ import com.simplemobiletools.commons.views.MyEditText import com.simplemobiletools.notes.pro.BuildConfig 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 @@ -110,6 +111,13 @@ class MainActivity : SimpleActivity() { storeStateVariables() } + override fun onDestroy() { + super.onDestroy() + if (!isChangingConfigurations) { + NotesDatabase.destroyInstance() + } + } + override fun onCreateOptionsMenu(menu: Menu): Boolean { menuInflater.inflate(R.menu.menu, menu) menu.apply { @@ -218,7 +226,7 @@ class MainActivity : SimpleActivity() { if (it as Int == 0) { displayNewNoteDialog(text) } else { - updateSelectedNote(notes[it - 1].id) + updateSelectedNote(notes[it - 1].id!!) addTextToCurrentNote(if (mCurrentNote.value.isEmpty()) text else "\n$text") } } @@ -249,7 +257,7 @@ class MainActivity : SimpleActivity() { onPageChangeListener { mCurrentNote = mNotes[it] - config.currentNoteId = mCurrentNote.id + config.currentNoteId = mCurrentNote.id!! } } @@ -561,13 +569,13 @@ class MainActivity : SimpleActivity() { } private fun doDeleteNote(note: Note, deleteFile: Boolean) { - dbHelper.deleteNote(mCurrentNote.id) + dbHelper.deleteNote(mCurrentNote.id!!) mNotes = dbHelper.getNotes() val firstNoteId = mNotes[0].id - updateSelectedNote(firstNoteId) + updateSelectedNote(firstNoteId!!) if (config.widgetNoteId == note.id) { - config.widgetNoteId = mCurrentNote.id + config.widgetNoteId = mCurrentNote.id!! updateWidgets() } 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 ce661f4b..88f36898 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 @@ -89,7 +89,7 @@ class WidgetConfigureActivity : SimpleActivity() { private fun showNoteSelector() { val items = ArrayList() mNotes.forEach { - items.add(RadioItem(it.id, it.title)) + items.add(RadioItem(it.id!!, it.title)) } RadioGroupDialog(this, items, mCurrentNoteId) { @@ -99,7 +99,7 @@ class WidgetConfigureActivity : SimpleActivity() { } private fun updateCurrentNote(note: Note) { - mCurrentNoteId = note.id + mCurrentNoteId = note.id!! notes_picker_value.text = note.title val sampleValue = if (note.value.isEmpty() || mIsCustomizingColors) getString(R.string.widget_config) else note.value notes_view.text = sampleValue diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt index 66013c2f..4b47bd3c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/adapters/NotesPagerAdapter.kt @@ -18,7 +18,7 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List, val activity override fun getItem(position: Int): NoteFragment { val bundle = Bundle() val id = notes[position].id - bundle.putInt(NOTE_ID, id) + bundle.putInt(NOTE_ID, id!!) if (fragments.containsKey(position)) { return fragments[position]!! diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/databases/NotesDatabase.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/databases/NotesDatabase.kt new file mode 100644 index 00000000..f62fa771 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/databases/NotesDatabase.kt @@ -0,0 +1,51 @@ +package com.simplemobiletools.notes.pro.databases + +import android.content.Context +import androidx.room.Database +import androidx.room.Room +import androidx.room.RoomDatabase +import androidx.sqlite.db.SupportSQLiteDatabase +import com.simplemobiletools.notes.pro.interfaces.NotesDao +import com.simplemobiletools.notes.pro.models.Note +import com.simplemobiletools.notes.pro.objects.MyExecutor +import java.util.concurrent.Executors + +@Database(entities = [Note::class], version = 1) +abstract class NotesDatabase : RoomDatabase() { + + abstract fun NotesDao(): NotesDao + + companion object { + private var db: NotesDatabase? = null + + fun getInstance(context: Context): NotesDatabase { + if (db == null) { + synchronized(NotesDatabase::class) { + if (db == null) { + db = Room.databaseBuilder(context.applicationContext, NotesDatabase::class.java, "notes.db") + .setQueryExecutor(MyExecutor.myExecutor) + .addCallback(object : Callback() { + override fun onCreate(db: SupportSQLiteDatabase) { + super.onCreate(db) + insertFirstNote() + } + }) + .build() + db!!.openHelper.setWriteAheadLoggingEnabled(true) + } + } + } + return db!! + } + + fun destroyInstance() { + db = null + } + + private fun insertFirstNote() { + Executors.newSingleThreadExecutor().execute { + + } + } + } +} 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 eb07f3f6..17da43c4 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 @@ -27,7 +27,7 @@ class OpenNoteDialog(val activity: Activity, val callback: (checkedId: Int) -> U open_note_item_radio_button.apply { text = note.title isChecked = note.id == activity.config.currentNoteId - id = note.id + id = note.id!! setOnClickListener { callback(id) diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/extensions/Context.kt index fb674abf..44d2df28 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/extensions/Context.kt @@ -5,12 +5,16 @@ import android.content.ComponentName import android.content.Context import android.content.Intent import com.simplemobiletools.notes.pro.R +import com.simplemobiletools.notes.pro.databases.NotesDatabase import com.simplemobiletools.notes.pro.helpers.* +import com.simplemobiletools.notes.pro.interfaces.NotesDao val Context.config: Config get() = Config.newInstance(applicationContext) val Context.dbHelper: DBHelper get() = DBHelper.newInstance(applicationContext) +val Context.notesDB: NotesDao get() = NotesDatabase.getInstance(applicationContext).NotesDao() + fun Context.getTextSize() = when (config.fontSize) { FONT_SIZE_SMALL -> resources.getDimension(R.dimen.smaller_text_size) FONT_SIZE_LARGE -> resources.getDimension(R.dimen.big_text_size) 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 fe762a11..bffd276b 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 @@ -170,17 +170,17 @@ class DBHelper private constructor(private val mContext: Context) : SQLiteOpenHe fun updateNoteValue(note: Note) { val values = ContentValues().apply { put(COL_VALUE, note.value) } - updateNote(note.id, values) + updateNote(note.id!!, values) } fun updateNoteTitle(note: Note) { val values = ContentValues().apply { put(COL_TITLE, note.title) } - updateNote(note.id, values) + updateNote(note.id!!, values) } fun updateNotePath(note: Note) { val values = ContentValues().apply { put(COL_PATH, note.path) } - updateNote(note.id, values) + updateNote(note.id!!, values) } private fun updateNote(id: Int, values: ContentValues) { 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 new file mode 100644 index 00000000..3cfbb554 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/interfaces/NotesDao.kt @@ -0,0 +1,19 @@ +package com.simplemobiletools.notes.pro.interfaces + +import androidx.room.Dao +import androidx.room.Insert +import androidx.room.OnConflictStrategy +import androidx.room.Query +import com.simplemobiletools.notes.pro.models.Note + +@Dao +interface NotesDao { + @Query("SELECT * FROM notes") + fun getNotes(): List + + @Query("SELECT * FROM notes WHERE id = :id") + fun getNoteWithId(id: Int): Note? + + @Insert(onConflict = OnConflictStrategy.REPLACE) + fun insertOrUpdate(note: Note): Long +} diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/models/Note.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/models/Note.kt index 3b462d2e..efff0898 100644 --- a/app/src/main/kotlin/com/simplemobiletools/notes/pro/models/Note.kt +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/models/Note.kt @@ -1,9 +1,20 @@ package com.simplemobiletools.notes.pro.models +import androidx.room.ColumnInfo +import androidx.room.Entity +import androidx.room.Index +import androidx.room.PrimaryKey import java.io.File import java.io.FileNotFoundException -data class Note(var id: Int, var title: String, var value: String, val type: Int, var path: String = "") { +@Entity(tableName = "notes", indices = [(Index(value = ["id"], unique = true))]) +data class Note( + @PrimaryKey(autoGenerate = true) var id: Int?, + @ColumnInfo(name = "title") var title: String, + @ColumnInfo(name = "value") var value: String, + @ColumnInfo(name = "type") var type: Int, + @ColumnInfo(name = "path") var path: String = "") { + fun getNoteStoredValue(): String? { return if (path.isNotEmpty()) { return try { diff --git a/app/src/main/kotlin/com/simplemobiletools/notes/pro/objects/MyExecutor.kt b/app/src/main/kotlin/com/simplemobiletools/notes/pro/objects/MyExecutor.kt new file mode 100644 index 00000000..a1e1aaa1 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/notes/pro/objects/MyExecutor.kt @@ -0,0 +1,7 @@ +package com.simplemobiletools.notes.pro.objects + +import java.util.concurrent.Executors + +object MyExecutor { + val myExecutor = Executors.newSingleThreadExecutor() +}