Search in sources :

Example 1 with Chat

use of com.romainpiel.model.Chat in project meatspace-android by RomainPiel.

the class ChatFragment method onActivityCreated.

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
        listViewState = savedInstanceState.getParcelable(STATE_LISTVIEW);
        listPosition = savedInstanceState.getInt(POSITION_LIST);
        itemPosition = savedInstanceState.getInt(POSITION_ITEM);
    }
    if (adapter == null) {
        adapter = new ChatAdapter(getActivity());
        adapter.setOnMuteClickListener(new OnMenuClickListener<Chat>() {

            @Override
            public void onMenuClick(Chat item) {
                BusManager.get().getChatBus().post(new MuteEvent(true, item.getValue().getFingerprint()));
            }
        });
    }
    if (device == null) {
        device = new Device(getActivity());
    }
    listView.setAdapter(adapter);
}
Also used : Device(com.romainpiel.model.Device) Chat(com.romainpiel.model.Chat) ChatAdapter(com.romainpiel.lib.ui.adapter.ChatAdapter) MuteEvent(com.romainpiel.lib.bus.MuteEvent)

Example 2 with Chat

use of com.romainpiel.model.Chat in project meatspace-android by RomainPiel.

the class ChatService method syncChatList.

/**
 * synchronize the chat list:
 * - with the muted users list
 * - init isFromMe variable
 *
 * @param chatList chat list to sync
 */
private void syncChatList(ChatList chatList) {
    String myFingerprint = new Device(this).getId();
    Collection<Chat> list = chatList.get();
    Chat.Value chatValue;
    String fingerprint;
    for (Chat chat : list) {
        chatValue = chat.getValue();
        fingerprint = chatValue.getFingerprint();
        chatValue.setMuted(mutedUsers.contains(fingerprint));
        chatValue.setFromMe(fingerprint.equals(myFingerprint));
    }
}
Also used : Device(com.romainpiel.model.Device) Chat(com.romainpiel.model.Chat)

Example 3 with Chat

use of com.romainpiel.model.Chat in project meatspace-android by RomainPiel.

the class ChatService method onEvent.

/**
 * Socket event callback
 *
 * @param dataString  raw data of the message
 * @param acknowledge socket channel details
 */
@Override
public void onEvent(final String dataString, Acknowledge acknowledge) {
    final Gson jsonParser = apiManager.getJsonParser();
    BackgroundExecutor.execute(new Runnable() {

        @Override
        public void run() {
            try {
                final SocketChatEvent event = jsonParser.fromJson(dataString, SocketChatEvent.class);
                final String name = event.getName();
                final List<Chat> chats = event.getChats();
                if (!ApiManager.EVENT_MESSAGE.equals(name) || chats == null)
                    return;
                handler.post(new Runnable() {

                    @Override
                    public void run() {
                        ChatList newChats = new ChatList(chats);
                        syncChatList(newChats);
                        saveAndPost(newChats);
                        int newMissedMessageCount = 0;
                        for (Chat chat : chats) {
                            // don't count if it's from me
                            if (!chat.getValue().isFromMe()) {
                                newMissedMessageCount++;
                            }
                        }
                        if (appInBackground && newMissedMessageCount > 0) {
                            missedMessageCount += newMissedMessageCount;
                            showForeground();
                        }
                    }
                });
            } catch (Exception e) {
                Debug.out(e);
            }
        }
    });
}
Also used : ChatList(com.romainpiel.model.ChatList) Chat(com.romainpiel.model.Chat) Gson(com.google.gson.Gson) ChatList(com.romainpiel.model.ChatList) List(java.util.List) SocketChatEvent(com.romainpiel.model.SocketChatEvent)

Example 4 with Chat

use of com.romainpiel.model.Chat in project meatspace-android by RomainPiel.

the class ChatItemView method bind.

public void bind(final Chat chat) {
    if (chat != null) {
        Chat.Value value = chat.getValue();
        try {
            ByteArrayInputStream in = new ByteArrayInputStream(value.getMedia().getBytes());
            GifDrawable gifFromStream = new GifDrawable(in);
            gif.setImageDrawable(gifFromStream);
            gif.setVisibility(VISIBLE);
        } catch (Exception e) {
            Debug.out(e);
            gif.setVisibility(INVISIBLE);
        }
        Date date = new Date(value.getCreated());
        timestamp.setText(com.romainpiel.lib.utils.DateUtils.formatTime(getContext(), date));
        message.setText(value.getMessage());
        if (!chat.getValue().isFromMe()) {
            menuButton.setVisibility(VISIBLE);
            popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    if (item.getItemId() == R.id.menu_card_mute && onMuteClickListener != null) {
                        onMuteClickListener.onMenuClick(chat);
                    }
                    return true;
                }
            });
        } else {
            menuButton.setVisibility(INVISIBLE);
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Chat(com.romainpiel.model.Chat) GifDrawable(pl.droidsonroids.gif.GifDrawable) MenuItem(android.view.MenuItem) Date(java.util.Date) PopupMenu(android.widget.PopupMenu)

Aggregations

Chat (com.romainpiel.model.Chat)4 Device (com.romainpiel.model.Device)2 MenuItem (android.view.MenuItem)1 PopupMenu (android.widget.PopupMenu)1 Gson (com.google.gson.Gson)1 MuteEvent (com.romainpiel.lib.bus.MuteEvent)1 ChatAdapter (com.romainpiel.lib.ui.adapter.ChatAdapter)1 ChatList (com.romainpiel.model.ChatList)1 SocketChatEvent (com.romainpiel.model.SocketChatEvent)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Date (java.util.Date)1 List (java.util.List)1 GifDrawable (pl.droidsonroids.gif.GifDrawable)1