Added a database to migrate entry storage to later; added checks to determine whether existing entries need to be migrated from preference storage to database

This commit is contained in:
denizk0461
2023-05-30 09:36:57 +02:00
parent 942af61b9d
commit cd075e9560
9 changed files with 119 additions and 13 deletions
+5 -1
View File
@@ -1,6 +1,7 @@
plugins { plugins {
id 'com.android.application' id 'com.android.application'
id 'org.jetbrains.kotlin.android' id 'org.jetbrains.kotlin.android'
id "kotlin-kapt"
} }
android { android {
@@ -11,7 +12,7 @@ android {
applicationId "com.denizk0461.textbasic" applicationId "com.denizk0461.textbasic"
minSdk 23 minSdk 23
targetSdk 33 targetSdk 33
versionCode 10 versionCode 11
versionName "2.0.2" versionName "2.0.2"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
@@ -56,4 +57,7 @@ dependencies {
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation "com.github.skydoves:colorpickerview:2.2.4" implementation "com.github.skydoves:colorpickerview:2.2.4"
implementation "androidx.room:room-runtime:2.5.1"
kapt "androidx.room:room-compiler:2.5.1"
} }
-1
View File
@@ -19,7 +19,6 @@
android:windowSoftInputMode="adjustPan"> android:windowSoftInputMode="adjustPan">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
@@ -0,0 +1,57 @@
package com.denizd.textbasic.db
import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.denizd.textbasic.model.Entry
/**
* Room database that holds all objects that need to be saved to persistent storage.
*/
@Database(
entities = [
Entry::class,
],
version = 1,
exportSchema = true,
)
abstract class AppDatabase : RoomDatabase() {
/**
* The database access object. Database transactions go through this.
*/
abstract fun dao(): EntryDao
companion object {
// The singular instance of this database
private var instance: AppDatabase? = null
/**
* Retrieve the instance of the database.
*
* @param context used to resolve the object
* @return the database
*/
fun getInstance(context: Context): AppDatabase {
/*
* Check if an object has already been instantiated. Only create a new instance if none
* exists.
*/
if (instance == null) {
synchronized(AppDatabase::class) {
instance = Room
.databaseBuilder(
context.applicationContext,
AppDatabase::class.java,
"text_basic_db",
)
// .fallbackToDestructiveMigration()
.build()
}
}
// Instance can never be null at this point
return instance!!
}
}
}
@@ -0,0 +1,20 @@
package com.denizd.textbasic.db
import androidx.lifecycle.LiveData
import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import com.denizd.textbasic.model.Entry
@Dao
interface EntryDao {
@Query("SELECT * FROM entries ORDER BY position")
fun getAllEntries(): LiveData<List<Entry>>
@Insert
fun insertEntries(newEntries: List<Entry>)
@Query("DELETE FROM entries")
fun deleteAllEntries()
}
@@ -6,6 +6,8 @@ import com.denizd.textbasic.util.SettingsPreference
class QuoteStorage private constructor(context: Context) { class QuoteStorage private constructor(context: Context) {
private val dao: EntryDao = AppDatabase.getInstance(context).dao()
companion object { companion object {
private lateinit var storage: QuoteStorage private lateinit var storage: QuoteStorage
@@ -26,8 +28,6 @@ class QuoteStorage private constructor(context: Context) {
private const val KEY_QUOTES = "quotes" private const val KEY_QUOTES = "quotes"
private const val KEY_QUOTE_COUNTER = "quotecounter" private const val KEY_QUOTE_COUNTER = "quotecounter"
const val KEY_TEXT_SIZE = "textsize" const val KEY_TEXT_SIZE = "textsize"
private const val KEY_INVERTED = "inverted"
private const val KEY_HIGH_CONTRAST = "hicontrast"
const val KEY_TEXT_TRANSPARENCY = "transparency_text" const val KEY_TEXT_TRANSPARENCY = "transparency_text"
const val KEY_BG_TRANSPARENCY = "transparency" const val KEY_BG_TRANSPARENCY = "transparency"
private const val KEY_RANDOM = "random" private const val KEY_RANDOM = "random"
@@ -37,7 +37,6 @@ class QuoteStorage private constructor(context: Context) {
private const val KEY_TYPEFACE_STYLE = "typefacestyle" private const val KEY_TYPEFACE_STYLE = "typefacestyle"
private const val KEY_AVG_WIDTH = "avgwidth" private const val KEY_AVG_WIDTH = "avgwidth"
private const val KEY_AVG_HEIGHT = "avgheight" private const val KEY_AVG_HEIGHT = "avgheight"
private const val KEY_OUTLINE_SIZE = "outlinesize"
const val KEY_OUTLINE_SIZE_NEW = "outlinesizeint" const val KEY_OUTLINE_SIZE_NEW = "outlinesizeint"
} }
@@ -131,4 +130,13 @@ class QuoteStorage private constructor(context: Context) {
.putString(KEY_QUOTES, quotes.joinToString(SEPARATOR.toString())) .putString(KEY_QUOTES, quotes.joinToString(SEPARATOR.toString()))
.apply() .apply()
} }
fun needsMigration(): Boolean = prefs.getBoolean(SettingsPreference.NEEDS_MIGRATION.key, true)
fun migrateEntries() {
// TODO migrate entries
prefs.edit().putBoolean(SettingsPreference.NEEDS_MIGRATION.key, false).apply()
}
fun noMigrationNecessary() {
prefs.edit().putBoolean(SettingsPreference.NEEDS_MIGRATION.key, false).apply()
}
} }
@@ -23,6 +23,15 @@ class QuoteFragment : BaseFragment(R.layout.fragment_quote), QuoteAdapter.OnDele
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
storage = QuoteStorage.getInstance(context) storage = QuoteStorage.getInstance(context)
if (storage.getAllQuotes().isEmpty()) {
storage.noMigrationNecessary()
}
if (storage.needsMigration()) {
storage.migrateEntries()
}
quoteAdapter = QuoteAdapter(storage.getAllQuotes(), this) quoteAdapter = QuoteAdapter(storage.getAllQuotes(), this)
binding.recyclerView.layoutManager = LinearLayoutManager(context) binding.recyclerView.layoutManager = LinearLayoutManager(context)
@@ -0,0 +1,14 @@
package com.denizd.textbasic.model
import androidx.room.Entity
import androidx.room.PrimaryKey
/**
* Stores a single entry. For use with a Room database.
*/
@Entity(tableName = "entries")
data class Entry(
@PrimaryKey val id: Int,
val text: String,
val position: Int,
)
@@ -1,6 +0,0 @@
package com.denizd.textbasic.model
/**
* Stores a single quote. For potential use with a Room database.
*/
data class Quote(val id: Int, var text: String)
@@ -4,10 +4,11 @@ import com.denizd.textbasic.db.QuoteStorage
enum class SettingsPreference(val key: String) { enum class SettingsPreference(val key: String) {
TEXT_SIZE(QuoteStorage.KEY_TEXT_SIZE), TEXT_SIZE(QuoteStorage.KEY_TEXT_SIZE),
TEXT_TRANSPARENCY(QuoteStorage.KEY_TEXT_TRANSPARENCY),
TEXT_COLOUR("textcolour"), TEXT_COLOUR("textcolour"),
HIGHLIGHT_INTENSITY(QuoteStorage.KEY_OUTLINE_SIZE_NEW), HIGHLIGHT_INTENSITY(QuoteStorage.KEY_OUTLINE_SIZE_NEW),
HIGHLIGHT_TRANSPARENCY(QuoteStorage.KEY_BG_TRANSPARENCY),
HIGHLIGHT_COLOUR("highlightcolour"), HIGHLIGHT_COLOUR("highlightcolour"),
// Whether a migration from preference storage to Room is needed for the entries
NEEDS_MIGRATION("needs_migration"),
; ;
} }