perf: 优化创建文件夹逻辑

This commit is contained in:
lisonge 2023-11-08 22:10:51 +08:00
parent 7357897ce2
commit d32f55a82c
6 changed files with 29 additions and 54 deletions

View File

@ -13,6 +13,7 @@ import li.songe.gkd.data.DeviceInfo
import li.songe.gkd.debug.clearHttpSubs
import li.songe.gkd.notif.initChannel
import li.songe.gkd.util.initAppState
import li.songe.gkd.util.initFolder
import li.songe.gkd.util.initStore
import li.songe.gkd.util.initSubsState
import li.songe.gkd.util.launchTry
@ -65,6 +66,7 @@ class App : Application() {
saveDays = 7
}
initFolder()
appScope.launchTry(Dispatchers.IO) {
initStore()
initAppState()

View File

@ -16,7 +16,7 @@ import kotlinx.coroutines.withContext
import kotlinx.parcelize.IgnoredOnParcel
import kotlinx.parcelize.Parcelize
import li.songe.gkd.db.DbSet
import li.songe.gkd.util.FolderExt
import li.songe.gkd.util.subsFolder
import java.io.File
@Entity(
@ -37,7 +37,7 @@ data class SubsItem(
@IgnoredOnParcel
val subsFile by lazy {
File(FolderExt.subsFolder.absolutePath.plus("/${id}.json"))
File(subsFolder.absolutePath.plus("/${id}.json"))
}
suspend fun removeAssets() {
@ -53,7 +53,7 @@ data class SubsItem(
fun getSubscriptionRaw(subsItemId: Long): SubscriptionRaw? {
return try {
val file = File(FolderExt.subsFolder.absolutePath.plus("/${subsItemId}.json"))
val file = File(subsFolder.absolutePath.plus("/${subsItemId}.json"))
if (!file.exists()) {
return null
}

View File

@ -15,15 +15,15 @@ import li.songe.gkd.appScope
import li.songe.gkd.data.SubsItem
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.dbFolder
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
app, AppDb::class.java, File(dbFolder, "gkd.db").absolutePath
).addCallback(createCallback()).fallbackToDestructiveMigration().build()
}
val subsItemDao by lazy { appDb.subsItemDao() }

View File

@ -25,10 +25,10 @@ import li.songe.gkd.data.SubscriptionRaw
import li.songe.gkd.db.DbSet
import li.songe.gkd.debug.SnapshotExt
import li.songe.gkd.util.FILE_UPLOAD_URL
import li.songe.gkd.util.FolderExt
import li.songe.gkd.util.LoadStatus
import li.songe.gkd.util.Singleton
import li.songe.gkd.util.checkUpdate
import li.songe.gkd.util.dbFolder
import li.songe.gkd.util.launchTry
import li.songe.gkd.util.storeFlow
import java.io.File
@ -61,7 +61,7 @@ class HomePageVm @Inject constructor() : ViewModel() {
}
appScope.launchTry(Dispatchers.IO) {
// 迁移快照记录
val oldDbFile = File(FolderExt.dbFolder, "snapshot.db")
val oldDbFile = File(dbFolder, "snapshot.db")
if (oldDbFile.exists()) {
SnapshotExt.snapshotDir.walk().maxDepth(1).filter { f -> f.isDirectory }
.mapNotNull { f -> f.name.toLongOrNull() }.forEach { snapshotId ->

View File

@ -1,31 +0,0 @@
package li.songe.gkd.util
import com.blankj.utilcode.util.PathUtils
import java.io.File
private val cacheParentDir by lazy {
File(PathUtils.getExternalAppCachePath())
}
val snapshotZipDir by lazy {
File(cacheParentDir, "snapshotZip").apply {
if (!exists()) {
mkdirs()
}
}
}
val newVersionApkDir by lazy {
File(cacheParentDir, "newVersionApk").apply {
if (!exists()) {
mkdirs()
}
}
}
val logZipDir by lazy {
File(cacheParentDir, "logZip").apply {
if (!exists()) {
mkdirs()
}
}
}

View File

@ -1,23 +1,27 @@
package li.songe.gkd.util
import com.blankj.utilcode.util.LogUtils
import li.songe.gkd.app
import java.io.File
object FolderExt {
private fun createFolder(name: String): File {
return File(
app.getExternalFilesDir(name)?.absolutePath
?: app.filesDir.absolutePath.plus(name)
).apply {
if (!exists()) {
mkdirs()
LogUtils.d("mkdirs", absolutePath)
}
private val filesDir by lazy {
app.getExternalFilesDir(null) ?: app.filesDir
}
val dbFolder by lazy { filesDir.resolve("db") }
val subsFolder by lazy { filesDir.resolve("subscription") }
val snapshotFolder by lazy { filesDir.resolve("snapshot") }
private val cacheDir by lazy {
app.externalCacheDir ?: app.cacheDir
}
val snapshotZipDir by lazy { cacheDir.resolve("snapshotZip") }
val newVersionApkDir by lazy { cacheDir.resolve("newVersionApk") }
val logZipDir by lazy { cacheDir.resolve("logZip") }
fun initFolder() {
listOf(
dbFolder, subsFolder, snapshotFolder, snapshotZipDir, newVersionApkDir, logZipDir
).forEach { f ->
if (!f.exists()) {
f.mkdirs()
}
}
val dbFolder by lazy { createFolder("db") }
val subsFolder by lazy { createFolder("subscription") }
val snapshotFolder by lazy { createFolder("snapshot") }
}