[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 { 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 onUriError(Uri uri);
void onCancel(); void onCancel();

View File

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

View File

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

View File

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