mirror of
				https://github.com/SimpleMobileTools/Simple-Notes.git
				synced 2025-06-05 17:00:23 +02:00 
			
		
		
		
	preparing the migration to Room database
This commit is contained in:
		| @@ -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' | ||||
| } | ||||
|   | ||||
| @@ -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() | ||||
|         } | ||||
|  | ||||
|   | ||||
| @@ -89,7 +89,7 @@ class WidgetConfigureActivity : SimpleActivity() { | ||||
|     private fun showNoteSelector() { | ||||
|         val items = ArrayList<RadioItem>() | ||||
|         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 | ||||
|   | ||||
| @@ -18,7 +18,7 @@ class NotesPagerAdapter(fm: FragmentManager, val notes: List<Note>, 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]!! | ||||
|   | ||||
| @@ -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 { | ||||
|  | ||||
|             } | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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) { | ||||
|   | ||||
| @@ -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<Note> | ||||
|  | ||||
|     @Query("SELECT * FROM notes WHERE id = :id") | ||||
|     fun getNoteWithId(id: Int): Note? | ||||
|  | ||||
|     @Insert(onConflict = OnConflictStrategy.REPLACE) | ||||
|     fun insertOrUpdate(note: Note): Long | ||||
| } | ||||
| @@ -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 { | ||||
|   | ||||
| @@ -0,0 +1,7 @@ | ||||
| package com.simplemobiletools.notes.pro.objects | ||||
|  | ||||
| import java.util.concurrent.Executors | ||||
|  | ||||
| object MyExecutor { | ||||
|     val myExecutor = Executors.newSingleThreadExecutor() | ||||
| } | ||||
		Reference in New Issue
	
	Block a user