|
| 1 | +package com.quickblox.sample.chat.managers; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.os.Handler; |
| 5 | +import android.os.Looper; |
| 6 | + |
| 7 | +import com.quickblox.chat.QBChatService; |
| 8 | +import com.quickblox.chat.QBSystemMessagesManager; |
| 9 | +import com.quickblox.chat.model.QBChatDialog; |
| 10 | +import com.quickblox.chat.model.QBChatMessage; |
| 11 | +import com.quickblox.chat.model.QBDialogType; |
| 12 | +import com.quickblox.sample.chat.utils.chat.ChatHelper; |
| 13 | +import com.quickblox.sample.chat.utils.qb.QbDialogHolder; |
| 14 | +import com.quickblox.sample.chat.utils.qb.QbDialogUtils; |
| 15 | +import com.quickblox.sample.chat.utils.qb.callback.QbEntityCallbackImpl; |
| 16 | +import com.quickblox.users.model.QBUser; |
| 17 | + |
| 18 | +import org.jivesoftware.smack.SmackException; |
| 19 | + |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.Set; |
| 24 | +import java.util.concurrent.CopyOnWriteArraySet; |
| 25 | + |
| 26 | +public class DialogsManager { |
| 27 | + |
| 28 | + public static final String PROPERTY_OCCUPANTS_IDS = "occupants_ids"; |
| 29 | + public static final String PROPERTY_DIALOG_TYPE = "dialog_type"; |
| 30 | + public static final String PROPERTY_DIALOG_NAME = "dialog_name"; |
| 31 | + public static final String PROPERTY_NOTIFICATION_TYPE = "notification_type"; |
| 32 | + public static final String CREATING_DIALOG = "creating_dialog"; |
| 33 | + |
| 34 | + private Set<ManagingDialogsCallbacks> managingDialogsCallbackListener = new CopyOnWriteArraySet<>(); |
| 35 | + |
| 36 | + private boolean isMessageCreatingDialog(QBChatMessage systemMessage){ |
| 37 | + return CREATING_DIALOG.equals(systemMessage.getProperty(PROPERTY_NOTIFICATION_TYPE)); |
| 38 | + } |
| 39 | + |
| 40 | + private QBChatMessage buildSystemMessageAboutCreatingGroupDialog(QBChatDialog dialog){ |
| 41 | + QBChatMessage qbChatMessage = new QBChatMessage(); |
| 42 | + qbChatMessage.setDialogId(dialog.getDialogId()); |
| 43 | + qbChatMessage.setProperty(PROPERTY_OCCUPANTS_IDS, QbDialogUtils.getOccupantsIdsStringFromList(dialog.getOccupants())); |
| 44 | + qbChatMessage.setProperty(PROPERTY_DIALOG_TYPE, String.valueOf(dialog.getType().getCode())); |
| 45 | + qbChatMessage.setProperty(PROPERTY_DIALOG_NAME, String.valueOf(dialog.getName())); |
| 46 | + qbChatMessage.setProperty(PROPERTY_NOTIFICATION_TYPE, CREATING_DIALOG); |
| 47 | + |
| 48 | + return qbChatMessage; |
| 49 | + } |
| 50 | + |
| 51 | + private QBChatDialog buildChatDialogFromSystemMessage(QBChatMessage qbChatMessage){ |
| 52 | + QBChatDialog chatDialog = new QBChatDialog(); |
| 53 | + chatDialog.setDialogId(qbChatMessage.getDialogId()); |
| 54 | + chatDialog.setOccupantsIds(QbDialogUtils.getOccupantsIdsListFromString((String) qbChatMessage.getProperty(PROPERTY_OCCUPANTS_IDS))); |
| 55 | + chatDialog.setType(QBDialogType.parseByCode(Integer.parseInt(qbChatMessage.getProperty(PROPERTY_DIALOG_TYPE).toString()))); |
| 56 | + chatDialog.setName(qbChatMessage.getProperty(PROPERTY_DIALOG_NAME).toString()); |
| 57 | + chatDialog.setUnreadMessageCount(0); |
| 58 | + |
| 59 | + return chatDialog; |
| 60 | + } |
| 61 | + |
| 62 | + public void sendSystemMessageAboutCreatingDialog(QBSystemMessagesManager systemMessagesManager, QBChatDialog dialog) { |
| 63 | + QBChatMessage systemMessageCreatingDialog = buildSystemMessageAboutCreatingGroupDialog(dialog); |
| 64 | + |
| 65 | + try { |
| 66 | + for (Integer recipientId : dialog.getOccupants()) { |
| 67 | + if (!recipientId.equals(QBChatService.getInstance().getUser().getId())) { |
| 68 | + systemMessageCreatingDialog.setRecipientId(recipientId); |
| 69 | + systemMessagesManager.sendSystemMessage(systemMessageCreatingDialog); |
| 70 | + } |
| 71 | + } |
| 72 | + } catch (SmackException.NotConnectedException e) { |
| 73 | + e.printStackTrace(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private void loadUsersFromDialog(QBChatDialog chatDialog){ |
| 78 | + ChatHelper.getInstance().getUsersFromDialog(chatDialog, new QbEntityCallbackImpl<ArrayList<QBUser>>()); |
| 79 | + } |
| 80 | + |
| 81 | + public void onGlobalMessageReceived(String dialogId, QBChatMessage chatMessage){ |
| 82 | + if (chatMessage.getBody() != null && chatMessage.isMarkable()) { //for excluding status messages until will be released v.3.1 |
| 83 | + if (QbDialogHolder.getInstance().hasDialogWithId(dialogId)) { |
| 84 | + QbDialogHolder.getInstance().updateDialog(dialogId, chatMessage); |
| 85 | + notifyListenersDialogUpdated(dialogId); |
| 86 | + } else { |
| 87 | + ChatHelper.getInstance().getDialogById(dialogId, new QbEntityCallbackImpl<QBChatDialog>() { |
| 88 | + @Override |
| 89 | + public void onSuccess(QBChatDialog chatDialog, Bundle bundle) { |
| 90 | + loadUsersFromDialog(chatDialog); |
| 91 | + QbDialogHolder.getInstance().addDialog(chatDialog); |
| 92 | + notifyListenersNewDialogLoaded(chatDialog); |
| 93 | + } |
| 94 | + }); |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public void onSystemMessageReceived(QBChatMessage systemMessage){ |
| 100 | + if (isMessageCreatingDialog(systemMessage)) { |
| 101 | + QBChatDialog chatDialog = buildChatDialogFromSystemMessage(systemMessage); |
| 102 | + chatDialog.initForChat(QBChatService.getInstance()); |
| 103 | + QbDialogHolder.getInstance().addDialog(chatDialog); |
| 104 | + notifyListenersDialogCreated(chatDialog); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private void notifyListenersDialogCreated(final QBChatDialog chatDialog){ |
| 109 | + new Handler(Looper.getMainLooper()).post(new Runnable() { |
| 110 | + @Override |
| 111 | + public void run() { |
| 112 | + for (ManagingDialogsCallbacks listener : getManagingDialogsCallbackListeners()){ |
| 113 | + listener.onDialogCreated(chatDialog); |
| 114 | + } |
| 115 | + } |
| 116 | + }); |
| 117 | + } |
| 118 | + |
| 119 | + private void notifyListenersDialogUpdated(final String dialogId){ |
| 120 | + new Handler(Looper.getMainLooper()).post(new Runnable() { |
| 121 | + @Override |
| 122 | + public void run() { |
| 123 | + for (ManagingDialogsCallbacks listener : getManagingDialogsCallbackListeners()){ |
| 124 | + listener.onDialogUpdated(dialogId); |
| 125 | + } |
| 126 | + } |
| 127 | + }); |
| 128 | + } |
| 129 | + |
| 130 | + private void notifyListenersNewDialogLoaded(final QBChatDialog chatDialog){ |
| 131 | + new Handler(Looper.getMainLooper()).post(new Runnable() { |
| 132 | + @Override |
| 133 | + public void run() { |
| 134 | + for (ManagingDialogsCallbacks listener : getManagingDialogsCallbackListeners()){ |
| 135 | + listener.onNewDialogLoaded(chatDialog); |
| 136 | + } |
| 137 | + } |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + public void addManagingDialogsCallbackListener(ManagingDialogsCallbacks listener){ |
| 142 | + if (listener != null){ |
| 143 | + managingDialogsCallbackListener.add(listener); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + public void removeManagingDialogsCallbackListener(ManagingDialogsCallbacks listener) { |
| 148 | + managingDialogsCallbackListener.remove(listener); |
| 149 | + } |
| 150 | + |
| 151 | + public Collection<ManagingDialogsCallbacks> getManagingDialogsCallbackListeners() { |
| 152 | + return Collections.unmodifiableCollection(managingDialogsCallbackListener); |
| 153 | + } |
| 154 | + |
| 155 | + public interface ManagingDialogsCallbacks{ |
| 156 | + |
| 157 | + void onDialogCreated(QBChatDialog chatDialog); |
| 158 | + |
| 159 | + void onDialogUpdated(String chatDialog); |
| 160 | + |
| 161 | + void onNewDialogLoaded(QBChatDialog chatDialog); |
| 162 | + } |
| 163 | +} |
0 commit comments