1515 * You should have received a copy of the GNU General Public License
1616 * along with this program. If not, see <https://www.gnu.org/licenses/>.
1717 */
18-
18+
1919package org.oxycblt.musikr.fs.device
2020
21- import android.content.ContentResolver
2221import android.content.Context
2322import android.net.Uri
2423import android.provider.DocumentsContract
@@ -35,7 +34,6 @@ import kotlinx.coroutines.flow.flattenMerge
3534import kotlinx.coroutines.flow.flow
3635import kotlinx.coroutines.flow.flowOn
3736import kotlinx.coroutines.flow.onCompletion
38- import kotlinx.coroutines.flow.onStart
3937import org.oxycblt.musikr.fs.MusicLocation
4038import org.oxycblt.musikr.fs.Path
4139
@@ -51,35 +49,34 @@ internal interface DeviceFS {
5149}
5250
5351@OptIn(ExperimentalCoroutinesApi ::class )
54- private class DeviceFSImpl (
55- private val context : Context ,
56- private val withHidden : Boolean
57- ) : DeviceFS {
52+ private class DeviceFSImpl (private val context : Context , private val withHidden : Boolean ) :
53+ DeviceFS {
5854 private val contentResolver = context.contentResolverSafe
5955 private val cache = DeviceFSCache (context)
6056
6157 override fun explore (locations : Flow <MusicLocation >): Flow <DeviceFile > =
6258 flow {
63- // Initialize the cache
64- cache.loadCache()
65- // Start a new traversal to track processed URIs for cleanup
66- cache.resetTraversalTracking()
67-
68- emitAll(
69- locations.flatMapMerge { location ->
70- exploreDirectoryImpl(
71- location.uri,
72- DocumentsContract .getTreeDocumentId(location.uri),
73- location.path,
74- null
75- )
76- }.flowOn(Dispatchers .IO )
77- )
78- }.onCompletion {
79- // Cleanup and save cache when exploration is complete
80- cache.cleanupCache()
81- cache.saveCache()
82- }
59+ // Initialize the cache
60+ cache.loadCache()
61+ // Start a new traversal to track processed URIs for cleanup
62+ cache.resetTraversalTracking()
63+
64+ emitAll(
65+ locations
66+ .flatMapMerge { location ->
67+ exploreDirectoryImpl(
68+ location.uri,
69+ DocumentsContract .getTreeDocumentId(location.uri),
70+ location.path,
71+ null )
72+ }
73+ .flowOn(Dispatchers .IO ))
74+ }
75+ .onCompletion {
76+ // Cleanup and save cache when exploration is complete
77+ cache.cleanupCache()
78+ cache.saveCache()
79+ }
8380
8481 private fun exploreDirectoryImpl (
8582 rootUri : Uri ,
@@ -117,18 +114,18 @@ private class DeviceFSImpl(
117114 // For directories, recursively process them
118115 val childId = DocumentsContract .getDocumentId(childUri)
119116 recursive.add(
120- exploreDirectoryImpl(rootUri, childId, newPath, directoryDeferred)
121- )
117+ exploreDirectoryImpl(rootUri, childId, newPath, directoryDeferred))
122118 } else {
123119 // For files, create DeviceFile objects and emit them
124- val file = DeviceFile (
125- uri = childUri,
126- mimeType = " " , // We don't cache mime type but it's not used elsewhere
127- path = newPath,
128- size = 0 , // We don't cache size but it's not used elsewhere
129- modifiedMs = child.modifiedDate,
130- parent = directoryDeferred
131- )
120+ val file =
121+ DeviceFile (
122+ uri = childUri,
123+ mimeType =
124+ " " , // We don't cache mime type but it's not used elsewhere
125+ path = newPath,
126+ size = 0 , // We don't cache size but it's not used elsewhere
127+ modifiedMs = child.modifiedDate,
128+ parent = directoryDeferred)
132129 children.add(file)
133130 emit(file)
134131 }
0 commit comments