[android] Get notified when all image previews have been loaded

Also fix crash when attaching image fails
This commit is contained in:
Torsten Grote
2019-02-05 16:53:12 -02:00
parent 419f37a4a9
commit 423ecc003b
4 changed files with 33 additions and 2 deletions

View File

@@ -79,6 +79,13 @@ public class ImagePreview extends ConstraintLayout {
interface ImagePreviewListener {
void onPreviewLoaded();
/**
* Called when Glide can't load a preview image.
*
* Warning: Glide may call this multiple times.
*/
void onUriError(Uri uri);
void onCancel();

View File

@@ -54,6 +54,7 @@ class ImagePreviewAdapter extends Adapter<ImagePreviewViewHolder> {
void removeUri(Uri uri) {
int pos = items.indexOf(uri);
if (pos == -1) return;
items.remove(uri);
notifyItemRemoved(pos);
}

View File

@@ -54,8 +54,8 @@ class ImagePreviewViewHolder extends ViewHolder {
public boolean onLoadFailed(@Nullable GlideException e,
Object model, Target<Drawable> target,
boolean isFirstResource) {
listener.onUriError(uri);
progressBar.setVisibility(INVISIBLE);
listener.onUriError(uri);
return false;
}
@@ -64,6 +64,7 @@ class ImagePreviewViewHolder extends ViewHolder {
Object model, Target<Drawable> target,
DataSource dataSource, boolean isFirstResource) {
progressBar.setVisibility(INVISIBLE);
listener.onPreviewLoaded();
return false;
}
})

View File

@@ -54,6 +54,7 @@ public class TextAttachmentController extends TextSendController
private CharSequence textHint;
private boolean hasImageSupport = false;
private List<Uri> imageUris = emptyList();
private int previewsLoaded = 0;
public TextAttachmentController(TextInputView v, ImagePreview imagePreview,
SendListener listener, AttachImageListener imageListener) {
@@ -185,6 +186,8 @@ public class TextAttachmentController extends TextSendController
imageUris = emptyList();
// show the image button again, so images can get attached
showImageButton(true);
// no preview has been loaded
previewsLoaded = 0;
}
@Override
@@ -204,21 +207,40 @@ public class TextAttachmentController extends TextSendController
return state.getSuperState();
}
@Override
public void onPreviewLoaded() {
previewsLoaded++;
checkAllPreviewsLoaded();
}
@Override
public void onUriError(Uri uri) {
imageUris.remove(uri);
boolean removed = imageUris.remove(uri);
if (!removed) {
// we have removed this Uri already, do not remove it again
return;
}
imagePreview.removeUri(uri);
if (imageUris.isEmpty()) onCancel();
Toast.makeText(textInput.getContext(), R.string.image_attach_error,
LENGTH_LONG).show();
checkAllPreviewsLoaded();
}
@Override
public void onCancel() {
textInput.clearText();
sendButton.setEnabled(true);
reset();
}
private void checkAllPreviewsLoaded() {
if (previewsLoaded == imageUris.size()) {
// all previews were loaded
// TODO allow sending
}
}
public void showImageOnboarding(Activity activity,
Runnable onOnboardingSeen) {
PromptStateChangeListener listener = (prompt, state) -> {