From 623cfde8b17630c730d30ce6af8b4ccca6f58905 Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 31 May 2023 13:07:28 +0930 Subject: [PATCH] Utilise dialog dsl in CallToggleListener --- .../preferences/CallToggleListener.kt | 39 ++++++++----------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/preferences/CallToggleListener.kt b/app/src/main/java/org/thoughtcrime/securesms/preferences/CallToggleListener.kt index de5f812fd0..a8042ad3b4 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/preferences/CallToggleListener.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/preferences/CallToggleListener.kt @@ -1,21 +1,33 @@ package org.thoughtcrime.securesms.preferences import android.Manifest -import android.app.AlertDialog -import android.content.DialogInterface -import androidx.appcompat.view.ContextThemeWrapper import androidx.fragment.app.Fragment import androidx.preference.Preference import network.loki.messenger.R import org.session.libsession.utilities.TextSecurePreferences import org.session.libsession.utilities.TextSecurePreferences.Companion.setBooleanPreference import org.thoughtcrime.securesms.permissions.Permissions +import org.thoughtcrime.securesms.sessionDialog internal class CallToggleListener( private val context: Fragment, private val setCallback: Function1 ) : Preference.OnPreferenceChangeListener { + override fun onPreferenceChange(preference: Preference, newValue: Any): Boolean { + if (newValue == false) return true + + // check if we've shown the info dialog and check for microphone permissions + context.sessionDialog { + title(R.string.dialog_voice_video_title) + text(R.string.dialog_voice_video_message) + button(R.string.dialog_link_preview_enable_button_title, R.string.AccessibilityId_enable) { requestMicrophonePermission() } + cancelButton() + } + + return false + } + private fun requestMicrophonePermission() { Permissions.with(context) .request(Manifest.permission.RECORD_AUDIO) @@ -30,23 +42,4 @@ internal class CallToggleListener( .onAnyDenied { setCallback(false) } .execute() } - - override fun onPreferenceChange(preference: Preference, newValue: Any): Boolean { - if (!(newValue as Boolean)) return true - - // check if we've shown the info dialog and check for microphone permissions - val dialog = AlertDialog.Builder( - ContextThemeWrapper( - context.requireContext(), R.style.ThemeOverlay_Session_AlertDialog - ) - ) - .setTitle(R.string.dialog_voice_video_title) - .setMessage(R.string.dialog_voice_video_message) - .setPositiveButton(R.string.dialog_link_preview_enable_button_title) { d: DialogInterface?, w: Int -> requestMicrophonePermission() } - .setNegativeButton(R.string.cancel) { d: DialogInterface?, w: Int -> } - .show() - val positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE) - positiveButton.contentDescription = "Enable" - return false - } -} \ No newline at end of file +}