Skip to content

Commit 51b2d36

Browse files
committed
speed up calculation
Signed-off-by: alperozturk96 <alper_ozturk@proton.me>
1 parent 5f5b9b3 commit 51b2d36

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

app/src/main/java/com/owncloud/android/datamodel/SyncedFolderObserver.kt

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ package com.owncloud.android.datamodel
99

1010
import com.nextcloud.client.account.User
1111
import com.nextcloud.client.database.dao.SyncedFolderDao
12-
import com.nextcloud.client.database.entity.SyncedFolderEntity
1312
import com.owncloud.android.lib.resources.files.model.ServerFileInterface
1413
import kotlinx.coroutines.CoroutineScope
1514
import kotlinx.coroutines.Dispatchers
1615
import kotlinx.coroutines.Job
1716
import kotlinx.coroutines.SupervisorJob
1817
import kotlinx.coroutines.flow.distinctUntilChanged
1918
import kotlinx.coroutines.launch
20-
import java.util.concurrent.CopyOnWriteArraySet
2119

2220
object SyncedFolderObserver {
2321

24-
private val entities = CopyOnWriteArraySet<SyncedFolderEntity>()
22+
@Volatile
23+
private var syncedFoldersMap = mapOf<String, Set<String>>()
24+
2525
private var job: Job? = null
2626
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob())
2727

@@ -32,23 +32,23 @@ object SyncedFolderObserver {
3232
dao.getAllAsFlow()
3333
.distinctUntilChanged()
3434
.collect { updatedEntities ->
35-
entities.clear()
36-
entities.addAll(updatedEntities)
35+
syncedFoldersMap = updatedEntities
36+
.filter { it.remotePath != null && it.account != null }
37+
.groupBy { it.account!! }
38+
.mapValues { (_, entities) ->
39+
entities.map { it.remotePath!!.trimEnd('/') }.toSet()
40+
}
3741
}
3842
}
3943
}
4044

45+
@Suppress("ReturnCount")
4146
fun isAutoUploadFolder(file: ServerFileInterface, user: User): Boolean {
42-
val normalizedRemotePath = file.remotePath
43-
.trimEnd('/')
44-
45-
return entities
46-
.stream()
47-
.filter { it.account == user.accountName }
48-
.filter { it.remotePath != null }
49-
.anyMatch { entity ->
50-
val entityRemotePath = entity.remotePath?.trimEnd('/') ?: return@anyMatch false
51-
normalizedRemotePath.contains(entityRemotePath)
52-
}
47+
val accountFolders = syncedFoldersMap[user.accountName] ?: return false
48+
val normalizedRemotePath = file.remotePath.trimEnd('/')
49+
if (normalizedRemotePath.isEmpty()) return false
50+
return accountFolders.any { entityPath ->
51+
normalizedRemotePath == entityPath
52+
}
5353
}
5454
}

0 commit comments

Comments
 (0)