🛠️ Optimize loading in Browse
* Sped up loading in Browse using coroutines Part-of: #91
This commit is contained in:
parent
cf7e85c267
commit
7dd1a496de
1 changed files with 27 additions and 12 deletions
|
|
@ -7,6 +7,10 @@ import android.os.Environment
|
||||||
import android.os.storage.StorageManager
|
import android.os.storage.StorageManager
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
|
import kotlinx.coroutines.async
|
||||||
|
import kotlinx.coroutines.awaitAll
|
||||||
|
import kotlinx.coroutines.sync.Semaphore
|
||||||
|
import kotlinx.coroutines.sync.withPermit
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import ua.acclorite.book_story.R
|
import ua.acclorite.book_story.R
|
||||||
import ua.acclorite.book_story.data.local.room.BookDao
|
import ua.acclorite.book_story.data.local.room.BookDao
|
||||||
|
|
@ -77,17 +81,28 @@ class FileSystemRepositoryImpl @Inject constructor(
|
||||||
/**
|
/**
|
||||||
* Get all verified files from directory (root).
|
* Get all verified files from directory (root).
|
||||||
*/
|
*/
|
||||||
fun File.getFilesFromDirectory(): List<SelectableFile> {
|
suspend fun File.getFilesFromDirectory(): List<SelectableFile> {
|
||||||
return walk().mapNotNull {
|
return withContext(Dispatchers.IO) {
|
||||||
if (!it.isValid()) return@mapNotNull null
|
val semaphore = Semaphore(5)
|
||||||
SelectableFile(
|
walk()
|
||||||
name = it.name,
|
.map { file ->
|
||||||
path = it.path,
|
async {
|
||||||
size = it.length(),
|
semaphore.withPermit {
|
||||||
lastModified = it.lastModified(),
|
if (!file.isValid()) return@async null
|
||||||
selected = false
|
SelectableFile(
|
||||||
)
|
name = file.name,
|
||||||
}.toList()
|
path = file.path,
|
||||||
|
size = file.length(),
|
||||||
|
lastModified = file.lastModified(),
|
||||||
|
selected = false
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
.toList()
|
||||||
|
.awaitAll()
|
||||||
|
.filterNotNull()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
@ -133,7 +148,7 @@ class FileSystemRepositoryImpl @Inject constructor(
|
||||||
val files = mutableListOf<SelectableFile>()
|
val files = mutableListOf<SelectableFile>()
|
||||||
|
|
||||||
for (volume in storageDirectories) {
|
for (volume in storageDirectories) {
|
||||||
files.addAll(withContext(Dispatchers.IO) { volume.getFilesFromDirectory() })
|
files.addAll(volume.getFilesFromDirectory())
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.i(GET_FILES_FROM_DEVICE, "Successfully got all matching files.")
|
Log.i(GET_FILES_FROM_DEVICE, "Successfully got all matching files.")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue