Skip to content

Commit

Permalink
fix: 创建数据库时添加默认订阅失败
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Oct 10, 2023
1 parent 0cbded5 commit f270910
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 31 deletions.
12 changes: 0 additions & 12 deletions app/src/main/java/li/songe/gkd/data/SubsItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,6 @@ data class SubsItem(
File(FolderExt.subsFolder.absolutePath.plus("/${id}.json"))
}

fun toContentValues(): ContentValues {
val values = ContentValues()
values.put("id", id)
values.put("ctime", ctime)
values.put("mtime", mtime)
values.put("enable", enable)
values.put("enable_update", enableUpdate)
values.put("`order`", order)
values.put("update_url", updateUrl)
return values
}

suspend fun removeAssets() {
DbSet.subsItemDao.delete(this)
DbSet.subsConfigDao.delete(id)
Expand Down
34 changes: 15 additions & 19 deletions app/src/main/java/li/songe/gkd/db/DbSet.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package li.songe.gkd.db

import android.database.sqlite.SQLiteDatabase
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.sqlite.db.SupportSQLiteDatabase
Expand All @@ -9,6 +8,7 @@ import io.ktor.client.request.get
import io.ktor.client.statement.bodyAsText
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.withTimeout
import li.songe.gkd.app
import li.songe.gkd.appScope
Expand All @@ -17,16 +17,15 @@ import li.songe.gkd.data.SubscriptionRaw
import li.songe.gkd.util.DEFAULT_SUBS_UPDATE_URL
import li.songe.gkd.util.FolderExt
import li.songe.gkd.util.Singleton
import li.songe.gkd.util.isMainProcess
import li.songe.gkd.util.launchTry
import java.io.File

object DbSet {
private val appDb by lazy {
Room.databaseBuilder(
app, AppDb::class.java, File(FolderExt.dbFolder, "gkd.db").absolutePath
).fallbackToDestructiveMigration().enableMultiInstanceInvalidation()
.addCallback(createCallback()).build()
).addCallback(createCallback()).fallbackToDestructiveMigration()
.enableMultiInstanceInvalidation().build()
}
val subsItemDao by lazy { appDb.subsItemDao() }
val subsConfigDao by lazy { appDb.subsConfigDao() }
Expand All @@ -37,34 +36,31 @@ object DbSet {
return object : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
if (!isMainProcess) return
appScope.launchTry(Dispatchers.IO) {
delay(3000)
if (subsItemDao.queryById(0).firstOrNull() != null) {
return@launchTry
}
val defaultSubsItem = SubsItem(
id = 0,
order = 0,
updateUrl = DEFAULT_SUBS_UPDATE_URL,
mtime = System.currentTimeMillis()
)
db.insert(
"subs_item",
SQLiteDatabase.CONFLICT_IGNORE,
defaultSubsItem.toContentValues()
)
try {
val s = System.currentTimeMillis()
val newSubsRaw = withTimeout(3000) {
subsItemDao.insert(defaultSubsItem)
val newSubsRaw = try {
withTimeout(3000) {
SubscriptionRaw.parse(
Singleton.client.get(defaultSubsItem.updateUrl!!).bodyAsText()
)
}
delay(1500 - (System.currentTimeMillis() - s))
val newSubsItem = defaultSubsItem.copy(mtime = System.currentTimeMillis())
newSubsItem.subsFile.writeText(SubscriptionRaw.stringify(newSubsRaw))
subsItemDao.update(newSubsItem)
} catch (e: Exception) {
e.printStackTrace()
LogUtils.d("创建数据库时获取订阅失败")
LogUtils.d("获取默认订阅失败")
LogUtils.d(e)
return@launchTry
}
defaultSubsItem.subsFile.writeText(SubscriptionRaw.stringify(newSubsRaw))
subsItemDao.update(defaultSubsItem.copy(mtime = System.currentTimeMillis()))
}
}
}
Expand Down

0 comments on commit f270910

Please sign in to comment.