mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-21 07:09:56 +01:00
[android] re-use the same LiveData for AttachmentResults
This commit is contained in:
@@ -49,8 +49,8 @@ public class AttachmentCreator {
|
|||||||
private final CopyOnWriteArrayList<AttachmentItemResult> itemResults =
|
private final CopyOnWriteArrayList<AttachmentItemResult> itemResults =
|
||||||
new CopyOnWriteArrayList<>();
|
new CopyOnWriteArrayList<>();
|
||||||
|
|
||||||
@Nullable
|
private final MutableLiveData<AttachmentResult> result =
|
||||||
private volatile MutableLiveData<AttachmentResult> result = null;
|
new MutableLiveData<>();
|
||||||
@Nullable
|
@Nullable
|
||||||
private AttachmentCreationTask task;
|
private AttachmentCreationTask task;
|
||||||
|
|
||||||
@@ -65,10 +65,8 @@ public class AttachmentCreator {
|
|||||||
@UiThread
|
@UiThread
|
||||||
public LiveData<AttachmentResult> storeAttachments(
|
public LiveData<AttachmentResult> storeAttachments(
|
||||||
LiveData<GroupId> groupId, Collection<Uri> newUris) {
|
LiveData<GroupId> groupId, Collection<Uri> newUris) {
|
||||||
if (this.result != null || !uris.isEmpty())
|
if (task != null || !uris.isEmpty())
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
MutableLiveData<AttachmentResult> result = new MutableLiveData<>();
|
|
||||||
this.result = result;
|
|
||||||
uris.addAll(newUris);
|
uris.addAll(newUris);
|
||||||
observeForeverOnce(groupId, id -> {
|
observeForeverOnce(groupId, id -> {
|
||||||
if (id == null) throw new IllegalStateException();
|
if (id == null) throw new IllegalStateException();
|
||||||
@@ -87,9 +85,7 @@ public class AttachmentCreator {
|
|||||||
*/
|
*/
|
||||||
@UiThread
|
@UiThread
|
||||||
public LiveData<AttachmentResult> getLiveAttachments() {
|
public LiveData<AttachmentResult> getLiveAttachments() {
|
||||||
//
|
if (task == null || uris.isEmpty())
|
||||||
MutableLiveData<AttachmentResult> result = this.result;
|
|
||||||
if (task == null || uris.isEmpty() || result == null)
|
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
// A task is already running. It will update the result LiveData.
|
// A task is already running. It will update the result LiveData.
|
||||||
// So nothing more to do here.
|
// So nothing more to do here.
|
||||||
@@ -99,8 +95,6 @@ public class AttachmentCreator {
|
|||||||
@IoExecutor
|
@IoExecutor
|
||||||
void onAttachmentHeaderReceived(Uri uri, AttachmentHeader h,
|
void onAttachmentHeaderReceived(Uri uri, AttachmentHeader h,
|
||||||
boolean needsSize) {
|
boolean needsSize) {
|
||||||
MutableLiveData<AttachmentResult> result = this.result;
|
|
||||||
if (result == null) return;
|
|
||||||
// get and cache AttachmentItem for ImagePreview
|
// get and cache AttachmentItem for ImagePreview
|
||||||
try {
|
try {
|
||||||
Attachment a = retriever.getMessageAttachment(h);
|
Attachment a = retriever.getMessageAttachment(h);
|
||||||
@@ -118,8 +112,6 @@ public class AttachmentCreator {
|
|||||||
|
|
||||||
@IoExecutor
|
@IoExecutor
|
||||||
void onAttachmentError(Uri uri, Throwable t) {
|
void onAttachmentError(Uri uri, Throwable t) {
|
||||||
MutableLiveData<AttachmentResult> result = this.result;
|
|
||||||
if (result == null) return;
|
|
||||||
// get error message
|
// get error message
|
||||||
String errorMsg;
|
String errorMsg;
|
||||||
if (t instanceof UnsupportedMimeTypeException) {
|
if (t instanceof UnsupportedMimeTypeException) {
|
||||||
@@ -143,8 +135,6 @@ public class AttachmentCreator {
|
|||||||
void onAttachmentCreationFinished() {
|
void onAttachmentCreationFinished() {
|
||||||
if (uris.size() != itemResults.size())
|
if (uris.size() != itemResults.size())
|
||||||
throw new IllegalStateException();
|
throw new IllegalStateException();
|
||||||
MutableLiveData<AttachmentResult> result = this.result;
|
|
||||||
if (result == null) return;
|
|
||||||
result.postValue(new AttachmentResult(itemResults, true));
|
result.postValue(new AttachmentResult(itemResults, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,6 +154,7 @@ public class AttachmentCreator {
|
|||||||
*
|
*
|
||||||
* @param id The MessageId of the sent message.
|
* @param id The MessageId of the sent message.
|
||||||
*/
|
*/
|
||||||
|
@UiThread
|
||||||
public void onAttachmentsSent(MessageId id) {
|
public void onAttachmentsSent(MessageId id) {
|
||||||
List<AttachmentItem> items = new ArrayList<>(itemResults.size());
|
List<AttachmentItem> items = new ArrayList<>(itemResults.size());
|
||||||
for (AttachmentItemResult itemResult : itemResults) {
|
for (AttachmentItemResult itemResult : itemResults) {
|
||||||
@@ -182,11 +173,6 @@ public class AttachmentCreator {
|
|||||||
public void cancel() {
|
public void cancel() {
|
||||||
if (task == null) throw new AssertionError();
|
if (task == null) throw new AssertionError();
|
||||||
task.cancel();
|
task.cancel();
|
||||||
// let observers know that they can remove themselves
|
|
||||||
MutableLiveData<AttachmentResult> result = this.result;
|
|
||||||
if (result != null) {
|
|
||||||
result.setValue(null);
|
|
||||||
}
|
|
||||||
deleteUnsentAttachments();
|
deleteUnsentAttachments();
|
||||||
resetState();
|
resetState();
|
||||||
}
|
}
|
||||||
@@ -196,7 +182,7 @@ public class AttachmentCreator {
|
|||||||
task = null;
|
task = null;
|
||||||
uris.clear();
|
uris.clear();
|
||||||
itemResults.clear();
|
itemResults.clear();
|
||||||
result = null;
|
result.setValue(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@UiThread
|
@UiThread
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import android.arch.lifecycle.LiveData;
|
|||||||
import android.arch.lifecycle.MutableLiveData;
|
import android.arch.lifecycle.MutableLiveData;
|
||||||
import android.arch.lifecycle.Transformations;
|
import android.arch.lifecycle.Transformations;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
import android.os.Handler;
|
||||||
import android.support.annotation.Nullable;
|
import android.support.annotation.Nullable;
|
||||||
import android.support.annotation.UiThread;
|
import android.support.annotation.UiThread;
|
||||||
|
|
||||||
@@ -46,6 +47,7 @@ import java.util.logging.Logger;
|
|||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import static android.os.Looper.getMainLooper;
|
||||||
import static java.util.Objects.requireNonNull;
|
import static java.util.Objects.requireNonNull;
|
||||||
import static java.util.logging.Level.WARNING;
|
import static java.util.logging.Level.WARNING;
|
||||||
import static java.util.logging.Logger.getLogger;
|
import static java.util.logging.Logger.getLogger;
|
||||||
@@ -302,7 +304,10 @@ public class ConversationViewModel extends AndroidViewModel
|
|||||||
message.getId(), message.getGroupId(),
|
message.getId(), message.getGroupId(),
|
||||||
message.getTimestamp(), true, true, false, false,
|
message.getTimestamp(), true, true, false, false,
|
||||||
text != null, attachments);
|
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
|
// TODO add text to cache when available here
|
||||||
addedHeader.postEvent(h);
|
addedHeader.postEvent(h);
|
||||||
} catch (DbException e) {
|
} catch (DbException e) {
|
||||||
|
|||||||
Reference in New Issue
Block a user