Remember the download selections

This commit is contained in:
Bnyro 2023-02-12 16:52:31 +01:00
parent 22af1f9362
commit 26a4eeea6f

View File

@ -7,7 +7,6 @@ import android.util.Log
import android.view.View import android.view.View
import android.widget.ArrayAdapter import android.widget.ArrayAdapter
import android.widget.Toast import android.widget.Toast
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
@ -166,26 +165,69 @@ class DownloadDialog(
} }
} }
/**
* Save the download selection to the preferences
*/
private fun saveSelections( private fun saveSelections(
videoStream: PipedStream?, videoStream: PipedStream?,
audioStream: PipedStream?, audioStream: PipedStream?,
subtitle: Subtitle? subtitle: Subtitle?
) { ) {
PreferenceHelper.putString(SUBTITLE_LANGUAGE, subtitle?.code.orEmpty()) PreferenceHelper.putString(SUBTITLE_LANGUAGE, subtitle?.code.orEmpty())
PreferenceHelper.putString(VIDEO_DOWNLOAD_FORMAT, videoStream?.format.orEmpty())
PreferenceHelper.putString(VIDEO_DOWNLOAD_QUALITY, videoStream?.quality.orEmpty())
PreferenceHelper.putString(AUDIO_DOWNLOAD_FORMAT, audioStream?.format.orEmpty())
PreferenceHelper.putString(AUDIO_DOWNLOAD_QUALITY, audioStream?.quality.orEmpty())
} }
private fun getSel(key: String) = PreferenceHelper.getString(key, "")
/**
* Restore the download selections from a previous session
*/
private fun restorePreviousSelections( private fun restorePreviousSelections(
videoStreams: List<PipedStream>, videoStreams: List<PipedStream>,
audioStream: List<PipedStream>, audioStreams: List<PipedStream>,
subtitles: List<Subtitle> subtitles: List<Subtitle>
) { ) {
if (binding.videoSpinner.size >= 1) binding.videoSpinner.setSelection(1) getStreamSelection(
if (binding.audioSpinner.size >= 1) binding.audioSpinner.setSelection(1) videoStreams,
getSel(VIDEO_DOWNLOAD_QUALITY),
val lastSubtitleSelection = PreferenceHelper.getString(SUBTITLE_LANGUAGE, "") getSel(VIDEO_DOWNLOAD_FORMAT)
subtitles.indexOfFirst { it.code == lastSubtitleSelection }.takeIf { it > 0 }?.let { )?.let {
binding.subtitleSpinner.setSelection(it) binding.videoSpinner.setSelection(it + 1)
} }
getStreamSelection(
audioStreams,
getSel(AUDIO_DOWNLOAD_QUALITY),
getSel(AUDIO_DOWNLOAD_FORMAT)
)?.let {
binding.audioSpinner.setSelection(it + 1)
}
subtitles.indexOfFirst { it.code == getSel(SUBTITLE_LANGUAGE) }.takeIf { it != -1 }?.let {
binding.subtitleSpinner.setSelection(it + 1)
}
}
private fun getStreamSelection(streams: List<PipedStream>, quality: String, format: String): Int? {
if (quality.isBlank()) return null
streams.forEachIndexed { index, pipedStream ->
if (quality == pipedStream.quality && format == pipedStream.format) return index
}
streams.forEachIndexed { index, pipedStream ->
if (quality == pipedStream.quality) return index
}
val qualityInt = quality.getWhileDigit() ?: return null
streams.forEachIndexed { index, pipedStream ->
if ((pipedStream.quality.getWhileDigit() ?: Int.MAX_VALUE) < qualityInt) return index
}
return null
} }
companion object { companion object {