Archived
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:
@@ -19,7 +19,6 @@
|
||||
android:windowSoftInputMode="adjustPan">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</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) {
|
||||
|
||||
private val dao: EntryDao = AppDatabase.getInstance(context).dao()
|
||||
|
||||
companion object {
|
||||
|
||||
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_QUOTE_COUNTER = "quotecounter"
|
||||
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_BG_TRANSPARENCY = "transparency"
|
||||
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_AVG_WIDTH = "avgwidth"
|
||||
private const val KEY_AVG_HEIGHT = "avgheight"
|
||||
private const val KEY_OUTLINE_SIZE = "outlinesize"
|
||||
const val KEY_OUTLINE_SIZE_NEW = "outlinesizeint"
|
||||
}
|
||||
|
||||
@@ -131,4 +130,13 @@ class QuoteStorage private constructor(context: Context) {
|
||||
.putString(KEY_QUOTES, quotes.joinToString(SEPARATOR.toString()))
|
||||
.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)
|
||||
|
||||
storage = QuoteStorage.getInstance(context)
|
||||
|
||||
if (storage.getAllQuotes().isEmpty()) {
|
||||
storage.noMigrationNecessary()
|
||||
}
|
||||
|
||||
if (storage.needsMigration()) {
|
||||
storage.migrateEntries()
|
||||
}
|
||||
|
||||
quoteAdapter = QuoteAdapter(storage.getAllQuotes(), this)
|
||||
|
||||
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) {
|
||||
TEXT_SIZE(QuoteStorage.KEY_TEXT_SIZE),
|
||||
TEXT_TRANSPARENCY(QuoteStorage.KEY_TEXT_TRANSPARENCY),
|
||||
TEXT_COLOUR("textcolour"),
|
||||
HIGHLIGHT_INTENSITY(QuoteStorage.KEY_OUTLINE_SIZE_NEW),
|
||||
HIGHLIGHT_TRANSPARENCY(QuoteStorage.KEY_BG_TRANSPARENCY),
|
||||
HIGHLIGHT_COLOUR("highlightcolour"),
|
||||
|
||||
// Whether a migration from preference storage to Room is needed for the entries
|
||||
NEEDS_MIGRATION("needs_migration"),
|
||||
;
|
||||
}
|
||||
Reference in New Issue
Block a user