all: reformat
This commit is contained in:
@@ -128,9 +128,7 @@ private class MusikrImpl(
|
|||||||
.explore(locations)
|
.explore(locations)
|
||||||
.buffer(Channel.UNLIMITED)
|
.buffer(Channel.UNLIMITED)
|
||||||
.onStart { onProgress(IndexingProgress.Songs(0, 0)) }
|
.onStart { onProgress(IndexingProgress.Songs(0, 0)) }
|
||||||
.onEach {
|
.onEach { onProgress(IndexingProgress.Songs(extractedCount, ++exploredCount)) }
|
||||||
onProgress(IndexingProgress.Songs(extractedCount, ++exploredCount))
|
|
||||||
}
|
|
||||||
val extracted =
|
val extracted =
|
||||||
extractStep
|
extractStep
|
||||||
.extract(explored)
|
.extract(explored)
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ package org.oxycblt.musikr.cache.db
|
|||||||
|
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.util.Log
|
|
||||||
import kotlinx.coroutines.sync.Mutex
|
import kotlinx.coroutines.sync.Mutex
|
||||||
import org.oxycblt.musikr.cache.Cache
|
import org.oxycblt.musikr.cache.Cache
|
||||||
import org.oxycblt.musikr.cache.CacheResult
|
import org.oxycblt.musikr.cache.CacheResult
|
||||||
|
|||||||
@@ -26,7 +26,6 @@ import android.util.Log
|
|||||||
import androidx.core.net.toUri
|
import androidx.core.net.toUri
|
||||||
import kotlinx.coroutines.CoroutineScope
|
import kotlinx.coroutines.CoroutineScope
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
||||||
import kotlinx.coroutines.SupervisorJob
|
import kotlinx.coroutines.SupervisorJob
|
||||||
import kotlinx.coroutines.coroutineScope
|
import kotlinx.coroutines.coroutineScope
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
@@ -43,7 +42,6 @@ import kotlinx.coroutines.flow.shareIn
|
|||||||
import kotlinx.coroutines.flow.takeWhile
|
import kotlinx.coroutines.flow.takeWhile
|
||||||
import org.oxycblt.musikr.fs.MusicLocation
|
import org.oxycblt.musikr.fs.MusicLocation
|
||||||
import org.oxycblt.musikr.fs.Path
|
import org.oxycblt.musikr.fs.Path
|
||||||
import org.oxycblt.musikr.fs.path.DocumentPathFactory
|
|
||||||
|
|
||||||
internal interface DeviceFS {
|
internal interface DeviceFS {
|
||||||
fun explore(locations: Flow<MusicLocation>, fileTree: FileTree): Flow<DeviceDirectory>
|
fun explore(locations: Flow<MusicLocation>, fileTree: FileTree): Flow<DeviceDirectory>
|
||||||
@@ -59,38 +57,26 @@ private class DeviceFSImpl(
|
|||||||
private val withHidden: Boolean
|
private val withHidden: Boolean
|
||||||
) : DeviceFS {
|
) : DeviceFS {
|
||||||
private val explorationScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
private val explorationScope = CoroutineScope(Dispatchers.IO + SupervisorJob())
|
||||||
override fun explore(locations: Flow<MusicLocation>, fileTree: FileTree) =
|
|
||||||
locations.mapNotNull { location ->
|
|
||||||
queryRoot(location, fileTree)
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun queryRoot(
|
override fun explore(locations: Flow<MusicLocation>, fileTree: FileTree) =
|
||||||
location: MusicLocation,
|
locations.mapNotNull { location -> queryRoot(location, fileTree) }
|
||||||
fileTree: FileTree
|
|
||||||
): DeviceDirectory? {
|
private suspend fun queryRoot(location: MusicLocation, fileTree: FileTree): DeviceDirectory? {
|
||||||
val treeDocumentId =
|
val treeDocumentId = DocumentsContract.getTreeDocumentId(location.uri)
|
||||||
DocumentsContract.getTreeDocumentId(location.uri)
|
|
||||||
val uri = DocumentsContract.buildDocumentUriUsingTree(location.uri, treeDocumentId)
|
val uri = DocumentsContract.buildDocumentUriUsingTree(location.uri, treeDocumentId)
|
||||||
val modifiedMs = contentResolver.useQuery(
|
val modifiedMs =
|
||||||
uri,
|
contentResolver.useQuery(
|
||||||
arrayOf(DocumentsContract.Document.COLUMN_LAST_MODIFIED)
|
uri, arrayOf(DocumentsContract.Document.COLUMN_LAST_MODIFIED)) { cursor ->
|
||||||
) { cursor ->
|
if (!cursor.moveToFirst()) return@useQuery null
|
||||||
if (!cursor.moveToFirst()) return@useQuery null
|
val lastModifiedIndex =
|
||||||
val lastModifiedIndex =
|
cursor.getColumnIndexOrThrow(
|
||||||
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_LAST_MODIFIED)
|
DocumentsContract.Document.COLUMN_LAST_MODIFIED)
|
||||||
cursor.getLong(lastModifiedIndex)
|
cursor.getLong(lastModifiedIndex)
|
||||||
}
|
}
|
||||||
if (modifiedMs == null) {
|
if (modifiedMs == null) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
return query(
|
return query(location.uri, treeDocumentId, location.path, modifiedMs, null, fileTree)
|
||||||
location.uri,
|
|
||||||
treeDocumentId,
|
|
||||||
location.path,
|
|
||||||
modifiedMs,
|
|
||||||
null,
|
|
||||||
fileTree
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun query(
|
private suspend fun query(
|
||||||
@@ -105,105 +91,110 @@ private class DeviceFSImpl(
|
|||||||
val cached = fileTree.queryDirectory(uri)
|
val cached = fileTree.queryDirectory(uri)
|
||||||
if (cached != null && cached.modifiedMs == modifiedMs) {
|
if (cached != null && cached.modifiedMs == modifiedMs) {
|
||||||
return@coroutineScope hydrateCached(
|
return@coroutineScope hydrateCached(
|
||||||
cached = cached,
|
cached = cached, parentDir = parent, path = path, fileTree = fileTree)
|
||||||
parentDir = parent,
|
|
||||||
path = path,
|
|
||||||
fileTree = fileTree
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
val dir = DeviceDirectoryImpl(
|
val dir =
|
||||||
uri = uri,
|
DeviceDirectoryImpl(
|
||||||
path = path,
|
uri = uri,
|
||||||
modifiedMs = modifiedMs,
|
path = path,
|
||||||
parent = parent,
|
modifiedMs = modifiedMs,
|
||||||
children = emptyFlow()
|
parent = parent,
|
||||||
)
|
children = emptyFlow())
|
||||||
dir.children = flow {
|
dir.children =
|
||||||
Log.d("DeviceFS", "Querying $uri")
|
flow {
|
||||||
contentResolver.useQuery(
|
Log.d("DeviceFS", "Querying $uri")
|
||||||
DocumentsContract.buildChildDocumentsUriUsingTree(
|
contentResolver.useQuery(
|
||||||
rootUri,
|
DocumentsContract.buildChildDocumentsUriUsingTree(
|
||||||
treeDocumentId,
|
rootUri,
|
||||||
), projection = PROJECTION, sortOrder = "${DocumentsContract.Document.COLUMN_DISPLAY_NAME} DESC"
|
treeDocumentId,
|
||||||
) { cursor ->
|
),
|
||||||
val childUriIndex =
|
projection = PROJECTION,
|
||||||
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_DOCUMENT_ID)
|
sortOrder = "${DocumentsContract.Document.COLUMN_DISPLAY_NAME} DESC") {
|
||||||
val displayNameIndex =
|
cursor ->
|
||||||
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_DISPLAY_NAME)
|
val childUriIndex =
|
||||||
val mimeTypeIndex =
|
cursor.getColumnIndexOrThrow(
|
||||||
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_MIME_TYPE)
|
DocumentsContract.Document.COLUMN_DOCUMENT_ID)
|
||||||
val sizeIndex = cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_SIZE)
|
val displayNameIndex =
|
||||||
val lastModifiedIndex =
|
cursor.getColumnIndexOrThrow(
|
||||||
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_LAST_MODIFIED)
|
DocumentsContract.Document.COLUMN_DISPLAY_NAME)
|
||||||
|
val mimeTypeIndex =
|
||||||
|
cursor.getColumnIndexOrThrow(
|
||||||
|
DocumentsContract.Document.COLUMN_MIME_TYPE)
|
||||||
|
val sizeIndex =
|
||||||
|
cursor.getColumnIndexOrThrow(DocumentsContract.Document.COLUMN_SIZE)
|
||||||
|
val lastModifiedIndex =
|
||||||
|
cursor.getColumnIndexOrThrow(
|
||||||
|
DocumentsContract.Document.COLUMN_LAST_MODIFIED)
|
||||||
|
|
||||||
val childSubdirUris = mutableListOf<String>()
|
val childSubdirUris = mutableListOf<String>()
|
||||||
val childFileUris = mutableListOf<String>()
|
val childFileUris = mutableListOf<String>()
|
||||||
|
|
||||||
val subqueries = mutableListOf<DeviceDirectory>()
|
val subqueries = mutableListOf<DeviceDirectory>()
|
||||||
|
|
||||||
while (cursor.moveToNext()) {
|
while (cursor.moveToNext()) {
|
||||||
val childId = cursor.getString(childUriIndex)
|
val childId = cursor.getString(childUriIndex)
|
||||||
val displayName = cursor.getString(displayNameIndex)
|
val displayName = cursor.getString(displayNameIndex)
|
||||||
|
|
||||||
// Skip hidden files/directories if ignoreHidden is true
|
// Skip hidden files/directories if ignoreHidden is true
|
||||||
if (!withHidden && displayName.startsWith(".")) {
|
if (!withHidden && displayName.startsWith(".")) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
val newPath = path.file(displayName)
|
val newPath = path.file(displayName)
|
||||||
val mimeType = cursor.getString(mimeTypeIndex)
|
val mimeType = cursor.getString(mimeTypeIndex)
|
||||||
val lastModified = cursor.getLong(lastModifiedIndex)
|
val lastModified = cursor.getLong(lastModifiedIndex)
|
||||||
|
|
||||||
if (mimeType == DocumentsContract.Document.MIME_TYPE_DIR) {
|
if (mimeType == DocumentsContract.Document.MIME_TYPE_DIR) {
|
||||||
val subdir =
|
val subdir =
|
||||||
query(
|
query(
|
||||||
rootUri = rootUri,
|
rootUri = rootUri,
|
||||||
treeDocumentId = childId,
|
treeDocumentId = childId,
|
||||||
path = newPath,
|
path = newPath,
|
||||||
modifiedMs = modifiedMs,
|
modifiedMs = modifiedMs,
|
||||||
parent = dir,
|
parent = dir,
|
||||||
fileTree = fileTree
|
fileTree = fileTree)
|
||||||
)
|
childSubdirUris.add(subdir.uri.toString())
|
||||||
childSubdirUris.add(subdir.uri.toString())
|
emit(StreamedFile.More(subdir))
|
||||||
emit(StreamedFile.More(subdir))
|
} else {
|
||||||
} else {
|
Log.d("DeviceFS", "Querying file $childId")
|
||||||
Log.d("DeviceFS", "Querying file $childId")
|
val size = cursor.getLong(sizeIndex)
|
||||||
val size = cursor.getLong(sizeIndex)
|
val childUri =
|
||||||
val childUri = DocumentsContract.buildDocumentUriUsingTree(rootUri, childId)
|
DocumentsContract.buildDocumentUriUsingTree(
|
||||||
val file =
|
rootUri, childId)
|
||||||
DeviceFile(
|
val file =
|
||||||
uri = childUri,
|
DeviceFile(
|
||||||
mimeType = mimeType,
|
uri = childUri,
|
||||||
path = newPath,
|
mimeType = mimeType,
|
||||||
size = size,
|
path = newPath,
|
||||||
modifiedMs = lastModified,
|
size = size,
|
||||||
parent = dir
|
modifiedMs = lastModified,
|
||||||
)
|
parent = dir)
|
||||||
val write = CachedFile(
|
val write =
|
||||||
uri = childUri.toString(),
|
CachedFile(
|
||||||
name = displayName,
|
uri = childUri.toString(),
|
||||||
modifiedMs = lastModified,
|
name = displayName,
|
||||||
mimeType = mimeType,
|
modifiedMs = lastModified,
|
||||||
size = size
|
mimeType = mimeType,
|
||||||
)
|
size = size)
|
||||||
fileTree.updateFile(childUri, write)
|
fileTree.updateFile(childUri, write)
|
||||||
childFileUris.add(childUri.toString())
|
childFileUris.add(childUri.toString())
|
||||||
emit(StreamedFile.More(file))
|
emit(StreamedFile.More(file))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
val writeDir =
|
||||||
|
CachedDirectory(
|
||||||
|
uri = uri.toString(),
|
||||||
|
name = requireNotNull(path.name),
|
||||||
|
modifiedMs = modifiedMs,
|
||||||
|
subdirUris = childSubdirUris,
|
||||||
|
fileUris = childFileUris)
|
||||||
|
fileTree.updateDirectory(uri, writeDir)
|
||||||
|
emit(StreamedFile.Done)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
val writeDir = CachedDirectory(
|
.shareIn(explorationScope, SharingStarted.Lazily, replay = Int.MAX_VALUE)
|
||||||
uri = uri.toString(),
|
.takeWhile { it is StreamedFile.More }
|
||||||
name = requireNotNull(path.name),
|
.map { (it as StreamedFile.More).value }
|
||||||
modifiedMs = modifiedMs,
|
|
||||||
subdirUris = childSubdirUris,
|
|
||||||
fileUris = childFileUris
|
|
||||||
)
|
|
||||||
fileTree.updateDirectory(uri, writeDir)
|
|
||||||
emit(StreamedFile.Done)
|
|
||||||
}
|
|
||||||
}.shareIn(explorationScope, SharingStarted.Lazily, replay = Int.MAX_VALUE)
|
|
||||||
.takeWhile { it is StreamedFile.More }
|
|
||||||
.map { (it as StreamedFile.More).value }
|
|
||||||
dir
|
dir
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,47 +204,52 @@ private class DeviceFSImpl(
|
|||||||
path: Path,
|
path: Path,
|
||||||
fileTree: FileTree
|
fileTree: FileTree
|
||||||
): DeviceDirectory = coroutineScope {
|
): DeviceDirectory = coroutineScope {
|
||||||
val dir = DeviceDirectoryImpl(
|
val dir =
|
||||||
uri = cached.uri.toUri(),
|
DeviceDirectoryImpl(
|
||||||
path = path,
|
uri = cached.uri.toUri(),
|
||||||
modifiedMs = cached.modifiedMs,
|
path = path,
|
||||||
parent = parentDir,
|
modifiedMs = cached.modifiedMs,
|
||||||
children = emptyFlow()
|
parent = parentDir,
|
||||||
)
|
children = emptyFlow())
|
||||||
dir.children = flow {
|
dir.children =
|
||||||
emitAll(
|
flow {
|
||||||
merge(
|
emitAll(
|
||||||
cached.subdirUris.asFlow().map { subdirUriString ->
|
merge(
|
||||||
val subdirUri = subdirUriString.toUri()
|
cached.subdirUris.asFlow().map { subdirUriString ->
|
||||||
val cachedSubdir = requireNotNull(fileTree.queryDirectory(subdirUri)) {
|
val subdirUri = subdirUriString.toUri()
|
||||||
"No cached subdir for $subdirUri, malformed cache! Rescan needed."
|
val cachedSubdir =
|
||||||
}
|
requireNotNull(fileTree.queryDirectory(subdirUri)) {
|
||||||
hydrateCached(cachedSubdir, dir, dir.path.file(cachedSubdir.name), fileTree)
|
"No cached subdir for $subdirUri, malformed cache! Rescan needed."
|
||||||
},
|
}
|
||||||
cached.fileUris.asFlow().map {
|
hydrateCached(
|
||||||
val cachedFile = requireNotNull(fileTree.queryFile(it.toUri()))
|
cachedSubdir,
|
||||||
DeviceFile(
|
dir,
|
||||||
uri = it.toUri(),
|
dir.path.file(cachedSubdir.name),
|
||||||
path = dir.path.file(cachedFile.name),
|
fileTree)
|
||||||
modifiedMs = cachedFile.modifiedMs,
|
},
|
||||||
mimeType = cachedFile.mimeType,
|
cached.fileUris.asFlow().map {
|
||||||
size = cachedFile.size,
|
val cachedFile = requireNotNull(fileTree.queryFile(it.toUri()))
|
||||||
parent = dir
|
DeviceFile(
|
||||||
)
|
uri = it.toUri(),
|
||||||
}
|
path = dir.path.file(cachedFile.name),
|
||||||
).map { StreamedFile.More(it) }
|
modifiedMs = cachedFile.modifiedMs,
|
||||||
)
|
mimeType = cachedFile.mimeType,
|
||||||
emit(StreamedFile.Done)
|
size = cachedFile.size,
|
||||||
}
|
parent = dir)
|
||||||
.flowOn(Dispatchers.IO)
|
})
|
||||||
.shareIn(explorationScope, SharingStarted.Lazily, replay = Int.MAX_VALUE)
|
.map { StreamedFile.More(it) })
|
||||||
.takeWhile { it is StreamedFile.More }
|
emit(StreamedFile.Done)
|
||||||
.map { (it as StreamedFile.More).value }
|
}
|
||||||
|
.flowOn(Dispatchers.IO)
|
||||||
|
.shareIn(explorationScope, SharingStarted.Lazily, replay = Int.MAX_VALUE)
|
||||||
|
.takeWhile { it is StreamedFile.More }
|
||||||
|
.map { (it as StreamedFile.More).value }
|
||||||
dir
|
dir
|
||||||
}
|
}
|
||||||
|
|
||||||
private sealed interface StreamedFile {
|
private sealed interface StreamedFile {
|
||||||
data class More(val value: DeviceFSEntry) : StreamedFile
|
data class More(val value: DeviceFSEntry) : StreamedFile
|
||||||
|
|
||||||
data object Done : StreamedFile
|
data object Done : StreamedFile
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -264,8 +260,7 @@ private class DeviceFSImpl(
|
|||||||
DocumentsContract.Document.COLUMN_DISPLAY_NAME,
|
DocumentsContract.Document.COLUMN_DISPLAY_NAME,
|
||||||
DocumentsContract.Document.COLUMN_MIME_TYPE,
|
DocumentsContract.Document.COLUMN_MIME_TYPE,
|
||||||
DocumentsContract.Document.COLUMN_SIZE,
|
DocumentsContract.Document.COLUMN_SIZE,
|
||||||
DocumentsContract.Document.COLUMN_LAST_MODIFIED
|
DocumentsContract.Document.COLUMN_LAST_MODIFIED)
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -276,6 +271,3 @@ private class DeviceDirectoryImpl(
|
|||||||
override val parent: DeviceDirectory?,
|
override val parent: DeviceDirectory?,
|
||||||
override var children: Flow<DeviceFSEntry>,
|
override var children: Flow<DeviceFSEntry>,
|
||||||
) : DeviceDirectory
|
) : DeviceDirectory
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -19,14 +19,8 @@
|
|||||||
package org.oxycblt.musikr.fs.device
|
package org.oxycblt.musikr.fs.device
|
||||||
|
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import kotlinx.coroutines.Deferred
|
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
|
||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.asFlow
|
|
||||||
import kotlinx.coroutines.flow.emitAll
|
import kotlinx.coroutines.flow.emitAll
|
||||||
import kotlinx.coroutines.flow.flatMapMerge
|
|
||||||
import kotlinx.coroutines.flow.flow
|
|
||||||
import kotlinx.coroutines.flow.flowOf
|
|
||||||
import kotlinx.coroutines.flow.transform
|
import kotlinx.coroutines.flow.transform
|
||||||
import org.oxycblt.musikr.fs.Path
|
import org.oxycblt.musikr.fs.Path
|
||||||
|
|
||||||
@@ -50,7 +44,8 @@ data class DeviceFile(
|
|||||||
val parent: DeviceDirectory
|
val parent: DeviceDirectory
|
||||||
) : DeviceFSEntry
|
) : DeviceFSEntry
|
||||||
|
|
||||||
fun DeviceDirectory.flatten(): Flow<DeviceFile> = children.transform {
|
fun DeviceDirectory.flatten(): Flow<DeviceFile> =
|
||||||
|
children.transform {
|
||||||
when (it) {
|
when (it) {
|
||||||
is DeviceDirectory -> emitAll(it.flatten())
|
is DeviceDirectory -> emitAll(it.flatten())
|
||||||
is DeviceFile -> emit(it)
|
is DeviceFile -> emit(it)
|
||||||
|
|||||||
@@ -21,13 +21,13 @@ package org.oxycblt.musikr.fs.device
|
|||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.net.Uri
|
import android.net.Uri
|
||||||
import android.util.Log
|
import android.util.Log
|
||||||
|
import java.io.File
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import kotlinx.serialization.Serializable
|
import kotlinx.serialization.Serializable
|
||||||
import kotlinx.serialization.json.Json
|
|
||||||
import kotlinx.serialization.decodeFromString
|
import kotlinx.serialization.decodeFromString
|
||||||
import kotlinx.serialization.encodeToString
|
import kotlinx.serialization.encodeToString
|
||||||
import java.io.File
|
import kotlinx.serialization.json.Json
|
||||||
|
|
||||||
interface FileTreeCache {
|
interface FileTreeCache {
|
||||||
fun read(): FileTree
|
fun read(): FileTree
|
||||||
@@ -35,9 +35,11 @@ interface FileTreeCache {
|
|||||||
|
|
||||||
interface FileTree {
|
interface FileTree {
|
||||||
suspend fun queryDirectory(uri: Uri): CachedDirectory?
|
suspend fun queryDirectory(uri: Uri): CachedDirectory?
|
||||||
|
|
||||||
suspend fun updateDirectory(uri: Uri, directory: CachedDirectory)
|
suspend fun updateDirectory(uri: Uri, directory: CachedDirectory)
|
||||||
|
|
||||||
suspend fun queryFile(uri: Uri): CachedFile?
|
suspend fun queryFile(uri: Uri): CachedFile?
|
||||||
|
|
||||||
suspend fun updateFile(uri: Uri, file: CachedFile)
|
suspend fun updateFile(uri: Uri, file: CachedFile)
|
||||||
|
|
||||||
suspend fun write()
|
suspend fun write()
|
||||||
@@ -91,7 +93,10 @@ class FileTreeCacheImpl(private val context: Context) : FileTreeCache {
|
|||||||
try {
|
try {
|
||||||
val jsonContent = cacheFile.readText()
|
val jsonContent = cacheFile.readText()
|
||||||
val fileTreeCache = json.decodeFromString<FileTreeData>(jsonContent)
|
val fileTreeCache = json.decodeFromString<FileTreeData>(jsonContent)
|
||||||
FileTreeImpl(context, fileTreeCache.directories.toMutableMap(), fileTreeCache.files.toMutableMap())
|
FileTreeImpl(
|
||||||
|
context,
|
||||||
|
fileTreeCache.directories.toMutableMap(),
|
||||||
|
fileTreeCache.files.toMutableMap())
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "Failed to read cache file", e)
|
Log.e(TAG, "Failed to read cache file", e)
|
||||||
FileTreeImpl(context, mutableMapOf(), mutableMapOf())
|
FileTreeImpl(context, mutableMapOf(), mutableMapOf())
|
||||||
@@ -143,21 +148,21 @@ class FileTreeImpl(
|
|||||||
mutableFiles[uriString] = file
|
mutableFiles[uriString] = file
|
||||||
}
|
}
|
||||||
|
|
||||||
override suspend fun write() = withContext(Dispatchers.IO) {
|
override suspend fun write() =
|
||||||
Log.d(TAG, "Writing cache to disk")
|
withContext(Dispatchers.IO) {
|
||||||
val cacheFile = File(context.cacheDir, CACHE_FILENAME)
|
Log.d(TAG, "Writing cache to disk")
|
||||||
try {
|
val cacheFile = File(context.cacheDir, CACHE_FILENAME)
|
||||||
val fileTreeCache = FileTreeData(
|
try {
|
||||||
directories = mutableDirectories.toMap(),
|
val fileTreeCache =
|
||||||
files = mutableFiles.toMap()
|
FileTreeData(
|
||||||
)
|
directories = mutableDirectories.toMap(), files = mutableFiles.toMap())
|
||||||
|
|
||||||
val jsonContent = json.encodeToString(fileTreeCache)
|
val jsonContent = json.encodeToString(fileTreeCache)
|
||||||
cacheFile.writeText(jsonContent)
|
cacheFile.writeText(jsonContent)
|
||||||
Log.d(TAG, "Successfully wrote cache to disk")
|
Log.d(TAG, "Successfully wrote cache to disk")
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
Log.e(TAG, "Failed to write cache file", e)
|
Log.e(TAG, "Failed to write cache file", e)
|
||||||
|
}
|
||||||
|
Unit
|
||||||
}
|
}
|
||||||
Unit
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -48,7 +48,10 @@ internal fun ContentResolver.safeQuery(
|
|||||||
selector: String? = null,
|
selector: String? = null,
|
||||||
args: Array<String>? = null,
|
args: Array<String>? = null,
|
||||||
sortOrder: String? = null
|
sortOrder: String? = null
|
||||||
) = requireNotNull(query(uri, projection, selector, args, sortOrder)) { "ContentResolver query failed" }
|
) =
|
||||||
|
requireNotNull(query(uri, projection, selector, args, sortOrder)) {
|
||||||
|
"ContentResolver query failed"
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A shortcut for [safeQuery] with [use] applied, automatically cleaning up the [Cursor]'s resources
|
* A shortcut for [safeQuery] with [use] applied, automatically cleaning up the [Cursor]'s resources
|
||||||
|
|||||||
Reference in New Issue
Block a user