added preferred download id & download now button on schedule downloads in the details sheet

This commit is contained in:
deniscerri 2023-04-27 19:53:07 +02:00
parent e0bc57655a
commit 562dd82845
No known key found for this signature in database
GPG key ID: 95C43D517D830350
7 changed files with 67 additions and 18 deletions

View file

@ -66,6 +66,7 @@ class MainActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ThemeUtil.updateTheme(this)
setContentView(R.layout.activity_main)
context = baseContext
resultViewModel = ViewModelProvider(this)[ResultViewModel::class.java]

View file

@ -4,12 +4,10 @@ import android.app.Activity
import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import android.content.res.Resources
import android.net.ConnectivityManager
import android.widget.Toast
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import androidx.work.*
import com.deniscerri.ytdlnis.App
@ -22,7 +20,6 @@ import com.deniscerri.ytdlnis.work.DownloadWorker
import com.google.gson.Gson
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.util.concurrent.TimeUnit
@ -41,6 +38,9 @@ class DownloadViewModel(private val application: Application) : AndroidViewModel
private var bestVideoFormat : Format
private var bestAudioFormat : Format
private var defaultVideoFormats : MutableList<Format>
private val videoQualityPreference: String
private val formatIDPreference: String
enum class Type {
audio, video, command
}
@ -60,6 +60,9 @@ class DownloadViewModel(private val application: Application) : AndroidViewModel
cancelledDownloads = repository.cancelledDownloads
erroredDownloads = repository.erroredDownloads
videoQualityPreference = sharedPreferences.getString("video_quality", application.getString(R.string.best_quality)).toString()
formatIDPreference = sharedPreferences.getString("format_id", "").toString()
val videoFormat = getApplication<App>().resources.getStringArray(R.array.video_formats)
var videoContainer = sharedPreferences.getString("video_format", "Default")
if (videoContainer == "Default") videoContainer = application.getString(R.string.defaultValue)
@ -236,7 +239,11 @@ class DownloadViewModel(private val application: Application) : AndroidViewModel
Type.audio -> {
return cloneFormat (
try {
formats.last { it.format_note.contains("audio", ignoreCase = true) }
try{
formats.first { it.format_note.contains("audio", ignoreCase = true) && it.format_id == formatIDPreference }
}catch (e: Exception){
formats.last { it.format_note.contains("audio", ignoreCase = true) }
}
}catch (e: Exception){
bestAudioFormat
}
@ -247,18 +254,24 @@ class DownloadViewModel(private val application: Application) : AndroidViewModel
return cloneFormat(
try {
val theFormats = formats.ifEmpty { defaultVideoFormats }
val qualityPreference = sharedPreferences.getString("video_quality", application.getString(R.string.best_quality))
if (qualityPreference == getApplication<App>().resources.getString(R.string.worst_quality)){
theFormats.first { !it.format_note.contains("audio", ignoreCase = true) }
}
if (qualityPreference == getApplication<App>().resources.getString(R.string.best_quality)){
theFormats.last { !it.format_note.contains("audio", ignoreCase = true) }
}
try{
theFormats.last { format -> format.format_note.contains(qualityPreference!!.substring(0, qualityPreference.length - 1)) }
try {
formats.first { !it.format_note.contains("audio", ignoreCase = true) && it.format_id == formatIDPreference }
}catch (e: Exception){
theFormats.last { !it.format_note.contains("audio", ignoreCase = true) }
when (videoQualityPreference) {
getApplication<App>().resources.getString(R.string.worst_quality) -> {
theFormats.first {!it.format_note.contains("audio", ignoreCase = true) }
}
getApplication<App>().resources.getString(R.string.best_quality) -> {
theFormats.last {!it.format_note.contains("audio", ignoreCase = true) }
}
else -> {
try{
theFormats.last {it.format_note.contains(videoQualityPreference.substring(0, videoQualityPreference.length - 1)) }
}catch (e: Exception){
theFormats.last { !it.format_note.contains("audio", ignoreCase = true) }
}
}
}
}
}catch (e: Exception){
bestVideoFormat

View file

@ -57,6 +57,7 @@ class ShareActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
ThemeUtil.updateTheme(this)
WindowCompat.setDecorFitsSystemWindows(window, false)
ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { v, insets ->
v.setPadding(0, 0, 0, 0)

View file

@ -211,11 +211,27 @@ class QueuedDownloadsFragment : Fragment(), GenericDownloadAdapter.OnItemClickLi
val openFile = bottomSheet.findViewById<Button>(R.id.bottomsheet_open_file_button)
openFile!!.visibility = View.GONE
val redownload = bottomSheet.findViewById<Button>(R.id.bottomsheet_redownload_button)
redownload!!.visibility = View.GONE
val downloadNow = bottomSheet.findViewById<Button>(R.id.bottomsheet_redownload_button)
if (item.downloadStartTime <= System.currentTimeMillis() / 1000) downloadNow!!.visibility = View.GONE
else{
downloadNow!!.text = getString(R.string.download_now)
downloadNow.setOnClickListener {
bottomSheet.dismiss()
downloadViewModel.deleteDownload(item)
item.downloadStartTime = 0
WorkManager.getInstance(requireContext()).cancelUniqueWork(item.id.toString())
runBlocking {
downloadViewModel.queueDownloads(listOf(item))
}
}
}
bottomSheet.show()
bottomSheet.behavior.peekHeight = 512
val displayMetrics = DisplayMetrics()
requireActivity().windowManager.defaultDisplay.getMetrics(displayMetrics)
bottomSheet.behavior.peekHeight = displayMetrics.heightPixels
bottomSheet.window!!.setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT

View file

@ -71,6 +71,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
private var videoFormat: ListPreference? = null
private var audioQuality: SeekBarPreference? = null
private var videoQuality: ListPreference? = null
private var formatID: EditTextPreference? = null
private var updateYTDL: Preference? = null
private var updateNightlyYTDL: SwitchPreferenceCompat? = null
private var updateFormats: SwitchPreferenceCompat? = null
@ -144,6 +145,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
videoFormat = findPreference("video_format")
audioQuality = findPreference("audio_quality")
videoQuality = findPreference("video_quality")
formatID = findPreference("format_id")
updateYTDL = findPreference("update_ytdl")
updateNightlyYTDL = findPreference("nightly_ytdl")
updateFormats = findPreference("update_formats")
@ -217,6 +219,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
editor.putString("video_format", videoFormat!!.value)
editor.putInt("audio_quality", audioQuality!!.value)
editor.putString("video_quality", videoQuality!!.value)
editor.putString("format_id", formatID!!.text)
editor.putBoolean("nightly_ytdl", updateNightlyYTDL!!.isChecked)
editor.putBoolean("update_formats", updateFormats!!.isChecked)
editor.putString("formats_source", formatSource!!.value)
@ -556,6 +559,14 @@ class SettingsFragment : PreferenceFragmentCompat() {
editor.apply()
true
}
formatID!!.summary = formatID!!.text
formatID!!.onPreferenceChangeListener =
Preference.OnPreferenceChangeListener { _: Preference?, newValue: Any ->
formatID!!.summary = newValue.toString()
editor.putString("format_id", newValue.toString())
editor.apply()
true
}
updateYTDL!!.onPreferenceClickListener =
Preference.OnPreferenceClickListener {
lifecycleScope.launch {

View file

@ -244,4 +244,6 @@
<string name="quick_download">Quick Download</string>
<string name="quick_download_summary">Don\'t fetch data beforehand</string>
<string name="remove_audio">Remove Audio</string>
<string name="preferred_format_id">Preferred Format ID</string>
<string name="download_now">Download Now</string>
</resources>

View file

@ -308,6 +308,11 @@
app:summary="@string/best_quality"
app:title="@string/video_quality" />
<EditTextPreference
app:key="format_id"
app:defaultValue=""
app:title="@string/preferred_format_id" />
</PreferenceCategory>