Skip to content

Commit 79a9fb4

Browse files
committed
speed up parseAndSaveShares
Signed-off-by: alperozturk <[email protected]>
1 parent 29d11cd commit 79a9fb4

File tree

4 files changed

+49
-36
lines changed

4 files changed

+49
-36
lines changed

app/src/main/java/com/nextcloud/utils/extensions/FileDataStorageManagerExtensions.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import kotlinx.coroutines.withContext
1515

1616
suspend fun FileDataStorageManager.saveShares(shares: List<OCShare>, accountName: String) {
1717
withContext(Dispatchers.IO) {
18-
//shareDao.clearSharesForAccount(accountName)
18+
// shareDao.clearSharesForAccount(accountName)
1919

2020
val entities = shares.map { share ->
2121
share.toEntity(accountName)
@@ -25,7 +25,6 @@ suspend fun FileDataStorageManager.saveShares(shares: List<OCShare>, accountName
2525
}
2626
}
2727

28-
2928
fun FileDataStorageManager.searchFilesByName(file: OCFile, accountName: String, query: String): List<OCFile> =
3029
fileDao.searchFilesInFolder(file.fileId, accountName, query).map {
3130
createFileInstance(it)

app/src/main/java/com/nextcloud/utils/extensions/OCShareExtensions.kt

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,28 @@ fun OCShare.hasFileRequestPermission(): Boolean = (isFolder && shareType?.isPubl
1414

1515
fun List<OCShare>.mergeDistinctByToken(other: List<OCShare>): List<OCShare> = (this + other).distinctBy { it.token }
1616

17-
fun OCShare.toEntity(accountName: String): ShareEntity {
18-
return ShareEntity(
19-
id = remoteId.toInt(), // so that db is not keep updating same files
20-
idRemoteShared = remoteId.toInt(),
21-
path = path,
22-
itemSource = itemSource.toInt(),
23-
fileSource = fileSource.toInt(),
24-
shareType = shareType?.value,
25-
shareWith = shareWith,
26-
permissions = permissions,
27-
sharedDate = sharedDate.toInt(),
28-
expirationDate = expirationDate.toInt(),
29-
token = token,
30-
shareWithDisplayName = sharedWithDisplayName,
31-
isDirectory = if (isFolder) 1 else 0,
32-
userId = userId,
33-
accountOwner = accountName,
34-
isPasswordProtected = if (isPasswordProtected) 1 else 0,
35-
note = note,
36-
hideDownload = if (isHideFileDownload) 1 else 0,
37-
shareLink = shareLink,
38-
shareLabel = label,
39-
attributes = attributes,
40-
downloadLimitLimit = fileDownloadLimit?.limit,
41-
downloadLimitCount = fileDownloadLimit?.count
42-
)
43-
}
17+
fun OCShare.toEntity(accountName: String): ShareEntity = ShareEntity(
18+
id = remoteId.toInt(), // so that db is not keep updating same files
19+
idRemoteShared = remoteId.toInt(),
20+
path = path,
21+
itemSource = itemSource.toInt(),
22+
fileSource = fileSource.toInt(),
23+
shareType = shareType?.value,
24+
shareWith = shareWith,
25+
permissions = permissions,
26+
sharedDate = sharedDate.toInt(),
27+
expirationDate = expirationDate.toInt(),
28+
token = token,
29+
shareWithDisplayName = sharedWithDisplayName,
30+
isDirectory = if (isFolder) 1 else 0,
31+
userId = userId,
32+
accountOwner = accountName,
33+
isPasswordProtected = if (isPasswordProtected) 1 else 0,
34+
note = note,
35+
hideDownload = if (isHideFileDownload) 1 else 0,
36+
shareLink = shareLink,
37+
shareLabel = label,
38+
attributes = attributes,
39+
downloadLimitLimit = fileDownloadLimit?.limit,
40+
downloadLimitCount = fileDownloadLimit?.count
41+
)

app/src/main/java/com/owncloud/android/ui/adapter/OCShareToOCFileConverter.kt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ object OCShareToOCFileConverter {
4545
}
4646

4747
suspend fun parseAndSaveShares(
48+
cachedFiles: List<OCFile>,
4849
data: List<Any>,
4950
storageManager: FileDataStorageManager?,
5051
accountName: String
@@ -58,11 +59,27 @@ object OCShareToOCFileConverter {
5859
return@withContext emptyList()
5960
}
6061

61-
val files = buildOCFilesFromShares(shares)
62+
val cachedPaths = cachedFiles.map { it.decryptedRemotePath }.toSet()
63+
64+
// Group shares by path to identify unique files
65+
val sharesByPath = shares
66+
.filter { it.path != null }
67+
.groupBy { it.path!! }
68+
69+
// Identify ONLY new file paths that aren't in cache
70+
val newSharesByPath = sharesByPath.filterKeys { path ->
71+
path !in cachedPaths
72+
}
73+
74+
if (newSharesByPath.isEmpty()) {
75+
return@withContext cachedFiles
76+
}
77+
78+
val newShares = newSharesByPath.values.flatten()
79+
val newFiles = buildOCFilesFromShares(newShares)
6280
val baseSavePath = FileStorageUtils.getSavePath(accountName)
6381

64-
// Parallelized file lookup
65-
val resolvedFiles = files.map { file ->
82+
val resolvedNewFiles = newFiles.map { file ->
6683
async {
6784
if (!file.isFolder && (file.storagePath == null || !File(file.storagePath).exists())) {
6885
val fullPath = baseSavePath + file.decryptedRemotePath
@@ -77,12 +94,10 @@ object OCShareToOCFileConverter {
7794
}
7895
}.awaitAll()
7996

80-
storageManager?.saveShares(shares, accountName)
81-
82-
resolvedFiles
97+
storageManager?.saveShares(newShares, accountName)
98+
cachedFiles + resolvedNewFiles
8399
}
84100

85-
86101
private fun buildOcFile(path: String, shares: List<OCShare>): OCFile {
87102
require(shares.all { it.path == path })
88103
// common attributes

app/src/main/java/com/owncloud/android/ui/fragment/OCFileListSearchTask.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ class OCFileListSearchTask(
9595

9696
val newList = if (searchType == SearchType.SHARED_FILTER) {
9797
OCShareToOCFileConverter.parseAndSaveShares(
98+
sortedFilesInDb,
9899
result.resultData ?: listOf(),
99100
fileDataStorageManager,
100101
currentUser.accountName

0 commit comments

Comments
 (0)