diff --git a/app/src/main/java/com/github/libretube/adapters/DownloadsAdapter.kt b/app/src/main/java/com/github/libretube/adapters/DownloadsAdapter.kt index 2b773b819..0be4e5129 100644 --- a/app/src/main/java/com/github/libretube/adapters/DownloadsAdapter.kt +++ b/app/src/main/java/com/github/libretube/adapters/DownloadsAdapter.kt @@ -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 + private val files: MutableList ) : RecyclerView.Adapter() { 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 { diff --git a/app/src/main/java/com/github/libretube/fragments/DownloadsFragment.kt b/app/src/main/java/com/github/libretube/fragments/DownloadsFragment.kt index 84f9baabf..cfc17f7a6 100644 --- a/app/src/main/java/com/github/libretube/fragments/DownloadsFragment.kt +++ b/app/src/main/java/com/github/libretube/fragments/DownloadsFragment.kt @@ -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) } } diff --git a/app/src/main/java/com/github/libretube/obj/DownloadedFile.kt b/app/src/main/java/com/github/libretube/obj/DownloadedFile.kt new file mode 100644 index 000000000..beb9ec185 --- /dev/null +++ b/app/src/main/java/com/github/libretube/obj/DownloadedFile.kt @@ -0,0 +1,7 @@ +package com.github.libretube.obj + +data class DownloadedFile( + val name: String, + val size: Long, + val type: Int +) diff --git a/app/src/main/java/com/github/libretube/util/DownloadHelper.kt b/app/src/main/java/com/github/libretube/util/DownloadHelper.kt index 79dbcaded..397d5f77a 100644 --- a/app/src/main/java/com/github/libretube/util/DownloadHelper.kt +++ b/app/src/main/java/com/github/libretube/util/DownloadHelper.kt @@ -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 { + val videoFiles = getVideoDir(context).listFiles() + val audioFiles = getAudioDir(context).listFiles()?.toMutableList() + + val files = mutableListOf() + + 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 + } }