From 7eec44463559867ad9102b5ab3e07f7a796bd260 Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sun, 10 Sep 2023 13:42:17 +0200 Subject: [PATCH] fix: issues with fragment result listener --- .../github/libretube/constants/IntentData.kt | 1 - .../libretube/ui/adapters/PlaylistsAdapter.kt | 6 ++++- .../ui/dialogs/AddToPlaylistDialog.kt | 6 ++++- .../libretube/ui/dialogs/BackupDialog.kt | 14 +++++++----- .../libretube/ui/dialogs/ColorPickerDialog.kt | 6 ++++- .../ui/dialogs/CreatePlaylistDialog.kt | 8 +++++-- .../ui/dialogs/DeleteAccountDialog.kt | 3 ++- .../ui/dialogs/DeletePlaylistDialog.kt | 11 +++++----- .../libretube/ui/dialogs/LoginDialog.kt | 3 ++- .../libretube/ui/dialogs/LogoutDialog.kt | 3 ++- .../ui/dialogs/PlaylistDescriptionDialog.kt | 3 ++- .../ui/dialogs/RenamePlaylistDialog.kt | 3 ++- .../libretube/ui/fragments/LibraryFragment.kt | 3 ++- .../ui/preferences/BackupRestoreSettings.kt | 3 ++- .../ui/preferences/InstanceSettings.kt | 6 ++++- .../ui/sheets/PlaylistOptionsBottomSheet.kt | 22 ++++++++++++++----- .../libretube/ui/views/ColorPreference.kt | 3 ++- 17 files changed, 71 insertions(+), 33 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 baa171603..d787d67d0 100644 --- a/app/src/main/java/com/github/libretube/constants/IntentData.kt +++ b/app/src/main/java/com/github/libretube/constants/IntentData.kt @@ -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" 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 916feae51..4e6151f42 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 @@ -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 = @@ -102,4 +102,8 @@ class PlaylistsAdapter( notifyItemRangeChanged(position, itemCount) } } + + companion object { + const val PLAYLISTS_ADAPTER_REQUEST_KEY = "playlists_adapter_request_key" + } } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/AddToPlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/AddToPlaylistDialog.kt index 3860563c2..5b5a21284 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/AddToPlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/AddToPlaylistDialog.kt @@ -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) @@ -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" + } } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/BackupDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/BackupDialog.kt index c9bd2e7f2..7b206d52b 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/BackupDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/BackupDialog.kt @@ -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" + } } 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 f235004b7..1e44d7d19 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 @@ -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) ) } @@ -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" + } } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt index 9a2cfd8de..54c989736 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt @@ -36,7 +36,7 @@ class CreatePlaylistDialog : DialogFragment() { } if (playlistId != null) { setFragmentResult( - IntentData.requestKey, + CREATE_PLAYLIST_DIALOG_REQUEST_KEY, bundleOf(IntentData.playlistTask to true) ) } @@ -72,7 +72,7 @@ class CreatePlaylistDialog : DialogFragment() { ) playlistId?.let { setFragmentResult( - IntentData.requestKey, + CREATE_PLAYLIST_DIALOG_REQUEST_KEY, bundleOf(IntentData.playlistTask to true) ) } @@ -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" + } } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/DeleteAccountDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/DeleteAccountDialog.kt index 9af9ef023..87d0b5fcb 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/DeleteAccountDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/DeleteAccountDialog.kt @@ -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 @@ -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() 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 b8700b4d4..c48966c2a 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 @@ -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 @@ -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) diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/LoginDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/LoginDialog.kt index 842c49c61..117d07b38 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/LoginDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/LoginDialog.kt @@ -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 @@ -100,7 +101,7 @@ class LoginDialog : DialogFragment() { withContext(Dispatchers.Main) { setFragmentResult( - IntentData.requestKey, + INSTANCE_DIALOG_REQUEST_KEY, bundleOf(IntentData.loginTask to true) ) } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/LogoutDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/LogoutDialog.kt index d5e290ad6..2d0814a6b 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/LogoutDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/LogoutDialog.kt @@ -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() { @@ -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() diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/PlaylistDescriptionDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/PlaylistDescriptionDialog.kt index f54cea0f9..d77660001 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/PlaylistDescriptionDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/PlaylistDescriptionDialog.kt @@ -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 @@ -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 { 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 3e9fc3c15..e2d8322d6 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 @@ -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 @@ -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 { diff --git a/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt index c74ab10aa..bcb78fa59 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/LibraryFragment.kt @@ -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 @@ -95,7 +96,7 @@ class LibraryFragment : Fragment() { } childFragmentManager.setFragmentResultListener( - IntentData.requestKey, + CREATE_PLAYLIST_DIALOG_REQUEST_KEY, this ) { _, resultBundle -> val isPlaylistCreated = resultBundle.getBoolean(IntentData.playlistTask) diff --git a/app/src/main/java/com/github/libretube/ui/preferences/BackupRestoreSettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/BackupRestoreSettings.kt index 33487cf63..913ed9e6f 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/BackupRestoreSettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/BackupRestoreSettings.kt @@ -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 @@ -178,7 +179,7 @@ class BackupRestoreSettings : BasePreferenceFragment() { } childFragmentManager.setFragmentResultListener( - IntentData.requestKey, + BACKUP_DIALOG_REQUEST_KEY, this ) { _, resultBundle -> val encodedBackupFile = resultBundle.getString(IntentData.backupFile)!! diff --git a/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt index 55e5aa89f..e01a1707b 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/InstanceSettings.kt @@ -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) @@ -170,4 +170,8 @@ class InstanceSettings : BasePreferenceFragment() { findPreference(PreferenceKeys.LOGOUT)?.isVisible = false findPreference(PreferenceKeys.DELETE_ACCOUNT)?.isEnabled = false } + + companion object { + const val INSTANCE_DIALOG_REQUEST_KEY = "instance_dialog_request_key" + } } 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 03315f558..d71209cec 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 @@ -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 @@ -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( @@ -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( @@ -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( @@ -158,4 +164,8 @@ class PlaylistOptionsBottomSheet( } super.onCreate(savedInstanceState) } + + companion object { + const val PLAYLIST_OPTIONS_REQUEST_KEY = "playlist_options_request_key" + } } 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 5be01e606..039e7724b 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 @@ -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 @@ -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)