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.activities.OfflinePlayerActivity
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DownloadedMediaRowBinding import com.github.libretube.databinding.DownloadedMediaRowBinding
import com.github.libretube.obj.DownloadedFile
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.io.File import java.io.File
class DownloadsAdapter( class DownloadsAdapter(
private val files: MutableList<File> private val files: MutableList<DownloadedFile>
) : RecyclerView.Adapter<DownloadsViewHolder>() { ) : RecyclerView.Adapter<DownloadsViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DownloadsViewHolder { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DownloadsViewHolder {
val binding = DownloadedMediaRowBinding.inflate( val binding = DownloadedMediaRowBinding.inflate(
@ -29,7 +30,7 @@ class DownloadsAdapter(
val file = files[position] val file = files[position]
holder.binding.apply { holder.binding.apply {
fileName.text = file.name fileName.text = file.name
fileSize.text = "${file.length() / (1024 * 1024)} MiB" fileSize.text = "${file.size / (1024 * 1024)} MiB"
root.setOnClickListener { root.setOnClickListener {
val intent = Intent(root.context, OfflinePlayerActivity::class.java).also { 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.adapters.DownloadsAdapter
import com.github.libretube.databinding.FragmentDownloadsBinding import com.github.libretube.databinding.FragmentDownloadsBinding
import com.github.libretube.extensions.BaseFragment import com.github.libretube.extensions.BaseFragment
import java.io.File import com.github.libretube.util.DownloadHelper
class DownloadsFragment : BaseFragment() { class DownloadsFragment : BaseFragment() {
private lateinit var binding: FragmentDownloadsBinding private lateinit var binding: FragmentDownloadsBinding
@ -25,14 +25,9 @@ class DownloadsFragment : BaseFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState) super.onViewCreated(view, savedInstanceState)
val downloadDir = File( val files = DownloadHelper.getDownloadedFiles(requireContext())
context?.getExternalFilesDir(null),
"video"
)
binding.downloads.layoutManager = LinearLayoutManager(context) binding.downloads.layoutManager = LinearLayoutManager(context)
binding.downloads.adapter = DownloadsAdapter( binding.downloads.adapter = DownloadsAdapter(files)
downloadDir.listFiles()?.toMutableList() ?: mutableListOf()
)
} }
} }

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