mirror of
https://github.com/gkd-kit/gkd.git
synced 2024-11-16 11:42:22 +08:00
perf: 优化创建文件夹逻辑
This commit is contained in:
parent
7357897ce2
commit
d32f55a82c
|
@ -13,6 +13,7 @@ import li.songe.gkd.data.DeviceInfo
|
||||||
import li.songe.gkd.debug.clearHttpSubs
|
import li.songe.gkd.debug.clearHttpSubs
|
||||||
import li.songe.gkd.notif.initChannel
|
import li.songe.gkd.notif.initChannel
|
||||||
import li.songe.gkd.util.initAppState
|
import li.songe.gkd.util.initAppState
|
||||||
|
import li.songe.gkd.util.initFolder
|
||||||
import li.songe.gkd.util.initStore
|
import li.songe.gkd.util.initStore
|
||||||
import li.songe.gkd.util.initSubsState
|
import li.songe.gkd.util.initSubsState
|
||||||
import li.songe.gkd.util.launchTry
|
import li.songe.gkd.util.launchTry
|
||||||
|
@ -65,6 +66,7 @@ class App : Application() {
|
||||||
saveDays = 7
|
saveDays = 7
|
||||||
}
|
}
|
||||||
|
|
||||||
|
initFolder()
|
||||||
appScope.launchTry(Dispatchers.IO) {
|
appScope.launchTry(Dispatchers.IO) {
|
||||||
initStore()
|
initStore()
|
||||||
initAppState()
|
initAppState()
|
||||||
|
|
|
@ -16,7 +16,7 @@ import kotlinx.coroutines.withContext
|
||||||
import kotlinx.parcelize.IgnoredOnParcel
|
import kotlinx.parcelize.IgnoredOnParcel
|
||||||
import kotlinx.parcelize.Parcelize
|
import kotlinx.parcelize.Parcelize
|
||||||
import li.songe.gkd.db.DbSet
|
import li.songe.gkd.db.DbSet
|
||||||
import li.songe.gkd.util.FolderExt
|
import li.songe.gkd.util.subsFolder
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
@Entity(
|
@Entity(
|
||||||
|
@ -37,7 +37,7 @@ data class SubsItem(
|
||||||
|
|
||||||
@IgnoredOnParcel
|
@IgnoredOnParcel
|
||||||
val subsFile by lazy {
|
val subsFile by lazy {
|
||||||
File(FolderExt.subsFolder.absolutePath.plus("/${id}.json"))
|
File(subsFolder.absolutePath.plus("/${id}.json"))
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun removeAssets() {
|
suspend fun removeAssets() {
|
||||||
|
@ -53,7 +53,7 @@ data class SubsItem(
|
||||||
|
|
||||||
fun getSubscriptionRaw(subsItemId: Long): SubscriptionRaw? {
|
fun getSubscriptionRaw(subsItemId: Long): SubscriptionRaw? {
|
||||||
return try {
|
return try {
|
||||||
val file = File(FolderExt.subsFolder.absolutePath.plus("/${subsItemId}.json"))
|
val file = File(subsFolder.absolutePath.plus("/${subsItemId}.json"))
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,15 +15,15 @@ import li.songe.gkd.appScope
|
||||||
import li.songe.gkd.data.SubsItem
|
import li.songe.gkd.data.SubsItem
|
||||||
import li.songe.gkd.data.SubscriptionRaw
|
import li.songe.gkd.data.SubscriptionRaw
|
||||||
import li.songe.gkd.util.DEFAULT_SUBS_UPDATE_URL
|
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.Singleton
|
||||||
|
import li.songe.gkd.util.dbFolder
|
||||||
import li.songe.gkd.util.launchTry
|
import li.songe.gkd.util.launchTry
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
|
||||||
object DbSet {
|
object DbSet {
|
||||||
private val appDb by lazy {
|
private val appDb by lazy {
|
||||||
Room.databaseBuilder(
|
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()
|
).addCallback(createCallback()).fallbackToDestructiveMigration().build()
|
||||||
}
|
}
|
||||||
val subsItemDao by lazy { appDb.subsItemDao() }
|
val subsItemDao by lazy { appDb.subsItemDao() }
|
||||||
|
|
|
@ -25,10 +25,10 @@ import li.songe.gkd.data.SubscriptionRaw
|
||||||
import li.songe.gkd.db.DbSet
|
import li.songe.gkd.db.DbSet
|
||||||
import li.songe.gkd.debug.SnapshotExt
|
import li.songe.gkd.debug.SnapshotExt
|
||||||
import li.songe.gkd.util.FILE_UPLOAD_URL
|
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.LoadStatus
|
||||||
import li.songe.gkd.util.Singleton
|
import li.songe.gkd.util.Singleton
|
||||||
import li.songe.gkd.util.checkUpdate
|
import li.songe.gkd.util.checkUpdate
|
||||||
|
import li.songe.gkd.util.dbFolder
|
||||||
import li.songe.gkd.util.launchTry
|
import li.songe.gkd.util.launchTry
|
||||||
import li.songe.gkd.util.storeFlow
|
import li.songe.gkd.util.storeFlow
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
@ -61,7 +61,7 @@ class HomePageVm @Inject constructor() : ViewModel() {
|
||||||
}
|
}
|
||||||
appScope.launchTry(Dispatchers.IO) {
|
appScope.launchTry(Dispatchers.IO) {
|
||||||
// 迁移快照记录
|
// 迁移快照记录
|
||||||
val oldDbFile = File(FolderExt.dbFolder, "snapshot.db")
|
val oldDbFile = File(dbFolder, "snapshot.db")
|
||||||
if (oldDbFile.exists()) {
|
if (oldDbFile.exists()) {
|
||||||
SnapshotExt.snapshotDir.walk().maxDepth(1).filter { f -> f.isDirectory }
|
SnapshotExt.snapshotDir.walk().maxDepth(1).filter { f -> f.isDirectory }
|
||||||
.mapNotNull { f -> f.name.toLongOrNull() }.forEach { snapshotId ->
|
.mapNotNull { f -> f.name.toLongOrNull() }.forEach { snapshotId ->
|
||||||
|
|
|
@ -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()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,23 +1,27 @@
|
||||||
package li.songe.gkd.util
|
package li.songe.gkd.util
|
||||||
|
|
||||||
import com.blankj.utilcode.util.LogUtils
|
|
||||||
import li.songe.gkd.app
|
import li.songe.gkd.app
|
||||||
import java.io.File
|
|
||||||
|
|
||||||
object FolderExt {
|
private val filesDir by lazy {
|
||||||
private fun createFolder(name: String): File {
|
app.getExternalFilesDir(null) ?: app.filesDir
|
||||||
return File(
|
}
|
||||||
app.getExternalFilesDir(name)?.absolutePath
|
val dbFolder by lazy { filesDir.resolve("db") }
|
||||||
?: app.filesDir.absolutePath.plus(name)
|
val subsFolder by lazy { filesDir.resolve("subscription") }
|
||||||
).apply {
|
val snapshotFolder by lazy { filesDir.resolve("snapshot") }
|
||||||
if (!exists()) {
|
|
||||||
mkdirs()
|
private val cacheDir by lazy {
|
||||||
LogUtils.d("mkdirs", absolutePath)
|
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") }
|
||||||
val dbFolder by lazy { createFolder("db") }
|
|
||||||
val subsFolder by lazy { createFolder("subscription") }
|
fun initFolder() {
|
||||||
val snapshotFolder by lazy { createFolder("snapshot") }
|
listOf(
|
||||||
|
dbFolder, subsFolder, snapshotFolder, snapshotZipDir, newVersionApkDir, logZipDir
|
||||||
|
).forEach { f ->
|
||||||
|
if (!f.exists()) {
|
||||||
|
f.mkdirs()
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user