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 duration = "duration"
const val updateInfo = "updateInfo"
const val requestKey = "requestKey"
const val backupFile = "backupFile"
const val playlistTask = "playlistTask"
const val loginTask = "loginTask"

View File

@ -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"
}
}

View File

@ -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"
}
}

View File

@ -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"
}
}

View File

@ -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"
}
}

View File

@ -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"
}
}

View File

@ -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()

View File

@ -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)

View File

@ -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)
)
}

View File

@ -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()

View File

@ -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 {

View File

@ -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 {

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.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)

View File

@ -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)!!

View File

@ -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<Preference>(PreferenceKeys.LOGOUT)?.isVisible = 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.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"
}
}

View File

@ -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)