Merge pull request #764 from Bnyro/master

fix crash
This commit is contained in:
Bnyro 2022-07-12 14:52:14 +00:00 committed by GitHub
commit 1f296fe128
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,6 +11,7 @@ import android.util.Log
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import android.widget.Toast import android.widget.Toast
import androidx.core.app.ActivityCompat import androidx.core.app.ActivityCompat
import androidx.core.view.size
import androidx.fragment.app.DialogFragment import androidx.fragment.app.DialogFragment
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import com.github.libretube.R import com.github.libretube.R
@ -39,7 +40,7 @@ class DownloadDialog : DialogFragment() {
val builder = MaterialAlertDialogBuilder(it) val builder = MaterialAlertDialogBuilder(it)
binding = DialogDownloadBinding.inflate(layoutInflater) binding = DialogDownloadBinding.inflate(layoutInflater)
fetchStreams() fetchAvailableSources()
// request storage permissions if not granted yet // request storage permissions if not granted yet
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
@ -82,7 +83,7 @@ class DownloadDialog : DialogFragment() {
} ?: throw IllegalStateException("Activity cannot be null") } ?: throw IllegalStateException("Activity cannot be null")
} }
private fun fetchStreams() { private fun fetchAvailableSources() {
lifecycleScope.launchWhenCreated { lifecycleScope.launchWhenCreated {
val response = try { val response = try {
RetrofitInstance.api.getStreams(videoId) RetrofitInstance.api.getStreams(videoId)
@ -110,9 +111,11 @@ class DownloadDialog : DialogFragment() {
// add all available video streams // add all available video streams
for (vid in streams.videoStreams!!) { for (vid in streams.videoStreams!!) {
val name = vid.quality + " " + vid.format if (vid.url != null) {
vidName.add(name) val name = vid.quality + " " + vid.format
vidUrl.add(vid.url!!) vidName.add(name)
vidUrl.add(vid.url!!)
}
} }
val audioName = arrayListOf<String>() val audioName = arrayListOf<String>()
@ -124,11 +127,14 @@ class DownloadDialog : DialogFragment() {
// add all available audio streams // add all available audio streams
for (audio in streams.audioStreams!!) { for (audio in streams.audioStreams!!) {
val name = audio.quality + " " + audio.format if (audio.url != null) {
audioName.add(name) val name = audio.quality + " " + audio.format
audioUrl.add(audio.url!!) audioName.add(name)
audioUrl.add(audio.url!!)
}
} }
// initialize the video sources
val videoArrayAdapter = ArrayAdapter( val videoArrayAdapter = ArrayAdapter(
requireContext(), requireContext(),
android.R.layout.simple_spinner_item, android.R.layout.simple_spinner_item,
@ -136,8 +142,9 @@ class DownloadDialog : DialogFragment() {
) )
videoArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) videoArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
binding.videoSpinner.adapter = videoArrayAdapter binding.videoSpinner.adapter = videoArrayAdapter
binding.videoSpinner.setSelection(1) if (binding.videoSpinner.size >= 1) binding.videoSpinner.setSelection(1)
// initialize the audio sources
val audioArrayAdapter = ArrayAdapter( val audioArrayAdapter = ArrayAdapter(
requireContext(), requireContext(),
android.R.layout.simple_spinner_item, android.R.layout.simple_spinner_item,
@ -145,7 +152,7 @@ class DownloadDialog : DialogFragment() {
) )
audioArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) audioArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
binding.audioSpinner.adapter = audioArrayAdapter binding.audioSpinner.adapter = audioArrayAdapter
binding.audioSpinner.setSelection(1) if (binding.audioSpinner.size >= 1) binding.audioSpinner.setSelection(1)
binding.download.setOnClickListener { binding.download.setOnClickListener {
val selectedAudioUrl = audioUrl[binding.audioSpinner.selectedItemPosition] val selectedAudioUrl = audioUrl[binding.audioSpinner.selectedItemPosition]