Merge pull request #5106 from Bnyro/master

refactor: make dialogs more uniform
This commit is contained in:
Bnyro 2023-11-03 15:46:15 +01:00 committed by GitHub
commit 3421ecae32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 137 additions and 304 deletions

View File

@ -2,6 +2,7 @@ package com.github.libretube.ui.dialogs
import android.annotation.SuppressLint
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.util.Log
import android.widget.ArrayAdapter
@ -14,6 +15,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.api.obj.Playlists
import com.github.libretube.constants.IntentData
import com.github.libretube.databinding.DialogAddToPlaylistBinding
import com.github.libretube.extensions.TAG
@ -31,6 +33,8 @@ class AddToPlaylistDialog : DialogFragment() {
private var videoId: String? = null
private val viewModel: PlaylistViewModel by activityViewModels()
var playlists = emptyList<Playlists>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
videoId = arguments?.getString(IntentData.videoId)
@ -48,15 +52,30 @@ class AddToPlaylistDialog : DialogFragment() {
fetchPlaylists(binding)
}
}
binding.createPlaylist.setOnClickListener {
CreatePlaylistDialog().show(childFragmentManager, null)
}
fetchPlaylists(binding)
return MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.addToPlaylist)
.setNegativeButton(R.string.createPlaylist, null)
.setPositiveButton(R.string.addToPlaylist, null)
.setView(binding.root)
.show()
.apply {
getButton(DialogInterface.BUTTON_NEGATIVE).setOnClickListener {
CreatePlaylistDialog().show(childFragmentManager, null)
}
getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {val index = binding.playlistsSpinner.selectedItemPosition
val playlist = playlists[index]
viewModel.lastSelectedPlaylistId = playlist.id!!
dialog?.hide()
lifecycleScope.launch {
addToPlaylist(playlist.id, playlist.name!!)
dialog?.dismiss()
}
}
}
}
private fun fetchPlaylists(binding: DialogAddToPlaylistBinding) {
@ -70,7 +89,7 @@ class AddToPlaylistDialog : DialogFragment() {
return@repeatOnLifecycle
}
val playlists = response.filter { !it.name.isNullOrEmpty() }
playlists = response.filter { !it.name.isNullOrEmpty() }
if (playlists.isEmpty()) return@repeatOnLifecycle
binding.playlistsSpinner.adapter =
@ -85,16 +104,6 @@ class AddToPlaylistDialog : DialogFragment() {
val latestIndex = response.indexOfFirst { it.id == id }.takeIf { it >= 0 } ?: 0
binding.playlistsSpinner.setSelection(latestIndex)
}
binding.addToPlaylist.setOnClickListener {
val index = binding.playlistsSpinner.selectedItemPosition
val playlist = playlists[index]
viewModel.lastSelectedPlaylistId = playlist.id!!
dialog?.hide()
lifecycleScope.launch {
addToPlaylist(playlist.id, playlist.name!!)
dialog?.dismiss()
}
}
}
}
}

View File

@ -50,10 +50,6 @@ class CreatePlaylistDialog : DialogFragment() {
}
}
binding.cancelButton.setOnClickListener {
dismiss()
}
binding.createNewPlaylist.setOnClickListener {
val appContext = context?.applicationContext
val listName = binding.playlistName.text?.toString()
@ -84,7 +80,9 @@ class CreatePlaylistDialog : DialogFragment() {
}
return MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.createPlaylist)
.setView(binding.root)
.setNegativeButton(R.string.cancel, null)
.show()
}

View File

@ -18,11 +18,10 @@ class CustomInstanceDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val binding = DialogCustomInstanceBinding.inflate(layoutInflater)
binding.cancel.setOnClickListener {
dismiss()
}
binding.addInstance.setOnClickListener {
return MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.customInstance)
.setView(binding.root)
.setPositiveButton(R.string.addInstance) { _, _ ->
val instanceName = binding.instanceName.text.toString()
val apiUrl = binding.instanceApiUrl.text.toString()
val frontendUrl = binding.instanceFrontendUrl.text.toString()
@ -44,9 +43,7 @@ class CustomInstanceDialog : DialogFragment() {
Toast.makeText(requireContext(), R.string.empty_instance, Toast.LENGTH_SHORT).show()
}
}
return MaterialAlertDialogBuilder(requireContext())
.setView(binding.root)
.setNegativeButton(R.string.cancel, null)
.show()
}
}

View File

@ -1,15 +1,14 @@
package com.github.libretube.ui.dialogs
import android.app.Dialog
import android.content.DialogInterface
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
@ -27,27 +26,30 @@ class DeleteAccountDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val binding = DialogDeleteAccountBinding.inflate(layoutInflater)
binding.cancelButton.setOnClickListener {
dialog?.dismiss()
}
binding.deleteAccountConfirm.setOnClickListener {
val password = binding.deletePassword.text?.toString()
if (!password.isNullOrEmpty()) {
deleteAccount(password)
} else {
Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show()
}
}
return MaterialAlertDialogBuilder(requireContext())
.setView(binding.root)
.setPositiveButton(R.string.deleteAccount, null)
.setNegativeButton(R.string.cancel, null)
.show()
.apply {
getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
requireDialog().hide()
val password = binding.deletePassword.text?.toString()
if (password.isNullOrEmpty()) {
Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
private fun deleteAccount(password: String) {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.CREATED) {
deleteAccount(password)
dismiss()
}
}
}
}
private fun deleteAccount(password: String) = lifecycleScope.launch {
val token = PreferenceHelper.getToken()
try {
@ -57,7 +59,7 @@ class DeleteAccountDialog : DialogFragment() {
} catch (e: Exception) {
Log.e(TAG(), e.toString())
Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show()
return@repeatOnLifecycle
return@launch
}
Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show()
@ -65,8 +67,5 @@ class DeleteAccountDialog : DialogFragment() {
InstanceSettings.INSTANCE_DIALOG_REQUEST_KEY,
bundleOf(IntentData.logoutTask to true)
)
dialog?.dismiss()
}
}
}
}

View File

@ -67,6 +67,7 @@ class DownloadDialog : DialogFragment() {
}
return MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.download)
.setView(binding.root)
.setPositiveButton(R.string.download, null)
.show()

View File

@ -1,6 +1,7 @@
package com.github.libretube.ui.dialogs
import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.util.Log
import android.util.Patterns
@ -30,7 +31,14 @@ class LoginDialog : DialogFragment() {
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val binding = DialogLoginBinding.inflate(layoutInflater)
binding.login.setOnClickListener {
return MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.login)
.setPositiveButton(R.string.login, null)
.setNegativeButton(R.string.register, null)
.setView(binding.root)
.show()
.apply {
getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener {
val email = binding.username.text?.toString()
val password = binding.password.text?.toString()
@ -40,29 +48,19 @@ class LoginDialog : DialogFragment() {
Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show()
}
}
binding.register.setOnClickListener {
getButton(DialogInterface.BUTTON_NEGATIVE).setOnClickListener {
val email = binding.username.text?.toString().orEmpty()
val password = binding.password.text?.toString().orEmpty()
if (isEmail(email)) {
MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.privacy_alert)
.setMessage(R.string.username_email)
.setNegativeButton(R.string.proceed) { _, _ ->
signIn(email, password, true)
}
.setPositiveButton(R.string.cancel, null)
.show()
showPrivacyAlertDialog(email, password)
} else if (email.isNotEmpty() && password.isNotEmpty()) {
signIn(email, password, true)
} else {
Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show()
}
}
return MaterialAlertDialogBuilder(requireContext())
.setView(binding.root)
.show()
}
}
private fun signIn(username: String, password: String, createNewAccount: Boolean = false) {
@ -109,6 +107,17 @@ class LoginDialog : DialogFragment() {
}
}
private fun showPrivacyAlertDialog(email: String, password: String) {
MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.privacy_alert)
.setMessage(R.string.username_email)
.setNegativeButton(R.string.proceed) { _, _ ->
signIn(email, password, true)
}
.setPositiveButton(R.string.cancel, null)
.show()
}
private fun isEmail(text: String): Boolean {
return Patterns.EMAIL_ADDRESS.toRegex().matches(text)
}

View File

@ -9,7 +9,6 @@ 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.github.libretube.ui.preferences.InstanceSettings
import com.google.android.material.dialog.MaterialAlertDialogBuilder
@ -17,23 +16,19 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
class LogoutDialog : DialogFragment() {
@SuppressLint("SetTextI18n")
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val binding = DialogLogoutBinding.inflate(layoutInflater)
val user = PreferenceHelper.getUsername()
binding.user.text = binding.user.text.toString() + " ($user)"
binding.logout.setOnClickListener {
return MaterialAlertDialogBuilder(requireContext())
.setTitle(R.string.logout)
.setMessage(getString(R.string.already_logged_in) + " ($user)")
.setPositiveButton(R.string.logout) { _, _ ->
Toast.makeText(context, R.string.loggedout, Toast.LENGTH_SHORT).show()
setFragmentResult(
InstanceSettings.INSTANCE_DIALOG_REQUEST_KEY,
bundleOf(IntentData.logoutTask to true)
)
dialog?.dismiss()
}
return MaterialAlertDialogBuilder(requireContext())
.setView(binding.root)
.show()
}
}

View File

@ -1,18 +0,0 @@
package com.github.libretube.ui.views
import android.content.Context
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.util.TypedValueCompat
import com.github.libretube.helpers.ThemeHelper
class AppNameTextView : AppCompatTextView {
constructor(context: Context, attributeSet: AttributeSet?) : super(context, attributeSet)
constructor(context: Context) : super(context, null)
init {
text = ThemeHelper.getStyledAppName(context)
textSize = TypedValueCompat.spToPx(10f, resources.displayMetrics)
}
}

View File

@ -6,38 +6,13 @@
android:layout_height="wrap_content"
android:orientation="vertical">
<com.github.libretube.ui.views.AppNameTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center" />
<com.github.libretube.ui.views.DropdownMenu
android:id="@+id/playlists_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="24dp"
android:layout_marginBottom="16dp"
android:layout_marginTop="8dp"
app:icon="@drawable/ic_playlist_add"
tools:ignore="RtlSymmetry" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:layout_marginEnd="16dp">
<Button
android:id="@+id/create_playlist"
style="@style/CustomDialogButton"
android:layout_marginEnd="8dp"
android:text="@string/createPlaylist" />
<Button
android:id="@+id/addToPlaylist"
style="@style/CustomDialogButton"
android:text="@string/addToPlaylist" />
</LinearLayout>
</LinearLayout>

View File

@ -2,13 +2,8 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.github.libretube.ui.views.AppNameTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center" />
android:orientation="vertical"
android:paddingTop="8dp">
<com.google.android.material.textfield.TextInputLayout
style="@style/CustomDialogTextInputLayout"
@ -48,16 +43,7 @@
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="20dp"
android:layout_marginBottom="10dp"
android:drawableStart="@drawable/ic_copy"
android:text="@string/createPlaylist" />
<Button
android:id="@+id/cancel_button"
style="@style/CustomDialogButton"
android:layout_gravity="end"
android:layout_marginTop="10dp"
android:layout_marginEnd="16dp"
android:text="@string/cancel" />
</LinearLayout>

View File

@ -2,13 +2,8 @@
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.github.libretube.ui.views.AppNameTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center" />
android:orientation="vertical"
android:paddingTop="8dp">
<com.google.android.material.textfield.TextInputLayout
style="@style/CustomDialogTextInputLayout"
@ -43,23 +38,4 @@
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="horizontal">
<Button
android:id="@+id/cancel"
style="@style/CustomDialogButton"
android:text="@string/cancel" />
<Button
android:id="@+id/addInstance"
style="@style/CustomDialogButton"
android:layout_marginEnd="16dp"
android:text="@string/addInstance" />
</LinearLayout>
</LinearLayout>

View File

@ -3,13 +3,8 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.github.libretube.ui.views.AppNameTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center" />
android:orientation="vertical"
android:paddingTop="8dp">
<com.google.android.material.textfield.TextInputLayout
style="@style/CustomDialogTextInputLayout"
@ -23,24 +18,4 @@
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="horizontal">
<Button
android:id="@+id/cancel_button"
style="@style/CustomDialogButton"
android:text="@string/cancel" />
<Button
android:id="@+id/delete_account_confirm"
style="@style/CustomDialogButton"
android:layout_marginEnd="16dp"
android:text="@string/deleteAccount" />
</LinearLayout>
</LinearLayout>

View File

@ -6,12 +6,6 @@
android:orientation="vertical"
android:paddingHorizontal="20dp">
<com.github.libretube.ui.views.AppNameTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center" />
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"

View File

@ -5,12 +5,6 @@
android:layout_height="wrap_content"
android:orientation="vertical">
<com.github.libretube.ui.views.AppNameTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center" />
<com.google.android.material.textfield.TextInputLayout
style="@style/CustomDialogTextInputLayout"
android:hint="@string/username">
@ -34,24 +28,4 @@
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="horizontal">
<Button
android:id="@+id/register"
style="@style/CustomDialogButton"
android:text="@string/register" />
<Button
android:id="@+id/login"
style="@style/CustomDialogButton"
android:layout_marginEnd="16dp"
android:text="@string/login" />
</LinearLayout>
</LinearLayout>

View File

@ -1,27 +0,0 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.github.libretube.ui.views.AppNameTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center" />
<TextView
android:id="@+id/user"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:padding="8dp"
android:text="@string/already_logged_in" />
<Button
android:id="@+id/logout"
style="@style/CustomDialogButton"
android:layout_marginEnd="16dp"
android:text="@string/logout" />
</LinearLayout>

View File

@ -43,16 +43,6 @@
<item name="android:paddingBottom">15dp</item>
</style>
<style name="CustomDialogButton" parent="@style/Widget.Material3.Button.TextButton">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">end</item>
<item name="android:layout_marginBottom">8dp</item>
<item name="android:padding">8dp</item>
</style>
<style name="CustomDialogTextInputLayout" parent="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="android:layout_width">match_parent</item>