Merge pull request #634 from jsoberg/#631-AdapterPositionSafetyForNPE

#631 - Fix NPE from invalid adapter position
This commit is contained in:
Jakob Nixdorf 2020-09-25 06:12:21 +02:00 committed by GitHub
commit f358dabcb1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -25,6 +25,8 @@ package org.shadowice.flocke.andotp.View;
import android.animation.ObjectAnimator; import android.animation.ObjectAnimator;
import android.content.Context; import android.content.Context;
import android.graphics.ColorFilter; import android.graphics.ColorFilter;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView; import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils; import android.text.TextUtils;
@ -105,56 +107,62 @@ public class EntryViewHolder extends RecyclerView.ViewHolder
visibleImg.getDrawable().setColorFilter(colorFilter); visibleImg.getDrawable().setColorFilter(colorFilter);
invisibleImg.getDrawable().setColorFilter(colorFilter); invisibleImg.getDrawable().setColorFilter(colorFilter);
// Setup onClickListeners setupOnClickListeners(menuButton, copyButton);
menuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (callback != null)
callback.onMenuButtonClicked(view, getAdapterPosition());
}
});
copyButton.setOnClickListener(new View.OnClickListener() { setTapToReveal(tapToReveal);
@Override }
public void onClick(View view) {
if (callback != null)
callback.onCopyButtonClicked(value.getTag().toString(), getAdapterPosition());
}
});
counterLayout.setOnClickListener(new View.OnClickListener() { private void setupOnClickListeners(ImageButton menuButton, ImageButton copyButton) {
@Override menuButton.setOnClickListener(view ->
public void onClick(View view) { adapterPositionSafeCallback((callback, adapterPosition) ->
if (callback != null) callback.onMenuButtonClicked(view, adapterPosition)
callback.onCounterClicked(getAdapterPosition()); )
} );
});
counterLayout.setOnLongClickListener(new View.OnLongClickListener() { copyButton.setOnClickListener(view ->
@Override adapterPositionSafeCallback((callback, adapterPosition) ->
public boolean onLongClick(View view) { callback.onCopyButtonClicked(value.getTag().toString(), adapterPosition)
if (callback != null) )
callback.onCounterLongPressed(getAdapterPosition()); );
return false; counterLayout.setOnClickListener(view ->
} adapterPositionSafeCallback(Callback::onCounterClicked)
);
counterLayout.setOnLongClickListener(view -> {
adapterPositionSafeCallback(Callback::onCounterLongPressed);
return false;
}); });
card.setOnClickListener(new SimpleDoubleClickListener() { card.setOnClickListener(new SimpleDoubleClickListener() {
@Override @Override
public void onSingleClick(View v) { public void onSingleClick(View v) {
if (callback != null) adapterPositionSafeCallback((callback, adapterPosition) ->
callback.onCardSingleClicked(getAdapterPosition(), value.getTag().toString()); callback.onCardSingleClicked(adapterPosition, value.getTag().toString())
);
} }
@Override @Override
public void onDoubleClick(View v) { public void onDoubleClick(View v) {
if (callback != null) adapterPositionSafeCallback((callback, adapterPosition) ->
callback.onCardDoubleClicked(getAdapterPosition(), value.getTag().toString()); callback.onCardDoubleClicked(adapterPosition, value.getTag().toString())
);
} }
}); });
}
setTapToReveal(tapToReveal); @FunctionalInterface
private interface AdapterPositionSafeCallbackConsumer {
/** The specified {@link Callback} is guaranteed to be non-null, and adapterPosition is
* guaranteed to be a valid position. */
void accept(@NonNull Callback callback, int adapterPosition);
}
private void adapterPositionSafeCallback(AdapterPositionSafeCallbackConsumer safeCallback) {
int clickedPosition = getAdapterPosition();
if (callback != null && clickedPosition != RecyclerView.NO_POSITION) {
safeCallback.accept(callback, clickedPosition);
}
} }
public void updateValues(Entry entry) { public void updateValues(Entry entry) {