LibreTube/app/src/main/java/com/github/libretube/util/DownloadHelper.kt

70 lines
1.8 KiB
Kotlin
Raw Normal View History

2022-09-10 15:21:50 +05:30
package com.github.libretube.util
import android.content.Context
2022-10-07 23:18:55 +05:30
import android.os.Build
2022-09-10 15:40:45 +05:30
import com.github.libretube.constants.DownloadType
import com.github.libretube.obj.DownloadedFile
2022-09-10 15:21:50 +05:30
import java.io.File
object DownloadHelper {
private fun getOfflineStorageDir(context: Context): File {
2022-10-07 23:18:55 +05:30
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return context.filesDir
return try {
context.getExternalFilesDir(null)!!
} catch (e: Exception) {
context.filesDir
}
2022-09-10 15:21:50 +05:30
}
fun getVideoDir(context: Context): File {
return File(
getOfflineStorageDir(context),
"video"
)
}
fun getAudioDir(context: Context): File {
return File(
getOfflineStorageDir(context),
"audio"
)
}
2022-09-10 15:40:45 +05:30
fun getDownloadedFiles(context: Context): MutableList<DownloadedFile> {
val videoFiles = getVideoDir(context).listFiles()
val audioFiles = getAudioDir(context).listFiles()?.toMutableList()
val files = mutableListOf<DownloadedFile>()
videoFiles?.forEach {
var type = DownloadType.VIDEO
audioFiles?.forEach { audioFile ->
if (audioFile.name == it.name) {
type = DownloadType.AUDIO_VIDEO
audioFiles.remove(audioFile)
}
}
files.add(
DownloadedFile(
name = it.name,
size = it.length(),
type = type
)
)
}
audioFiles?.forEach {
files.add(
DownloadedFile(
name = it.name,
size = it.length(),
type = DownloadType.AUDIO
)
)
}
return files
}
2022-09-10 15:21:50 +05:30
}