Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: remove arguments from dialog fragment constructors #4684

Merged
merged 8 commits into from
Sep 8, 2023
14 changes: 14 additions & 0 deletions app/src/main/java/com/github/libretube/constants/IntentData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
object IntentData {
const val downloadData = "downloadData"
const val playerData = "playerData"
const val id = "id"

Check failure on line 6 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:6:15: error: Property name should use the screaming snake case notation when the value can not be changed (standard:property-naming)
const val videoId = "videoId"
const val channelId = "channelId"
const val channelName = "channelName"
Expand All @@ -15,4 +16,17 @@
const val comment = "comment"
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)
const val duration = "duration"

Check failure on line 24 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:24:15: error: Property name should use the screaming snake case notation when the value can not be changed (standard:property-naming)
const val updateInfo = "updateInfo"

Check failure on line 25 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:25:15: error: Property name should use the screaming snake case notation when the value can not be changed (standard:property-naming)
const val requestKey = "requestKey"

Check failure on line 26 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:26:15: error: Property name should use the screaming snake case notation when the value can not be changed (standard:property-naming)
const val backupFile = "backupFile"
const val playlistTask = "playlistTask"
const val loginTask = "loginTask"
const val logoutTask = "logoutTask"
const val color = "color"
}
6 changes: 5 additions & 1 deletion app/src/main/java/com/github/libretube/obj/ShareData.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package com.github.libretube.obj

import android.os.Parcelable
import kotlinx.parcelize.Parcelize

@Parcelize
data class ShareData(
val currentChannel: String? = null,
val currentVideo: String? = null,
val currentPlaylist: String? = null,
var currentPosition: Long? = null
)
) : Parcelable
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
Expand Up @@ -14,6 +14,7 @@ import androidx.lifecycle.repeatOnLifecycle
import com.github.libretube.R
import com.github.libretube.api.PlaylistsHelper
import com.github.libretube.api.RetrofitInstance
import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DialogAddToPlaylistBinding
import com.github.libretube.extensions.TAG
import com.github.libretube.extensions.toastFromMainDispatcher
Expand All @@ -24,20 +25,31 @@ import kotlinx.coroutines.launch

/**
* Dialog to insert new videos to a playlist
* @param videoId The id of the video to add. If non is provided, insert the whole playing queue
* videoId: The id of the video to add. If non is provided, insert the whole playing queue
*/
class AddToPlaylistDialog(
private val videoId: String? = null
) : DialogFragment() {
class AddToPlaylistDialog : DialogFragment() {
private var videoId: String? = null
private val viewModel: PlaylistViewModel by activityViewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
videoId = arguments?.getString(IntentData.videoId)
}

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)
Expand Down Expand Up @@ -93,7 +105,7 @@ class AddToPlaylistDialog(
val streams = when {
videoId != null -> listOfNotNull(
runCatching {
RetrofitInstance.api.getStreams(videoId!!).toStreamItem(videoId)
RetrofitInstance.api.getStreams(videoId!!).toStreamItem(videoId!!)
}.getOrNull()
)

Expand Down
18 changes: 14 additions & 4 deletions app/src/main/java/com/github/libretube/ui/dialogs/BackupDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,26 @@ 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
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
Expand Down Expand Up @@ -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()
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 @@ -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
Expand All @@ -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)

Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

Expand Down Expand Up @@ -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()
}
}
Expand Down
Loading
Loading