fixed chat loading issue andriod
This commit is contained in:
@@ -11,6 +11,7 @@ import com.bumptech.glide.Glide;
|
|||||||
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
import com.bumptech.glide.load.engine.DiskCacheStrategy;
|
||||||
import com.bumptech.glide.load.model.GlideUrl;
|
import com.bumptech.glide.load.model.GlideUrl;
|
||||||
import com.bumptech.glide.load.model.LazyHeaders;
|
import com.bumptech.glide.load.model.LazyHeaders;
|
||||||
|
import com.bumptech.glide.signature.ObjectKey;
|
||||||
import com.example.petstoremobile.R;
|
import com.example.petstoremobile.R;
|
||||||
import com.example.petstoremobile.databinding.ItemMessageReceivedBinding;
|
import com.example.petstoremobile.databinding.ItemMessageReceivedBinding;
|
||||||
import com.example.petstoremobile.databinding.ItemMessageSentBinding;
|
import com.example.petstoremobile.databinding.ItemMessageSentBinding;
|
||||||
@@ -35,6 +36,13 @@ public class MessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
|
|||||||
public MessageAdapter(List<Message> messages, Long currentUserId) {
|
public MessageAdapter(List<Message> messages, Long currentUserId) {
|
||||||
this.messages = messages;
|
this.messages = messages;
|
||||||
this.currentUserId = currentUserId;
|
this.currentUserId = currentUserId;
|
||||||
|
setHasStableIds(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public long getItemId(int position) {
|
||||||
|
Message m = messages.get(position);
|
||||||
|
return m.getId() != null ? m.getId() : position;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setCurrentUserId(Long id) {
|
public void setCurrentUserId(Long id) {
|
||||||
@@ -150,6 +158,7 @@ public class MessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (url == null) {
|
if (url == null) {
|
||||||
|
Glide.with(iv.getContext()).clear(iv);
|
||||||
iv.setVisibility(View.GONE);
|
iv.setVisibility(View.GONE);
|
||||||
tvName.setVisibility(View.GONE);
|
tvName.setVisibility(View.GONE);
|
||||||
return;
|
return;
|
||||||
@@ -166,18 +175,25 @@ public class MessageAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder
|
|||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Use a signature to prevent Glide from showing stale cached images for the same URL/ID
|
||||||
|
String signatureKey = (m.getTimestamp() != null ? m.getTimestamp() : "") + m.getId();
|
||||||
|
|
||||||
|
Glide.with(iv.getContext()).clear(iv);
|
||||||
Glide.with(iv.getContext())
|
Glide.with(iv.getContext())
|
||||||
.load(loadTarget)
|
.load(loadTarget)
|
||||||
|
.signature(new ObjectKey(signatureKey))
|
||||||
.diskCacheStrategy(DiskCacheStrategy.ALL)
|
.diskCacheStrategy(DiskCacheStrategy.ALL)
|
||||||
.placeholder(R.drawable.placeholder)
|
.placeholder(R.drawable.placeholder)
|
||||||
.error(R.drawable.placeholder)
|
.error(R.drawable.placeholder)
|
||||||
.into(iv);
|
.into(iv);
|
||||||
} else {
|
} else {
|
||||||
|
Glide.with(iv.getContext()).clear(iv);
|
||||||
iv.setVisibility(View.GONE);
|
iv.setVisibility(View.GONE);
|
||||||
tvName.setVisibility(View.VISIBLE);
|
tvName.setVisibility(View.VISIBLE);
|
||||||
tvName.setText(m.getAttachmentName() != null ? m.getAttachmentName() : "Attachment");
|
tvName.setText(m.getAttachmentName() != null ? m.getAttachmentName() : "Attachment");
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
Glide.with(iv.getContext()).clear(iv);
|
||||||
iv.setVisibility(View.GONE);
|
iv.setVisibility(View.GONE);
|
||||||
tvName.setVisibility(View.GONE);
|
tvName.setVisibility(View.GONE);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -182,6 +182,7 @@ public class ChatFragment extends Fragment implements ChatAdapter.OnChatClickLis
|
|||||||
LinearLayoutManager lm = new LinearLayoutManager(getContext());
|
LinearLayoutManager lm = new LinearLayoutManager(getContext());
|
||||||
lm.setStackFromEnd(true);
|
lm.setStackFromEnd(true);
|
||||||
binding.rvMessages.setLayoutManager(lm);
|
binding.rvMessages.setLayoutManager(lm);
|
||||||
|
binding.rvMessages.setItemAnimator(null);
|
||||||
binding.rvMessages.setAdapter(messageAdapter);
|
binding.rvMessages.setAdapter(messageAdapter);
|
||||||
setConversationActive(false, null);
|
setConversationActive(false, null);
|
||||||
}
|
}
|
||||||
@@ -285,9 +286,14 @@ public class ChatFragment extends Fragment implements ChatAdapter.OnChatClickLis
|
|||||||
});
|
});
|
||||||
|
|
||||||
viewModel.getMessageList().observe(getViewLifecycleOwner(), list -> {
|
viewModel.getMessageList().observe(getViewLifecycleOwner(), list -> {
|
||||||
|
int prevSize = messageList.size();
|
||||||
messageList.clear();
|
messageList.clear();
|
||||||
messageList.addAll(list);
|
messageList.addAll(list);
|
||||||
|
if (prevSize > 0 && list.size() == prevSize + 1) {
|
||||||
|
messageAdapter.notifyItemInserted(list.size() - 1);
|
||||||
|
} else {
|
||||||
messageAdapter.notifyDataSetChanged();
|
messageAdapter.notifyDataSetChanged();
|
||||||
|
}
|
||||||
scrollToBottom();
|
scrollToBottom();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ public class MessageResponse {
|
|||||||
private String content;
|
private String content;
|
||||||
private LocalDateTime timestamp;
|
private LocalDateTime timestamp;
|
||||||
private Boolean isRead;
|
private Boolean isRead;
|
||||||
|
private String attachmentName;
|
||||||
|
private String attachmentUrl;
|
||||||
|
|
||||||
public MessageResponse() {
|
public MessageResponse() {
|
||||||
}
|
}
|
||||||
@@ -87,4 +89,20 @@ public class MessageResponse {
|
|||||||
public void setIsRead(Boolean isRead) {
|
public void setIsRead(Boolean isRead) {
|
||||||
this.isRead = isRead;
|
this.isRead = isRead;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getAttachmentName() {
|
||||||
|
return attachmentName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAttachmentName(String attachmentName) {
|
||||||
|
this.attachmentName = attachmentName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAttachmentUrl() {
|
||||||
|
return attachmentUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAttachmentUrl(String attachmentUrl) {
|
||||||
|
this.attachmentUrl = attachmentUrl;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user