Prevent NPE in safety number change dialog.

This commit is contained in:
Cody Henthorne 2021-04-14 17:28:07 -04:00
parent 8d6d8019fe
commit 9363f0ebb4
2 changed files with 18 additions and 3 deletions

View file

@ -7,6 +7,7 @@ import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@ -156,13 +157,19 @@ public final class SafetyNumberChangeDialog extends DialogFragment implements Sa
builder.setTitle(R.string.safety_number_change_dialog__safety_number_changes)
.setView(dialogView)
.setCancelable(false)
.setPositiveButton(continueText, this::handleSendAnyway)
.setNegativeButton(cancelText, this::handleCancel);
setCancelable(false);
return builder.create();
AlertDialog dialog = builder.create();
dialog.setOnShowListener(d -> {
Button positive = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
positive.setEnabled(false);
viewModel.getTrustOrVerifyReady().observe(getViewLifecycleOwner(), positive::setEnabled);
});
return dialog;
}
@Override

View file

@ -22,6 +22,8 @@ public final class SafetyNumberChangeViewModel extends ViewModel {
private final SafetyNumberChangeRepository safetyNumberChangeRepository;
private final MutableLiveData<Collection<RecipientId>> recipientIds;
private final LiveData<SafetyNumberChangeState> safetyNumberChangeState;
private final LiveData<List<ChangedRecipient>> changedRecipients;
private final LiveData<Boolean> trustOrVerifyReady;
private SafetyNumberChangeViewModel(@NonNull List<RecipientId> recipientIds,
@Nullable Long messageId,
@ -31,10 +33,16 @@ public final class SafetyNumberChangeViewModel extends ViewModel {
this.safetyNumberChangeRepository = safetyNumberChangeRepository;
this.recipientIds = new MutableLiveData<>(recipientIds);
this.safetyNumberChangeState = LiveDataUtil.mapAsync(this.recipientIds, ids -> this.safetyNumberChangeRepository.getSafetyNumberChangeState(ids, messageId, messageType));
this.changedRecipients = Transformations.map(safetyNumberChangeState, SafetyNumberChangeState::getChangedRecipients);
this.trustOrVerifyReady = Transformations.map(safetyNumberChangeState, Objects::nonNull);
}
@NonNull LiveData<List<ChangedRecipient>> getChangedRecipients() {
return Transformations.map(safetyNumberChangeState, SafetyNumberChangeState::getChangedRecipients);
return changedRecipients;
}
@NonNull LiveData<Boolean> getTrustOrVerifyReady() {
return trustOrVerifyReady;
}
@NonNull LiveData<TrustAndVerifyResult> trustOrVerifyChangedRecipients() {