refactor: use Bundle for DialogFragment arguments

- AddToPlaylistDialog
- BackupDialog
- CreatePlaylistDialog
- DeleteAccountDialog
- LoginDialog
- LogoutDialog
This commit is contained in:
Ansh Tyagi 2023-09-04 21:36:47 +05:30 committed by Bnyro
parent 35565ad0c7
commit 1044fcd153
11 changed files with 112 additions and 41 deletions

View File

@ -23,4 +23,8 @@ object IntentData {
const val duration = "duration"
const val updateInfo = "updateInfo"
const val requestKey = "requestKey"
const val backupFile = "backupFile"
const val playlistTask = "playlistTask"
const val loginTask = "loginTask"
const val logoutTask = "logoutTask"
}

View File

@ -3,6 +3,8 @@ package com.github.libretube.extensions
import android.os.Build
import android.os.Bundle
import android.os.Parcelable
import androidx.annotation.OptIn
import androidx.core.os.BuildCompat
import androidx.core.os.BundleCompat
import java.io.Serializable
@ -10,8 +12,9 @@ inline fun <reified T : Parcelable> Bundle.parcelable(key: String?): T? {
return BundleCompat.getParcelable(this, key, T::class.java)
}
@OptIn(BuildCompat.PrereleaseSdkCheck::class)
inline fun <reified T : Serializable> Bundle.serializable(key: String): T? {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
return if (BuildCompat.isAtLeastU()) {
getSerializable(key, T::class.java)
} else {
@Suppress("DEPRECATION")

View File

@ -39,10 +39,17 @@ class AddToPlaylistDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val binding = DialogAddToPlaylistBinding.inflate(layoutInflater)
binding.createPlaylist.setOnClickListener {
CreatePlaylistDialog {
childFragmentManager.setFragmentResultListener(
IntentData.requestKey,
this
) { _, resultBundle ->
val addedToPlaylist = resultBundle.getBoolean(IntentData.playlistTask)
if (addedToPlaylist) {
fetchPlaylists(binding)
}.show(childFragmentManager, null)
}
}
binding.createPlaylist.setOnClickListener {
CreatePlaylistDialog().show(childFragmentManager, null)
}
fetchPlaylists(binding)

View File

@ -3,9 +3,12 @@ package com.github.libretube.ui.dialogs
import android.app.Dialog
import android.os.Bundle
import androidx.annotation.StringRes
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.constants.IntentData
import com.github.libretube.db.DatabaseHolder.Database
import com.github.libretube.helpers.PreferenceHelper
import com.github.libretube.obj.BackupFile
@ -13,12 +16,13 @@ import com.github.libretube.obj.PreferenceItem
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonNull
import kotlinx.serialization.json.JsonPrimitive
class BackupDialog(
private val createBackupFile: (BackupFile) -> Unit
) : DialogFragment() {
class BackupDialog : DialogFragment() {
sealed class BackupOption(
@StringRes val name: Int,
val onSelected: suspend (BackupFile) -> Unit
@ -97,7 +101,13 @@ class BackupDialog(
backupOptions.forEachIndexed { index, option ->
if (selected[index]) option.onSelected(backupFile)
}
createBackupFile(backupFile)
val encodedBackupFile = Json.encodeToString(backupFile)
withContext(Dispatchers.Main) {
setFragmentResult(
IntentData.requestKey,
bundleOf(IntentData.backupFile to encodedBackupFile)
)
}
}
}
.create()

View File

@ -3,10 +3,13 @@ package com.github.libretube.ui.dialogs
import android.app.Dialog
import android.os.Bundle
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.DialogCreatePlaylistBinding
import com.github.libretube.extensions.toastFromMainDispatcher
import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -15,9 +18,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
class CreatePlaylistDialog(
private val onSuccess: () -> Unit = {}
) : DialogFragment() {
class CreatePlaylistDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val binding = DialogCreatePlaylistBinding.inflate(layoutInflater)
@ -34,7 +35,10 @@ class CreatePlaylistDialog(
}.getOrNull()
}
if (playlistId != null) {
onSuccess()
setFragmentResult(
IntentData.requestKey,
bundleOf(IntentData.playlistTask to true)
)
}
appContext?.toastFromMainDispatcher(
if (playlistId != null) R.string.playlistCloned else R.string.server_error
@ -66,7 +70,12 @@ class CreatePlaylistDialog(
appContext?.toastFromMainDispatcher(
if (playlistId != null) R.string.playlistCreated else R.string.unknown_error
)
playlistId?.let { onSuccess() }
playlistId?.let {
setFragmentResult(
IntentData.requestKey,
bundleOf(IntentData.playlistTask to true)
)
}
dismiss()
}
} else {

View File

@ -4,13 +4,16 @@ import android.app.Dialog
import android.os.Bundle
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.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.github.libretube.R
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.DeleteUserRequest
import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DialogDeleteAccountBinding
import com.github.libretube.extensions.TAG
import com.github.libretube.helpers.PreferenceHelper
@ -19,9 +22,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
class DeleteAccountDialog(
private val onLogout: () -> Unit
) : DialogFragment() {
class DeleteAccountDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val binding = DialogDeleteAccountBinding.inflate(layoutInflater)
@ -59,7 +60,10 @@ class DeleteAccountDialog(
}
Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show()
onLogout.invoke()
setFragmentResult(
IntentData.requestKey,
bundleOf(IntentData.logoutTask to true)
)
dialog?.dismiss()
}
}

View File

@ -5,13 +5,16 @@ import android.os.Bundle
import android.util.Log
import android.util.Patterns
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.JsonHelper
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.Login
import com.github.libretube.api.obj.Token
import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DialogLoginBinding
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toastFromMainDispatcher
@ -22,9 +25,7 @@ import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.HttpException
class LoginDialog(
private val onLogin: () -> Unit
) : DialogFragment() {
class LoginDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val binding = DialogLoginBinding.inflate(layoutInflater)
@ -98,7 +99,10 @@ class LoginDialog(
PreferenceHelper.setUsername(login.username)
withContext(Dispatchers.Main) {
onLogin.invoke()
setFragmentResult(
IntentData.requestKey,
bundleOf(IntentData.loginTask to true)
)
}
dialog?.dismiss()
}

View File

@ -4,15 +4,16 @@ import android.annotation.SuppressLint
import android.app.Dialog
import android.os.Bundle
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.DialogLogoutBinding
import com.github.libretube.helpers.PreferenceHelper
import com.google.android.material.dialog.MaterialAlertDialogBuilder
class LogoutDialog(
private val onLogout: () -> Unit
) : DialogFragment() {
class LogoutDialog : DialogFragment() {
@SuppressLint("SetTextI18n")
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val binding = DialogLogoutBinding.inflate(layoutInflater)
@ -23,7 +24,10 @@ class LogoutDialog(
binding.logout.setOnClickListener {
Toast.makeText(context, R.string.loggedout, Toast.LENGTH_SHORT).show()
onLogout.invoke()
setFragmentResult(
IntentData.requestKey,
bundleOf(IntentData.logoutTask to true)
)
dialog?.dismiss()
}

View File

@ -21,6 +21,7 @@ import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.api.obj.Playlists
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.databinding.FragmentLibraryBinding
import com.github.libretube.db.DatabaseHolder
@ -92,10 +93,19 @@ class LibraryFragment : Fragment() {
fetchPlaylists()
initBookmarks()
}
binding.createPlaylist.setOnClickListener {
CreatePlaylistDialog {
childFragmentManager.setFragmentResultListener(
IntentData.requestKey,
this
) { _, resultBundle ->
val isPlaylistCreated = resultBundle.getBoolean(IntentData.playlistTask)
if (isPlaylistCreated) {
fetchPlaylists()
}.show(childFragmentManager, CreatePlaylistDialog::class.java.name)
}
}
binding.createPlaylist.setOnClickListener {
CreatePlaylistDialog()
.show(childFragmentManager, CreatePlaylistDialog::class.java.name)
}
val sortOptions = resources.getStringArray(R.array.playlistSortingOptions)

View File

@ -7,6 +7,7 @@ import androidx.annotation.StringRes
import androidx.lifecycle.lifecycleScope
import androidx.preference.Preference
import com.github.libretube.R
import com.github.libretube.constants.IntentData
import com.github.libretube.enums.ImportFormat
import com.github.libretube.helpers.BackupHelper
import com.github.libretube.helpers.ImportHelper
@ -21,6 +22,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
class BackupRestoreSettings : BasePreferenceFragment() {
private val backupDateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH:mm:ss")
@ -175,14 +177,18 @@ class BackupRestoreSettings : BasePreferenceFragment() {
true
}
childFragmentManager.setFragmentResultListener(
IntentData.requestKey,
this
) { _, resultBundle ->
val encodedBackupFile = resultBundle.getString(IntentData.backupFile)!!
backupFile = Json.decodeFromString(encodedBackupFile)
val timestamp = backupDateTimeFormatter.format(LocalDateTime.now())
createBackupFile.launch("libretube-backup-$timestamp.json")
}
val advancedBackup = findPreference<Preference>("backup")
advancedBackup?.setOnPreferenceClickListener {
BackupDialog {
backupFile = it
val timestamp = backupDateTimeFormatter.format(LocalDateTime.now())
createBackupFile.launch("libretube-backup-$timestamp.json")
}
.show(childFragmentManager, null)
BackupDialog().show(childFragmentManager, null)
true
}

View File

@ -11,6 +11,7 @@ import com.github.libretube.R
import com.github.libretube.api.InstanceHelper
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.api.obj.Instances
import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys
import com.github.libretube.db.DatabaseHolder.Database
import com.github.libretube.extensions.toastFromMainDispatcher
@ -100,24 +101,33 @@ class InstanceSettings : BasePreferenceFragment() {
logout?.isVisible = token.isNotEmpty()
deleteAccount?.isEnabled = token.isNotEmpty()
login?.setOnPreferenceClickListener {
LoginDialog {
login.isVisible = false
childFragmentManager.setFragmentResultListener(
IntentData.requestKey,
this
) { _, resultBundle ->
val isLoggedIn = resultBundle.getBoolean(IntentData.loginTask)
val isLoggedOut = resultBundle.getBoolean(IntentData.logoutTask)
if (isLoggedIn) {
login?.isVisible = false
logout?.isVisible = true
deleteAccount?.isEnabled = true
} else if (isLoggedOut) {
logoutAndUpdateUI()
}
.show(childFragmentManager, LoginDialog::class.java.name)
}
login?.setOnPreferenceClickListener {
LoginDialog().show(childFragmentManager, LoginDialog::class.java.name)
true
}
logout?.setOnPreferenceClickListener {
LogoutDialog(this::logoutAndUpdateUI)
.show(childFragmentManager, LogoutDialog::class.java.name)
LogoutDialog().show(childFragmentManager, LogoutDialog::class.java.name)
true
}
deleteAccount?.setOnPreferenceClickListener {
DeleteAccountDialog(this::logoutAndUpdateUI)
DeleteAccountDialog()
.show(childFragmentManager, DeleteAccountDialog::class.java.name)
true
}