Fixed attachment observing

This commit is contained in:
M66B 2018-08-13 06:23:46 +00:00
parent 4050350a0e
commit a4b843ecd5
5 changed files with 43 additions and 28 deletions

View File

@ -29,12 +29,21 @@ import androidx.room.Update;
@Dao
public interface DaoAttachment {
@Query("SELECT id, message, sequence, name, type, size, progress" +
@Query("SELECT attachment.id, attachment.message, sequence, name, type, size, progress" +
", (NOT content IS NULL) as content" +
" FROM attachment" +
" WHERE message = :message" +
" WHERE message = :id" +
" ORDER BY sequence")
LiveData<List<TupleAttachment>> liveAttachments(long message);
LiveData<List<TupleAttachment>> liveAttachments(long id);
@Query("SELECT attachment.id, attachment.message, sequence, name, type, size, progress" +
", (NOT content IS NULL) as content" +
" FROM attachment" +
" JOIN message ON message.id = attachment.message" +
" WHERE folder = :folder" +
" AND msgid = :msgid" +
" ORDER BY sequence")
LiveData<List<TupleAttachment>> liveAttachments(long folder, String msgid);
@Query("SELECT * FROM attachment" +
" WHERE message = :message" +

View File

@ -103,8 +103,10 @@ public interface DaoMessage {
" WHERE message.id = :id")
LiveData<TupleMessageEx> liveMessage(long id);
@Query("SELECT * FROM message WHERE msgid = :msgid")
LiveData<EntityMessage> liveMessageByMsgId(String msgid);
@Query("SELECT * FROM message" +
" WHERE folder = :folder" +
" AND msgid = :msgid")
LiveData<EntityMessage> liveMessageByMsgId(long folder, String msgid);
@Query("SELECT uid FROM message WHERE folder = :folder AND received >= :received AND NOT uid IS NULL")
List<Long> getUids(long folder, long received);

View File

@ -585,12 +585,25 @@ public class FragmentCompose extends FragmentEx {
return draft;
}
@Override
protected void onLoaded(Bundle args, EntityMessage draft) {
FragmentCompose.this.draft = draft;
Log.i(Helper.TAG, "Loaded draft id=" + draft.id + " msgid=" + draft.msgid);
DB.getInstance(getContext()).message().liveMessageByMsgId(draft.msgid).observe(getViewLifecycleOwner(), new Observer<EntityMessage>() {
DB db = DB.getInstance(getContext());
db.attachment().liveAttachments(draft.folder, draft.msgid).observe(getViewLifecycleOwner(),
new Observer<List<TupleAttachment>>() {
@Override
public void onChanged(@Nullable List<TupleAttachment> attachments) {
if (attachments != null)
adapter.set(attachments);
grpAttachments.setVisibility(attachments != null && attachments.size() > 0 ? View.VISIBLE : View.GONE);
}
});
db.message().liveMessageByMsgId(draft.folder, draft.msgid).observe(getViewLifecycleOwner(), new Observer<EntityMessage>() {
boolean observed = false;
@Override
@ -606,17 +619,6 @@ public class FragmentCompose extends FragmentEx {
DB db = DB.getInstance(getContext());
db.attachment().liveAttachments(draft.id).removeObservers(getViewLifecycleOwner());
db.attachment().liveAttachments(draft.id).observe(getViewLifecycleOwner(),
new Observer<List<TupleAttachment>>() {
@Override
public void onChanged(@Nullable List<TupleAttachment> attachments) {
if (attachments != null)
adapter.set(attachments);
grpAttachments.setVisibility(attachments != null && attachments.size() > 0 ? View.VISIBLE : View.GONE);
}
});
// Set controls only once
if (observed)
return;

View File

@ -275,17 +275,6 @@ public class FragmentMessage extends FragmentEx {
tvFrom.setTextColor(colorUnseen);
tvTime.setTextColor(colorUnseen);
db.attachment().liveAttachments(id).removeObservers(getViewLifecycleOwner());
db.attachment().liveAttachments(id).observe(getViewLifecycleOwner(),
new Observer<List<TupleAttachment>>() {
@Override
public void onChanged(@Nullable List<TupleAttachment> attachments) {
if (attachments != null)
adapter.set(attachments);
grpAttachments.setVisibility(attachments != null && attachments.size() > 0 ? View.VISIBLE : View.GONE);
}
});
tvError.setText(message.error);
tvError.setVisibility(message.error == null ? View.GONE : View.VISIBLE);
@ -350,6 +339,18 @@ public class FragmentMessage extends FragmentEx {
grpReady.setVisibility(View.VISIBLE);
}
});
// Observe attachments
db.attachment().liveAttachments(id).observe(getViewLifecycleOwner(),
new Observer<List<TupleAttachment>>() {
@Override
public void onChanged(@Nullable List<TupleAttachment> attachments) {
if (attachments != null)
adapter.set(attachments);
grpAttachments.setVisibility(attachments != null && attachments.size() > 0 ? View.VISIBLE : View.GONE);
}
});
}
@Override

View File

@ -1185,6 +1185,7 @@ public class ServiceSynchronize extends LifecycleService {
DB db = DB.getInstance(this);
try {
int result = 0;
db.beginTransaction();
// Find message by uid (fast, no headers required)