forked from criticalmaps/criticalmaps-android
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatMessageAdapter.java
More file actions
68 lines (53 loc) · 2.35 KB
/
ChatMessageAdapter.java
File metadata and controls
68 lines (53 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package de.stephanlindauer.criticalmaps.adapter;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.text.DateFormat;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import de.stephanlindauer.criticalmaps.databinding.ViewChatmessageBinding;
import de.stephanlindauer.criticalmaps.model.chat.ReceivedChatMessage;
import de.stephanlindauer.criticalmaps.utils.TimeToWordStringConverter;
public class ChatMessageAdapter extends RecyclerView.Adapter<ChatMessageAdapter.ChatMessageViewHolder> {
private List<ReceivedChatMessage> chatMessages;
static class ChatMessageViewHolder extends RecyclerView.ViewHolder {
private final ViewChatmessageBinding binding;
private final DateFormat dateFormatter = DateFormat.getDateTimeInstance(
DateFormat.DEFAULT, DateFormat.SHORT, Locale.getDefault());
ChatMessageViewHolder(ViewChatmessageBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
void bind(ReceivedChatMessage message) {
binding.chatmessageMessageText.setText(message.getMessage());
dateFormatter.setTimeZone(TimeZone.getDefault());
binding.chatmessageLabelText.setText(TimeToWordStringConverter.getTimeAgo(
message.getTimestamp(), itemView.getContext()));
}
}
public ChatMessageAdapter(List<ReceivedChatMessage> chatMessages) {
this.chatMessages = chatMessages;
}
@NonNull
@Override
public ChatMessageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
final ViewChatmessageBinding binding =
ViewChatmessageBinding.inflate(inflater, parent, false);
return new ChatMessageViewHolder(binding);
}
@Override
public void onBindViewHolder(@NonNull ChatMessageViewHolder holder, int position) {
holder.bind(chatMessages.get(position));
}
@Override
public int getItemCount() {
return chatMessages.size();
}
public void updateData(List<ReceivedChatMessage> savedAndOutgoingMessages) {
this.chatMessages = savedAndOutgoingMessages;
notifyDataSetChanged();
}
}