Skip to content

Commit

Permalink
Updated song extract statusListener logic
Browse files Browse the repository at this point in the history
  • Loading branch information
pakka-papad committed Dec 8, 2023
1 parent 5638d27 commit d5d954e
Showing 1 changed file with 23 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ import com.github.pakka_papad.data.ZenCrashReporter
import com.github.pakka_papad.formatToDate
import com.github.pakka_papad.toMBfromB
import com.github.pakka_papad.toMS
import kotlinx.coroutines.CompletionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import java.io.File
import java.io.FileNotFoundException
import java.util.TreeMap
import java.util.concurrent.atomic.AtomicInteger

class SongExtractor(
private val scope: CoroutineScope,
Expand Down Expand Up @@ -216,7 +218,12 @@ class SongExtractor(
val songIdIndex = cursor.getColumnIndex(MediaStore.Audio.Media._ID)
val dSongs = ArrayList<Deferred<Song?>>()
val total = cursor.count
var parsed = 0
val parsed = AtomicInteger(0)
val parseCompletionHandler = object : CompletionHandler {
override fun invoke(cause: Throwable?) {
statusListener?.invoke(parsed.incrementAndGet(), total)
}
}
cursor.moveToFirst()
do {
try {
Expand All @@ -231,19 +238,21 @@ class SongExtractor(
val title = cursor.getString(titleIndex).trim()
val album = cursor.getString(albumIndex).trim()
albumArtMap[album] = cursor.getLong(albumIdIndex)
dSongs.add(scope.async {
getSong(
path = songPath,
size = size,
addedDate = addedDate,
modifiedDate = modifiedDate,
songId = songId,
title = title,
album = album,
)
})
parsed++
statusListener?.invoke(parsed, total)
dSongs.add(
scope.async {
getSong(
path = songPath,
size = size,
addedDate = addedDate,
modifiedDate = modifiedDate,
songId = songId,
title = title,
album = album,
)
}.apply {
invokeOnCompletion(parseCompletionHandler)
}
)
} catch (_: Exception){

}
Expand Down

0 comments on commit d5d954e

Please sign in to comment.