From c5f821add0585bfdd5ac4a76bb5723c8ffa56f16 Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 31 May 2023 21:58:38 +0930 Subject: [PATCH] Utilise dialog dsl in ExpirationDialog --- .../securesms/ExpirationDialog.java | 88 ------------------- .../securesms/ExpirationDialog.kt | 53 +++++++++++ .../securesms/SessionDialogBuilder.kt | 9 +- .../conversation/v2/ConversationActivityV2.kt | 11 +-- 4 files changed, 62 insertions(+), 99 deletions(-) delete mode 100644 app/src/main/java/org/thoughtcrime/securesms/ExpirationDialog.java create mode 100644 app/src/main/java/org/thoughtcrime/securesms/ExpirationDialog.kt diff --git a/app/src/main/java/org/thoughtcrime/securesms/ExpirationDialog.java b/app/src/main/java/org/thoughtcrime/securesms/ExpirationDialog.java deleted file mode 100644 index 469629ed3f..0000000000 --- a/app/src/main/java/org/thoughtcrime/securesms/ExpirationDialog.java +++ /dev/null @@ -1,88 +0,0 @@ -package org.thoughtcrime.securesms; - -import android.content.Context; -import androidx.annotation.NonNull; -import androidx.appcompat.app.AlertDialog; -import android.view.LayoutInflater; -import android.view.View; -import android.widget.TextView; - -import org.session.libsession.utilities.ExpirationUtil; - -import cn.carbswang.android.numberpickerview.library.NumberPickerView; -import network.loki.messenger.R; - -public class ExpirationDialog extends AlertDialog { - - protected ExpirationDialog(Context context) { - super(context); - } - - protected ExpirationDialog(Context context, int theme) { - super(context, theme); - } - - protected ExpirationDialog(Context context, boolean cancelable, OnCancelListener cancelListener) { - super(context, cancelable, cancelListener); - } - - public static void show(final Context context, - final int currentExpiration, - final @NonNull OnClickListener listener) - { - final View view = createNumberPickerView(context, currentExpiration); - - AlertDialog.Builder builder = new AlertDialog.Builder(context); - builder.setTitle(context.getString(R.string.ExpirationDialog_disappearing_messages)); - builder.setView(view); - builder.setPositiveButton(android.R.string.ok, (dialog, which) -> { - int selected = ((NumberPickerView)view.findViewById(R.id.expiration_number_picker)).getValue(); - listener.onClick(context.getResources().getIntArray(R.array.expiration_times)[selected]); - }); - builder.setNegativeButton(android.R.string.cancel, null); - builder.show(); - } - - private static View createNumberPickerView(final Context context, final int currentExpiration) { - final LayoutInflater inflater = LayoutInflater.from(context); - final View view = inflater.inflate(R.layout.expiration_dialog, null); - final NumberPickerView numberPickerView = view.findViewById(R.id.expiration_number_picker); - final TextView textView = view.findViewById(R.id.expiration_details); - final int[] expirationTimes = context.getResources().getIntArray(R.array.expiration_times); - final String[] expirationDisplayValues = new String[expirationTimes.length]; - - int selectedIndex = expirationTimes.length - 1; - - for (int i=0;i= expirationTimes[i]) && - (i == expirationTimes.length -1 || currentExpiration < expirationTimes[i+1])) { - selectedIndex = i; - } - } - - numberPickerView.setDisplayedValues(expirationDisplayValues); - numberPickerView.setMinValue(0); - numberPickerView.setMaxValue(expirationTimes.length-1); - - NumberPickerView.OnValueChangeListener listener = (picker, oldVal, newVal) -> { - if (newVal == 0) { - textView.setText(R.string.ExpirationDialog_your_messages_will_not_expire); - } else { - textView.setText(context.getString(R.string.ExpirationDialog_your_messages_will_disappear_s_after_they_have_been_seen, picker.getDisplayedValues()[newVal])); - } - }; - - numberPickerView.setOnValueChangedListener(listener); - numberPickerView.setValue(selectedIndex); - listener.onValueChange(numberPickerView, selectedIndex, selectedIndex); - - return view; - } - - public interface OnClickListener { - public void onClick(int expirationTime); - } - -} diff --git a/app/src/main/java/org/thoughtcrime/securesms/ExpirationDialog.kt b/app/src/main/java/org/thoughtcrime/securesms/ExpirationDialog.kt new file mode 100644 index 0000000000..c12f9c46eb --- /dev/null +++ b/app/src/main/java/org/thoughtcrime/securesms/ExpirationDialog.kt @@ -0,0 +1,53 @@ +package org.thoughtcrime.securesms + +import android.content.Context +import android.view.LayoutInflater +import android.widget.TextView +import androidx.appcompat.app.AlertDialog +import cn.carbswang.android.numberpickerview.library.NumberPickerView +import network.loki.messenger.R +import org.session.libsession.utilities.ExpirationUtil + +fun Context.showExpirationDialog( + currentExpiration: Int, + onExpirationTime: (Int) -> Unit +): AlertDialog { + val view = LayoutInflater.from(this).inflate(R.layout.expiration_dialog, null) + val numberPickerView = view.findViewById(R.id.expiration_number_picker) + + fun updateText(index: Int) { + view.findViewById(R.id.expiration_details).text = when (index) { + 0 -> getString(R.string.ExpirationDialog_your_messages_will_not_expire) + else -> getString( + R.string.ExpirationDialog_your_messages_will_disappear_s_after_they_have_been_seen, + numberPickerView.displayedValues[index] + ) + } + } + + val expirationTimes = resources.getIntArray(R.array.expiration_times) + val expirationDisplayValues = expirationTimes + .map { ExpirationUtil.getExpirationDisplayValue(this, it) } + .toTypedArray() + + val selectedIndex = expirationTimes.withIndex() + .firstOrNull { it.value >= currentExpiration }?.index + ?: (expirationTimes.size - 1) + + numberPickerView.apply { + displayedValues = expirationDisplayValues + minValue = 0 + maxValue = expirationTimes.size - 1 + setOnValueChangedListener { _, _, index -> updateText(index) } + value = selectedIndex + } + + updateText(selectedIndex) + + return sessionDialog { + title(getString(R.string.ExpirationDialog_disappearing_messages)) + view(view) + okButton { onExpirationTime(numberPickerView.let { expirationTimes[it.value] }) } + cancelButton() + } +} diff --git a/app/src/main/java/org/thoughtcrime/securesms/SessionDialogBuilder.kt b/app/src/main/java/org/thoughtcrime/securesms/SessionDialogBuilder.kt index 10d86e4a9f..e76591a9ec 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/SessionDialogBuilder.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/SessionDialogBuilder.kt @@ -29,8 +29,8 @@ annotation class DialogDsl @DialogDsl class SessionDialogBuilder(val context: Context) { - val dp20 = toPx(20, context.resources) - val dp40 = toPx(40, context.resources) + private val dp20 = toPx(20, context.resources) + private val dp40 = toPx(40, context.resources) private val dialogBuilder: AlertDialog.Builder = AlertDialog.Builder(context) @@ -114,6 +114,7 @@ class SessionDialogBuilder(val context: Context) { listener ) + fun okButton(listener: (() -> Unit) = {}) = button(android.R.string.ok, listener = listener) fun cancelButton(listener: (() -> Unit) = {}) = button(android.R.string.cancel, R.string.AccessibilityId_cancel_button, listener = listener) fun button( @@ -132,8 +133,8 @@ class SessionDialogBuilder(val context: Context) { } }.let(buttonLayout::addView) - fun create(): AlertDialog = dialogBuilder.create() - fun show(): AlertDialog = dialogBuilder.show() + fun create(): AlertDialog = dialogBuilder.create().also { dialog = it } + fun show(): AlertDialog = dialogBuilder.show().also { dialog = it } } fun Context.sessionDialog(build: SessionDialogBuilder.() -> Unit): AlertDialog = diff --git a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt index 166c8f8267..81a559f7cf 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/conversation/v2/ConversationActivityV2.kt @@ -70,9 +70,7 @@ import org.session.libsignal.utilities.ListenableFuture import org.session.libsignal.utilities.Log import org.session.libsignal.utilities.guava.Optional import org.session.libsignal.utilities.hexEncodedPrivateKey -import org.thoughtcrime.securesms.ApplicationContext -import org.thoughtcrime.securesms.ExpirationDialog -import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity +import org.thoughtcrime.securesms.* import org.thoughtcrime.securesms.attachments.ScreenshotObserver import org.thoughtcrime.securesms.audio.AudioRecorder import org.thoughtcrime.securesms.contacts.SelectContactsActivity.Companion.selectedContactsKey @@ -113,7 +111,6 @@ import org.thoughtcrime.securesms.mms.* import org.thoughtcrime.securesms.permissions.Permissions import org.thoughtcrime.securesms.reactions.ReactionsDialogFragment import org.thoughtcrime.securesms.reactions.any.ReactWithAnyEmojiDialogFragment -import org.thoughtcrime.securesms.sessionDialog import org.thoughtcrime.securesms.util.* import java.lang.ref.WeakReference import java.util.* @@ -1001,13 +998,13 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe val group = groupDb.getGroup(thread.address.toGroupString()).orNull() if (group?.isActive == false) { return } } - ExpirationDialog.show(this, thread.expireMessages) { expirationTime: Int -> + showExpirationDialog(thread.expireMessages) { expirationTime -> recipientDb.setExpireMessages(thread, expirationTime) val message = ExpirationTimerUpdate(expirationTime) message.recipient = thread.address.serialize() message.sentTimestamp = SnodeAPI.nowWithOffset - val expiringMessageManager = ApplicationContext.getInstance(this).expiringMessageManager - expiringMessageManager.setExpirationTimer(message) + ApplicationContext.getInstance(this) + .expiringMessageManager.setExpirationTimer(message) MessageSender.send(message, thread.address) invalidateOptionsMenu() }