recognise downloaded audio files

This commit is contained in:
Bnyro 2022-09-10 12:10:45 +02:00
parent cf46afdc86
commit b838e319f5
4 changed files with 51 additions and 10 deletions

View File

@ -9,11 +9,12 @@ import com.github.libretube.R
import com.github.libretube.activities.OfflinePlayerActivity
import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DownloadedMediaRowBinding
import com.github.libretube.obj.DownloadedFile
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.io.File
class DownloadsAdapter(
private val files: MutableList<File>
private val files: MutableList<DownloadedFile>
) : RecyclerView.Adapter<DownloadsViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DownloadsViewHolder {
val binding = DownloadedMediaRowBinding.inflate(
@ -29,7 +30,7 @@ class DownloadsAdapter(
val file = files[position]
holder.binding.apply {
fileName.text = file.name
fileSize.text = "${file.length() / (1024 * 1024)} MiB"
fileSize.text = "${file.size / (1024 * 1024)} MiB"
root.setOnClickListener {
val intent = Intent(root.context, OfflinePlayerActivity::class.java).also {

View File

@ -8,7 +8,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import com.github.libretube.adapters.DownloadsAdapter
import com.github.libretube.databinding.FragmentDownloadsBinding
import com.github.libretube.extensions.BaseFragment
import java.io.File
import com.github.libretube.util.DownloadHelper
class DownloadsFragment : BaseFragment() {
private lateinit var binding: FragmentDownloadsBinding
@ -25,14 +25,9 @@ class DownloadsFragment : BaseFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val downloadDir = File(
context?.getExternalFilesDir(null),
"video"
)
val files = DownloadHelper.getDownloadedFiles(requireContext())
binding.downloads.layoutManager = LinearLayoutManager(context)
binding.downloads.adapter = DownloadsAdapter(
downloadDir.listFiles()?.toMutableList() ?: mutableListOf()
)
binding.downloads.adapter = DownloadsAdapter(files)
}
}

View File

@ -0,0 +1,7 @@
package com.github.libretube.obj
data class DownloadedFile(
val name: String,
val size: Long,
val type: Int
)

View File

@ -1,6 +1,8 @@
package com.github.libretube.util
import android.content.Context
import com.github.libretube.constants.DownloadType
import com.github.libretube.obj.DownloadedFile
import java.io.File
object DownloadHelper {
@ -21,4 +23,40 @@ object DownloadHelper {
"audio"
)
}
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
}
}