This commit is contained in:
andrew 2023-05-29 18:02:20 +09:30
parent 5519f17775
commit 03aa19aae4
3 changed files with 113 additions and 24 deletions

View File

@ -0,0 +1,86 @@
package org.thoughtcrime.securesms
import android.content.Context
import android.view.View
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import android.widget.Button
import android.widget.LinearLayout
import android.widget.LinearLayout.VERTICAL
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.annotation.StyleRes
import androidx.appcompat.app.AlertDialog
import androidx.core.view.setMargins
import androidx.core.view.updateMargins
import network.loki.messenger.R
import org.thoughtcrime.securesms.util.toPx
class SessionDialogBuilder(val context: Context) {
private val dialog: AlertDialog = AlertDialog.Builder(context).create()
private val root = LinearLayout(context).apply { orientation = VERTICAL }
.also(dialog::setView)
fun title(@StringRes id: Int) {
TextView(context, null, 0, R.style.TextAppearance_AppCompat_Title)
.apply { textAlignment = View.TEXT_ALIGNMENT_CENTER }
.apply { setText(id) }
.apply { layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)
.apply { setMargins(toPx(20, resources)) }
}.let(root::addView)
}
fun text(@StringRes id: Int, style: Int = 0) {
TextView(context, null, 0, style)
.apply { textAlignment = View.TEXT_ALIGNMENT_CENTER }
.apply { setText(id) }
.apply { layoutParams = LinearLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT)
.apply { toPx(40, resources).let { updateMargins(it, 0, it, 0) } }
}.let(root::addView)
}
fun buttons(build: ButtonsBuilder.() -> Unit) {
ButtonsBuilder(context, dialog).build(build).let(root::addView)
}
fun show(): AlertDialog = dialog.apply { show() }
}
class ButtonsBuilder(val context: Context, val dialog: AlertDialog) {
val root = LinearLayout(context)
fun destructiveButton(@StringRes text: Int, @StringRes contentDescription: Int, listener: () -> Unit = {}) {
button(text, contentDescription, R.style.Widget_Session_Button_Dialog_DestructiveText, listener)
}
fun cancelButton() = button(android.R.string.cancel)
fun button(
@StringRes text: Int,
@StringRes contentDescriptionRes: Int = 0,
@StyleRes style: Int = R.style.Widget_Session_Button_Dialog_UnimportantText,
listener: (() -> Unit) = {}) {
Button(context, null, 0, style)
.apply { setText(text) }
.apply { setOnClickListener {
listener.invoke()
dialog.dismiss()
contentDescription = resources.getString(contentDescriptionRes)
} }
.apply {
layoutParams = LinearLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT, 1f)
.apply { setMargins(toPx(20, resources)) }
}
.let(root::addView)
}
internal fun build(build: ButtonsBuilder.() -> Unit): LinearLayout {
build()
return root
}
}
fun Context.sessionDialog(build: SessionDialogBuilder.() -> Unit): AlertDialog =
SessionDialogBuilder(this).apply { build() }.show()

View File

@ -21,7 +21,6 @@ import android.widget.Toast
import androidx.activity.viewModels import androidx.activity.viewModels
import androidx.annotation.DimenRes import androidx.annotation.DimenRes
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.core.view.isGone
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.lifecycle.Observer import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider import androidx.lifecycle.ViewModelProvider
@ -113,6 +112,7 @@ import org.thoughtcrime.securesms.mms.*
import org.thoughtcrime.securesms.permissions.Permissions import org.thoughtcrime.securesms.permissions.Permissions
import org.thoughtcrime.securesms.reactions.ReactionsDialogFragment import org.thoughtcrime.securesms.reactions.ReactionsDialogFragment
import org.thoughtcrime.securesms.reactions.any.ReactWithAnyEmojiDialogFragment import org.thoughtcrime.securesms.reactions.any.ReactWithAnyEmojiDialogFragment
import org.thoughtcrime.securesms.sessionDialog
import org.thoughtcrime.securesms.util.* import org.thoughtcrime.securesms.util.*
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import java.util.* import java.util.*
@ -962,21 +962,20 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe
} }
override fun block(deleteThread: Boolean) { override fun block(deleteThread: Boolean) {
val title = R.string.RecipientPreferenceActivity_block_this_contact_question sessionDialog {
val message = R.string.RecipientPreferenceActivity_you_will_no_longer_receive_messages_and_calls_from_this_contact title(R.string.RecipientPreferenceActivity_block_this_contact_question)
val dialog = AlertDialog.Builder(this) text(R.string.RecipientPreferenceActivity_you_will_no_longer_receive_messages_and_calls_from_this_contact)
.setTitle(title) buttons {
.setMessage(message) destructiveButton(R.string.RecipientPreferenceActivity_block, R.string.AccessibilityId_block_confirm) {
.setNegativeButton(android.R.string.cancel, null) viewModel.block()
.setPositiveButton(R.string.RecipientPreferenceActivity_block) { _, _ -> if (deleteThread) {
viewModel.block() viewModel.deleteThread()
if (deleteThread) { finish()
viewModel.deleteThread() }
finish()
} }
}.show() cancelButton()
val button = dialog.getButton(DialogInterface.BUTTON_POSITIVE) }
button.setContentDescription("Confirm block") }
} }
override fun copySessionID(sessionId: String) { override fun copySessionID(sessionId: String) {
@ -1016,15 +1015,17 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe
} }
override fun unblock() { override fun unblock() {
val title = R.string.ConversationActivity_unblock_this_contact_question sessionDialog {
val message = R.string.ConversationActivity_you_will_once_again_be_able_to_receive_messages_and_calls_from_this_contact title(R.string.ConversationActivity_unblock_this_contact_question)
AlertDialog.Builder(this) text(R.string.ConversationActivity_you_will_once_again_be_able_to_receive_messages_and_calls_from_this_contact)
.setTitle(title) buttons {
.setMessage(message) destructiveButton(
.setNegativeButton(android.R.string.cancel, null) R.string.ConversationActivity_unblock,
.setPositiveButton(R.string.ConversationActivity_unblock) { _, _ -> R.string.AccessibilityId_block_confirm
viewModel.unblock() ) { viewModel.unblock() }
}.show() cancelButton()
}
}
} }
// `position` is the adapter position; not the visual position // `position` is the adapter position; not the visual position

View File

@ -71,6 +71,7 @@
<!-- TODO These button styles require proper background selectors for up/down visual state. --> <!-- TODO These button styles require proper background selectors for up/down visual state. -->
<style name="Widget.Session.Button.Common" parent=""> <style name="Widget.Session.Button.Common" parent="">
<item name="android:gravity">center</item>
<item name="android:textAllCaps">false</item> <item name="android:textAllCaps">false</item>
<item name="android:textSize">@dimen/medium_font_size</item> <item name="android:textSize">@dimen/medium_font_size</item>
<item name="android:fontFamily">sans-serif-medium</item> <item name="android:fontFamily">sans-serif-medium</item>
@ -117,6 +118,7 @@
</style> </style>
<style name="Widget.Session.Button.Dialog" parent=""> <style name="Widget.Session.Button.Dialog" parent="">
<item name="android:gravity">center</item>
<item name="android:textAllCaps">false</item> <item name="android:textAllCaps">false</item>
<item name="android:textSize">@dimen/small_font_size</item> <item name="android:textSize">@dimen/small_font_size</item>
<item name="android:textColor">?android:textColorPrimary</item> <item name="android:textColor">?android:textColorPrimary</item>