From 1f91842c5274a44d6b7e1f46a5471e9df693ce56 Mon Sep 17 00:00:00 2001 From: Torsten Grote Date: Fri, 14 Jun 2019 10:03:43 -0300 Subject: [PATCH] [android] re-use the same LiveData for AttachmentResults --- .../android/attachment/AttachmentCreator.java | 26 +++++-------------- .../conversation/ConversationViewModel.java | 7 ++++- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentCreator.java b/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentCreator.java index a8af9d89a..41dd9ea3b 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentCreator.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentCreator.java @@ -49,8 +49,8 @@ public class AttachmentCreator { private final CopyOnWriteArrayList itemResults = new CopyOnWriteArrayList<>(); - @Nullable - private volatile MutableLiveData result = null; + private final MutableLiveData result = + new MutableLiveData<>(); @Nullable private AttachmentCreationTask task; @@ -65,10 +65,8 @@ public class AttachmentCreator { @UiThread public LiveData storeAttachments( LiveData groupId, Collection newUris) { - if (this.result != null || !uris.isEmpty()) + if (task != null || !uris.isEmpty()) throw new IllegalStateException(); - MutableLiveData result = new MutableLiveData<>(); - this.result = result; uris.addAll(newUris); observeForeverOnce(groupId, id -> { if (id == null) throw new IllegalStateException(); @@ -87,9 +85,7 @@ public class AttachmentCreator { */ @UiThread public LiveData getLiveAttachments() { - // - MutableLiveData result = this.result; - if (task == null || uris.isEmpty() || result == null) + if (task == null || uris.isEmpty()) throw new IllegalStateException(); // A task is already running. It will update the result LiveData. // So nothing more to do here. @@ -99,8 +95,6 @@ public class AttachmentCreator { @IoExecutor void onAttachmentHeaderReceived(Uri uri, AttachmentHeader h, boolean needsSize) { - MutableLiveData result = this.result; - if (result == null) return; // get and cache AttachmentItem for ImagePreview try { Attachment a = retriever.getMessageAttachment(h); @@ -118,8 +112,6 @@ public class AttachmentCreator { @IoExecutor void onAttachmentError(Uri uri, Throwable t) { - MutableLiveData result = this.result; - if (result == null) return; // get error message String errorMsg; if (t instanceof UnsupportedMimeTypeException) { @@ -143,8 +135,6 @@ public class AttachmentCreator { void onAttachmentCreationFinished() { if (uris.size() != itemResults.size()) throw new IllegalStateException(); - MutableLiveData result = this.result; - if (result == null) return; result.postValue(new AttachmentResult(itemResults, true)); } @@ -164,6 +154,7 @@ public class AttachmentCreator { * * @param id The MessageId of the sent message. */ + @UiThread public void onAttachmentsSent(MessageId id) { List items = new ArrayList<>(itemResults.size()); for (AttachmentItemResult itemResult : itemResults) { @@ -182,11 +173,6 @@ public class AttachmentCreator { public void cancel() { if (task == null) throw new AssertionError(); task.cancel(); - // let observers know that they can remove themselves - MutableLiveData result = this.result; - if (result != null) { - result.setValue(null); - } deleteUnsentAttachments(); resetState(); } @@ -196,7 +182,7 @@ public class AttachmentCreator { task = null; uris.clear(); itemResults.clear(); - result = null; + result.setValue(null); } @UiThread diff --git a/briar-android/src/main/java/org/briarproject/briar/android/conversation/ConversationViewModel.java b/briar-android/src/main/java/org/briarproject/briar/android/conversation/ConversationViewModel.java index a746f8ac6..0c5fd7649 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/conversation/ConversationViewModel.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/conversation/ConversationViewModel.java @@ -6,6 +6,7 @@ import android.arch.lifecycle.LiveData; import android.arch.lifecycle.MutableLiveData; import android.arch.lifecycle.Transformations; import android.net.Uri; +import android.os.Handler; import android.support.annotation.Nullable; import android.support.annotation.UiThread; @@ -46,6 +47,7 @@ import java.util.logging.Logger; import javax.inject.Inject; +import static android.os.Looper.getMainLooper; import static java.util.Objects.requireNonNull; import static java.util.logging.Level.WARNING; import static java.util.logging.Logger.getLogger; @@ -302,7 +304,10 @@ public class ConversationViewModel extends AndroidViewModel message.getId(), message.getGroupId(), message.getTimestamp(), true, true, false, false, text != null, attachments); - attachmentCreator.onAttachmentsSent(m.getMessage().getId()); + MessageId id = m.getMessage().getId(); + // use the UiThread to call onAttachmentsSent + new Handler(getMainLooper()).post(() -> + attachmentCreator.onAttachmentsSent(id)); // TODO add text to cache when available here addedHeader.postEvent(h); } catch (DbException e) {