From cd63e730119200d9fdc2afbea9bef6a1fb8c675c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20=C3=87erri?= <64997243+deniscerri@users.noreply.github.com> Date: Sat, 11 Mar 2023 08:59:03 +0100 Subject: [PATCH] added redownload button on deleted history items --- .../database/viewmodel/DownloadViewModel.kt | 29 +++++++++++++++++++ .../ui/downloadcard/DownloadAudioFragment.kt | 6 ++-- .../ui/downloadcard/DownloadVideoFragment.kt | 5 ++-- .../ytdlnis/ui/downloads/HistoryFragment.kt | 25 ++++++++++++---- app/src/main/res/layout/format_item.xml | 6 +++- .../res/layout/fragment_download_audio.xml | 2 +- .../res/layout/fragment_download_video.xml | 2 +- .../history_item_details_bottom_sheet.xml | 23 ++++++++++----- .../res/layout/search_suggestion_item.xml | 2 ++ app/src/main/res/values/strings.xml | 1 + 10 files changed, 80 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/com/deniscerri/ytdlnis/database/viewmodel/DownloadViewModel.kt b/app/src/main/java/com/deniscerri/ytdlnis/database/viewmodel/DownloadViewModel.kt index 8770783f..41eef16d 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/database/viewmodel/DownloadViewModel.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/database/viewmodel/DownloadViewModel.kt @@ -119,6 +119,35 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application } + fun createDownloadItemFromHistory(historyItem: HistoryItem) : DownloadItem { + val embedSubs = sharedPreferences.getBoolean("embed_subtitles", false) + val addChapters = sharedPreferences.getBoolean("add_chapters", false) + val saveThumb = sharedPreferences.getBoolean("write_thumbnail", false) + val embedThumb = sharedPreferences.getBoolean("embed_thumbnail", false) + val customFileNameTemplate = sharedPreferences.getString("file_name_template", "%(uploader)s - %(title)s") + + val downloadPath = when(historyItem.type){ + Type.audio -> sharedPreferences.getString("music_path", getApplication().resources.getString(R.string.music_path)) + Type.video -> sharedPreferences.getString("video_path", getApplication().resources.getString(R.string.video_path)) + else -> sharedPreferences.getString("command_path", getApplication().resources.getString(R.string.command_path)) + } + + val audioPreferences = AudioPreferences(embedThumb) + val videoPreferences = VideoPreferences(embedSubs, addChapters) + + return DownloadItem(0, + historyItem.url, + historyItem.title, + historyItem.author, + historyItem.thumb, + historyItem.duration, + historyItem.type, + historyItem.format, + downloadPath!!, historyItem.website, "", "", audioPreferences, videoPreferences,customFileNameTemplate!!, saveThumb, DownloadRepository.Status.Processing.toString(), 0 + ) + + } + private fun getFormat(resultItem: ResultItem?, type: Type) : Format { when(type) { diff --git a/app/src/main/java/com/deniscerri/ytdlnis/ui/downloadcard/DownloadAudioFragment.kt b/app/src/main/java/com/deniscerri/ytdlnis/ui/downloadcard/DownloadAudioFragment.kt index b59de53b..8272e66f 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/ui/downloadcard/DownloadAudioFragment.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/ui/downloadcard/DownloadAudioFragment.kt @@ -111,6 +111,7 @@ class DownloadAudioFragment(private var resultItem: ResultItem, private var curr var formats = resultItem.formats.filter { it.format_note.contains("audio", ignoreCase = true) } val containers = requireContext().resources.getStringArray(R.array.audio_containers) + val containerPreference = sharedPreferences.getString("audio_format", getString(R.string.defaultValue)) val container = view.findViewById(R.id.downloadContainer) val containerAutoCompleteTextView = view.findViewById(R.id.container_textview) @@ -125,7 +126,6 @@ class DownloadAudioFragment(private var resultItem: ResultItem, private var curr val listener = object : OnFormatClickListener { override fun onFormatClick(allFormats: List, item: Format) { downloadItem.format = item - if (containers.contains(item.container)){ downloadItem.format.container = item.container containerAutoCompleteTextView.setText(item.container, false) @@ -157,7 +157,9 @@ class DownloadAudioFragment(private var resultItem: ResultItem, private var curr ) ) - containerAutoCompleteTextView!!.setText(downloadItem.format.container, false) + downloadItem.format.container = containerPreference!! + containerAutoCompleteTextView.setText(downloadItem.format.container, false) + (container!!.editText as AutoCompleteTextView?)!!.onItemClickListener = AdapterView.OnItemClickListener { _: AdapterView<*>?, _: View?, index: Int, _: Long -> downloadItem.format.container = containers[index] diff --git a/app/src/main/java/com/deniscerri/ytdlnis/ui/downloadcard/DownloadVideoFragment.kt b/app/src/main/java/com/deniscerri/ytdlnis/ui/downloadcard/DownloadVideoFragment.kt index 880d37e3..81dc56b3 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/ui/downloadcard/DownloadVideoFragment.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/ui/downloadcard/DownloadVideoFragment.kt @@ -117,7 +117,7 @@ class DownloadVideoFragment(private val resultItem: ResultItem, private var curr var formats = mutableListOf() formats.addAll(resultItem.formats.filter { !it.format_note.contains("audio", ignoreCase = true) }) val videoFormats = resources.getStringArray(R.array.video_formats) - val containerPreference = sharedPreferences.getString("video_format", "Default") + val containerPreference = sharedPreferences.getString("video_format", getString(R.string.defaultValue)) if (formats.isEmpty()) { @@ -139,14 +139,13 @@ class DownloadVideoFragment(private val resultItem: ResultItem, private var curr ) ) downloadItem.format.container = containerPreference!! + containerAutoCompleteTextView!!.setText(downloadItem.format.container, false) (container!!.editText as AutoCompleteTextView?)!!.onItemClickListener = AdapterView.OnItemClickListener { _: AdapterView<*>?, _: View?, index: Int, _: Long -> downloadItem.format.container = containers[index] } - containerAutoCompleteTextView!!.setText(downloadItem.format.container, false) - val formatCard = view.findViewById(R.id.format_card_constraintLayout) val chosenFormat = downloadItem.format uiUtil.populateFormatCard(formatCard, chosenFormat) diff --git a/app/src/main/java/com/deniscerri/ytdlnis/ui/downloads/HistoryFragment.kt b/app/src/main/java/com/deniscerri/ytdlnis/ui/downloads/HistoryFragment.kt index 5b827547..721aad29 100644 --- a/app/src/main/java/com/deniscerri/ytdlnis/ui/downloads/HistoryFragment.kt +++ b/app/src/main/java/com/deniscerri/ytdlnis/ui/downloads/HistoryFragment.kt @@ -4,8 +4,6 @@ import android.app.Activity import android.content.Context import android.content.DialogInterface import android.content.res.Configuration -import android.graphics.ColorMatrix -import android.graphics.ColorMatrixColorFilter import android.os.Bundle import android.os.Handler import android.os.Looper @@ -48,6 +46,7 @@ import java.io.File */ class HistoryFragment : Fragment(), HistoryAdapter.OnItemClickListener{ private lateinit var historyViewModel : HistoryViewModel + private lateinit var downloadViewModel : DownloadViewModel private var fragmentView: View? = null private var activity: Activity? = null @@ -146,6 +145,8 @@ class HistoryFragment : Fragment(), HistoryAdapter.OnItemClickListener{ scrollToTop() } + downloadViewModel = ViewModelProvider(this)[DownloadViewModel::class.java] + initMenu() initChips() } @@ -406,7 +407,7 @@ class HistoryFragment : Fragment(), HistoryAdapter.OnItemClickListener{ val codec = bottomSheet!!.findViewById(R.id.codec) val fileSize = bottomSheet!!.findViewById(R.id.file_size) - if (item.format.format_note == "?" || item.format.format_note == "") formatNote!!.visibility = View.GONE + if (item.format.format_note == "?" || item.format.format_note == "") formatNote!!.visibility = GONE else formatNote!!.text = item.format.format_note val codecText = @@ -418,14 +419,14 @@ class HistoryFragment : Fragment(), HistoryAdapter.OnItemClickListener{ item.format.acodec.uppercase() } if (codecText == "" || codecText == "none"){ - codec!!.visibility = View.GONE + codec!!.visibility = GONE }else{ - codec!!.visibility = View.VISIBLE + codec!!.visibility = VISIBLE codec.text = codecText } val fileSizeReadable = fileUtil!!.convertFileSize(item.format.filesize) - if (fileSizeReadable == "?") fileSize!!.visibility = View.GONE + if (fileSizeReadable == "?") fileSize!!.visibility = GONE else fileSize!!.text = fileSizeReadable val link = bottomSheet!!.findViewById