@@ -7,13 +7,18 @@ import android.os.Build
77import android.provider.MediaStore
88import android.webkit.MimeTypeMap
99import com.lassi.R
10+ import com.lassi.common.extenstions.catch
1011import com.lassi.common.utils.KeyUtils
1112import com.lassi.common.utils.Logger
13+ import com.lassi.data.common.Result
1214import com.lassi.data.mediadirectory.Folder
1315import com.lassi.domain.media.LassiConfig
1416import com.lassi.domain.media.MediaRepository
1517import com.lassi.domain.media.MediaType
16- import io.reactivex.Single
18+ import kotlinx.coroutines.Dispatchers
19+ import kotlinx.coroutines.flow.Flow
20+ import kotlinx.coroutines.flow.flow
21+ import kotlinx.coroutines.flow.flowOn
1722import java.io.File
1823import java.util.*
1924import kotlin.collections.ArrayList
@@ -27,75 +32,78 @@ class MediaRepositoryImpl(private val context: Context) : MediaRepository {
2732 private val minFileSize = LassiConfig .getConfig().minFileSize * 1024L
2833 private val maxFileSize = LassiConfig .getConfig().maxFileSize * 1024L
2934
30- @SuppressLint(" Range" )
31- override fun fetchFolders (): Single <ArrayList <Folder >> {
32- val projection = getProjections()
33- val cursor = query(projection)
34- cursor ? : return Single .error(Throwable ())
35- folderMap.clear()
36- fetchedFolders.clear()
37- try {
38- if (cursor.moveToLast()) {
39- do {
40- val id = cursor.getLong(cursor.getColumnIndex(projection[0 ]))
41- val name = cursor.getString(cursor.getColumnIndex(projection[1 ]))
42- val path = cursor.getString(cursor.getColumnIndex(projection[2 ]))
43- val bucket = cursor.getString(cursor.getColumnIndex(projection[3 ]))
44- val size =
45- cursor.getLong(cursor.getColumnIndex(MediaStore .Files .FileColumns .SIZE ))
46- val albumCoverPath =
47- if (LassiConfig .getConfig().mediaType == MediaType .AUDIO ) {
48- val albumId = cursor.getString(cursor.getColumnIndex(projection[5 ]))
49- if (albumId != null ) {
50- getAlbumArt(albumId)
51- } else {
52- continue
53- }
54- } else {
55- " "
56- }
57- val duration =
58- if (LassiConfig .getConfig().mediaType == MediaType .VIDEO ) {
59- cursor.getLong(cursor.getColumnIndex(projection[4 ]))
60- } else {
61- 0
62- }
35+ override suspend fun fetchFolders (): Flow <Result <ArrayList <Folder >>> {
36+ return flow {
37+ val projection = getProjections()
38+ val cursor = query(projection)
39+ cursor?.let {
40+ folderMap.clear()
41+ fetchedFolders.clear()
42+ try {
43+ if (cursor.moveToLast()) {
44+ do {
45+ val id = cursor.getLong(cursor.getColumnIndex(projection[0 ]))
46+ val name = cursor.getString(cursor.getColumnIndex(projection[1 ]))
47+ val path = cursor.getString(cursor.getColumnIndex(projection[2 ]))
48+ val bucket = cursor.getString(cursor.getColumnIndex(projection[3 ]))
49+ val size =
50+ cursor.getLong(cursor.getColumnIndex(MediaStore .Files .FileColumns .SIZE ))
51+ val albumCoverPath =
52+ if (LassiConfig .getConfig().mediaType == MediaType .AUDIO ) {
53+ val albumId =
54+ cursor.getString(cursor.getColumnIndex(projection[5 ]))
55+ if (albumId != null ) {
56+ getAlbumArt(albumId)
57+ } else {
58+ continue
59+ }
60+ } else {
61+ " "
62+ }
63+ val duration =
64+ if (LassiConfig .getConfig().mediaType == MediaType .VIDEO ) {
65+ cursor.getLong(cursor.getColumnIndex(projection[4 ]))
66+ } else {
67+ 0
68+ }
6369
64- val file = makeSafeFile(path)
65- if (file != null && file.exists()) {
66- if (LassiConfig .getConfig().mediaType == MediaType .VIDEO ||
67- LassiConfig .getConfig().mediaType == MediaType .AUDIO
68- ) {
69- checkDurationAndAddFileToFolder(
70- bucket,
71- id,
72- name,
73- path,
74- duration,
75- albumCoverPath,
76- size
77- )
78- } else {
79- Logger .e(" MediaRepositoryImpl" , " $name >> $size " )
70+ val file = makeSafeFile(path)
71+ if (file != null && file.exists()) {
72+ if (LassiConfig .getConfig().mediaType == MediaType .VIDEO ||
73+ LassiConfig .getConfig().mediaType == MediaType .AUDIO
74+ ) {
75+ checkDurationAndAddFileToFolder(
76+ bucket,
77+ id,
78+ name,
79+ path,
80+ duration,
81+ albumCoverPath,
82+ size
83+ )
84+ } else {
85+ Logger .e(" MediaRepositoryImpl" , " $name >> $size " )
8086
81- if (isValidFileSize(size)) {
82- addFileToFolder(
83- bucket,
84- MiMedia (id, name, path, duration, albumCoverPath)
85- )
87+ if (isValidFileSize(size)) {
88+ addFileToFolder(
89+ bucket,
90+ MiMedia (id, name, path, duration, albumCoverPath)
91+ )
92+ }
93+ }
8694 }
87- }
95+ } while (cursor.moveToPrevious())
8896 }
89- } while (cursor.moveToPrevious())
90- }
91- } catch (e : Exception ) {
92- Logger .e( " MediaRepositoryImpl " , " fetchFolders >> $e " )
93- return Single .error(e )
94- } finally {
95- cursor.close( )
96- }
97- fetchedFolders.addAll(folderMap.values )
98- return Single .just(fetchedFolders )
97+ } catch (e : Exception ) {
98+ Logger .e( " MediaRepositoryImpl " , " fetchFolders >> $e " )
99+ emit( Result . Error ( Throwable ()))
100+ } finally {
101+ cursor.close( )
102+ }
103+ fetchedFolders.addAll(folderMap.values )
104+ emit( Result . Success (fetchedFolders))
105+ } ? : emit( Result . Error ( Throwable ()) )
106+ }. catch ().flowOn( Dispatchers . IO )
99107 }
100108
101109 private fun checkDurationAndAddFileToFolder (
@@ -331,26 +339,29 @@ class MediaRepositoryImpl(private val context: Context) : MediaRepository {
331339 }
332340
333341 @SuppressLint(" Range" )
334- override fun fetchDocs (): Single <ArrayList <MiMedia >> {
335- val projection = getProjections()
336- val cursor = query(projection)
337- cursor ? : return Single .error(Throwable ())
338- Logger .e(" MediaRepositoryImpl" , " Fetch documents size ${cursor.count} " )
339- val docs = ArrayList <MiMedia >()
340- try {
341- if (cursor.moveToLast()) {
342- do {
343- val id = cursor.getLong(cursor.getColumnIndex(projection[0 ]))
344- val name = cursor.getString(cursor.getColumnIndex(projection[1 ]))
345- val path = cursor.getString(cursor.getColumnIndex(projection[2 ]))
346- docs.add(MiMedia (id, name, path, 0 ))
347- } while (cursor.moveToPrevious())
348- }
349- } catch (e: Exception ) {
350- Logger .e(" MediaRepositoryImpl" , " fetchFolders >> $e " )
351- } finally {
352- cursor.close()
353- }
354- return Single .just(docs)
342+ override suspend fun fetchDocs (): Flow <Result <ArrayList <MiMedia >>> {
343+ return flow {
344+ val projection = getProjections()
345+ val cursor = query(projection)
346+ cursor?.let {
347+ Logger .e(" MediaRepositoryImpl" , " Fetch documents size ${cursor.count} " )
348+ val docs = ArrayList <MiMedia >()
349+ try {
350+ if (cursor.moveToLast()) {
351+ do {
352+ val id = cursor.getLong(cursor.getColumnIndex(projection[0 ]))
353+ val name = cursor.getString(cursor.getColumnIndex(projection[1 ]))
354+ val path = cursor.getString(cursor.getColumnIndex(projection[2 ]))
355+ docs.add(MiMedia (id, name, path, 0 ))
356+ } while (cursor.moveToPrevious())
357+ }
358+ } catch (e: Exception ) {
359+ Logger .e(" MediaRepositoryImpl" , " fetchFolders >> $e " )
360+ } finally {
361+ cursor.close()
362+ }
363+ emit(Result .Success (docs))
364+ } ? : emit(Result .Error (Throwable ()))
365+ }.catch ().flowOn(Dispatchers .IO )
355366 }
356367}
0 commit comments