refactor: remove constructors from PlaylistOptionsBottomSheet

This commit is contained in:
Bnyro 2023-09-13 16:37:16 +02:00
parent efa1c44b8d
commit 2bf504c030
5 changed files with 80 additions and 75 deletions

View File

@ -1,11 +1,14 @@
package com.github.libretube.ui.adapters package com.github.libretube.ui.adapters
import android.content.Context
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.PlaylistBookmarkRowBinding import com.github.libretube.databinding.PlaylistBookmarkRowBinding
import com.github.libretube.databinding.PlaylistsRowBinding import com.github.libretube.databinding.PlaylistsRowBinding
import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.DatabaseHolder
@ -13,6 +16,7 @@ import com.github.libretube.db.obj.PlaylistBookmark
import com.github.libretube.enums.PlaylistType import com.github.libretube.enums.PlaylistType
import com.github.libretube.helpers.ImageHelper import com.github.libretube.helpers.ImageHelper
import com.github.libretube.helpers.NavigationHelper import com.github.libretube.helpers.NavigationHelper
import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet
import com.github.libretube.ui.viewholders.PlaylistBookmarkViewHolder import com.github.libretube.ui.viewholders.PlaylistBookmarkViewHolder
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
@ -38,6 +42,18 @@ class PlaylistBookmarkAdapter(
override fun getItemCount() = bookmarks.size override fun getItemCount() = bookmarks.size
private fun showPlaylistOptions(context: Context, bookmark: PlaylistBookmark) {
val sheet = PlaylistOptionsBottomSheet()
sheet.arguments = bundleOf(
IntentData.playlistId to bookmark.playlistId,
IntentData.playlistName to bookmark.playlistName,
IntentData.playlistType to PlaylistType.PUBLIC
)
sheet.show(
(context as BaseActivity).supportFragmentManager
)
}
override fun onBindViewHolder(holder: PlaylistBookmarkViewHolder, position: Int) { override fun onBindViewHolder(holder: PlaylistBookmarkViewHolder, position: Int) {
val bookmark = bookmarks[position] val bookmark = bookmarks[position]
holder.playlistBookmarkBinding?.apply { holder.playlistBookmarkBinding?.apply {
@ -54,13 +70,7 @@ class PlaylistBookmarkAdapter(
} }
root.setOnLongClickListener { root.setOnLongClickListener {
PlaylistOptionsBottomSheet( showPlaylistOptions(root.context, bookmark)
playlistId = bookmark.playlistId,
playlistName = bookmark.playlistName.orEmpty(),
playlistType = PlaylistType.PUBLIC
).show(
(root.context as AppCompatActivity).supportFragmentManager
)
true true
} }
} }
@ -98,13 +108,7 @@ class PlaylistBookmarkAdapter(
} }
root.setOnLongClickListener { root.setOnLongClickListener {
PlaylistOptionsBottomSheet( showPlaylistOptions(root.context, bookmark)
playlistId = bookmark.playlistId,
playlistName = bookmark.playlistName.orEmpty(),
playlistType = PlaylistType.PUBLIC
).show(
(root.context as AppCompatActivity).supportFragmentManager
)
true true
} }
} }

View File

@ -2,6 +2,7 @@ package com.github.libretube.ui.adapters
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.obj.Playlists import com.github.libretube.api.obj.Playlists
@ -12,6 +13,7 @@ import com.github.libretube.helpers.ImageHelper
import com.github.libretube.helpers.NavigationHelper import com.github.libretube.helpers.NavigationHelper
import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet.Companion.PLAYLIST_OPTIONS_REQUEST_KEY
import com.github.libretube.ui.viewholders.PlaylistsViewHolder import com.github.libretube.ui.viewholders.PlaylistsViewHolder
class PlaylistsAdapter( class PlaylistsAdapter(
@ -56,7 +58,7 @@ class PlaylistsAdapter(
val fragmentManager = (root.context as BaseActivity).supportFragmentManager val fragmentManager = (root.context as BaseActivity).supportFragmentManager
root.setOnLongClickListener { root.setOnLongClickListener {
fragmentManager.setFragmentResultListener( fragmentManager.setFragmentResultListener(
PLAYLISTS_ADAPTER_REQUEST_KEY, PLAYLIST_OPTIONS_REQUEST_KEY,
(root.context as BaseActivity) (root.context as BaseActivity)
) { _, resultBundle -> ) { _, resultBundle ->
val newPlaylistDescription = val newPlaylistDescription =
@ -82,10 +84,11 @@ class PlaylistsAdapter(
} }
} }
val playlistOptionsDialog = PlaylistOptionsBottomSheet( val playlistOptionsDialog = PlaylistOptionsBottomSheet()
playlistId = playlist.id!!, playlistOptionsDialog.arguments = bundleOf(
playlistName = playlist.name!!, IntentData.playlistId to playlist.id!!,
playlistType = playlistType IntentData.playlistName to playlist.name!!,
IntentData.playlistType to playlistType
) )
playlistOptionsDialog.show( playlistOptionsDialog.show(
fragmentManager, fragmentManager,
@ -103,8 +106,4 @@ class PlaylistsAdapter(
notifyItemRangeChanged(position, itemCount) notifyItemRangeChanged(position, itemCount)
} }
} }
companion object {
const val PLAYLISTS_ADAPTER_REQUEST_KEY = "playlists_adapter_request_key"
}
} }

View File

@ -171,8 +171,13 @@ class SearchAdapter(
root.setOnLongClickListener { root.setOnLongClickListener {
val playlistId = item.url.toID() val playlistId = item.url.toID()
val playlistName = item.name!! val playlistName = item.name!!
PlaylistOptionsBottomSheet(playlistId, playlistName, PlaylistType.PUBLIC) val sheet = PlaylistOptionsBottomSheet()
.show( sheet.arguments = bundleOf(
IntentData.playlistId to playlistId,
IntentData.playlistName to playlistName,
IntentData.playlistType to PlaylistType.PUBLIC
)
sheet.show(
(root.context as BaseActivity).supportFragmentManager, (root.context as BaseActivity).supportFragmentManager,
PlaylistOptionsBottomSheet::class.java.name PlaylistOptionsBottomSheet::class.java.name
) )

View File

@ -38,6 +38,7 @@ import com.github.libretube.helpers.NavigationHelper
import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.activities.MainActivity import com.github.libretube.ui.activities.MainActivity
import com.github.libretube.ui.adapters.PlaylistAdapter import com.github.libretube.ui.adapters.PlaylistAdapter
import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.models.PlayerViewModel import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.ui.sheets.BaseBottomSheet import com.github.libretube.ui.sheets.BaseBottomSheet
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet
@ -171,24 +172,42 @@ class PlaylistFragment : Fragment() {
// show playlist options // show playlist options
binding.optionsMenu.setOnClickListener { binding.optionsMenu.setOnClickListener {
PlaylistOptionsBottomSheet( val sheet = PlaylistOptionsBottomSheet()
playlistId = playlistId.orEmpty(), sheet.arguments = bundleOf(
playlistName = playlistName.orEmpty(), IntentData.playlistId to playlistId.orEmpty(),
playlistType = playlistType, IntentData.playlistName to playlistName.orEmpty(),
onDelete = { IntentData.playlistType to playlistType
findNavController().popBackStack() )
},
onRename = { val fragmentManager = (context as BaseActivity).supportFragmentManager
fragmentManager.setFragmentResultListener(
PlaylistOptionsBottomSheet.PLAYLIST_OPTIONS_REQUEST_KEY,
(context as BaseActivity)
) { _, resultBundle ->
val newPlaylistDescription =
resultBundle.getString(IntentData.playlistDescription)
val newPlaylistName =
resultBundle.getString(IntentData.playlistName)
val isPlaylistToBeDeleted =
resultBundle.getBoolean(IntentData.playlistTask)
newPlaylistDescription?.let {
binding.playlistDescription.text = it
response.description = it
}
newPlaylistName?.let {
binding.playlistName.text = it binding.playlistName.text = it
playlistName = it playlistName = it
},
onChangeDescription = {
binding.playlistDescription.text = it
} }
).show(
childFragmentManager, if (isPlaylistToBeDeleted) {
PlaylistOptionsBottomSheet::class.java.name // TODO move back: navController().popBackStack() crashes
) return@setFragmentResultListener
}
}
sheet.show(fragmentManager)
} }
binding.playAll.setOnClickListener { binding.playAll.setOnClickListener {

View File

@ -9,11 +9,11 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.db.DatabaseHolder import com.github.libretube.db.DatabaseHolder
import com.github.libretube.enums.PlaylistType import com.github.libretube.enums.PlaylistType
import com.github.libretube.enums.ShareObjectType import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.serializable
import com.github.libretube.extensions.toID import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toastFromMainDispatcher import com.github.libretube.extensions.toastFromMainDispatcher
import com.github.libretube.helpers.BackgroundHelper import com.github.libretube.helpers.BackgroundHelper
import com.github.libretube.obj.ShareData import com.github.libretube.obj.ShareData
import com.github.libretube.ui.adapters.PlaylistsAdapter.Companion.PLAYLISTS_ADAPTER_REQUEST_KEY
import com.github.libretube.ui.base.BaseActivity import com.github.libretube.ui.base.BaseActivity
import com.github.libretube.ui.dialogs.DeletePlaylistDialog import com.github.libretube.ui.dialogs.DeletePlaylistDialog
import com.github.libretube.ui.dialogs.PlaylistDescriptionDialog import com.github.libretube.ui.dialogs.PlaylistDescriptionDialog
@ -24,16 +24,18 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
class PlaylistOptionsBottomSheet( class PlaylistOptionsBottomSheet : BaseBottomSheet() {
private val playlistId: String, private lateinit var playlistName: String
private val playlistName: String, private lateinit var playlistId: String
private val playlistType: PlaylistType, private lateinit var playlistType: PlaylistType
private val onRename: (newName: String) -> Unit = {},
private val onChangeDescription: (newDescription: String) -> Unit = {},
private val onDelete: () -> Unit = {}
) : BaseBottomSheet() {
private val shareData = ShareData(currentPlaylist = playlistName)
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
arguments?.let {
playlistName = it.getString(IntentData.playlistName)!!
playlistId = it.getString(IntentData.playlistId)!!
playlistType = it.serializable(IntentData.playlistType)!!
}
// options for the dialog // options for the dialog
val optionsList = mutableListOf( val optionsList = mutableListOf(
getString(R.string.playOnBackground) getString(R.string.playOnBackground)
@ -98,21 +100,13 @@ class PlaylistOptionsBottomSheet(
newShareDialog.arguments = bundleOf( newShareDialog.arguments = bundleOf(
IntentData.id to playlistId, IntentData.id to playlistId,
IntentData.shareObjectType to ShareObjectType.PLAYLIST, IntentData.shareObjectType to ShareObjectType.PLAYLIST,
IntentData.shareData to shareData IntentData.shareData to ShareData(currentPlaylist = playlistName)
) )
// using parentFragmentManager, childFragmentManager doesn't work here // using parentFragmentManager, childFragmentManager doesn't work here
newShareDialog.show(parentFragmentManager, ShareDialog::class.java.name) newShareDialog.show(parentFragmentManager, ShareDialog::class.java.name)
} }
getString(R.string.deletePlaylist) -> { getString(R.string.deletePlaylist) -> {
mFragmentManager.setFragmentResultListener(
PLAYLIST_OPTIONS_REQUEST_KEY,
context as BaseActivity
) { _, bundle ->
if (bundle.getBoolean(IntentData.playlistTask, true)) onDelete()
// forward the result the playlists adapter if visible
mFragmentManager.setFragmentResult(PLAYLISTS_ADAPTER_REQUEST_KEY, bundle)
}
val newDeletePlaylistDialog = DeletePlaylistDialog() val newDeletePlaylistDialog = DeletePlaylistDialog()
newDeletePlaylistDialog.arguments = bundleOf( newDeletePlaylistDialog.arguments = bundleOf(
IntentData.playlistId to playlistId, IntentData.playlistId to playlistId,
@ -122,14 +116,6 @@ class PlaylistOptionsBottomSheet(
} }
getString(R.string.renamePlaylist) -> { getString(R.string.renamePlaylist) -> {
mFragmentManager.setFragmentResultListener(
PLAYLIST_OPTIONS_REQUEST_KEY,
context as BaseActivity
) { _, bundle ->
onRename(bundle.getString(IntentData.playlistName, ""))
// forward the result the playlists adapter if visible
mFragmentManager.setFragmentResult(PLAYLISTS_ADAPTER_REQUEST_KEY, bundle)
}
val newRenamePlaylistDialog = RenamePlaylistDialog() val newRenamePlaylistDialog = RenamePlaylistDialog()
newRenamePlaylistDialog.arguments = bundleOf( newRenamePlaylistDialog.arguments = bundleOf(
IntentData.playlistId to playlistId, IntentData.playlistId to playlistId,
@ -139,14 +125,6 @@ class PlaylistOptionsBottomSheet(
} }
getString(R.string.change_playlist_description) -> { getString(R.string.change_playlist_description) -> {
mFragmentManager.setFragmentResultListener(
PLAYLIST_OPTIONS_REQUEST_KEY,
context as BaseActivity
) { _, bundle ->
onChangeDescription(bundle.getString(IntentData.playlistName, ""))
// forward the result the playlists adapter if visible
mFragmentManager.setFragmentResult(PLAYLISTS_ADAPTER_REQUEST_KEY, bundle)
}
val newPlaylistDescriptionDialog = PlaylistDescriptionDialog() val newPlaylistDescriptionDialog = PlaylistDescriptionDialog()
newPlaylistDescriptionDialog.arguments = bundleOf( newPlaylistDescriptionDialog.arguments = bundleOf(
IntentData.playlistId to playlistId, IntentData.playlistId to playlistId,