From 31769c756d0bfa05ac392777b32798a585f321a1 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 10 Sep 2023 14:08:39 +0200 Subject: [PATCH] fix: remove fragment constructors from video and channel options bottom sheet --- .../github/libretube/api/obj/StreamItem.kt | 5 +- .../github/libretube/constants/IntentData.kt | 1 + .../ui/adapters/LegacySubscriptionAdapter.kt | 10 +++- .../libretube/ui/adapters/PlaylistAdapter.kt | 15 ++++-- .../libretube/ui/adapters/PlaylistsAdapter.kt | 54 +++++++++---------- .../libretube/ui/adapters/SearchAdapter.kt | 22 +++++--- .../ui/adapters/SubscriptionChannelAdapter.kt | 10 +++- .../libretube/ui/adapters/VideosAdapter.kt | 30 +++++------ .../ui/adapters/WatchHistoryAdapter.kt | 14 +++-- .../ui/fragments/AudioPlayerFragment.kt | 5 +- .../ui/sheets/ChannelOptionsBottomSheet.kt | 14 ++--- .../github/libretube/ui/sheets/StatsSheet.kt | 4 +- .../ui/sheets/VideoOptionsBottomSheet.kt | 24 +++++---- 13 files changed, 122 insertions(+), 86 deletions(-) diff --git a/app/src/main/java/com/github/libretube/api/obj/StreamItem.kt b/app/src/main/java/com/github/libretube/api/obj/StreamItem.kt index 0345068e9..77ef0dee3 100644 --- a/app/src/main/java/com/github/libretube/api/obj/StreamItem.kt +++ b/app/src/main/java/com/github/libretube/api/obj/StreamItem.kt @@ -1,10 +1,13 @@ package com.github.libretube.api.obj +import android.os.Parcelable import com.github.libretube.db.obj.LocalPlaylistItem import com.github.libretube.extensions.toID +import kotlinx.parcelize.Parcelize import kotlinx.serialization.Serializable @Serializable +@Parcelize data class StreamItem( val url: String? = null, val type: String? = null, @@ -20,7 +23,7 @@ data class StreamItem( val uploaded: Long? = null, val shortDescription: String? = null, val isShort: Boolean = false -) { +): Parcelable { fun toLocalPlaylistItem(playlistId: String): LocalPlaylistItem { return LocalPlaylistItem( playlistId = playlistId.toInt(), diff --git a/app/src/main/java/com/github/libretube/constants/IntentData.kt b/app/src/main/java/com/github/libretube/constants/IntentData.kt index d787d67d0..472a1cbe1 100644 --- a/app/src/main/java/com/github/libretube/constants/IntentData.kt +++ b/app/src/main/java/com/github/libretube/constants/IntentData.kt @@ -28,4 +28,5 @@ object IntentData { const val loginTask = "loginTask" const val logoutTask = "logoutTask" const val color = "color" + const val streamItem = "streamItem" } diff --git a/app/src/main/java/com/github/libretube/ui/adapters/LegacySubscriptionAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/LegacySubscriptionAdapter.kt index 3d8114638..2fa97a00b 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/LegacySubscriptionAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/LegacySubscriptionAdapter.kt @@ -2,7 +2,9 @@ package com.github.libretube.ui.adapters import android.view.LayoutInflater import android.view.ViewGroup +import androidx.core.os.bundleOf import androidx.recyclerview.widget.RecyclerView +import com.github.libretube.constants.IntentData import com.github.libretube.databinding.LegacySubscriptionChannelBinding import com.github.libretube.extensions.toID import com.github.libretube.helpers.ImageHelper @@ -40,8 +42,12 @@ class LegacySubscriptionAdapter( } root.setOnLongClickListener { - ChannelOptionsBottomSheet(subscription.url.toID(), subscription.name) - .show((root.context as BaseActivity).supportFragmentManager) + val channelOptionsSheet = ChannelOptionsBottomSheet() + channelOptionsSheet.arguments = bundleOf( + IntentData.channelId to subscription.url.toID(), + IntentData.channelName to subscription.name + ) + channelOptionsSheet.show((root.context as BaseActivity).supportFragmentManager) true } } diff --git a/app/src/main/java/com/github/libretube/ui/adapters/PlaylistAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/PlaylistAdapter.kt index 91ac35d07..58f2c6357 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/PlaylistAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/PlaylistAdapter.kt @@ -5,12 +5,14 @@ import android.content.Context import android.util.Log import android.view.LayoutInflater import android.view.ViewGroup +import androidx.core.os.bundleOf import androidx.core.view.isGone import androidx.core.view.updatePadding import androidx.recyclerview.widget.RecyclerView import com.github.libretube.R import com.github.libretube.api.PlaylistsHelper import com.github.libretube.api.obj.StreamItem +import com.github.libretube.constants.IntentData import com.github.libretube.databinding.VideoRowBinding import com.github.libretube.enums.PlaylistType import com.github.libretube.extensions.TAG @@ -23,6 +25,7 @@ import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.extensions.setFormattedDuration import com.github.libretube.ui.extensions.setWatchProgressLength import com.github.libretube.ui.sheets.VideoOptionsBottomSheet +import com.github.libretube.ui.sheets.VideoOptionsBottomSheet.Companion.VIDEO_OPTIONS_SHEET_REQUEST_KEY import com.github.libretube.ui.viewholders.PlaylistViewHolder import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -80,14 +83,16 @@ class PlaylistAdapter( NavigationHelper.navigateVideo(root.context, streamItem.url, playlistId) } val videoId = streamItem.url!!.toID() + + val activity = (root.context as BaseActivity) + val fragmentManager = activity.supportFragmentManager root.setOnLongClickListener { - VideoOptionsBottomSheet(streamItem) { + fragmentManager.setFragmentResultListener(VIDEO_OPTIONS_SHEET_REQUEST_KEY, activity) { _, _ -> notifyItemChanged(position) } - .show( - (root.context as BaseActivity).supportFragmentManager, - VideoOptionsBottomSheet::class.java.name - ) + val sheet = VideoOptionsBottomSheet() + sheet.arguments = bundleOf(IntentData.streamItem to streamItem) + sheet.show(fragmentManager, VideoOptionsBottomSheet::class.java.name) true } diff --git a/app/src/main/java/com/github/libretube/ui/adapters/PlaylistsAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/PlaylistsAdapter.kt index 4e6151f42..c6d48d264 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/PlaylistsAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/PlaylistsAdapter.kt @@ -53,34 +53,34 @@ class PlaylistsAdapter( } val fragmentManager = (root.context as BaseActivity).supportFragmentManager - fragmentManager.setFragmentResultListener( - PLAYLISTS_ADAPTER_REQUEST_KEY, - (root.context as BaseActivity) - ) { _, resultBundle -> - val newPlaylistDescription = - resultBundle.getString(IntentData.playlistDescription) - val newPlaylistName = - resultBundle.getString(IntentData.playlistName) - val isPlaylistToBeDeleted = - resultBundle.getBoolean(IntentData.playlistTask) - - newPlaylistDescription?.let { - playlistDescription.text = it - playlist.shortDescription = it - } - - newPlaylistName?.let { - playlistTitle.text = it - playlist.name = it - } - - if (isPlaylistToBeDeleted) { - // try to refresh the playlists in the library on deletion success - onDelete(position, root.context as BaseActivity) - } - } - root.setOnLongClickListener { + fragmentManager.setFragmentResultListener( + PLAYLISTS_ADAPTER_REQUEST_KEY, + (root.context as BaseActivity) + ) { _, resultBundle -> + val newPlaylistDescription = + resultBundle.getString(IntentData.playlistDescription) + val newPlaylistName = + resultBundle.getString(IntentData.playlistName) + val isPlaylistToBeDeleted = + resultBundle.getBoolean(IntentData.playlistTask) + + newPlaylistDescription?.let { + playlistDescription.text = it + playlist.shortDescription = it + } + + newPlaylistName?.let { + playlistTitle.text = it + playlist.name = it + } + + if (isPlaylistToBeDeleted) { + // try to refresh the playlists in the library on deletion success + onDelete(position, root.context as BaseActivity) + } + } + val playlistOptionsDialog = PlaylistOptionsBottomSheet( playlistId = playlist.id!!, playlistName = playlist.name!!, diff --git a/app/src/main/java/com/github/libretube/ui/adapters/SearchAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/SearchAdapter.kt index 723654ec6..b045f7a92 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/SearchAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/SearchAdapter.kt @@ -2,11 +2,13 @@ package com.github.libretube.ui.adapters import android.view.LayoutInflater import android.view.ViewGroup +import androidx.core.os.bundleOf import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListAdapter import com.github.libretube.R import com.github.libretube.api.obj.ContentItem import com.github.libretube.api.obj.StreamItem +import com.github.libretube.constants.IntentData import com.github.libretube.databinding.ChannelRowBinding import com.github.libretube.databinding.PlaylistsRowBinding import com.github.libretube.databinding.VideoRowBinding @@ -96,14 +98,16 @@ class SearchAdapter( NavigationHelper.navigateVideo(root.context, item.url) } val videoId = item.url.toID() + + val activity = (root.context as BaseActivity) + val fragmentManager = activity.supportFragmentManager root.setOnLongClickListener { - VideoOptionsBottomSheet(item.toStreamItem()) { + fragmentManager.setFragmentResultListener(VideoOptionsBottomSheet.VIDEO_OPTIONS_SHEET_REQUEST_KEY, activity) { _, _ -> notifyItemChanged(position) } - .show( - (root.context as BaseActivity).supportFragmentManager, - VideoOptionsBottomSheet::class.java.name - ) + val sheet = VideoOptionsBottomSheet() + sheet.arguments = bundleOf(IntentData.streamItem to item) + sheet.show(fragmentManager, SearchAdapter::class.java.name) true } channelContainer.setOnClickListener { @@ -134,8 +138,12 @@ class SearchAdapter( } root.setOnLongClickListener { - ChannelOptionsBottomSheet(item.url.toID(), item.name) - .show((root.context as BaseActivity).supportFragmentManager) + val channelOptionsSheet = ChannelOptionsBottomSheet() + channelOptionsSheet.arguments = bundleOf( + IntentData.channelId to item.url.toID(), + IntentData.channelName to item.name + ) + channelOptionsSheet.show((root.context as BaseActivity).supportFragmentManager) true } diff --git a/app/src/main/java/com/github/libretube/ui/adapters/SubscriptionChannelAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/SubscriptionChannelAdapter.kt index 546b99e3e..395ce6f57 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/SubscriptionChannelAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/SubscriptionChannelAdapter.kt @@ -2,8 +2,10 @@ package com.github.libretube.ui.adapters import android.view.LayoutInflater import android.view.ViewGroup +import androidx.core.os.bundleOf import androidx.recyclerview.widget.RecyclerView import com.github.libretube.api.obj.Subscription +import com.github.libretube.constants.IntentData import com.github.libretube.databinding.ChannelSubscriptionRowBinding import com.github.libretube.extensions.toID import com.github.libretube.helpers.ImageHelper @@ -47,8 +49,12 @@ class SubscriptionChannelAdapter( NavigationHelper.navigateChannel(root.context, subscription.url) } root.setOnLongClickListener { - ChannelOptionsBottomSheet(subscription.url.toID(), subscription.name) - .show((root.context as BaseActivity).supportFragmentManager) + val channelOptionsSheet = ChannelOptionsBottomSheet() + channelOptionsSheet.arguments = bundleOf( + IntentData.channelId to subscription.url.toID(), + IntentData.channelName to subscription.name + ) + channelOptionsSheet.show((root.context as BaseActivity).supportFragmentManager) true } diff --git a/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt index 4a0869596..bea968b22 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/VideosAdapter.kt @@ -5,6 +5,7 @@ import android.content.Context import android.text.format.DateUtils import android.view.LayoutInflater import android.view.ViewGroup +import androidx.core.os.bundleOf import androidx.core.view.isGone import androidx.core.view.updateLayoutParams import androidx.recyclerview.widget.GridLayoutManager @@ -13,6 +14,7 @@ import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView.LayoutManager import com.github.libretube.R import com.github.libretube.api.obj.StreamItem +import com.github.libretube.constants.IntentData import com.github.libretube.constants.PreferenceKeys import com.github.libretube.databinding.AllCaughtUpRowBinding import com.github.libretube.databinding.TrendingRowBinding @@ -103,9 +105,7 @@ class VideosAdapter( @SuppressLint("SetTextI18n") override fun onBindViewHolder(holder: VideosViewHolder, position: Int) { val video = streamItems[position] - val videoId = video.url?.toID() - val videoName = video.title videoId?.let { (holder.trendingRowBinding?.watchProgress ?: holder.videoRowBinding!!.watchProgress) @@ -116,6 +116,9 @@ class VideosAdapter( holder.videoRowBinding ?: holder.trendingRowBinding ?: holder.allCaughtUpBinding )!!.root.context + val activity = (context as BaseActivity) + val fragmentManager = activity.supportFragmentManager + val uploadDate = video.uploaded?.takeIf { it > 0 }?.let { TextUtils.formatRelativeDate(context, it) } @@ -146,17 +149,14 @@ class VideosAdapter( root.setOnClickListener { NavigationHelper.navigateVideo(root.context, video.url) } - root.setOnLongClickListener { - if (videoId == null || videoName == null) return@setOnLongClickListener true - VideoOptionsBottomSheet(video) { + root.setOnLongClickListener { + fragmentManager.setFragmentResultListener(VideoOptionsBottomSheet.VIDEO_OPTIONS_SHEET_REQUEST_KEY, activity) { _, _ -> notifyItemChanged(position) } - .show( - (root.context as BaseActivity).supportFragmentManager, - VideoOptionsBottomSheet::class.java.name - ) - + val sheet = VideoOptionsBottomSheet() + sheet.arguments = bundleOf(IntentData.streamItem to video) + sheet.show(fragmentManager, VideosAdapter::class.java.name) true } } @@ -191,14 +191,12 @@ class VideosAdapter( } root.setOnLongClickListener { - if (videoId == null || videoName == null) return@setOnLongClickListener true - VideoOptionsBottomSheet(video) { + fragmentManager.setFragmentResultListener(VideoOptionsBottomSheet.VIDEO_OPTIONS_SHEET_REQUEST_KEY, activity) { _, _ -> notifyItemChanged(position) } - .show( - (root.context as BaseActivity).supportFragmentManager, - VideoOptionsBottomSheet::class.java.name - ) + val sheet = VideoOptionsBottomSheet() + sheet.arguments = bundleOf(IntentData.streamItem to video) + sheet.show(fragmentManager, VideosAdapter::class.java.name) true } } diff --git a/app/src/main/java/com/github/libretube/ui/adapters/WatchHistoryAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/WatchHistoryAdapter.kt index 2592af4b0..0133a2a12 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/WatchHistoryAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/WatchHistoryAdapter.kt @@ -2,7 +2,9 @@ package com.github.libretube.ui.adapters import android.view.LayoutInflater import android.view.ViewGroup +import androidx.core.os.bundleOf import androidx.recyclerview.widget.RecyclerView +import com.github.libretube.constants.IntentData import com.github.libretube.databinding.VideoRowBinding import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.obj.WatchHistoryItem @@ -67,14 +69,16 @@ class WatchHistoryAdapter( root.setOnClickListener { NavigationHelper.navigateVideo(root.context, video.videoId) } + + val activity = (root.context as BaseActivity) + val fragmentManager = activity.supportFragmentManager root.setOnLongClickListener { - VideoOptionsBottomSheet(video.toStreamItem()) { + fragmentManager.setFragmentResultListener(VideoOptionsBottomSheet.VIDEO_OPTIONS_SHEET_REQUEST_KEY, activity) { _, _ -> notifyItemChanged(position) } - .show( - (root.context as BaseActivity).supportFragmentManager, - VideoOptionsBottomSheet::class.java.name - ) + val sheet = VideoOptionsBottomSheet() + sheet.arguments = bundleOf(IntentData.streamItem to video) + sheet.show(fragmentManager, WatchHistoryAdapter::class.java.name) true } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt index 7670743b6..c6c8699bd 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/AudioPlayerFragment.kt @@ -397,9 +397,8 @@ class AudioPlayerFragment : Fragment(), AudioPlayerOptions { override fun onLongTap() { val current = PlayingQueue.getCurrent() ?: return - VideoOptionsBottomSheet( - current - ) + VideoOptionsBottomSheet() + .apply { arguments = bundleOf(IntentData.shareData to current) } .show(childFragmentManager) } diff --git a/app/src/main/java/com/github/libretube/ui/sheets/ChannelOptionsBottomSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/ChannelOptionsBottomSheet.kt index 0355db4ae..0d25700c9 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/ChannelOptionsBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/ChannelOptionsBottomSheet.kt @@ -21,14 +21,16 @@ import kotlinx.coroutines.withContext * * Needs the [channelId] to load the content from the right video. */ -class ChannelOptionsBottomSheet( - private val channelId: String, - channelName: String? -) : BaseBottomSheet() { - private val shareData = ShareData(currentChannel = channelName) +class ChannelOptionsBottomSheet : BaseBottomSheet() { + private lateinit var channelId: String + private var channelName: String? = null + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + channelId = arguments?.getString(IntentData.channelId)!! + channelName = arguments?.getString(IntentData.channelName) + // List that stores the different menu options. In the future could be add more options here. val optionsList = mutableListOf( getString(R.string.share), @@ -42,7 +44,7 @@ class ChannelOptionsBottomSheet( val bundle = bundleOf( IntentData.id to channelId, IntentData.shareObjectType to ShareObjectType.CHANNEL, - IntentData.shareData to shareData + IntentData.shareData to ShareData(currentChannel = channelName) ) val newShareDialog = ShareDialog() newShareDialog.arguments = bundle diff --git a/app/src/main/java/com/github/libretube/ui/sheets/StatsSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/StatsSheet.kt index 7a7ff14a4..665cd0c25 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/StatsSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/StatsSheet.kt @@ -40,9 +40,7 @@ class StatsSheet( ) binding.audioInfo.setText( "${player.audioFormat?.codecs.orEmpty()} ${ - TextUtils.formatBitrate( - player.audioFormat?.bitrate - ) + TextUtils.formatBitrate(player.audioFormat?.bitrate) }" ) binding.videoQuality.setText( diff --git a/app/src/main/java/com/github/libretube/ui/sheets/VideoOptionsBottomSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/VideoOptionsBottomSheet.kt index 6341c864a..b9144217d 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/VideoOptionsBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/VideoOptionsBottomSheet.kt @@ -2,6 +2,7 @@ package com.github.libretube.ui.sheets import android.os.Bundle import androidx.core.os.bundleOf +import androidx.fragment.app.setFragmentResult import androidx.navigation.fragment.NavHostFragment import com.github.libretube.R import com.github.libretube.api.obj.StreamItem @@ -11,6 +12,7 @@ import com.github.libretube.db.DatabaseHelper import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.obj.WatchPosition import com.github.libretube.enums.ShareObjectType +import com.github.libretube.extensions.parcelable import com.github.libretube.extensions.toID import com.github.libretube.helpers.BackgroundHelper import com.github.libretube.helpers.NavigationHelper @@ -32,12 +34,12 @@ import kotlinx.coroutines.withContext * * Needs the [streamItem] to load the content from the right video. */ -class VideoOptionsBottomSheet( - private val streamItem: StreamItem, - private val onVideoChanged: () -> Unit = {} -) : BaseBottomSheet() { - private val shareData = ShareData(currentVideo = streamItem.title) +class VideoOptionsBottomSheet : BaseBottomSheet() { + private lateinit var streamItem: StreamItem + override fun onCreate(savedInstanceState: Bundle?) { + streamItem = arguments?.parcelable(IntentData.streamItem)!! + val videoId = streamItem.url?.toID() ?: return // List that stores the different menu options. In the future could be add more options here. val optionsList = mutableListOf( @@ -64,7 +66,7 @@ class VideoOptionsBottomSheet( if (streamItem.duration == null || watchPositionEntry == null || - watchPositionEntry.position < streamItem.duration * 1000 * 0.9 + watchPositionEntry.position < streamItem.duration!! * 1000 * 0.9 ) { optionsList += getString(R.string.mark_as_watched) } @@ -101,7 +103,7 @@ class VideoOptionsBottomSheet( val bundle = bundleOf( IntentData.id to videoId, IntentData.shareObjectType to ShareObjectType.VIDEO, - IntentData.shareData to shareData + IntentData.shareData to ShareData(currentVideo = streamItem.title) ) val newShareDialog = ShareDialog() newShareDialog.arguments = bundle @@ -134,7 +136,7 @@ class VideoOptionsBottomSheet( ?.firstOrNull() as? SubscriptionsFragment fragment?.feedAdapter?.removeItemById(videoId) } - onVideoChanged() + setFragmentResult(VIDEO_OPTIONS_SHEET_REQUEST_KEY, bundleOf()) } getString(R.string.mark_as_unwatched) -> { @@ -142,11 +144,15 @@ class VideoOptionsBottomSheet( DatabaseHolder.Database.watchPositionDao().deleteByVideoId(videoId) DatabaseHolder.Database.watchHistoryDao().deleteByVideoId(videoId) } - onVideoChanged() + setFragmentResult(VIDEO_OPTIONS_SHEET_REQUEST_KEY, bundleOf()) } } } super.onCreate(savedInstanceState) } + + companion object { + const val VIDEO_OPTIONS_SHEET_REQUEST_KEY = "video_options_sheet_request_key" + } }