From b5e7aeb5cde017f968c6fb181a50e2214b3cccd4 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 7 Feb 2023 19:31:43 +0100 Subject: [PATCH 1/2] Convert delete download dialog to confirmation dialog --- .../libretube/services/DownloadService.kt | 2 +- .../libretube/ui/adapters/DownloadsAdapter.kt | 36 ++++++++----------- .../ui/fragments/DownloadsFragment.kt | 3 +- .../ui/fragments/SearchResultFragment.kt | 2 +- .../ui/preferences/BackupRestoreSettings.kt | 4 +-- 5 files changed, 20 insertions(+), 27 deletions(-) diff --git a/app/src/main/java/com/github/libretube/services/DownloadService.kt b/app/src/main/java/com/github/libretube/services/DownloadService.kt index 36219d1cf..eefe9affd 100644 --- a/app/src/main/java/com/github/libretube/services/DownloadService.kt +++ b/app/src/main/java/com/github/libretube/services/DownloadService.kt @@ -8,9 +8,9 @@ import android.os.Binder import android.os.IBinder import android.util.SparseBooleanArray import androidx.core.app.NotificationCompat +import androidx.core.app.ServiceCompat import androidx.core.util.set import androidx.core.util.valueIterator -import androidx.core.app.ServiceCompat import com.github.libretube.R import com.github.libretube.api.CronetHelper import com.github.libretube.api.RetrofitInstance diff --git a/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt index df46893c3..59d0a673d 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/DownloadsAdapter.kt @@ -91,29 +91,23 @@ class DownloadsAdapter( root.setOnLongClickListener { MaterialAlertDialogBuilder(root.context) - .setItems( - arrayOf( - root.context.getString(R.string.delete) - ) - ) { _, index -> - when (index) { - 0 -> { - items.map { File(it.path) }.forEach { file -> - if (file.exists()) { - try { - file.delete() - } catch (_: Exception) { } - } - } - - query { - DatabaseHolder.Database.downloadDao().deleteDownload(download) - } - downloads.removeAt(position) - notifyItemRemoved(position) - notifyItemRangeChanged(position, itemCount) + .setTitle(R.string.delete) + .setMessage(R.string.irreversible) + .setPositiveButton(R.string.okay) { _, _ -> + items.map { File(it.path) }.forEach { file -> + if (file.exists()) { + try { + file.delete() + } catch (_: Exception) { } } } + + query { + DatabaseHolder.Database.downloadDao().deleteDownload(download) + } + downloads.removeAt(position) + notifyItemRemoved(position) + notifyItemRangeChanged(position, itemCount) } .setNegativeButton(R.string.cancel, null) .show() diff --git a/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt index b23146823..52d0ebfac 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt @@ -9,7 +9,6 @@ import android.os.IBinder import android.view.LayoutInflater import android.view.View import android.view.ViewGroup -import androidx.core.view.size import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -109,7 +108,7 @@ class DownloadsFragment : BaseFragment() { binding.downloads.adapter?.registerAdapterDataObserver( object : RecyclerView.AdapterDataObserver() { override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) { - if (binding.downloads.size == 0) { + if (itemCount == 0) { binding.downloads.visibility = View.GONE binding.downloadsEmpty.visibility = View.VISIBLE } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt index 860b019cd..64840a23c 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/SearchResultFragment.kt @@ -19,9 +19,9 @@ import com.github.libretube.extensions.hideKeyboard import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.ui.adapters.SearchAdapter import com.github.libretube.ui.base.BaseFragment +import java.io.IOException import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.withContext -import java.io.IOException import retrofit2.HttpException class SearchResultFragment : BaseFragment() { diff --git a/app/src/main/java/com/github/libretube/ui/preferences/BackupRestoreSettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/BackupRestoreSettings.kt index 9deee4061..119c71ced 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/BackupRestoreSettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/BackupRestoreSettings.kt @@ -11,10 +11,10 @@ import com.github.libretube.helpers.ImportHelper import com.github.libretube.obj.BackupFile import com.github.libretube.ui.base.BasePreferenceFragment import com.github.libretube.ui.dialogs.BackupDialog -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.launch import java.time.LocalDateTime import java.time.format.DateTimeFormatter +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch class BackupRestoreSettings : BasePreferenceFragment() { private val backupDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm:ss") From fb0054d15e282cb6a3974b5154dbecaeff87c3d6 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Tue, 7 Feb 2023 19:36:59 +0100 Subject: [PATCH 2/2] Fix the downloads empty indicator --- .../com/github/libretube/ui/fragments/DownloadsFragment.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt index 52d0ebfac..3d862d05e 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/DownloadsFragment.kt @@ -108,11 +108,11 @@ class DownloadsFragment : BaseFragment() { binding.downloads.adapter?.registerAdapterDataObserver( object : RecyclerView.AdapterDataObserver() { override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) { - if (itemCount == 0) { + super.onItemRangeRemoved(positionStart, itemCount) + if (binding.downloads.adapter?.itemCount == 0) { binding.downloads.visibility = View.GONE binding.downloadsEmpty.visibility = View.VISIBLE } - super.onItemRangeRemoved(positionStart, itemCount) } } )