Skip to content

Commit

Permalink
perf: 优化创建文件夹逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Nov 8, 2023
1 parent 7357897 commit d32f55a
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 54 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/li/songe/gkd/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -65,6 +66,7 @@ class App : Application() {
saveDays = 7
}

initFolder()
appScope.launchTry(Dispatchers.IO) {
initStore()
initAppState()
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/li/songe/gkd/data/SubsItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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() {
Expand All @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/li/songe/gkd/db/DbSet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() }
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/li/songe/gkd/ui/HomePageVm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ->
Expand Down
31 changes: 0 additions & 31 deletions app/src/main/java/li/songe/gkd/util/Cache.kt

This file was deleted.

36 changes: 20 additions & 16 deletions app/src/main/java/li/songe/gkd/util/FolderExt.kt
Original file line number Diff line number Diff line change
@@ -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") }
}

0 comments on commit d32f55a

Please sign in to comment.