Refresco del catálogo por versionCode en vez de user_version
El catálogo solo cambia dentro de un APK nuevo y cada build autoincrementa versionCode: comparar el pref CATALOG_INSTALLED_FOR_VERSION_CODE basta. Elimina el paso manual de tocar PRAGMA user_version que se podía olvidar.
This commit is contained in:
1 parent
3137a35448
commit
5810b2ec57
3 files changed
+18
-45
No files matched your search
@@ -4,6 +4,7 @@ import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.database.sqlite.SQLiteOpenHelper
|
||||
import android.os.Build
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
@@ -14,9 +15,9 @@ import java.util.Calendar
|
||||
* Rediseño 2026-10: dos bases de datos separadas.
|
||||
*
|
||||
* - catalogo.sqlite — SOLO catálogo (FRASES, ANIMES), read-only. Se distribuye como
|
||||
* asset en el repo y se edita con herramientas SQL. La versión va en PRAGMA
|
||||
* user_version del propio fichero; la app lo recopia cuando el asset trae una
|
||||
* versión mayor. Regla editorial: los IDs nunca se renumeran, solo se añaden.
|
||||
* asset en el repo y se edita con herramientas SQL. La app lo recopia del asset
|
||||
* cuando cambia el versionCode (el catálogo solo cambia dentro de un APK nuevo).
|
||||
* Regla editorial: los IDs nunca se renumeran, solo se añaden.
|
||||
* - app.sqlite — SOLO estado del usuario (FRASESVI, FRASESFAV, ANIMESON). Nunca se
|
||||
* toca al actualizar el catálogo: los favoritos sobreviven a cualquier actualización
|
||||
* de frases.
|
||||
@@ -36,6 +37,7 @@ class SQLiteHelper(ctx: Context) : SQLiteOpenHelper(ctx, "app.sqlite", null, 1)
|
||||
companion object {
|
||||
private const val CATALOG_NAME = "catalogo.sqlite"
|
||||
private const val LEGACY_NAME = "otaku.sqlite"
|
||||
private const val PREFS = "com.jkanetwork.st.frasesdeanime.PREFS"
|
||||
|
||||
private var instance: SQLiteHelper? = null
|
||||
|
||||
@@ -67,54 +69,26 @@ class SQLiteHelper(ctx: Context) : SQLiteOpenHelper(ctx, "app.sqlite", null, 1)
|
||||
|
||||
/* ---------------- Catálogo (assets -> databases) ---------------- */
|
||||
|
||||
/** Catálogo read-only, recopiado del asset si su user_version es mayor */
|
||||
/** Catálogo read-only. Se recopia cuando cambia el versionCode: el catálogo solo
|
||||
* cambia dentro de un APK nuevo, y cada build autoincrementa versionCode. */
|
||||
@Synchronized
|
||||
private fun catalog(): SQLiteDatabase {
|
||||
catDb?.let { return it }
|
||||
val file = context.getDatabasePath(CATALOG_NAME)
|
||||
if (!file.exists() || installedCatalogVersion(file) < assetCatalogVersion()) {
|
||||
val prefs = context.getSharedPreferences(PREFS, 0)
|
||||
val installedFor = prefs.getInt("CATALOG_INSTALLED_FOR_VERSION_CODE", -1)
|
||||
if (!file.exists() || installedFor != versionCode()) {
|
||||
copyAssetCatalog(file)
|
||||
prefs.edit().putInt("CATALOG_INSTALLED_FOR_VERSION_CODE", versionCode()).commit()
|
||||
}
|
||||
catDb = SQLiteDatabase.openDatabase(file.path, null, SQLiteDatabase.OPEN_READONLY)
|
||||
return catDb!!
|
||||
}
|
||||
|
||||
private fun installedCatalogVersion(file: File): Int {
|
||||
return try {
|
||||
val db = SQLiteDatabase.openDatabase(file.path, null, SQLiteDatabase.OPEN_READONLY)
|
||||
try {
|
||||
db.version
|
||||
} finally {
|
||||
db.close()
|
||||
}
|
||||
} catch (e: Exception) {
|
||||
-1
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* user_version del asset leyendo la cabecera SQLite directamente (offset 60,
|
||||
* 4 bytes big-endian). Así no hay que copiar el asset solo para leer la versión.
|
||||
*/
|
||||
private fun assetCatalogVersion(): Int {
|
||||
return try {
|
||||
context.assets.open(CATALOG_NAME).use { ins ->
|
||||
val header = ByteArray(64)
|
||||
var read = 0
|
||||
while (read < 64) {
|
||||
val r = ins.read(header, read, 64 - read)
|
||||
if (r < 0) break
|
||||
read += r
|
||||
}
|
||||
if (read < 64) return -1
|
||||
((header[60].toInt() and 0xFF) shl 24) or
|
||||
((header[61].toInt() and 0xFF) shl 16) or
|
||||
((header[62].toInt() and 0xFF) shl 8) or
|
||||
(header[63].toInt() and 0xFF)
|
||||
}
|
||||
} catch (e: IOException) {
|
||||
-1
|
||||
}
|
||||
private fun versionCode(): Int {
|
||||
val info = context.packageManager.getPackageInfo(context.packageName, 0)
|
||||
return if (Build.VERSION.SDK_INT >= 28) info.longVersionCode.toInt()
|
||||
else @Suppress("DEPRECATION") info.versionCode
|
||||
}
|
||||
|
||||
private fun copyAssetCatalog(file: File) {
|
||||
|
||||
Reference in new issue
Block a user