mirror of
https://github.com/oxen-io/session-android.git
synced 2024-12-25 17:27:45 +00:00
Utilise dialog dsl in ExpirationDialog
This commit is contained in:
parent
50271685af
commit
c5f821add0
@ -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.length;i++) {
|
|
||||||
expirationDisplayValues[i] = ExpirationUtil.getExpirationDisplayValue(context, expirationTimes[i]);
|
|
||||||
|
|
||||||
if ((currentExpiration >= 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -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<NumberPickerView>(R.id.expiration_number_picker)
|
||||||
|
|
||||||
|
fun updateText(index: Int) {
|
||||||
|
view.findViewById<TextView>(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()
|
||||||
|
}
|
||||||
|
}
|
@ -29,8 +29,8 @@ annotation class DialogDsl
|
|||||||
@DialogDsl
|
@DialogDsl
|
||||||
class SessionDialogBuilder(val context: Context) {
|
class SessionDialogBuilder(val context: Context) {
|
||||||
|
|
||||||
val dp20 = toPx(20, context.resources)
|
private val dp20 = toPx(20, context.resources)
|
||||||
val dp40 = toPx(40, context.resources)
|
private val dp40 = toPx(40, context.resources)
|
||||||
|
|
||||||
private val dialogBuilder: AlertDialog.Builder = AlertDialog.Builder(context)
|
private val dialogBuilder: AlertDialog.Builder = AlertDialog.Builder(context)
|
||||||
|
|
||||||
@ -114,6 +114,7 @@ class SessionDialogBuilder(val context: Context) {
|
|||||||
listener
|
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 cancelButton(listener: (() -> Unit) = {}) = button(android.R.string.cancel, R.string.AccessibilityId_cancel_button, listener = listener)
|
||||||
|
|
||||||
fun button(
|
fun button(
|
||||||
@ -132,8 +133,8 @@ class SessionDialogBuilder(val context: Context) {
|
|||||||
}
|
}
|
||||||
}.let(buttonLayout::addView)
|
}.let(buttonLayout::addView)
|
||||||
|
|
||||||
fun create(): AlertDialog = dialogBuilder.create()
|
fun create(): AlertDialog = dialogBuilder.create().also { dialog = it }
|
||||||
fun show(): AlertDialog = dialogBuilder.show()
|
fun show(): AlertDialog = dialogBuilder.show().also { dialog = it }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Context.sessionDialog(build: SessionDialogBuilder.() -> Unit): AlertDialog =
|
fun Context.sessionDialog(build: SessionDialogBuilder.() -> Unit): AlertDialog =
|
||||||
|
@ -70,9 +70,7 @@ import org.session.libsignal.utilities.ListenableFuture
|
|||||||
import org.session.libsignal.utilities.Log
|
import org.session.libsignal.utilities.Log
|
||||||
import org.session.libsignal.utilities.guava.Optional
|
import org.session.libsignal.utilities.guava.Optional
|
||||||
import org.session.libsignal.utilities.hexEncodedPrivateKey
|
import org.session.libsignal.utilities.hexEncodedPrivateKey
|
||||||
import org.thoughtcrime.securesms.ApplicationContext
|
import org.thoughtcrime.securesms.*
|
||||||
import org.thoughtcrime.securesms.ExpirationDialog
|
|
||||||
import org.thoughtcrime.securesms.PassphraseRequiredActionBarActivity
|
|
||||||
import org.thoughtcrime.securesms.attachments.ScreenshotObserver
|
import org.thoughtcrime.securesms.attachments.ScreenshotObserver
|
||||||
import org.thoughtcrime.securesms.audio.AudioRecorder
|
import org.thoughtcrime.securesms.audio.AudioRecorder
|
||||||
import org.thoughtcrime.securesms.contacts.SelectContactsActivity.Companion.selectedContactsKey
|
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.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.*
|
||||||
@ -1001,13 +998,13 @@ class ConversationActivityV2 : PassphraseRequiredActionBarActivity(), InputBarDe
|
|||||||
val group = groupDb.getGroup(thread.address.toGroupString()).orNull()
|
val group = groupDb.getGroup(thread.address.toGroupString()).orNull()
|
||||||
if (group?.isActive == false) { return }
|
if (group?.isActive == false) { return }
|
||||||
}
|
}
|
||||||
ExpirationDialog.show(this, thread.expireMessages) { expirationTime: Int ->
|
showExpirationDialog(thread.expireMessages) { expirationTime ->
|
||||||
recipientDb.setExpireMessages(thread, expirationTime)
|
recipientDb.setExpireMessages(thread, expirationTime)
|
||||||
val message = ExpirationTimerUpdate(expirationTime)
|
val message = ExpirationTimerUpdate(expirationTime)
|
||||||
message.recipient = thread.address.serialize()
|
message.recipient = thread.address.serialize()
|
||||||
message.sentTimestamp = SnodeAPI.nowWithOffset
|
message.sentTimestamp = SnodeAPI.nowWithOffset
|
||||||
val expiringMessageManager = ApplicationContext.getInstance(this).expiringMessageManager
|
ApplicationContext.getInstance(this)
|
||||||
expiringMessageManager.setExpirationTimer(message)
|
.expiringMessageManager.setExpirationTimer(message)
|
||||||
MessageSender.send(message, thread.address)
|
MessageSender.send(message, thread.address)
|
||||||
invalidateOptionsMenu()
|
invalidateOptionsMenu()
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user