Skip to content

Commit

Permalink
refactor: use Bundle for DialogFragment arguments
Browse files Browse the repository at this point in the history
- ColorPickerDialog
- DeletePlaylistDialog
- RenamePlaylistDialog
  • Loading branch information
anshtya authored and Bnyro committed Sep 8, 2023
1 parent 1044fcd commit f260c48
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ object IntentData {
const val minimizeByDefault = "minimizeByDefault"
const val query = "query"
const val playlistDescription = "playlistDescription"

Check failure on line 19 in app/src/main/java/com/github/libretube/constants/IntentData.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Property name should use the screaming snake case notation when the value can not be changed Raw Output: app/src/main/java/com/github/libretube/constants/IntentData.kt:19:15: error: Property name should use the screaming snake case notation when the value can not be changed (standard:property-naming)
const val playlistName = "playlistName"

Check failure on line 20 in app/src/main/java/com/github/libretube/constants/IntentData.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Property name should use the screaming snake case notation when the value can not be changed Raw Output: app/src/main/java/com/github/libretube/constants/IntentData.kt:20:15: error: Property name should use the screaming snake case notation when the value can not be changed (standard:property-naming)
const val shareObjectType = "shareObjectType"

Check failure on line 21 in app/src/main/java/com/github/libretube/constants/IntentData.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Property name should use the screaming snake case notation when the value can not be changed Raw Output: app/src/main/java/com/github/libretube/constants/IntentData.kt:21:15: error: Property name should use the screaming snake case notation when the value can not be changed (standard:property-naming)
const val shareData = "shareData"

Check failure on line 22 in app/src/main/java/com/github/libretube/constants/IntentData.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Property name should use the screaming snake case notation when the value can not be changed Raw Output: app/src/main/java/com/github/libretube/constants/IntentData.kt:22:15: error: Property name should use the screaming snake case notation when the value can not be changed (standard:property-naming)
const val currentPosition = "currentPosition"

Check failure on line 23 in app/src/main/java/com/github/libretube/constants/IntentData.kt

View workflow job for this annotation

GitHub Actions / Check Code Quality

[ktlint] reported by reviewdog 🐶 Property name should use the screaming snake case notation when the value can not be changed Raw Output: app/src/main/java/com/github/libretube/constants/IntentData.kt:23:15: error: Property name should use the screaming snake case notation when the value can not be changed (standard:property-naming)
Expand All @@ -27,4 +28,5 @@ object IntentData {
const val playlistTask = "playlistTask"
const val loginTask = "loginTask"
const val logoutTask = "logoutTask"
const val color = "color"
}
5 changes: 1 addition & 4 deletions app/src/main/java/com/github/libretube/extensions/Bundle.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,15 @@ 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

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 (BuildCompat.isAtLeastU()) {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.UPSIDE_DOWN_CAKE) {
getSerializable(key, T::class.java)
} else {
@Suppress("DEPRECATION")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R
import com.github.libretube.api.obj.Playlists
import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.PlaylistsRowBinding
import com.github.libretube.enums.PlaylistType
import com.github.libretube.helpers.ImageHelper
Expand Down Expand Up @@ -51,25 +52,42 @@ class PlaylistsAdapter(
NavigationHelper.navigatePlaylist(root.context, playlist.id, playlistType)
}

val fragmentManager = (root.context as BaseActivity).supportFragmentManager
fragmentManager.setFragmentResultListener(
IntentData.requestKey,
(root.context as BaseActivity)
) { _, resultBundle ->
val newPlaylistDescription =
resultBundle.getString(IntentData.playlistDescription)
val newPlaylistName =
resultBundle.getString(IntentData.playlistName)
val isPlaylistToBeDeleted =
resultBundle.getBoolean(IntentData.playlistTask)

newPlaylistDescription?.let {
playlistDescription.text = it
playlist.shortDescription = it
}

newPlaylistName?.let {
playlistTitle.text = it
playlist.name = it
}

if (isPlaylistToBeDeleted) {
// try to refresh the playlists in the library on deletion success
onDelete(position, root.context as BaseActivity)
}
}

root.setOnLongClickListener {
val playlistOptionsDialog = PlaylistOptionsBottomSheet(
playlistId = playlist.id!!,
playlistName = playlist.name!!,
playlistType = playlistType,
onDelete = {
onDelete(position, root.context as BaseActivity)
},
onRename = {
playlistTitle.text = it
playlist.name = it
},
onChangeDescription = {
playlistDescription.text = it
playlist.shortDescription = it
}
playlistType = playlistType
)
playlistOptionsDialog.show(
(root.context as BaseActivity).supportFragmentManager,
fragmentManager,
PlaylistOptionsBottomSheet::class.java.name
)
true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
package com.github.libretube.ui.dialogs

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.SeekBar
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.DialogColorPickerBinding
import com.google.android.material.dialog.MaterialAlertDialogBuilder

class ColorPickerDialog(
private val context: Context,
private val initialColor: Int,
private val onColorSelectedListener: OnColorSelectedListener
) : DialogFragment(), SeekBar.OnSeekBarChangeListener {
class ColorPickerDialog : DialogFragment(), SeekBar.OnSeekBarChangeListener {
private var initialColor: Int? = null

private var _binding: DialogColorPickerBinding? = null
private val binding get() = _binding!!

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initialColor = arguments?.getInt(IntentData.color)!!
}

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
_binding = DialogColorPickerBinding.inflate(layoutInflater)

// Set initial color
setColor(initialColor)
setColor(initialColor!!)

binding.alphaSeekBar.setOnSeekBarChangeListener(this)
binding.redSeekBar.setOnSeekBarChangeListener(this)
Expand Down Expand Up @@ -74,7 +78,11 @@ class ColorPickerDialog(
return MaterialAlertDialogBuilder(requireContext())
.setView(binding.root)
.setPositiveButton(R.string.okay) { _, _ ->
onColorSelectedListener.onColorSelected(getColor())
val color = getColor()
setFragmentResult(
IntentData.requestKey,
bundleOf(IntentData.color to color)
)
}
.setNegativeButton(R.string.cancel, null)
.show()
Expand Down Expand Up @@ -134,8 +142,4 @@ class ColorPickerDialog(
private fun colorToString(color: Int): String {
return String.format("#%08X", color)
}

fun interface OnColorSelectedListener {
fun onColorSelected(color: Int)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,31 @@ package com.github.libretube.ui.dialogs

import android.app.Dialog
import android.os.Bundle
import androidx.core.os.bundleOf
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.setFragmentResult
import com.github.libretube.R
import com.github.libretube.api.PlaylistsHelper
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.google.android.material.dialog.MaterialAlertDialogBuilder
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class DeletePlaylistDialog(
private val playlistId: String,
private val playlistType: PlaylistType,
private val onSuccess: () -> Unit
) : DialogFragment() {
class DeletePlaylistDialog : DialogFragment() {
private lateinit var playlistId: String
private lateinit var playlistType: PlaylistType
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
playlistId = it.getString(IntentData.playlistId)!!
playlistType = it.serializable(IntentData.playlistType)!!
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
return MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.deletePlaylist)
Expand All @@ -30,9 +39,10 @@ class DeletePlaylistDialog(
if (success) R.string.success else R.string.fail
)
withContext(Dispatchers.Main) {
runCatching {
onSuccess.invoke()
}
setFragmentResult(
IntentData.requestKey,
bundleOf(IntentData.playlistTask to true)
)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import android.os.Bundle
import android.text.InputType
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.lifecycleScope
import com.github.libretube.R
import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DialogTextPreferenceBinding
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toastFromMainDispatcher
Expand All @@ -18,11 +21,17 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class RenamePlaylistDialog(
private val playlistId: String,
private val currentPlaylistName: String,
private val onSuccess: (String) -> Unit
) : DialogFragment() {
class RenamePlaylistDialog : DialogFragment() {
private lateinit var playlistId: String
private lateinit var currentPlaylistName: String

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
arguments?.let {
playlistId = it.getString(IntentData.playlistId)!!
currentPlaylistName = it.getString(IntentData.playlistName)!!
}
}
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val binding = DialogTextPreferenceBinding.inflate(layoutInflater)
binding.input.inputType = InputType.TYPE_CLASS_TEXT
Expand Down Expand Up @@ -62,7 +71,10 @@ class RenamePlaylistDialog(
}
if (success) {
appContext.toastFromMainDispatcher(R.string.success)
onSuccess.invoke(newPlaylistName)
setFragmentResult(
IntentData.requestKey,
bundleOf(IntentData.playlistName to newPlaylistName)
)
} else {
appContext.toastFromMainDispatcher(R.string.server_error)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,33 +102,33 @@ class PlaylistOptionsBottomSheet(
}

getString(R.string.deletePlaylist) -> {
DeletePlaylistDialog(playlistId, playlistType) {
// try to refresh the playlists in the library on deletion success
onDelete()
}.show(parentFragmentManager, null)
val bundle = bundleOf(
IntentData.playlistId to playlistId,
IntentData.playlistType to playlistType
)
val newDeletePlaylistDialog = DeletePlaylistDialog()
newDeletePlaylistDialog.arguments = bundle
newDeletePlaylistDialog.show(parentFragmentManager, null)
}

getString(R.string.renamePlaylist) -> {
RenamePlaylistDialog(playlistId, playlistName, onRename)
.show(parentFragmentManager, null)
val bundle = bundleOf(
IntentData.playlistId to playlistId,
IntentData.playlistName to playlistName
)
val newRenamePlaylistDialog = RenamePlaylistDialog()
newRenamePlaylistDialog.arguments = bundle
newRenamePlaylistDialog.show(parentFragmentManager, null)
}

getString(R.string.change_playlist_description) -> {
val bundle = bundleOf(
IntentData.playlistId to playlistId,
IntentData.playlistDescription to ""
)
val newShareDialog = PlaylistDescriptionDialog()
newShareDialog.arguments = bundle
newShareDialog.show(parentFragmentManager, null)
parentFragmentManager.setFragmentResultListener(
IntentData.requestKey,
this
) { _, resultBundle ->
val newDescription =
resultBundle.getString(IntentData.playlistDescription)!!
onChangeDescription.invoke(newDescription)
}
val newPlaylistDescriptionDialog = PlaylistDescriptionDialog()
newPlaylistDescriptionDialog.arguments = bundle
newPlaylistDescriptionDialog.show(parentFragmentManager, null)
}

else -> {
Expand Down
19 changes: 14 additions & 5 deletions app/src/main/java/com/github/libretube/ui/views/ColorPreference.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import androidx.preference.Preference
import androidx.preference.PreferenceViewHolder
import com.github.libretube.R
import com.github.libretube.constants.IntentData
import com.github.libretube.ui.dialogs.ColorPickerDialog

class ColorPreference(context: Context, attrs: AttributeSet) : Preference(context, attrs) {
Expand Down Expand Up @@ -60,11 +62,18 @@ class ColorPreference(context: Context, attrs: AttributeSet) : Preference(contex

private fun showColorPickerDialog() {
(if (currentColor is Int) currentColor else Color.BLACK)?.let {
val dialog = ColorPickerDialog(context, it) { color -> setColor(color) }
dialog.show(
(context as AppCompatActivity).supportFragmentManager,
this::class.java.name
)
val bundle = bundleOf(IntentData.color to it)
val dialog = ColorPickerDialog()
val fragmentManager = (context as AppCompatActivity).supportFragmentManager
fragmentManager.setFragmentResultListener(
IntentData.requestKey,
context as AppCompatActivity
) { _, resultBundle ->
val newColor = resultBundle.getInt(IntentData.color)
setColor(newColor)
}
dialog.arguments = bundle
dialog.show(fragmentManager, this::class.java.name)
}
}

Expand Down

0 comments on commit f260c48

Please sign in to comment.