fixed queued downloads not clearing & added yt-dlp version in settings

This commit is contained in:
deniscerri 2023-03-14 09:15:09 +01:00
parent bf4884c70a
commit 3c35a601a1
No known key found for this signature in database
GPG key ID: 95C43D517D830350
9 changed files with 93 additions and 72 deletions

View file

@ -43,6 +43,9 @@ interface DownloadDao {
@Query("DELETE FROM downloads WHERE status='Error'") @Query("DELETE FROM downloads WHERE status='Error'")
suspend fun deleteErrored() suspend fun deleteErrored()
@Query("DELETE FROM downloads WHERE status='Queued'")
suspend fun deleteQueued()
@Query("DELETE FROM downloads WHERE status='Processing' AND id=:id") @Query("DELETE FROM downloads WHERE status='Processing' AND id=:id")
suspend fun deleteSingleProcessing(id: Long) suspend fun deleteSingleProcessing(id: Long)

View file

@ -48,6 +48,10 @@ class DownloadRepository(private val downloadDao: DownloadDao) {
downloadDao.deleteErrored() downloadDao.deleteErrored()
} }
suspend fun deleteQueued(){
downloadDao.deleteQueued()
}
suspend fun deleteSingleProcessing(item: DownloadItem){ suspend fun deleteSingleProcessing(item: DownloadItem){
downloadDao.deleteSingleProcessing(item.id) downloadDao.deleteSingleProcessing(item.id)
} }

View file

@ -246,6 +246,10 @@ class DownloadViewModel(application: Application) : AndroidViewModel(application
repository.deleteErrored() repository.deleteErrored()
} }
fun deleteQueued() = viewModelScope.launch(Dispatchers.IO) {
repository.deleteQueued()
}
fun cloneDownloadItem(item: DownloadItem) : DownloadItem { fun cloneDownloadItem(item: DownloadItem) : DownloadItem {
val string = Gson().toJson(item, DownloadItem::class.java) val string = Gson().toJson(item, DownloadItem::class.java)
return Gson().fromJson(string, DownloadItem::class.java) return Gson().fromJson(string, DownloadItem::class.java)

View file

@ -4,8 +4,10 @@ import android.content.Context
import android.os.Bundle import android.os.Bundle
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
import android.view.ViewGroup
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.size
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2 import androidx.viewpager2.widget.ViewPager2
@ -45,19 +47,6 @@ class DownloadQueueActivity : AppCompatActivity(){
} }
val fragments = mutableListOf(ActiveDownloadsFragment(), QueuedDownloadsFragment(), CancelledDownloadsFragment(), ErroredDownloadsFragment()) val fragments = mutableListOf(ActiveDownloadsFragment(), QueuedDownloadsFragment(), CancelledDownloadsFragment(), ErroredDownloadsFragment())
//
// lifecycleScope.launch{
// withContext(Dispatchers.IO){
// val active = commandTemplateDao.getTotalNumber()
// if(nr > 0){
// fragments.add(DownloadCommandFragment(resultItem))
// }else{
// (tabLayout.getChildAt(0) as? ViewGroup)?.getChildAt(2)?.isEnabled = false
// (tabLayout.getChildAt(0) as? ViewGroup)?.getChildAt(2)?.alpha = 0.3f
// }
// }
// }
fragmentAdapter = DownloadListFragmentAdapter( fragmentAdapter = DownloadListFragmentAdapter(
supportFragmentManager, supportFragmentManager,
@ -87,10 +76,6 @@ class DownloadQueueActivity : AppCompatActivity(){
}) })
initMenu() initMenu()
// downloadViewModel.activeDownloads.observe(this){
// if (it.isEmpty()) tabLayout.getTabAt(0)!!.view.visibility = View.GONE
// else tabLayout.getTabAt(0)!!.view.visibility = View.VISIBLE
// }
} }
private fun initMenu() { private fun initMenu() {
@ -99,6 +84,7 @@ class DownloadQueueActivity : AppCompatActivity(){
when(m.itemId){ when(m.itemId){
R.id.clear_queue -> { R.id.clear_queue -> {
cancelAllDownloads() cancelAllDownloads()
downloadViewModel.deleteQueued()
} }
R.id.clear_cancelled -> { R.id.clear_cancelled -> {
downloadViewModel.deleteCancelled() downloadViewModel.deleteCancelled()
@ -116,7 +102,7 @@ class DownloadQueueActivity : AppCompatActivity(){
} }
private fun cancelAllDownloads() { private fun cancelAllDownloads() {
workManager.cancelAllWork(); workManager.cancelAllWork()
} }

View file

@ -11,6 +11,7 @@ import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatDelegate import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat import androidx.core.os.LocaleListCompat
import androidx.lifecycle.lifecycleScope
import androidx.preference.* import androidx.preference.*
import androidx.work.WorkInfo import androidx.work.WorkInfo
import androidx.work.WorkManager import androidx.work.WorkManager
@ -19,6 +20,8 @@ import com.deniscerri.ytdlnis.R
import com.deniscerri.ytdlnis.util.FileUtil import com.deniscerri.ytdlnis.util.FileUtil
import com.deniscerri.ytdlnis.util.UpdateUtil import com.deniscerri.ytdlnis.util.UpdateUtil
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.yausername.youtubedl_android.YoutubeDL
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import java.util.* import java.util.*
@ -54,6 +57,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
private var updateApp: SwitchPreferenceCompat? = null private var updateApp: SwitchPreferenceCompat? = null
private var exportPreferences : Preference? = null private var exportPreferences : Preference? = null
private var importPreferences : Preference? = null private var importPreferences : Preference? = null
private var ytdlVersion: Preference? = null
private var version: Preference? = null private var version: Preference? = null
@ -113,6 +117,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
updateApp = findPreference("update_app") updateApp = findPreference("update_app")
exportPreferences = findPreference("export_preferences") exportPreferences = findPreference("export_preferences")
importPreferences = findPreference("import_preferences") importPreferences = findPreference("import_preferences")
ytdlVersion = findPreference("ytdl-version")
ytdlVersion!!.summary = preferences.getString("ytdl-version", "NULL")
version = findPreference("version") version = findPreference("version")
version!!.summary = BuildConfig.VERSION_NAME version!!.summary = BuildConfig.VERSION_NAME
@ -398,7 +404,28 @@ class SettingsFragment : PreferenceFragmentCompat() {
} }
updateYTDL!!.onPreferenceClickListener = updateYTDL!!.onPreferenceClickListener =
Preference.OnPreferenceClickListener { Preference.OnPreferenceClickListener {
updateUtil!!.updateYoutubeDL() lifecycleScope.launch {
when (updateUtil!!.updateYoutubeDL()) {
YoutubeDL.UpdateStatus.DONE -> {
Toast.makeText(
context,
requireContext().getString(R.string.ytld_update_success),
Toast.LENGTH_LONG
).show()
YoutubeDL.getInstance().version(context)?.let {
editor.putString("ytdl-version", it)
editor.apply()
ytdlVersion!!.summary = it
}
}
YoutubeDL.UpdateStatus.ALREADY_UP_TO_DATE -> Toast.makeText(
context,
requireContext().getString(R.string.you_are_in_latest_version),
Toast.LENGTH_LONG
).show()
else -> Toast.makeText(context, this.toString(), Toast.LENGTH_LONG).show()
}
}
true true
} }
updateNightlyYTDL!!.onPreferenceChangeListener = updateNightlyYTDL!!.onPreferenceChangeListener =

View file

@ -18,6 +18,8 @@ import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.json.JSONException import org.json.JSONException
import org.json.JSONObject import org.json.JSONObject
import java.io.BufferedReader import java.io.BufferedReader
@ -143,56 +145,47 @@ class UpdateUtil(var context: Context) {
} }
} }
fun updateYoutubeDL() { suspend fun updateYoutubeDL() : UpdateStatus? =
val sharedPreferences = withContext(Dispatchers.IO){
context.getSharedPreferences("root_preferences", Activity.MODE_PRIVATE) val sharedPreferences =
if (updatingYTDL) { context.getSharedPreferences("root_preferences", Activity.MODE_PRIVATE)
Toast.makeText( val editor = sharedPreferences.edit()
context, if (updatingYTDL) {
context.getString(R.string.ytdl_already_updating), withContext(Dispatchers.Main){
Toast.LENGTH_LONG Toast.makeText(
).show()
return
}
Toast.makeText(
context,
context.getString(R.string.ytdl_updating_started),
Toast.LENGTH_SHORT
).show()
updatingYTDL = true
val disposable = Observable.fromCallable {
YoutubeDL.getInstance().updateYoutubeDL(
context, if (sharedPreferences.getBoolean("nightly_ytdl", false) ) ytdlpNightly else null
)
}
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ status: UpdateStatus ->
when (status) {
UpdateStatus.DONE ->
Toast.makeText(
context,
context.getString(R.string.ytld_update_success),
Toast.LENGTH_LONG
).show()
UpdateStatus.ALREADY_UP_TO_DATE -> Toast.makeText(
context, context,
context.getString(R.string.you_are_in_latest_version), context.getString(R.string.ytdl_already_updating),
Toast.LENGTH_LONG Toast.LENGTH_LONG
).show() ).show()
else -> Toast.makeText(context, status.toString(), Toast.LENGTH_LONG).show()
} }
updatingYTDL = false UpdateStatus.ALREADY_UP_TO_DATE
} as ((UpdateStatus?) -> Unit)?) { e: Throwable? -> }
if (BuildConfig.DEBUG) Log.e(tag, context.getString(R.string.ytdl_update_failed), e) withContext(Dispatchers.Main){
Toast.makeText( Toast.makeText(
context, context,
context.getString(R.string.ytdl_update_failed), context.getString(R.string.ytdl_updating_started),
Toast.LENGTH_LONG Toast.LENGTH_SHORT
).show() ).show()
}
updatingYTDL = true
YoutubeDL.getInstance().updateYoutubeDL(
context, if (sharedPreferences.getBoolean("nightly_ytdl", false) ) ytdlpNightly else null
).apply {
updatingYTDL = false updatingYTDL = false
} }
compositeDisposable.add(disposable)
// .onFailure {
// if (BuildConfig.DEBUG) Log.e(tag, context.getString(R.string.ytdl_update_failed), e)
// Toast.makeText(
// context,
// context.getString(R.string.ytdl_update_failed),
// Toast.LENGTH_LONG
// ).show()
// updatingYTDL = false
// }
//}
} }
companion object { companion object {

View file

@ -225,4 +225,5 @@
<string name="clear_temporary_files_summary">Delete cached files from crashed/errored/cancelled downloads</string> <string name="clear_temporary_files_summary">Delete cached files from crashed/errored/cancelled downloads</string>
<string name="cache_cleared">Temporary files cleared</string> <string name="cache_cleared">Temporary files cleared</string>
<string name="downloads_running_try_later">Download worker is running. Try again later</string> <string name="downloads_running_try_later">Download worker is running. Try again later</string>
<string name="ytdl_version">yt-dlp Version</string>
</resources> </resources>

View file

@ -6,18 +6,6 @@
app:enabled="false" app:enabled="false"
android:layout="@layout/fragment_more"/> android:layout="@layout/fragment_more"/>
<Preference
app:icon="@drawable/ic_code"
app:key="rreth"
app:summary="https://github.com/deniscerri/ytdlnis"
app:allowDividerBelow="true"
app:title="@string/source_code">
<intent
android:action="android.intent.action.VIEW"
android:data="https://github.com/deniscerri/ytdlnis" />
</Preference>
<Preference <Preference
app:icon="@drawable/ic_terminal" app:icon="@drawable/ic_terminal"
app:key="run_command" app:key="run_command"

View file

@ -293,6 +293,21 @@
android:data="https://hosted.weblate.org/projects/ytdlnis/" /> android:data="https://hosted.weblate.org/projects/ytdlnis/" />
</Preference> </Preference>
<Preference
app:icon="@drawable/ic_code"
app:key="rreth"
app:summary="https://github.com/deniscerri/ytdlnis"
app:allowDividerBelow="true"
app:title="@string/source_code">
<intent
android:action="android.intent.action.VIEW"
android:data="https://github.com/deniscerri/ytdlnis" />
</Preference>
<Preference
app:icon="@drawable/ic_info"
app:key="ytdl-version"
app:title="@string/ytdl_version"/>
<Preference <Preference
app:icon="@drawable/ic_info" app:icon="@drawable/ic_info"