Search in sources :

Example 1 with SocketChatEvent

use of com.romainpiel.model.SocketChatEvent 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)

Aggregations

Gson (com.google.gson.Gson)1 Chat (com.romainpiel.model.Chat)1 ChatList (com.romainpiel.model.ChatList)1 SocketChatEvent (com.romainpiel.model.SocketChatEvent)1 List (java.util.List)1