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
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.PlaylistBookmarkRowBinding
import com.github.libretube.databinding.PlaylistsRowBinding
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.helpers.ImageHelper
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.viewholders.PlaylistBookmarkViewHolder
import kotlinx.coroutines.CoroutineScope
@ -38,6 +42,18 @@ class PlaylistBookmarkAdapter(
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) {
val bookmark = bookmarks[position]
holder.playlistBookmarkBinding?.apply {
@ -54,13 +70,7 @@ class PlaylistBookmarkAdapter(
}
root.setOnLongClickListener {
PlaylistOptionsBottomSheet(
playlistId = bookmark.playlistId,
playlistName = bookmark.playlistName.orEmpty(),
playlistType = PlaylistType.PUBLIC
).show(
(root.context as AppCompatActivity).supportFragmentManager
)
showPlaylistOptions(root.context, bookmark)
true
}
}
@ -98,13 +108,7 @@ class PlaylistBookmarkAdapter(
}
root.setOnLongClickListener {
PlaylistOptionsBottomSheet(
playlistId = bookmark.playlistId,
playlistName = bookmark.playlistName.orEmpty(),
playlistType = PlaylistType.PUBLIC
).show(
(root.context as AppCompatActivity).supportFragmentManager
)
showPlaylistOptions(root.context, bookmark)
true
}
}

View File

@ -2,6 +2,7 @@ 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.R
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.ui.base.BaseActivity
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
class PlaylistsAdapter(
@ -56,7 +58,7 @@ class PlaylistsAdapter(
val fragmentManager = (root.context as BaseActivity).supportFragmentManager
root.setOnLongClickListener {
fragmentManager.setFragmentResultListener(
PLAYLISTS_ADAPTER_REQUEST_KEY,
PLAYLIST_OPTIONS_REQUEST_KEY,
(root.context as BaseActivity)
) { _, resultBundle ->
val newPlaylistDescription =
@ -82,10 +84,11 @@ class PlaylistsAdapter(
}
}
val playlistOptionsDialog = PlaylistOptionsBottomSheet(
playlistId = playlist.id!!,
playlistName = playlist.name!!,
playlistType = playlistType
val playlistOptionsDialog = PlaylistOptionsBottomSheet()
playlistOptionsDialog.arguments = bundleOf(
IntentData.playlistId to playlist.id!!,
IntentData.playlistName to playlist.name!!,
IntentData.playlistType to playlistType
)
playlistOptionsDialog.show(
fragmentManager,
@ -103,8 +106,4 @@ class PlaylistsAdapter(
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 {
val playlistId = item.url.toID()
val playlistName = item.name!!
PlaylistOptionsBottomSheet(playlistId, playlistName, PlaylistType.PUBLIC)
.show(
val sheet = PlaylistOptionsBottomSheet()
sheet.arguments = bundleOf(
IntentData.playlistId to playlistId,
IntentData.playlistName to playlistName,
IntentData.playlistType to PlaylistType.PUBLIC
)
sheet.show(
(root.context as BaseActivity).supportFragmentManager,
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.ui.activities.MainActivity
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.sheets.BaseBottomSheet
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet
@ -171,24 +172,42 @@ class PlaylistFragment : Fragment() {
// show playlist options
binding.optionsMenu.setOnClickListener {
PlaylistOptionsBottomSheet(
playlistId = playlistId.orEmpty(),
playlistName = playlistName.orEmpty(),
playlistType = playlistType,
onDelete = {
findNavController().popBackStack()
},
onRename = {
val sheet = PlaylistOptionsBottomSheet()
sheet.arguments = bundleOf(
IntentData.playlistId to playlistId.orEmpty(),
IntentData.playlistName to playlistName.orEmpty(),
IntentData.playlistType to playlistType
)
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
playlistName = it
},
onChangeDescription = {
binding.playlistDescription.text = it
}
).show(
childFragmentManager,
PlaylistOptionsBottomSheet::class.java.name
)
if (isPlaylistToBeDeleted) {
// TODO move back: navController().popBackStack() crashes
return@setFragmentResultListener
}
}
sheet.show(fragmentManager)
}
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.enums.PlaylistType
import com.github.libretube.enums.ShareObjectType
import com.github.libretube.extensions.serializable
import com.github.libretube.extensions.toID
import com.github.libretube.extensions.toastFromMainDispatcher
import com.github.libretube.helpers.BackgroundHelper
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.dialogs.DeletePlaylistDialog
import com.github.libretube.ui.dialogs.PlaylistDescriptionDialog
@ -24,16 +24,18 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
class PlaylistOptionsBottomSheet(
private val playlistId: String,
private val playlistName: String,
private val 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)
class PlaylistOptionsBottomSheet : BaseBottomSheet() {
private lateinit var playlistName: String
private lateinit var playlistId: String
private lateinit var playlistType: PlaylistType
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
val optionsList = mutableListOf(
getString(R.string.playOnBackground)
@ -98,21 +100,13 @@ class PlaylistOptionsBottomSheet(
newShareDialog.arguments = bundleOf(
IntentData.id to playlistId,
IntentData.shareObjectType to ShareObjectType.PLAYLIST,
IntentData.shareData to shareData
IntentData.shareData to ShareData(currentPlaylist = playlistName)
)
// using parentFragmentManager, childFragmentManager doesn't work here
newShareDialog.show(parentFragmentManager, ShareDialog::class.java.name)
}
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()
newDeletePlaylistDialog.arguments = bundleOf(
IntentData.playlistId to playlistId,
@ -122,14 +116,6 @@ class PlaylistOptionsBottomSheet(
}
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()
newRenamePlaylistDialog.arguments = bundleOf(
IntentData.playlistId to playlistId,
@ -139,14 +125,6 @@ class PlaylistOptionsBottomSheet(
}
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()
newPlaylistDescriptionDialog.arguments = bundleOf(
IntentData.playlistId to playlistId,