From 75ae26fa4755530794267041de1038f3302ec306 Mon Sep 17 00:00:00 2001 From: Matthieu <24-artectrex@users.noreply.shinice.net> Date: Mon, 28 Nov 2022 12:30:30 +0100 Subject: [PATCH] Material dialogs --- .../java/org/pixeldroid/app/LoginActivity.kt | 17 ++++++++--------- .../app/postCreation/PostCreationFragment.kt | 15 ++++++++------- .../app/postCreation/PostSubmissionFragment.kt | 12 ++++++------ .../app/postCreation/camera/CameraFragment.kt | 13 +++++++++---- .../pixeldroid/app/posts/StatusViewHolder.kt | 17 +++++++---------- .../app/profile/CollectionActivity.kt | 4 ++-- .../app/profile/EditProfileActivity.kt | 4 ++-- .../pixeldroid/app/profile/ProfileActivity.kt | 4 ++-- .../pixeldroid/app/settings/SettingsActivity.kt | 4 ++-- 9 files changed, 46 insertions(+), 44 deletions(-) diff --git a/app/src/main/java/org/pixeldroid/app/LoginActivity.kt b/app/src/main/java/org/pixeldroid/app/LoginActivity.kt index 70da2bb4..b34f8098 100644 --- a/app/src/main/java/org/pixeldroid/app/LoginActivity.kt +++ b/app/src/main/java/org/pixeldroid/app/LoginActivity.kt @@ -9,6 +9,7 @@ import android.os.Bundle import android.view.View import android.view.inputmethod.InputMethodManager import androidx.lifecycle.lifecycleScope +import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.gson.Gson import kotlinx.coroutines.Deferred import kotlinx.coroutines.async @@ -106,13 +107,11 @@ class LoginActivity : BaseThemedWithoutBarActivity() { private fun whatsAnInstance() { - val builder = AlertDialog.Builder(this) - builder.apply { - setView(layoutInflater.inflate(R.layout.whats_an_instance_explanation, null)) - setPositiveButton(android.R.string.ok) { _, _ -> } - } - // Create the AlertDialog - builder.show() + MaterialAlertDialogBuilder(this) + .setView(layoutInflater.inflate(R.layout.whats_an_instance_explanation, null)) + .setPositiveButton(android.R.string.ok) { _, _ -> } + // Create the AlertDialog + .show() } private fun hideKeyboard() { @@ -202,7 +201,7 @@ class LoginActivity : BaseThemedWithoutBarActivity() { if (!nodeInfo.software?.name.orEmpty().contains("pixelfed")) { - AlertDialog.Builder(this@LoginActivity).apply { + MaterialAlertDialogBuilder(this@LoginActivity).apply { setMessage(R.string.instance_not_pixelfed_warning) setPositiveButton(R.string.instance_not_pixelfed_continue) { _, _ -> promptOAuth(normalizedDomain, clientId) @@ -213,7 +212,7 @@ class LoginActivity : BaseThemedWithoutBarActivity() { } }.show() } else if (nodeInfo.metadata?.config?.features?.mobile_apis != true) { - AlertDialog.Builder(this@LoginActivity).apply { + MaterialAlertDialogBuilder(this@LoginActivity).apply { setMessage(R.string.api_not_enabled_dialog) setNegativeButton(android.R.string.ok) { _, _ -> loadingAnimation(false) diff --git a/app/src/main/java/org/pixeldroid/app/postCreation/PostCreationFragment.kt b/app/src/main/java/org/pixeldroid/app/postCreation/PostCreationFragment.kt index 4d24cdbc..9c7bc3f6 100644 --- a/app/src/main/java/org/pixeldroid/app/postCreation/PostCreationFragment.kt +++ b/app/src/main/java/org/pixeldroid/app/postCreation/PostCreationFragment.kt @@ -1,13 +1,14 @@ package org.pixeldroid.app.postCreation import android.app.Activity -import android.app.AlertDialog import android.content.ContentResolver import android.content.ContentValues import android.content.Intent import android.media.MediaScannerConnection import android.net.Uri -import android.os.* +import android.os.Build +import android.os.Bundle +import android.os.Environment import android.provider.MediaStore import android.util.Log import android.view.LayoutInflater @@ -26,6 +27,7 @@ import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle import androidx.navigation.fragment.findNavController import androidx.recyclerview.widget.RecyclerView +import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.snackbar.Snackbar import kotlinx.coroutines.launch import org.pixeldroid.app.R @@ -42,7 +44,7 @@ import org.pixeldroid.media_editor.photoEdit.VideoEditActivity import java.io.File import java.io.OutputStream import java.text.SimpleDateFormat -import java.util.* +import java.util.Locale class PostCreationFragment : BaseFragment() { @@ -103,7 +105,7 @@ class PostCreationFragment : BaseFragment() { viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { model.uiState.collect { uiState -> uiState.userMessage?.let { - AlertDialog.Builder(binding.root.context).apply { + MaterialAlertDialogBuilder(binding.root.context).apply { setMessage(it) setNegativeButton(android.R.string.ok) { _, _ -> } }.show() @@ -173,8 +175,7 @@ class PostCreationFragment : BaseFragment() { override fun handleOnBackPressed() { val redraft = requireActivity().intent.getBooleanExtra(PostCreationActivity.POST_REDRAFT, false) if (redraft) { - val builder = AlertDialog.Builder(binding.root.context) - builder.apply { + MaterialAlertDialogBuilder(binding.root.context).apply { setMessage(R.string.redraft_dialog_cancel) setPositiveButton(android.R.string.ok) { _, _ -> requireActivity().finish() @@ -275,7 +276,7 @@ class PostCreationFragment : BaseFragment() { private fun validatePost(): Boolean { if (model.getPhotoData().value?.all { !it.video || it.videoEncodeComplete } == false) { - AlertDialog.Builder(requireActivity()).apply { + MaterialAlertDialogBuilder(requireActivity()).apply { setMessage(R.string.still_encoding) setNegativeButton(android.R.string.ok) { _, _ -> } }.show() diff --git a/app/src/main/java/org/pixeldroid/app/postCreation/PostSubmissionFragment.kt b/app/src/main/java/org/pixeldroid/app/postCreation/PostSubmissionFragment.kt index bda9bac7..4ce76ab0 100644 --- a/app/src/main/java/org/pixeldroid/app/postCreation/PostSubmissionFragment.kt +++ b/app/src/main/java/org/pixeldroid/app/postCreation/PostSubmissionFragment.kt @@ -1,6 +1,5 @@ package org.pixeldroid.app.postCreation -import android.app.AlertDialog import android.os.Bundle import android.view.LayoutInflater import android.view.Menu @@ -17,6 +16,7 @@ import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle import androidx.navigation.fragment.findNavController import androidx.navigation.ui.setupWithNavController +import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.launch import org.pixeldroid.app.R import org.pixeldroid.app.databinding.FragmentPostSubmissionBinding @@ -81,10 +81,10 @@ class PostSubmissionFragment : BaseFragment() { viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { model.uiState.collect { uiState -> uiState.userMessage?.let { - AlertDialog.Builder(binding.root.context).apply { - setMessage(it) - setNegativeButton(android.R.string.ok) { _, _ -> } - }.show() + MaterialAlertDialogBuilder(binding.root.context) + .setMessage(it) + .setNegativeButton(android.R.string.ok) { _, _ -> } + .show() // Notify the ViewModel the message is displayed model.userMessageShown() @@ -147,7 +147,7 @@ class PostSubmissionFragment : BaseFragment() { // Handle the menu selection return when (menuItem.itemId) { R.id.action_switch_accounts -> { - AlertDialog.Builder(requireActivity()).apply { + MaterialAlertDialogBuilder(requireActivity()).apply { setIcon(R.drawable.switch_account) setTitle(R.string.switch_accounts) setSingleChoiceItems(accounts.map { it.username + " (${it.fullHandle})" }.toTypedArray(), selectedAccount) { dialog, which -> diff --git a/app/src/main/java/org/pixeldroid/app/postCreation/camera/CameraFragment.kt b/app/src/main/java/org/pixeldroid/app/postCreation/camera/CameraFragment.kt index dc368607..55b79c40 100644 --- a/app/src/main/java/org/pixeldroid/app/postCreation/camera/CameraFragment.kt +++ b/app/src/main/java/org/pixeldroid/app/postCreation/camera/CameraFragment.kt @@ -18,9 +18,13 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.activity.result.contract.ActivityResultContracts -import androidx.appcompat.app.AlertDialog -import androidx.camera.core.* +import androidx.camera.core.AspectRatio +import androidx.camera.core.Camera +import androidx.camera.core.CameraSelector +import androidx.camera.core.ImageCapture import androidx.camera.core.ImageCapture.Metadata +import androidx.camera.core.ImageCaptureException +import androidx.camera.core.Preview import androidx.camera.lifecycle.ProcessCameraProvider import androidx.constraintlayout.widget.ConstraintLayout import androidx.core.content.ContextCompat @@ -29,6 +33,7 @@ import androidx.core.view.setPadding import androidx.lifecycle.lifecycleScope import com.bumptech.glide.Glide import com.bumptech.glide.request.RequestOptions +import com.google.android.material.dialog.MaterialAlertDialogBuilder import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import org.pixeldroid.app.R @@ -264,7 +269,7 @@ class CameraFragment : BaseFragment() { if (isGranted) { updateGalleryThumbnail() } else if(!filePermissionDialogLaunched){ - AlertDialog.Builder(requireContext()) + MaterialAlertDialogBuilder(requireContext()) .setMessage(getString(R.string.no_storage_permission)) .setPositiveButton(android.R.string.ok) { _, _ ->}.show() filePermissionDialogLaunched = true @@ -346,7 +351,7 @@ class CameraFragment : BaseFragment() { if (isGranted) { bindCameraUseCases() } else { - AlertDialog.Builder(requireContext()) + MaterialAlertDialogBuilder(requireContext()) .setMessage(R.string.no_camera_permission) .setPositiveButton(android.R.string.ok) { _, _ ->}.show() } diff --git a/app/src/main/java/org/pixeldroid/app/posts/StatusViewHolder.kt b/app/src/main/java/org/pixeldroid/app/posts/StatusViewHolder.kt index cd77cd61..af29b15b 100644 --- a/app/src/main/java/org/pixeldroid/app/posts/StatusViewHolder.kt +++ b/app/src/main/java/org/pixeldroid/app/posts/StatusViewHolder.kt @@ -2,7 +2,6 @@ package org.pixeldroid.app.posts import android.Manifest import android.app.Activity -import android.app.AlertDialog import android.content.ClipData import android.content.Intent import android.graphics.Typeface @@ -33,6 +32,7 @@ import com.bumptech.glide.request.target.CustomViewTarget import com.bumptech.glide.request.transition.Transition import com.davemorrissey.labs.subscaleview.ImageSource import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView +import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.snackbar.Snackbar import com.karumi.dexter.Dexter import com.karumi.dexter.listener.PermissionDeniedResponse @@ -444,24 +444,21 @@ class StatusViewHolder(val binding: PostFragmentBinding) : RecyclerView.ViewHold true } R.id.post_more_menu_delete -> { - val builder = AlertDialog.Builder(binding.root.context) - builder.apply { - setMessage(R.string.delete_dialog) - setPositiveButton(android.R.string.ok) { _, _ -> + MaterialAlertDialogBuilder(binding.root.context) + .setMessage(R.string.delete_dialog) + .setPositiveButton(android.R.string.ok) { _, _ -> lifecycleScope.launch { deletePost(apiHolder.api ?: apiHolder.setToCurrentUser(), db) } } - setNegativeButton(android.R.string.cancel) { _, _ -> } - show() - } + .setNegativeButton(android.R.string.cancel) { _, _ -> } + .show() true } R.id.post_more_menu_redraft -> { - val builder = AlertDialog.Builder(binding.root.context) - builder.apply { + MaterialAlertDialogBuilder(binding.root.context).apply { setMessage(R.string.redraft_dialog_launch) setPositiveButton(android.R.string.ok) { _, _ -> diff --git a/app/src/main/java/org/pixeldroid/app/profile/CollectionActivity.kt b/app/src/main/java/org/pixeldroid/app/profile/CollectionActivity.kt index 24bffe56..a244cdcd 100644 --- a/app/src/main/java/org/pixeldroid/app/profile/CollectionActivity.kt +++ b/app/src/main/java/org/pixeldroid/app/profile/CollectionActivity.kt @@ -1,12 +1,12 @@ package org.pixeldroid.app.profile -import android.app.AlertDialog import android.content.Intent import android.os.Bundle import android.view.Menu import android.view.MenuInflater import android.view.MenuItem import androidx.lifecycle.lifecycleScope +import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.snackbar.Snackbar import kotlinx.coroutines.launch import org.pixeldroid.app.R @@ -96,7 +96,7 @@ class CollectionActivity : BaseThemedWithBarActivity() { override fun onOptionsItemSelected(item: MenuItem): Boolean { return when (item.itemId) { R.id.delete_collection -> { - AlertDialog.Builder(this).apply { + MaterialAlertDialogBuilder(this).apply { setMessage(R.string.delete_collection_warning) setPositiveButton(android.R.string.ok) { _, _ -> // Delete collection diff --git a/app/src/main/java/org/pixeldroid/app/profile/EditProfileActivity.kt b/app/src/main/java/org/pixeldroid/app/profile/EditProfileActivity.kt index 7709f7ab..48cb7b95 100644 --- a/app/src/main/java/org/pixeldroid/app/profile/EditProfileActivity.kt +++ b/app/src/main/java/org/pixeldroid/app/profile/EditProfileActivity.kt @@ -1,7 +1,6 @@ package org.pixeldroid.app.profile import android.app.Activity -import android.app.AlertDialog import android.content.Intent import android.os.Bundle import android.view.Menu @@ -15,6 +14,7 @@ import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle import com.bumptech.glide.Glide import com.bumptech.glide.request.RequestOptions +import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.snackbar.Snackbar import kotlinx.coroutines.launch import org.pixeldroid.app.R @@ -135,7 +135,7 @@ class EditProfileActivity : BaseThemedWithBarActivity() { @Deprecated("Deprecated in Java") override fun onBackPressed() { if(model.madeChanges()){ - AlertDialog.Builder(binding.root.context).apply { + MaterialAlertDialogBuilder(binding.root.context).apply { setMessage(getString(R.string.profile_save_changes)) setNegativeButton(android.R.string.cancel) { _, _ -> } setPositiveButton(android.R.string.ok) { _, _ -> super.onBackPressed()} diff --git a/app/src/main/java/org/pixeldroid/app/profile/ProfileActivity.kt b/app/src/main/java/org/pixeldroid/app/profile/ProfileActivity.kt index 5eaa8dd7..042e33ce 100644 --- a/app/src/main/java/org/pixeldroid/app/profile/ProfileActivity.kt +++ b/app/src/main/java/org/pixeldroid/app/profile/ProfileActivity.kt @@ -6,11 +6,11 @@ import android.text.method.LinkMovementMethod import android.util.Log import android.view.View import android.widget.Toast -import androidx.appcompat.app.AlertDialog import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import androidx.lifecycle.lifecycleScope import androidx.viewpager2.adapter.FragmentStateAdapter +import com.google.android.material.dialog.MaterialAlertDialogBuilder import com.google.android.material.tabs.TabLayout import com.google.android.material.tabs.TabLayoutMediator import kotlinx.coroutines.launch @@ -331,7 +331,7 @@ class ProfileActivity : BaseThemedWithBarActivity() { setOnClickListener { if(account.locked == true && requested){ - AlertDialog.Builder(context) + MaterialAlertDialogBuilder(context) .setMessage(R.string.dialog_message_cancel_follow_request) .setPositiveButton(android.R.string.ok) { _, _ -> unfollow() diff --git a/app/src/main/java/org/pixeldroid/app/settings/SettingsActivity.kt b/app/src/main/java/org/pixeldroid/app/settings/SettingsActivity.kt index a413384e..d35b3de3 100644 --- a/app/src/main/java/org/pixeldroid/app/settings/SettingsActivity.kt +++ b/app/src/main/java/org/pixeldroid/app/settings/SettingsActivity.kt @@ -6,7 +6,6 @@ import android.content.SharedPreferences import android.content.res.XmlResourceParser import android.os.Build import android.os.Bundle -import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatDelegate import androidx.core.os.LocaleListCompat import androidx.fragment.app.DialogFragment @@ -14,6 +13,7 @@ import androidx.preference.ListPreference import androidx.preference.Preference import androidx.preference.PreferenceFragmentCompat import androidx.preference.PreferenceManager +import com.google.android.material.dialog.MaterialAlertDialogBuilder import org.pixeldroid.app.MainActivity import org.pixeldroid.app.R import org.pixeldroid.app.utils.BaseThemedWithBarActivity @@ -150,7 +150,7 @@ class LanguageSettingFragment : DialogFragment() { else index + 1 } - return AlertDialog.Builder(requireContext()).apply { + return MaterialAlertDialogBuilder(requireContext()).apply { setIcon(R.drawable.translate_black_24dp) setTitle(R.string.language) setSingleChoiceItems((mutableListOf(getString(R.string.default_system)) + list.map {