Utilise dialog dsl in CallToggleListener

This commit is contained in:
andrew 2023-05-31 13:07:28 +09:30
parent 391735dc28
commit 623cfde8b1
1 changed files with 16 additions and 23 deletions

View File

@ -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<Boolean?, Void>
) : 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
}
}
}