Commit 70d29af2 authored by Torsten Grote's avatar Torsten Grote

[android] Allow sending message with attachments before previews are loaded

parent baedb14e
......@@ -68,7 +68,7 @@ public class ImagePreview extends ConstraintLayout {
imageList.setLayoutParams(params);
}
setVisibility(VISIBLE);
ImagePreviewAdapter adapter = new ImagePreviewAdapter(items, listener);
ImagePreviewAdapter adapter = new ImagePreviewAdapter(items);
imageList.setAdapter(adapter);
}
......@@ -79,16 +79,6 @@ 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 onError();
void onCancel();
}
......
......@@ -9,7 +9,6 @@ import android.view.ViewGroup;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.briar.R;
import org.briarproject.briar.android.conversation.AttachmentResult;
import org.briarproject.briar.android.view.ImagePreview.ImagePreviewListener;
import java.util.ArrayList;
import java.util.Collection;
......@@ -22,14 +21,11 @@ import static java.util.Objects.requireNonNull;
class ImagePreviewAdapter extends Adapter<ImagePreviewViewHolder> {
private final List<ImagePreviewItem> items;
private final ImagePreviewListener listener;
@LayoutRes
private final int layout;
ImagePreviewAdapter(Collection<ImagePreviewItem> items,
ImagePreviewListener listener) {
ImagePreviewAdapter(Collection<ImagePreviewItem> items) {
this.items = new ArrayList<>(items);
this.listener = listener;
this.layout = items.size() == 1 ?
R.layout.list_item_image_preview_single :
R.layout.list_item_image_preview;
......@@ -40,7 +36,7 @@ class ImagePreviewAdapter extends Adapter<ImagePreviewViewHolder> {
int type) {
View v = LayoutInflater.from(viewGroup.getContext())
.inflate(layout, viewGroup, false);
return new ImagePreviewViewHolder(v, requireNonNull(listener));
return new ImagePreviewViewHolder(v);
}
@Override
......
......@@ -16,7 +16,6 @@ import com.bumptech.glide.request.target.Target;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.briar.R;
import org.briarproject.briar.android.conversation.glide.GlideApp;
import org.briarproject.briar.android.view.ImagePreview.ImagePreviewListener;
import static android.view.View.INVISIBLE;
import static com.bumptech.glide.load.engine.DiskCacheStrategy.NONE;
......@@ -29,14 +28,11 @@ class ImagePreviewViewHolder extends ViewHolder {
@DrawableRes
private static final int ERROR_RES = R.drawable.ic_image_broken;
private final ImagePreviewListener listener;
private final ImageView imageView;
private final ProgressBar progressBar;
ImagePreviewViewHolder(View v, ImagePreviewListener listener) {
ImagePreviewViewHolder(View v) {
super(v);
this.listener = listener;
this.imageView = v.findViewById(R.id.imageView);
this.progressBar = v.findViewById(R.id.progressBar);
}
......@@ -55,7 +51,6 @@ class ImagePreviewViewHolder extends ViewHolder {
Object model, Target<Drawable> target,
boolean isFirstResource) {
progressBar.setVisibility(INVISIBLE);
listener.onError();
return false;
}
......@@ -64,7 +59,6 @@ class ImagePreviewViewHolder extends ViewHolder {
Object model, Target<Drawable> target,
DataSource dataSource, boolean isFirstResource) {
progressBar.setVisibility(INVISIBLE);
listener.onPreviewLoaded();
return false;
}
})
......
......@@ -27,6 +27,7 @@ import java.util.List;
import uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt;
import uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt.PromptStateChangeListener;
import static android.arch.lifecycle.Lifecycle.State.DESTROYED;
import static android.content.Intent.ACTION_GET_CONTENT;
import static android.content.Intent.ACTION_OPEN_DOCUMENT;
import static android.content.Intent.CATEGORY_OPENABLE;
......@@ -56,8 +57,8 @@ public class TextAttachmentController extends TextSendController
private CharSequence textHint;
private List<Uri> imageUris = emptyList();
private int previewsLoaded = 0;
private boolean loadingPreviews = false;
private int urisLoaded = 0;
private boolean loadingUris = false;
public TextAttachmentController(TextInputView v, ImagePreview imagePreview,
SendListener listener, AttachImageListener imageListener,
......@@ -76,10 +77,10 @@ public class TextAttachmentController extends TextSendController
@Override
protected void updateViewState() {
textInput.setEnabled(ready && !loadingPreviews);
boolean sendEnabled = ready && !loadingPreviews &&
textInput.setEnabled(ready && !loadingUris);
boolean sendEnabled = ready && !loadingUris &&
(!textIsEmpty || canSendEmptyText());
if (loadingPreviews) {
if (loadingUris) {
sendButton.showProgress(true);
} else if (imageUris.isEmpty()) {
sendButton.showProgress(false);
......@@ -121,7 +122,9 @@ public class TextAttachmentController extends TextSendController
return;
}
Intent intent = getAttachFileIntent();
requireNonNull(imageListener).onAttachImage(intent);
if (imageListener.getLifecycle().getCurrentState() != DESTROYED) {
requireNonNull(imageListener).onAttachImage(intent);
}
}
private Intent getAttachFileIntent() {
......@@ -152,7 +155,7 @@ public class TextAttachmentController extends TextSendController
private void onNewUris() {
if (imageUris.isEmpty()) return;
loadingPreviews = true;
loadingUris = true;
updateViewState();
textInput.setHint(R.string.image_caption_hint);
List<ImagePreviewItem> items = ImagePreviewItem.fromUris(imageUris);
......@@ -170,6 +173,8 @@ public class TextAttachmentController extends TextSendController
onError(result.getErrorMsg());
} else {
imagePreview.loadPreviewImage(result);
urisLoaded++;
checkAllUrisLoaded();
}
}
......@@ -180,9 +185,9 @@ public class TextAttachmentController extends TextSendController
imagePreview.setVisibility(GONE);
// reset image URIs
imageUris = emptyList();
// no preview has been loaded
previewsLoaded = 0;
loadingPreviews = false;
// no URIs has been loaded
urisLoaded = 0;
loadingUris = false;
// show the image button again, so images can get attached
updateViewState();
}
......@@ -204,17 +209,6 @@ public class TextAttachmentController extends TextSendController
return state.getSuperState();
}
@Override
public void onPreviewLoaded() {
previewsLoaded++;
checkAllPreviewsLoaded();
}
@Override
public void onError() {
onError(null);
}
@UiThread
private void onError(@Nullable String errorMsg) {
if (errorMsg == null) {
......@@ -232,10 +226,10 @@ public class TextAttachmentController extends TextSendController
reset();
}
private void checkAllPreviewsLoaded() {
if (previewsLoaded == imageUris.size()) {
loadingPreviews = false;
// all previews were loaded
private void checkAllUrisLoaded() {
if (urisLoaded == imageUris.size()) {
loadingUris = false;
// all images were turned into attachments
updateViewState();
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment