Skip to content

Commit

Permalink
Merge pull request #4742 from Bnyro/master
Browse files Browse the repository at this point in the history
fix: issues with fragment result listener
  • Loading branch information
Bnyro authored Sep 10, 2023
2 parents 740d336 + 7eec444 commit 5e2e606
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ object IntentData {
const val currentPosition = "currentPosition"
const val duration = "duration"
const val updateInfo = "updateInfo"
const val requestKey = "requestKey"
const val backupFile = "backupFile"
const val playlistTask = "playlistTask"
const val loginTask = "loginTask"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class PlaylistsAdapter(

val fragmentManager = (root.context as BaseActivity).supportFragmentManager
fragmentManager.setFragmentResultListener(
IntentData.requestKey,
PLAYLISTS_ADAPTER_REQUEST_KEY,
(root.context as BaseActivity)
) { _, resultBundle ->
val newPlaylistDescription =
Expand Down Expand Up @@ -102,4 +102,8 @@ class PlaylistsAdapter(
notifyItemRangeChanged(position, itemCount)
}
}

companion object {
const val PLAYLISTS_ADAPTER_REQUEST_KEY = "playlists_adapter_request_key"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class AddToPlaylistDialog : DialogFragment() {
val binding = DialogAddToPlaylistBinding.inflate(layoutInflater)

childFragmentManager.setFragmentResultListener(
IntentData.requestKey,
ADD_TO_PLAYLIST_DIALOG_REQUEST_KEY,
this
) { _, resultBundle ->
val addedToPlaylist = resultBundle.getBoolean(IntentData.playlistTask)
Expand Down Expand Up @@ -128,4 +128,8 @@ class AddToPlaylistDialog : DialogFragment() {
appContext.toastFromMainDispatcher(R.string.fail)
}
}

companion object {
const val ADD_TO_PLAYLIST_DIALOG_REQUEST_KEY = "add_to_playlist_dialog_request_key"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,16 @@ class BackupDialog : DialogFragment() {
if (selected[index]) option.onSelected(backupFile)
}
val encodedBackupFile = Json.encodeToString(backupFile)
withContext(Dispatchers.Main) {
setFragmentResult(
IntentData.requestKey,
bundleOf(IntentData.backupFile to encodedBackupFile)
)
}
setFragmentResult(
BACKUP_DIALOG_REQUEST_KEY,
bundleOf(IntentData.backupFile to encodedBackupFile)
)
}
}
.create()
}

companion object {
const val BACKUP_DIALOG_REQUEST_KEY = "backup_dialog_request_key"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class ColorPickerDialog : DialogFragment(), SeekBar.OnSeekBarChangeListener {
.setPositiveButton(R.string.okay) { _, _ ->
val color = getColor()
setFragmentResult(
IntentData.requestKey,
COLOR_PICKER_REQUEST_KEY,
bundleOf(IntentData.color to color)
)
}
Expand Down Expand Up @@ -142,4 +142,8 @@ class ColorPickerDialog : DialogFragment(), SeekBar.OnSeekBarChangeListener {
private fun colorToString(color: Int): String {
return String.format("#%08X", color)
}

companion object {
const val COLOR_PICKER_REQUEST_KEY = "color_picker_request_key"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class CreatePlaylistDialog : DialogFragment() {
}
if (playlistId != null) {
setFragmentResult(
IntentData.requestKey,
CREATE_PLAYLIST_DIALOG_REQUEST_KEY,
bundleOf(IntentData.playlistTask to true)
)
}
Expand Down Expand Up @@ -72,7 +72,7 @@ class CreatePlaylistDialog : DialogFragment() {
)
playlistId?.let {
setFragmentResult(
IntentData.requestKey,
CREATE_PLAYLIST_DIALOG_REQUEST_KEY,
bundleOf(IntentData.playlistTask to true)
)
}
Expand All @@ -87,4 +87,8 @@ class CreatePlaylistDialog : DialogFragment() {
.setView(binding.root)
.show()
}

companion object {
const val CREATE_PLAYLIST_DIALOG_REQUEST_KEY = "create_playlist_dialog_request_key"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DialogDeleteAccountBinding
import com.github.libretube.extensions.TAG
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.preferences.InstanceSettings
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -61,7 +62,7 @@ class DeleteAccountDialog : DialogFragment() {
Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show()

setFragmentResult(
IntentData.requestKey,
InstanceSettings.INSTANCE_DIALOG_REQUEST_KEY,
bundleOf(IntentData.logoutTask to true)
)
dialog?.dismiss()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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.github.libretube.ui.sheets.PlaylistOptionsBottomSheet
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -38,12 +39,10 @@ class DeletePlaylistDialog : DialogFragment() {
appContext?.toastFromMainDispatcher(
if (success) R.string.success else R.string.fail
)
withContext(Dispatchers.Main) {
setFragmentResult(
IntentData.requestKey,
bundleOf(IntentData.playlistTask to true)
)
}
setFragmentResult(
PlaylistOptionsBottomSheet.PLAYLIST_OPTIONS_REQUEST_KEY,
bundleOf(IntentData.playlistTask to true)
)
}
}
.setNegativeButton(R.string.cancel, null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import com.github.libretube.databinding.DialogLoginBinding
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toastFromMainDispatcher
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.preferences.InstanceSettings.Companion.INSTANCE_DIALOG_REQUEST_KEY
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -100,7 +101,7 @@ class LoginDialog : DialogFragment() {

withContext(Dispatchers.Main) {
setFragmentResult(
IntentData.requestKey,
INSTANCE_DIALOG_REQUEST_KEY,
bundleOf(IntentData.loginTask to true)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.github.libretube.R
import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DialogLogoutBinding
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.preferences.InstanceSettings
import com.google.android.material.dialog.MaterialAlertDialogBuilder

class LogoutDialog : DialogFragment() {
Expand All @@ -25,7 +26,7 @@ class LogoutDialog : DialogFragment() {
Toast.makeText(context, R.string.loggedout, Toast.LENGTH_SHORT).show()

setFragmentResult(
IntentData.requestKey,
InstanceSettings.INSTANCE_DIALOG_REQUEST_KEY,
bundleOf(IntentData.logoutTask to true)
)
dialog?.dismiss()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DialogTextPreferenceBinding
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toastFromMainDispatcher
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet.Companion.PLAYLIST_OPTIONS_REQUEST_KEY
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -79,7 +80,7 @@ class PlaylistDescriptionDialog : DialogFragment() {
if (success) {
appContext.toastFromMainDispatcher(R.string.success)
setFragmentResult(
IntentData.requestKey,
PLAYLIST_OPTIONS_REQUEST_KEY,
bundleOf(IntentData.playlistDescription to newDescription)
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DialogTextPreferenceBinding
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toastFromMainDispatcher
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet.Companion.PLAYLIST_OPTIONS_REQUEST_KEY
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -72,7 +73,7 @@ class RenamePlaylistDialog : DialogFragment() {
if (success) {
appContext.toastFromMainDispatcher(R.string.success)
setFragmentResult(
IntentData.requestKey,
PLAYLIST_OPTIONS_REQUEST_KEY,
bundleOf(IntentData.playlistName to newPlaylistName)
)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.adapters.PlaylistBookmarkAdapter
import com.github.libretube.ui.adapters.PlaylistsAdapter
import com.github.libretube.ui.dialogs.CreatePlaylistDialog
import com.github.libretube.ui.dialogs.CreatePlaylistDialog.Companion.CREATE_PLAYLIST_DIALOG_REQUEST_KEY
import com.github.libretube.ui.models.PlayerViewModel
import com.github.libretube.ui.sheets.BaseBottomSheet
import kotlinx.coroutines.Dispatchers
Expand Down Expand Up @@ -95,7 +96,7 @@ class LibraryFragment : Fragment() {
}

childFragmentManager.setFragmentResultListener(
IntentData.requestKey,
CREATE_PLAYLIST_DIALOG_REQUEST_KEY,
this
) { _, resultBundle ->
val isPlaylistCreated = resultBundle.getBoolean(IntentData.playlistTask)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import com.github.libretube.helpers.ImportHelper
import com.github.libretube.obj.BackupFile
import com.github.libretube.ui.base.BasePreferenceFragment
import com.github.libretube.ui.dialogs.BackupDialog
import com.github.libretube.ui.dialogs.BackupDialog.Companion.BACKUP_DIALOG_REQUEST_KEY
import com.github.libretube.ui.dialogs.RequireRestartDialog
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.time.LocalDateTime
Expand Down Expand Up @@ -178,7 +179,7 @@ class BackupRestoreSettings : BasePreferenceFragment() {
}

childFragmentManager.setFragmentResultListener(
IntentData.requestKey,
BACKUP_DIALOG_REQUEST_KEY,
this
) { _, resultBundle ->
val encodedBackupFile = resultBundle.getString(IntentData.backupFile)!!
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class InstanceSettings : BasePreferenceFragment() {
deleteAccount?.isEnabled = token.isNotEmpty()

childFragmentManager.setFragmentResultListener(
IntentData.requestKey,
INSTANCE_DIALOG_REQUEST_KEY,
this
) { _, resultBundle ->
val isLoggedIn = resultBundle.getBoolean(IntentData.loginTask)
Expand Down Expand Up @@ -170,4 +170,8 @@ class InstanceSettings : BasePreferenceFragment() {
findPreference<Preference>(PreferenceKeys.LOGOUT)?.isVisible = false
findPreference<Preference>(PreferenceKeys.DELETE_ACCOUNT)?.isEnabled = false
}

companion object {
const val INSTANCE_DIALOG_REQUEST_KEY = "instance_dialog_request_key"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ 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
Expand Down Expand Up @@ -93,20 +94,21 @@ class PlaylistOptionsBottomSheet(
}
// share the playlist
getString(R.string.share) -> {
val bundle = bundleOf(
val newShareDialog = ShareDialog()
newShareDialog.arguments = bundleOf(
IntentData.id to playlistId,
IntentData.shareObjectType to ShareObjectType.PLAYLIST,
IntentData.shareData to shareData
)
val newShareDialog = ShareDialog()
newShareDialog.arguments = bundle
// using parentFragmentManager, childFragmentManager doesn't work here
newShareDialog.show(parentFragmentManager, ShareDialog::class.java.name)
}

getString(R.string.deletePlaylist) -> {
mFragmentManager.setFragmentResultListener(IntentData.requestKey, context as BaseActivity) { _, bundle ->
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(
Expand All @@ -117,8 +119,10 @@ class PlaylistOptionsBottomSheet(
}

getString(R.string.renamePlaylist) -> {
mFragmentManager.setFragmentResultListener(IntentData.requestKey, context as BaseActivity) { _, bundle ->
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(
Expand All @@ -129,8 +133,10 @@ class PlaylistOptionsBottomSheet(
}

getString(R.string.change_playlist_description) -> {
mFragmentManager.setFragmentResultListener(IntentData.requestKey, context as BaseActivity) { _, bundle ->
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(
Expand Down Expand Up @@ -158,4 +164,8 @@ class PlaylistOptionsBottomSheet(
}
super.onCreate(savedInstanceState)
}

companion object {
const val PLAYLIST_OPTIONS_REQUEST_KEY = "playlist_options_request_key"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import androidx.preference.PreferenceViewHolder
import com.github.libretube.R
import com.github.libretube.constants.IntentData
import com.github.libretube.ui.dialogs.ColorPickerDialog
import com.github.libretube.ui.dialogs.ColorPickerDialog.Companion.COLOR_PICKER_REQUEST_KEY

class ColorPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs) {
private lateinit var circleView: View
Expand Down Expand Up @@ -66,7 +67,7 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex
val dialog = ColorPickerDialog()
val fragmentManager = (context as AppCompatActivity).supportFragmentManager
fragmentManager.setFragmentResultListener(
IntentData.requestKey,
COLOR_PICKER_REQUEST_KEY,
context as AppCompatActivity
) { _, resultBundle ->
val newColor = resultBundle.getInt(IntentData.color)
Expand Down

0 comments on commit 5e2e606

Please sign in to comment.