Merge pull request #4742 from Bnyro/master

fix: issues with fragment result listener
This commit is contained in:
Bnyro 2023-09-10 13:42:31 +02:00 committed by GitHub
commit 5e2e6069f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
17 changed files with 71 additions and 33 deletions

View File

@ -23,7 +23,6 @@ object IntentData {
const val currentPosition = "currentPosition" const val currentPosition = "currentPosition"
const val duration = "duration" const val duration = "duration"
const val updateInfo = "updateInfo" const val updateInfo = "updateInfo"
const val requestKey = "requestKey"
const val backupFile = "backupFile" const val backupFile = "backupFile"
const val playlistTask = "playlistTask" const val playlistTask = "playlistTask"
const val loginTask = "loginTask" const val loginTask = "loginTask"

View File

@ -54,7 +54,7 @@ class PlaylistsAdapter(
val fragmentManager = (root.context as BaseActivity).supportFragmentManager val fragmentManager = (root.context as BaseActivity).supportFragmentManager
fragmentManager.setFragmentResultListener( fragmentManager.setFragmentResultListener(
IntentData.requestKey, PLAYLISTS_ADAPTER_REQUEST_KEY,
(root.context as BaseActivity) (root.context as BaseActivity)
) { _, resultBundle -> ) { _, resultBundle ->
val newPlaylistDescription = val newPlaylistDescription =
@ -102,4 +102,8 @@ class PlaylistsAdapter(
notifyItemRangeChanged(position, itemCount) notifyItemRangeChanged(position, itemCount)
} }
} }
companion object {
const val PLAYLISTS_ADAPTER_REQUEST_KEY = "playlists_adapter_request_key"
}
} }

View File

@ -40,7 +40,7 @@ class AddToPlaylistDialog : DialogFragment() {
val binding = DialogAddToPlaylistBinding.inflate(layoutInflater) val binding = DialogAddToPlaylistBinding.inflate(layoutInflater)
childFragmentManager.setFragmentResultListener( childFragmentManager.setFragmentResultListener(
IntentData.requestKey, ADD_TO_PLAYLIST_DIALOG_REQUEST_KEY,
this this
) { _, resultBundle -> ) { _, resultBundle ->
val addedToPlaylist = resultBundle.getBoolean(IntentData.playlistTask) val addedToPlaylist = resultBundle.getBoolean(IntentData.playlistTask)
@ -128,4 +128,8 @@ class AddToPlaylistDialog : DialogFragment() {
appContext.toastFromMainDispatcher(R.string.fail) appContext.toastFromMainDispatcher(R.string.fail)
} }
} }
companion object {
const val ADD_TO_PLAYLIST_DIALOG_REQUEST_KEY = "add_to_playlist_dialog_request_key"
}
} }

View File

@ -102,14 +102,16 @@ class BackupDialog : DialogFragment() {
if (selected[index]) option.onSelected(backupFile) if (selected[index]) option.onSelected(backupFile)
} }
val encodedBackupFile = Json.encodeToString(backupFile) val encodedBackupFile = Json.encodeToString(backupFile)
withContext(Dispatchers.Main) { setFragmentResult(
setFragmentResult( BACKUP_DIALOG_REQUEST_KEY,
IntentData.requestKey, bundleOf(IntentData.backupFile to encodedBackupFile)
bundleOf(IntentData.backupFile to encodedBackupFile) )
)
}
} }
} }
.create() .create()
} }
companion object {
const val BACKUP_DIALOG_REQUEST_KEY = "backup_dialog_request_key"
}
} }

View File

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

View File

@ -36,7 +36,7 @@ class CreatePlaylistDialog : DialogFragment() {
} }
if (playlistId != null) { if (playlistId != null) {
setFragmentResult( setFragmentResult(
IntentData.requestKey, CREATE_PLAYLIST_DIALOG_REQUEST_KEY,
bundleOf(IntentData.playlistTask to true) bundleOf(IntentData.playlistTask to true)
) )
} }
@ -72,7 +72,7 @@ class CreatePlaylistDialog : DialogFragment() {
) )
playlistId?.let { playlistId?.let {
setFragmentResult( setFragmentResult(
IntentData.requestKey, CREATE_PLAYLIST_DIALOG_REQUEST_KEY,
bundleOf(IntentData.playlistTask to true) bundleOf(IntentData.playlistTask to true)
) )
} }
@ -87,4 +87,8 @@ class CreatePlaylistDialog : DialogFragment() {
.setView(binding.root) .setView(binding.root)
.show() .show()
} }
companion object {
const val CREATE_PLAYLIST_DIALOG_REQUEST_KEY = "create_playlist_dialog_request_key"
}
} }

View File

@ -17,6 +17,7 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DialogDeleteAccountBinding import com.github.libretube.databinding.DialogDeleteAccountBinding
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.preferences.InstanceSettings
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -61,7 +62,7 @@ class DeleteAccountDialog : DialogFragment() {
Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show()
setFragmentResult( setFragmentResult(
IntentData.requestKey, InstanceSettings.INSTANCE_DIALOG_REQUEST_KEY,
bundleOf(IntentData.logoutTask to true) bundleOf(IntentData.logoutTask to true)
) )
dialog?.dismiss() dialog?.dismiss()

View File

@ -11,6 +11,7 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.enums.PlaylistType import com.github.libretube.enums.PlaylistType
import com.github.libretube.extensions.serializable import com.github.libretube.extensions.serializable
import com.github.libretube.extensions.toastFromMainDispatcher import com.github.libretube.extensions.toastFromMainDispatcher
import com.github.libretube.ui.sheets.PlaylistOptionsBottomSheet
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -38,12 +39,10 @@ class DeletePlaylistDialog : DialogFragment() {
appContext?.toastFromMainDispatcher( appContext?.toastFromMainDispatcher(
if (success) R.string.success else R.string.fail if (success) R.string.success else R.string.fail
) )
withContext(Dispatchers.Main) { setFragmentResult(
setFragmentResult( PlaylistOptionsBottomSheet.PLAYLIST_OPTIONS_REQUEST_KEY,
IntentData.requestKey, bundleOf(IntentData.playlistTask to true)
bundleOf(IntentData.playlistTask to true) )
)
}
} }
} }
.setNegativeButton(R.string.cancel, null) .setNegativeButton(R.string.cancel, null)

View File

@ -19,6 +19,7 @@ import com.github.libretube.databinding.DialogLoginBinding
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toastFromMainDispatcher import com.github.libretube.extensions.toastFromMainDispatcher
import com.github.libretube.helpers.PreferenceHelper 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 com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -100,7 +101,7 @@ class LoginDialog : DialogFragment() {
withContext(Dispatchers.Main) { withContext(Dispatchers.Main) {
setFragmentResult( setFragmentResult(
IntentData.requestKey, INSTANCE_DIALOG_REQUEST_KEY,
bundleOf(IntentData.loginTask to true) bundleOf(IntentData.loginTask to true)
) )
} }

View File

@ -11,6 +11,7 @@ import com.github.libretube.R
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DialogLogoutBinding import com.github.libretube.databinding.DialogLogoutBinding
import com.github.libretube.helpers.PreferenceHelper import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.preferences.InstanceSettings
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
class LogoutDialog : DialogFragment() { class LogoutDialog : DialogFragment() {
@ -25,7 +26,7 @@ class LogoutDialog : DialogFragment() {
Toast.makeText(context, R.string.loggedout, Toast.LENGTH_SHORT).show() Toast.makeText(context, R.string.loggedout, Toast.LENGTH_SHORT).show()
setFragmentResult( setFragmentResult(
IntentData.requestKey, InstanceSettings.INSTANCE_DIALOG_REQUEST_KEY,
bundleOf(IntentData.logoutTask to true) bundleOf(IntentData.logoutTask to true)
) )
dialog?.dismiss() dialog?.dismiss()

View File

@ -16,6 +16,7 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DialogTextPreferenceBinding import com.github.libretube.databinding.DialogTextPreferenceBinding
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toastFromMainDispatcher 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 com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -79,7 +80,7 @@ class PlaylistDescriptionDialog : DialogFragment() {
if (success) { if (success) {
appContext.toastFromMainDispatcher(R.string.success) appContext.toastFromMainDispatcher(R.string.success)
setFragmentResult( setFragmentResult(
IntentData.requestKey, PLAYLIST_OPTIONS_REQUEST_KEY,
bundleOf(IntentData.playlistDescription to newDescription) bundleOf(IntentData.playlistDescription to newDescription)
) )
} else { } else {

View File

@ -16,6 +16,7 @@ import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DialogTextPreferenceBinding import com.github.libretube.databinding.DialogTextPreferenceBinding
import com.github.libretube.extensions.TAG import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toastFromMainDispatcher 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 com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
@ -72,7 +73,7 @@ class RenamePlaylistDialog : DialogFragment() {
if (success) { if (success) {
appContext.toastFromMainDispatcher(R.string.success) appContext.toastFromMainDispatcher(R.string.success)
setFragmentResult( setFragmentResult(
IntentData.requestKey, PLAYLIST_OPTIONS_REQUEST_KEY,
bundleOf(IntentData.playlistName to newPlaylistName) bundleOf(IntentData.playlistName to newPlaylistName)
) )
} else { } else {

View File

@ -32,6 +32,7 @@ import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.ui.adapters.PlaylistBookmarkAdapter import com.github.libretube.ui.adapters.PlaylistBookmarkAdapter
import com.github.libretube.ui.adapters.PlaylistsAdapter import com.github.libretube.ui.adapters.PlaylistsAdapter
import com.github.libretube.ui.dialogs.CreatePlaylistDialog 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.models.PlayerViewModel
import com.github.libretube.ui.sheets.BaseBottomSheet import com.github.libretube.ui.sheets.BaseBottomSheet
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
@ -95,7 +96,7 @@ class LibraryFragment : Fragment() {
} }
childFragmentManager.setFragmentResultListener( childFragmentManager.setFragmentResultListener(
IntentData.requestKey, CREATE_PLAYLIST_DIALOG_REQUEST_KEY,
this this
) { _, resultBundle -> ) { _, resultBundle ->
val isPlaylistCreated = resultBundle.getBoolean(IntentData.playlistTask) val isPlaylistCreated = resultBundle.getBoolean(IntentData.playlistTask)

View File

@ -14,6 +14,7 @@ import com.github.libretube.helpers.ImportHelper
import com.github.libretube.obj.BackupFile import com.github.libretube.obj.BackupFile
import com.github.libretube.ui.base.BasePreferenceFragment import com.github.libretube.ui.base.BasePreferenceFragment
import com.github.libretube.ui.dialogs.BackupDialog 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.github.libretube.ui.dialogs.RequireRestartDialog
import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.dialog.MaterialAlertDialogBuilder
import java.time.LocalDateTime import java.time.LocalDateTime
@ -178,7 +179,7 @@ class BackupRestoreSettings : BasePreferenceFragment() {
} }
childFragmentManager.setFragmentResultListener( childFragmentManager.setFragmentResultListener(
IntentData.requestKey, BACKUP_DIALOG_REQUEST_KEY,
this this
) { _, resultBundle -> ) { _, resultBundle ->
val encodedBackupFile = resultBundle.getString(IntentData.backupFile)!! val encodedBackupFile = resultBundle.getString(IntentData.backupFile)!!

View File

@ -102,7 +102,7 @@ class InstanceSettings : BasePreferenceFragment() {
deleteAccount?.isEnabled = token.isNotEmpty() deleteAccount?.isEnabled = token.isNotEmpty()
childFragmentManager.setFragmentResultListener( childFragmentManager.setFragmentResultListener(
IntentData.requestKey, INSTANCE_DIALOG_REQUEST_KEY,
this this
) { _, resultBundle -> ) { _, resultBundle ->
val isLoggedIn = resultBundle.getBoolean(IntentData.loginTask) val isLoggedIn = resultBundle.getBoolean(IntentData.loginTask)
@ -170,4 +170,8 @@ class InstanceSettings : BasePreferenceFragment() {
findPreference<Preference>(PreferenceKeys.LOGOUT)?.isVisible = false findPreference<Preference>(PreferenceKeys.LOGOUT)?.isVisible = false
findPreference<Preference>(PreferenceKeys.DELETE_ACCOUNT)?.isEnabled = false findPreference<Preference>(PreferenceKeys.DELETE_ACCOUNT)?.isEnabled = false
} }
companion object {
const val INSTANCE_DIALOG_REQUEST_KEY = "instance_dialog_request_key"
}
} }

View File

@ -13,6 +13,7 @@ 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
@ -93,20 +94,21 @@ class PlaylistOptionsBottomSheet(
} }
// share the playlist // share the playlist
getString(R.string.share) -> { getString(R.string.share) -> {
val bundle = bundleOf( val newShareDialog = ShareDialog()
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
) )
val newShareDialog = ShareDialog()
newShareDialog.arguments = bundle
// 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(IntentData.requestKey, context as BaseActivity) { _, bundle -> mFragmentManager.setFragmentResultListener(PLAYLIST_OPTIONS_REQUEST_KEY, context as BaseActivity) { _, bundle ->
if (bundle.getBoolean(IntentData.playlistTask, true)) onDelete() 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(
@ -117,8 +119,10 @@ class PlaylistOptionsBottomSheet(
} }
getString(R.string.renamePlaylist) -> { 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, "")) 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(
@ -129,8 +133,10 @@ class PlaylistOptionsBottomSheet(
} }
getString(R.string.change_playlist_description) -> { 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, "")) 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(
@ -158,4 +164,8 @@ class PlaylistOptionsBottomSheet(
} }
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
} }
companion object {
const val PLAYLIST_OPTIONS_REQUEST_KEY = "playlist_options_request_key"
}
} }

View File

@ -13,6 +13,7 @@ import androidx.preference.PreferenceViewHolder
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.ui.dialogs.ColorPickerDialog 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) { class ColorPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs) {
private lateinit var circleView: View private lateinit var circleView: View
@ -66,7 +67,7 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex
val dialog = ColorPickerDialog() val dialog = ColorPickerDialog()
val fragmentManager = (context as AppCompatActivity).supportFragmentManager val fragmentManager = (context as AppCompatActivity).supportFragmentManager
fragmentManager.setFragmentResultListener( fragmentManager.setFragmentResultListener(
IntentData.requestKey, COLOR_PICKER_REQUEST_KEY,
context as AppCompatActivity context as AppCompatActivity
) { _, resultBundle -> ) { _, resultBundle ->
val newColor = resultBundle.getInt(IntentData.color) val newColor = resultBundle.getInt(IntentData.color)