@@ -9,19 +9,19 @@ package com.owncloud.android.datamodel
99
1010import com.nextcloud.client.account.User
1111import com.nextcloud.client.database.dao.SyncedFolderDao
12- import com.nextcloud.client.database.entity.SyncedFolderEntity
1312import com.owncloud.android.lib.resources.files.model.ServerFileInterface
1413import kotlinx.coroutines.CoroutineScope
1514import kotlinx.coroutines.Dispatchers
1615import kotlinx.coroutines.Job
1716import kotlinx.coroutines.SupervisorJob
1817import kotlinx.coroutines.flow.distinctUntilChanged
1918import kotlinx.coroutines.launch
20- import java.util.concurrent.CopyOnWriteArraySet
2119
2220object 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