From 0ebe784a90a14bdaebfb97c31c995200b9412f5d Mon Sep 17 00:00:00 2001 From: Bnyro Date: Mon, 11 Sep 2023 11:09:22 +0200 Subject: [PATCH] refactor: remove constructors from PlaybackOptionsSheet --- .../libretube/ui/adapters/DownloadsAdapter.kt | 20 ++++++++++---- .../ui/sheets/DownloadOptionsBottomSheet.kt | 27 ++++++++++++------- 2 files changed, 33 insertions(+), 14 deletions(-) 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 18db170fd..d4d161160 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 @@ -5,7 +5,7 @@ import android.content.Context import android.content.Intent import android.view.LayoutInflater import android.view.ViewGroup -import androidx.appcompat.app.AppCompatActivity +import androidx.core.os.bundleOf import androidx.core.view.isGone import androidx.core.view.isVisible import androidx.recyclerview.widget.RecyclerView @@ -17,7 +17,9 @@ import com.github.libretube.db.obj.DownloadWithItems import com.github.libretube.extensions.formatAsFileSize import com.github.libretube.helpers.ImageHelper import com.github.libretube.ui.activities.OfflinePlayerActivity +import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.sheets.DownloadOptionsBottomSheet +import com.github.libretube.ui.sheets.DownloadOptionsBottomSheet.Companion.DELETE_DOWNLOAD_REQUEST_KEY import com.github.libretube.ui.viewholders.DownloadsViewHolder import com.github.libretube.util.TextUtils import com.google.android.material.dialog.MaterialAlertDialogBuilder @@ -97,11 +99,19 @@ class DownloadsAdapter( } root.setOnLongClickListener { - DownloadOptionsBottomSheet(download) { + val activity = root.context as BaseActivity + val fragmentManager = activity.supportFragmentManager + fragmentManager.setFragmentResultListener(DELETE_DOWNLOAD_REQUEST_KEY, activity) { _, _ -> showDeleteDialog(root.context, position) - }.show( - (root.context as AppCompatActivity).supportFragmentManager - ) + } + DownloadOptionsBottomSheet() + .apply { + arguments = bundleOf( + IntentData.videoId to download.videoId, + IntentData.channelName to download.uploader + ) + } + .show(fragmentManager) true } } diff --git a/app/src/main/java/com/github/libretube/ui/sheets/DownloadOptionsBottomSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/DownloadOptionsBottomSheet.kt index f7fe98c03..73640cd1a 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/DownloadOptionsBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/DownloadOptionsBottomSheet.kt @@ -4,6 +4,7 @@ import android.content.Intent import android.os.Bundle import androidx.core.content.ContextCompat import androidx.core.os.bundleOf +import androidx.fragment.app.setFragmentResult import com.github.libretube.R import com.github.libretube.constants.IntentData import com.github.libretube.db.obj.Download @@ -13,34 +14,38 @@ import com.github.libretube.obj.ShareData import com.github.libretube.services.OfflinePlayerService import com.github.libretube.ui.dialogs.ShareDialog -class DownloadOptionsBottomSheet( - private val download: Download, - private val onDelete: () -> Unit -) : BaseBottomSheet() { +class DownloadOptionsBottomSheet : BaseBottomSheet() { + private lateinit var videoId: String + private lateinit var uploader: String + override fun onCreate(savedInstanceState: Bundle?) { + videoId = arguments?.getString(IntentData.videoId)!! + uploader = arguments?.getString(IntentData.channelName)!! + val options = listOf( R.string.playOnBackground, R.string.go_to_video, R.string.share, R.string.delete ).map { getString(it) } + setSimpleItems(options) { selectedIndex -> when (selectedIndex) { 0 -> { val playerIntent = Intent(requireContext(), OfflinePlayerService::class.java) - .putExtra(IntentData.videoId, download.videoId) + .putExtra(IntentData.videoId, videoId) context?.stopService(playerIntent) ContextCompat.startForegroundService(requireContext(), playerIntent) } 1 -> { - NavigationHelper.navigateVideo(requireContext(), videoId = download.videoId) + NavigationHelper.navigateVideo(requireContext(), videoId = videoId) } 2 -> { - val shareData = ShareData(currentVideo = download.uploader) + val shareData = ShareData(currentVideo = uploader) val bundle = bundleOf( - IntentData.id to download.videoId, + IntentData.id to videoId, IntentData.shareObjectType to ShareObjectType.CHANNEL, IntentData.shareData to shareData ) @@ -50,7 +55,7 @@ class DownloadOptionsBottomSheet( } 3 -> { - onDelete.invoke() + setFragmentResult(DELETE_DOWNLOAD_REQUEST_KEY, bundleOf()) dialog?.dismiss() } } @@ -58,4 +63,8 @@ class DownloadOptionsBottomSheet( super.onCreate(savedInstanceState) } + + companion object { + const val DELETE_DOWNLOAD_REQUEST_KEY = "delete_download_request_key" + } }