Search in sources :

Example 1 with ChatMessage

use of org.moire.ultrasonic.domain.ChatMessage in project ultrasonic by ultrasonic.

the class ChatAdapter method getView.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ChatMessage message = this.getItem(position);
    ViewHolder holder;
    int layout;
    String messageUser = message.getUsername();
    Date messageTime = new java.util.Date(message.getTime());
    String messageText = message.getMessage();
    String me = Util.getUserName(activity, Util.getActiveServer(activity));
    layout = messageUser.equals(me) ? R.layout.chat_item_reverse : R.layout.chat_item;
    if (convertView == null) {
        convertView = inflateView(layout, parent);
        holder = createViewHolder(layout, convertView);
    } else {
        holder = (ViewHolder) convertView.getTag();
        if (!holder.chatMessage.equals(message)) {
            convertView = inflateView(layout, parent);
            holder = createViewHolder(layout, convertView);
        }
    }
    holder.chatMessage = message;
    DateFormat timeFormat = android.text.format.DateFormat.getTimeFormat(activity);
    String messageTimeFormatted = String.format("[%s]", timeFormat.format(messageTime));
    ImageLoader imageLoader = activity.getImageLoader();
    if (imageLoader != null) {
        imageLoader.loadAvatarImage(holder.avatar, messageUser, false, holder.avatar.getWidth(), false, true);
    }
    holder.username.setText(messageUser);
    holder.message.setText(messageText);
    holder.time.setText(messageTimeFormatted);
    return convertView;
}
Also used : ChatMessage(org.moire.ultrasonic.domain.ChatMessage) DateFormat(java.text.DateFormat) ImageLoader(org.moire.ultrasonic.util.ImageLoader) Date(java.util.Date)

Example 2 with ChatMessage

use of org.moire.ultrasonic.domain.ChatMessage in project ultrasonic by ultrasonic.

the class ChatActivity method load.

private synchronized void load() {
    BackgroundTask<List<ChatMessage>> task = new TabActivityBackgroundTask<List<ChatMessage>>(this, false) {

        @Override
        protected List<ChatMessage> doInBackground() throws Throwable {
            MusicService musicService = MusicServiceFactory.getMusicService(ChatActivity.this);
            return musicService.getChatMessages(lastChatMessageTime, ChatActivity.this, this);
        }

        @Override
        protected void done(List<ChatMessage> result) {
            if (result != null && !result.isEmpty()) {
                // Reset lastChatMessageTime if we have a newer message
                for (ChatMessage message : result) {
                    if (message.getTime() > lastChatMessageTime) {
                        lastChatMessageTime = message.getTime();
                    }
                }
                // Reverse results to show them on the bottom
                Collections.reverse(result);
                messageList.addAll(result);
                ListAdapter chatAdapter = new ChatAdapter(ChatActivity.this, messageList);
                chatListView.setAdapter(chatAdapter);
            }
        }
    };
    task.execute();
}
Also used : TabActivityBackgroundTask(org.moire.ultrasonic.util.TabActivityBackgroundTask) MusicService(org.moire.ultrasonic.service.MusicService) ChatMessage(org.moire.ultrasonic.domain.ChatMessage) ArrayList(java.util.ArrayList) List(java.util.List) ChatAdapter(org.moire.ultrasonic.view.ChatAdapter) ListAdapter(android.widget.ListAdapter)

Aggregations

ChatMessage (org.moire.ultrasonic.domain.ChatMessage)2 ListAdapter (android.widget.ListAdapter)1 DateFormat (java.text.DateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 List (java.util.List)1 MusicService (org.moire.ultrasonic.service.MusicService)1 ImageLoader (org.moire.ultrasonic.util.ImageLoader)1 TabActivityBackgroundTask (org.moire.ultrasonic.util.TabActivityBackgroundTask)1 ChatAdapter (org.moire.ultrasonic.view.ChatAdapter)1