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

pull/49/head
deniscerri 3 years ago
parent bf4884c70a
commit 3c35a601a1
No known key found for this signature in database
GPG Key ID: 95C43D517D830350

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

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

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

@ -4,8 +4,10 @@ import android.content.Context
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.size
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.RecyclerView
import androidx.viewpager2.widget.ViewPager2
@ -45,19 +47,6 @@ class DownloadQueueActivity : AppCompatActivity(){
}
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(
supportFragmentManager,
@ -87,10 +76,6 @@ class DownloadQueueActivity : AppCompatActivity(){
})
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() {
@ -99,6 +84,7 @@ class DownloadQueueActivity : AppCompatActivity(){
when(m.itemId){
R.id.clear_queue -> {
cancelAllDownloads()
downloadViewModel.deleteQueued()
}
R.id.clear_cancelled -> {
downloadViewModel.deleteCancelled()
@ -116,7 +102,7 @@ class DownloadQueueActivity : AppCompatActivity(){
}
private fun cancelAllDownloads() {
workManager.cancelAllWork();
workManager.cancelAllWork()
}

@ -11,6 +11,7 @@ import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.os.LocaleListCompat
import androidx.lifecycle.lifecycleScope
import androidx.preference.*
import androidx.work.WorkInfo
import androidx.work.WorkManager
@ -19,6 +20,8 @@ import com.deniscerri.ytdlnis.R
import com.deniscerri.ytdlnis.util.FileUtil
import com.deniscerri.ytdlnis.util.UpdateUtil
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.yausername.youtubedl_android.YoutubeDL
import kotlinx.coroutines.launch
import kotlinx.serialization.json.Json
import java.util.*
@ -54,6 +57,7 @@ class SettingsFragment : PreferenceFragmentCompat() {
private var updateApp: SwitchPreferenceCompat? = null
private var exportPreferences : Preference? = null
private var importPreferences : Preference? = null
private var ytdlVersion: Preference? = null
private var version: Preference? = null
@ -113,6 +117,8 @@ class SettingsFragment : PreferenceFragmentCompat() {
updateApp = findPreference("update_app")
exportPreferences = findPreference("export_preferences")
importPreferences = findPreference("import_preferences")
ytdlVersion = findPreference("ytdl-version")
ytdlVersion!!.summary = preferences.getString("ytdl-version", "NULL")
version = findPreference("version")
version!!.summary = BuildConfig.VERSION_NAME
@ -398,7 +404,28 @@ class SettingsFragment : PreferenceFragmentCompat() {
}
updateYTDL!!.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
}
updateNightlyYTDL!!.onPreferenceChangeListener =

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

@ -225,4 +225,5 @@
<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="downloads_running_try_later">Download worker is running. Try again later</string>
<string name="ytdl_version">yt-dlp Version</string>
</resources>

@ -6,18 +6,6 @@
app:enabled="false"
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
app:icon="@drawable/ic_terminal"
app:key="run_command"

@ -293,6 +293,21 @@
android:data="https://hosted.weblate.org/projects/ytdlnis/" />
</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
app:icon="@drawable/ic_info"

Loading…
Cancel
Save