Skip to content

Commit 7ac02e5

Browse files
Refactor GetDuplicatesUseCase to suspend
1 parent ee2b53e commit 7ac02e5

File tree

2 files changed

+4
-5
lines changed

2 files changed

+4
-5
lines changed

app/src/main/kotlin/com/d4rk/cleaner/app/clean/scanner/domain/operations/FileAnalyzer.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import java.io.File
1212
class FileAnalyzer(
1313
private val getDuplicatesUseCase: GetDuplicatesUseCase
1414
) {
15-
fun computeGroupedFiles(
15+
suspend fun computeGroupedFiles(
1616
scannedFiles: List<File>,
1717
emptyFolders: List<File>,
1818
fileTypesData: FileTypesData,

app/src/main/kotlin/com/d4rk/cleaner/app/clean/scanner/domain/usecases/GetDuplicatesUseCase.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import com.d4rk.cleaner.core.utils.extensions.partialMd5
66
import kotlinx.coroutines.async
77
import kotlinx.coroutines.awaitAll
88
import kotlinx.coroutines.coroutineScope
9-
import kotlinx.coroutines.runBlocking
109
import kotlinx.coroutines.withContext
1110
import java.io.File
1211
import java.util.concurrent.ConcurrentHashMap
@@ -17,14 +16,14 @@ class GetDuplicatesUseCase(
1716
private val hashCache = ConcurrentHashMap<String, String>()
1817
private val chunkSize = 100
1918

20-
operator fun invoke(files: List<File>): List<List<FileEntry>> = runBlocking {
19+
suspend operator fun invoke(files: List<File>): List<List<FileEntry>> {
2120
val candidates = files.filter { it.isFile }
2221
.groupBy { it.length() to it.lastModified() }
2322
.values
2423
.filter { it.size > 1 }
2524
.flatten()
2625

27-
if (candidates.isEmpty()) return@runBlocking emptyList()
26+
if (candidates.isEmpty()) return emptyList()
2827

2928
val hashed = coroutineScope {
3029
candidates.chunked(chunkSize).map { chunk ->
@@ -40,7 +39,7 @@ class GetDuplicatesUseCase(
4039
}.awaitAll().flatten()
4140
}
4241

43-
hashed.groupBy({ it.first }, { it.second })
42+
return hashed.groupBy({ it.first }, { it.second })
4443
.values
4544
.filter { it.size > 1 }
4645
.map { group ->

0 commit comments

Comments
 (0)