diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/AddToPlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/AddToPlaylistDialog.kt index 94a2647d4..f8897607a 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/AddToPlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/AddToPlaylistDialog.kt @@ -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() + 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() - } - } } } } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt index 54c989736..999d6dab9 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/CreatePlaylistDialog.kt @@ -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() } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/CustomInstanceDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/CustomInstanceDialog.kt index f56248178..72ae8d28b 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/CustomInstanceDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/CustomInstanceDialog.kt @@ -18,35 +18,32 @@ class CustomInstanceDialog : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val binding = DialogCustomInstanceBinding.inflate(layoutInflater) - binding.cancel.setOnClickListener { - dismiss() - } + 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() - binding.addInstance.setOnClickListener { - val instanceName = binding.instanceName.text.toString() - val apiUrl = binding.instanceApiUrl.text.toString() - val frontendUrl = binding.instanceFrontendUrl.text.toString() - - if (instanceName.isNotEmpty() && apiUrl.isNotEmpty() && frontendUrl.isNotEmpty()) { - if (apiUrl.toHttpUrlOrNull() != null && frontendUrl.toHttpUrlOrNull() != null) { - lifecycleScope.launch { - Database.customInstanceDao() - .insert(CustomInstance(instanceName, apiUrl, frontendUrl)) - ActivityCompat.recreate(requireActivity()) - dismiss() + if (instanceName.isNotEmpty() && apiUrl.isNotEmpty() && frontendUrl.isNotEmpty()) { + if (apiUrl.toHttpUrlOrNull() != null && frontendUrl.toHttpUrlOrNull() != null) { + lifecycleScope.launch { + Database.customInstanceDao() + .insert(CustomInstance(instanceName, apiUrl, frontendUrl)) + ActivityCompat.recreate(requireActivity()) + dismiss() + } + } else { + Toast.makeText(requireContext(), R.string.invalid_url, Toast.LENGTH_SHORT) + .show() } } else { - Toast.makeText(requireContext(), R.string.invalid_url, Toast.LENGTH_SHORT) - .show() + // at least one empty input + Toast.makeText(requireContext(), R.string.empty_instance, Toast.LENGTH_SHORT).show() } - } else { - // at least one empty input - Toast.makeText(requireContext(), R.string.empty_instance, Toast.LENGTH_SHORT).show() } - } - - return MaterialAlertDialogBuilder(requireContext()) - .setView(binding.root) + .setNegativeButton(R.string.cancel, null) .show() } } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/DeleteAccountDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/DeleteAccountDialog.kt index 87d0b5fcb..eb6fe5385 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/DeleteAccountDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/DeleteAccountDialog.kt @@ -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,46 +26,46 @@ 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 + } + + lifecycleScope.launch { + deleteAccount(password) + dismiss() + } + } + } } - private fun deleteAccount(password: String) { - lifecycleScope.launch { - repeatOnLifecycle(Lifecycle.State.CREATED) { - val token = PreferenceHelper.getToken() + private fun deleteAccount(password: String) = lifecycleScope.launch { + val token = PreferenceHelper.getToken() - try { - withContext(Dispatchers.IO) { - RetrofitInstance.authApi.deleteAccount(token, DeleteUserRequest(password)) - } - } catch (e: Exception) { - Log.e(TAG(), e.toString()) - Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show() - return@repeatOnLifecycle - } - Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() - - setFragmentResult( - InstanceSettings.INSTANCE_DIALOG_REQUEST_KEY, - bundleOf(IntentData.logoutTask to true) - ) - dialog?.dismiss() + try { + withContext(Dispatchers.IO) { + RetrofitInstance.authApi.deleteAccount(token, DeleteUserRequest(password)) } + } catch (e: Exception) { + Log.e(TAG(), e.toString()) + Toast.makeText(context, R.string.unknown_error, Toast.LENGTH_SHORT).show() + return@launch } + Toast.makeText(context, R.string.success, Toast.LENGTH_SHORT).show() + + setFragmentResult( + InstanceSettings.INSTANCE_DIALOG_REQUEST_KEY, + bundleOf(IntentData.logoutTask to true) + ) } } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/DownloadDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/DownloadDialog.kt index 98b98229a..27868973d 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/DownloadDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/DownloadDialog.kt @@ -67,6 +67,7 @@ class DownloadDialog : DialogFragment() { } return MaterialAlertDialogBuilder(requireContext()) + .setTitle(R.string.download) .setView(binding.root) .setPositiveButton(R.string.download, null) .show() diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/LoginDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/LoginDialog.kt index 117d07b38..99a6a917c 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/LoginDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/LoginDialog.kt @@ -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,39 +31,36 @@ class LoginDialog : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { val binding = DialogLoginBinding.inflate(layoutInflater) - binding.login.setOnClickListener { - val email = binding.username.text?.toString() - val password = binding.password.text?.toString() - - if (!email.isNullOrEmpty() && !password.isNullOrEmpty()) { - signIn(email, password) - } else { - Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show() - } - } - binding.register.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() - } else if (email.isNotEmpty() && password.isNotEmpty()) { - signIn(email, password, true) - } else { - Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show() - } - } - 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() + + if (!email.isNullOrEmpty() && !password.isNullOrEmpty()) { + signIn(email, password) + } else { + Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).show() + } + } + getButton(DialogInterface.BUTTON_NEGATIVE).setOnClickListener { + val email = binding.username.text?.toString().orEmpty() + val password = binding.password.text?.toString().orEmpty() + + if (isEmail(email)) { + showPrivacyAlertDialog(email, password) + } else if (email.isNotEmpty() && password.isNotEmpty()) { + signIn(email, password, true) + } else { + Toast.makeText(context, R.string.empty, Toast.LENGTH_SHORT).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) } diff --git a/app/src/main/java/com/github/libretube/ui/dialogs/LogoutDialog.kt b/app/src/main/java/com/github/libretube/ui/dialogs/LogoutDialog.kt index 2d0814a6b..9f5bcb0f8 100644 --- a/app/src/main/java/com/github/libretube/ui/dialogs/LogoutDialog.kt +++ b/app/src/main/java/com/github/libretube/ui/dialogs/LogoutDialog.kt @@ -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 { - 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) + .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) + ) + } .show() } } diff --git a/app/src/main/java/com/github/libretube/ui/views/AppNameTextView.kt b/app/src/main/java/com/github/libretube/ui/views/AppNameTextView.kt deleted file mode 100644 index 0ca7946f5..000000000 --- a/app/src/main/java/com/github/libretube/ui/views/AppNameTextView.kt +++ /dev/null @@ -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) - } -} diff --git a/app/src/main/res/layout/dialog_add_to_playlist.xml b/app/src/main/res/layout/dialog_add_to_playlist.xml index b4bd0004c..ca9075ad2 100644 --- a/app/src/main/res/layout/dialog_add_to_playlist.xml +++ b/app/src/main/res/layout/dialog_add_to_playlist.xml @@ -6,38 +6,13 @@ android:layout_height="wrap_content" android:orientation="vertical"> - - - - -