Skip to content

Commit

Permalink
perf: init load subs file by db
Browse files Browse the repository at this point in the history
  • Loading branch information
lisonge committed Apr 7, 2024
1 parent 80532e4 commit 4ff55c9
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 4 deletions.
3 changes: 1 addition & 2 deletions app/src/main/kotlin/li/songe/gkd/MainViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import li.songe.gkd.data.RawSubscription
import li.songe.gkd.data.SubsItem
Expand Down Expand Up @@ -43,7 +42,7 @@ class MainViewModel : ViewModel() {
id = -2, order = -2, mtime = System.currentTimeMillis()
)
viewModelScope.launchTry(Dispatchers.IO) {
val subsItems = DbSet.subsItemDao.query().first()
val subsItems = DbSet.subsItemDao.queryAll()
if (!subsItems.any { s -> s.id == localSubsItem.id }) {
updateSubscription(
RawSubscription(
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/kotlin/li/songe/gkd/data/SubsItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ data class SubsItem(
@Query("SELECT * FROM subs_item ORDER BY `order`")
fun query(): Flow<List<SubsItem>>

@Query("SELECT * FROM subs_item ORDER BY `order`")
fun queryAll(): List<SubsItem>

@Query("SELECT * FROM subs_item WHERE id=:id")
fun queryById(id: Long): Flow<SubsItem?>
}
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/kotlin/li/songe/gkd/util/SubsState.kt
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ fun initSubsState() {
subsRefreshingFlow.value = true
if (subsFolder.exists() && subsFolder.isDirectory) {
updateSubsFileMutex.withLock {
val fileRegex = Regex("^-?\\d+\\.json$")
val filenames = DbSet.subsItemDao.queryAll().map { s -> "${s.id}.json" }
val files =
subsFolder.listFiles { f -> f.isFile && f.name.matches(fileRegex) }
subsFolder.listFiles { f -> f.isFile && filenames.contains(f.name) }
?: emptyArray()
val subscriptions = files.mapNotNull { f ->
try {
Expand Down

0 comments on commit 4ff55c9

Please sign in to comment.