From 061eecfedcb060391ae360cf4b9fe0f70d4ab6d0 Mon Sep 17 00:00:00 2001 From: Ansh Tyagi Date: Tue, 5 Sep 2023 18:11:42 +0530 Subject: [PATCH] refactor: use Bundle for DialogFragment arguments - ColorPickerDialog - DeletePlaylistDialog - RenamePlaylistDialog --- .../github/libretube/constants/IntentData.kt | 2 + .../libretube/ui/adapters/PlaylistsAdapter.kt | 44 +++++++++++++------ .../libretube/ui/dialogs/ColorPickerDialog.kt | 28 +++++++----- .../ui/dialogs/DeletePlaylistDialog.kt | 26 +++++++---- .../ui/dialogs/RenamePlaylistDialog.kt | 24 +++++++--- .../ui/sheets/PlaylistOptionsBottomSheet.kt | 34 +++++++------- .../libretube/ui/views/ColorPreference.kt | 19 +++++--- 7 files changed, 116 insertions(+), 61 deletions(-) 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 f0a893745e..baa171603b 100644 --- a/app/src/main/java/com/github/libretube/constants/IntentData.kt +++ b/app/src/main/java/com/github/libretube/constants/IntentData.kt @@ -17,6 +17,7 @@ object IntentData { const val minimizeByDefault = "minimizeByDefault" const val query = "query" const val playlistDescription = "playlistDescription" + const val playlistName = "playlistName" const val shareObjectType = "shareObjectType" const val shareData = "shareData" const val currentPosition = "currentPosition" @@ -27,4 +28,5 @@ object IntentData { const val playlistTask = "playlistTask" const val loginTask = "loginTask" const val logoutTask = "logoutTask" + const val color = "color" } 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 fb583a2e8a..916feae518 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 @@ -5,6 +5,7 @@ import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import com.github.libretube.R import com.github.libretube.api.obj.Playlists +import com.github.libretube.constants.IntentData import com.github.libretube.databinding.PlaylistsRowBinding import com.github.libretube.enums.PlaylistType import com.github.libretube.helpers.ImageHelper @@ -51,25 +52,42 @@ class PlaylistsAdapter( NavigationHelper.navigatePlaylist(root.context, playlist.id, playlistType) } + val fragmentManager = (root.context as BaseActivity).supportFragmentManager + fragmentManager.setFragmentResultListener( + IntentData.requestKey, + (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 { val playlistOptionsDialog = PlaylistOptionsBottomSheet( playlistId = playlist.id!!, playlistName = playlist.name!!, - playlistType = playlistType, - onDelete = { - onDelete(position, root.context as BaseActivity) - }, - onRename = { - playlistTitle.text = it - playlist.name = it - }, - onChangeDescription = { - playlistDescription.text = it - playlist.shortDescription = it - } + playlistType = playlistType ) playlistOptionsDialog.show( - (root.context as BaseActivity).supportFragmentManager, + fragmentManager, PlaylistOptionsBottomSheet::class.java.name ) true diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/ColorPickerDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/ColorPickerDialog.kt index 6409af5983..f235004b77 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/ColorPickerDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/ColorPickerDialog.kt @@ -1,32 +1,36 @@ package com.github.libretube.ui.dialogs import android.app.Dialog -import android.content.Context import android.graphics.Color import android.os.Bundle import android.text.Editable import android.text.TextWatcher import android.widget.SeekBar import android.widget.Toast +import androidx.core.os.bundleOf import androidx.fragment.app.DialogFragment +import androidx.fragment.app.setFragmentResult import com.github.libretube.R +import com.github.libretube.constants.IntentData import com.github.libretube.databinding.DialogColorPickerBinding import com.google.android.material.dialog.MaterialAlertDialogBuilder -class ColorPickerDialog( - private val context: Context, - private val initialColor: Int, - private val onColorSelectedListener: OnColorSelectedListener -) : DialogFragment(), SeekBar.OnSeekBarChangeListener { +class ColorPickerDialog : DialogFragment(), SeekBar.OnSeekBarChangeListener { + private var initialColor: Int? = null private var _binding: DialogColorPickerBinding? = null private val binding get() = _binding!! + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + initialColor = arguments?.getInt(IntentData.color)!! + } + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { _binding = DialogColorPickerBinding.inflate(layoutInflater) // Set initial color - setColor(initialColor) + setColor(initialColor!!) binding.alphaSeekBar.setOnSeekBarChangeListener(this) binding.redSeekBar.setOnSeekBarChangeListener(this) @@ -74,7 +78,11 @@ class ColorPickerDialog( return MaterialAlertDialogBuilder(requireContext()) .setView(binding.root) .setPositiveButton(R.string.okay) { _, _ -> - onColorSelectedListener.onColorSelected(getColor()) + val color = getColor() + setFragmentResult( + IntentData.requestKey, + bundleOf(IntentData.color to color) + ) } .setNegativeButton(R.string.cancel, null) .show() @@ -134,8 +142,4 @@ class ColorPickerDialog( private fun colorToString(color: Int): String { return String.format("#%08X", color) } - - fun interface OnColorSelectedListener { - fun onColorSelected(color: Int) - } } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/DeletePlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/DeletePlaylistDialog.kt index b2b8c180e9..b8700b4d45 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/DeletePlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/DeletePlaylistDialog.kt @@ -2,10 +2,14 @@ package com.github.libretube.ui.dialogs import android.app.Dialog import android.os.Bundle +import androidx.core.os.bundleOf import androidx.fragment.app.DialogFragment +import androidx.fragment.app.setFragmentResult import com.github.libretube.R import com.github.libretube.api.PlaylistsHelper +import com.github.libretube.constants.IntentData import com.github.libretube.enums.PlaylistType +import com.github.libretube.extensions.serializable import com.github.libretube.extensions.toastFromMainDispatcher import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.CoroutineScope @@ -13,11 +17,16 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -class DeletePlaylistDialog( - private val playlistId: String, - private val playlistType: PlaylistType, - private val onSuccess: () -> Unit -) : DialogFragment() { +class DeletePlaylistDialog : DialogFragment() { + private lateinit var playlistId: String + private lateinit var playlistType: PlaylistType + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + arguments?.let { + playlistId = it.getString(IntentData.playlistId)!! + playlistType = it.serializable(IntentData.playlistType)!! + } + } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { return MaterialAlertDialogBuilder(requireContext()) .setTitle(R.string.deletePlaylist) @@ -30,9 +39,10 @@ class DeletePlaylistDialog( if (success) R.string.success else R.string.fail ) withContext(Dispatchers.Main) { - runCatching { - onSuccess.invoke() - } + setFragmentResult( + IntentData.requestKey, + bundleOf(IntentData.playlistTask to true) + ) } } } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt index a8783f3411..3e9fc3c153 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/RenamePlaylistDialog.kt @@ -6,10 +6,13 @@ import android.os.Bundle import android.text.InputType import android.util.Log import android.widget.Toast +import androidx.core.os.bundleOf import androidx.fragment.app.DialogFragment +import androidx.fragment.app.setFragmentResult import androidx.lifecycle.lifecycleScope import com.github.libretube.R import com.github.libretube.api.PlaylistsHelper +import com.github.libretube.constants.IntentData import com.github.libretube.databinding.DialogTextPreferenceBinding import com.github.libretube.extensions.TAG import com.github.libretube.extensions.toastFromMainDispatcher @@ -18,11 +21,17 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext -class RenamePlaylistDialog( - private val playlistId: String, - private val currentPlaylistName: String, - private val onSuccess: (String) -> Unit -) : DialogFragment() { +class RenamePlaylistDialog : DialogFragment() { + private lateinit var playlistId: String + private lateinit var currentPlaylistName: String + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + arguments?.let { + playlistId = it.getString(IntentData.playlistId)!! + currentPlaylistName = it.getString(IntentData.playlistName)!! + } + } override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val binding = DialogTextPreferenceBinding.inflate(layoutInflater) binding.input.inputType = InputType.TYPE_CLASS_TEXT @@ -62,7 +71,10 @@ class RenamePlaylistDialog( } if (success) { appContext.toastFromMainDispatcher(R.string.success) - onSuccess.invoke(newPlaylistName) + setFragmentResult( + IntentData.requestKey, + bundleOf(IntentData.playlistName to newPlaylistName) + ) } else { appContext.toastFromMainDispatcher(R.string.server_error) } diff --git a/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt b/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt index a885219140..3a285a1ae6 100644 --- a/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt +++ b/app/src/main/java/com/github/libretube/ui/sheets/PlaylistOptionsBottomSheet.kt @@ -102,15 +102,23 @@ class PlaylistOptionsBottomSheet( } getString(R.string.deletePlaylist) -> { - DeletePlaylistDialog(playlistId, playlistType) { - // try to refresh the playlists in the library on deletion success - onDelete() - }.show(parentFragmentManager, null) + val bundle = bundleOf( + IntentData.playlistId to playlistId, + IntentData.playlistType to playlistType + ) + val newDeletePlaylistDialog = DeletePlaylistDialog() + newDeletePlaylistDialog.arguments = bundle + newDeletePlaylistDialog.show(parentFragmentManager, null) } getString(R.string.renamePlaylist) -> { - RenamePlaylistDialog(playlistId, playlistName, onRename) - .show(parentFragmentManager, null) + val bundle = bundleOf( + IntentData.playlistId to playlistId, + IntentData.playlistName to playlistName + ) + val newRenamePlaylistDialog = RenamePlaylistDialog() + newRenamePlaylistDialog.arguments = bundle + newRenamePlaylistDialog.show(parentFragmentManager, null) } getString(R.string.change_playlist_description) -> { @@ -118,17 +126,9 @@ class PlaylistOptionsBottomSheet( IntentData.playlistId to playlistId, IntentData.playlistDescription to "" ) - val newShareDialog = PlaylistDescriptionDialog() - newShareDialog.arguments = bundle - newShareDialog.show(parentFragmentManager, null) - parentFragmentManager.setFragmentResultListener( - IntentData.requestKey, - this - ) { _, resultBundle -> - val newDescription = - resultBundle.getString(IntentData.playlistDescription)!! - onChangeDescription.invoke(newDescription) - } + val newPlaylistDescriptionDialog = PlaylistDescriptionDialog() + newPlaylistDescriptionDialog.arguments = bundle + newPlaylistDescriptionDialog.show(parentFragmentManager, null) } else -> { diff --git a/app/src/main/java/com/github/libretube/ui/views/ColorPreference.kt b/app/src/main/java/com/github/libretube/ui/views/ColorPreference.kt index 3c93b3dd0f..5be01e6064 100644 --- a/app/src/main/java/com/github/libretube/ui/views/ColorPreference.kt +++ b/app/src/main/java/com/github/libretube/ui/views/ColorPreference.kt @@ -7,9 +7,11 @@ import android.util.AttributeSet import android.view.View import android.widget.TextView import androidx.appcompat.app.AppCompatActivity +import androidx.core.os.bundleOf import androidx.preference.Preference import androidx.preference.PreferenceViewHolder import com.github.libretube.R +import com.github.libretube.constants.IntentData import com.github.libretube.ui.dialogs.ColorPickerDialog class ColorPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs) { @@ -60,11 +62,18 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex private fun showColorPickerDialog() { (if (currentColor is Int) currentColor else Color.BLACK)?.let { - val dialog = ColorPickerDialog(context, it) { color -> setColor(color) } - dialog.show( - (context as AppCompatActivity).supportFragmentManager, - this::class.java.name - ) + val bundle = bundleOf(IntentData.color to it) + val dialog = ColorPickerDialog() + val fragmentManager = (context as AppCompatActivity).supportFragmentManager + fragmentManager.setFragmentResultListener( + IntentData.requestKey, + context as AppCompatActivity + ) { _, resultBundle -> + val newColor = resultBundle.getInt(IntentData.color) + setColor(newColor) + } + dialog.arguments = bundle + dialog.show(fragmentManager, this::class.java.name) } }