Example #1
0
        public void Refresh()
        {
            DataChat chat = ChatManager.GetChat(mCurrentContact.ID);

            if (chat == null)
            {
                Clear();
                return;
            }

            bool addedNewMessage = false;

            // Add new messages
            for (int i = 0; i < chat.Messages.Length; i++)
            {
                DataChatMessage message = chat.Messages[i];

                if (mListedEntries.Contains(message.ID))
                {
                    continue;                     // Message is already in the log
                }
                mListedEntries.Add(message.ID);

                bool shouldAppend = false;

                // Check if this message should be appended to the latest entry or create new entry
                if (mChatLog.Count > 0)
                {
                    ChatUIEntry lastEntry = mChatLog[mChatLog.Count - 1];

                    if (lastEntry.FromUser.ID == message.User.ID &&
                        (message.CreatedAt.Value - lastEntry.LastMessageTime) < TimeSpan.FromMinutes(4))
                    {
                        shouldAppend = true;
                    }
                }

                if (shouldAppend)
                {
                    mChatLog[mChatLog.Count - 1].AddMessage(message);
                }
                else
                {
                    ChatUIEntry newEntry = Instantiate(message.User.IsMe ? mEntryMe : mEntryOther, mChatLogRoot);
                    mChatLog.Add(newEntry);
                    newEntry.Initialize(message);
                }

                addedNewMessage = true;
            }

            if (addedNewMessage)
            {
                Vector2 position = mChatLogRoot.anchoredPosition;
                position.y = 0;
                mChatLogRoot.anchoredPosition = position;
            }
        }
Example #2
0
        private static void OnGetChatSuccess(string json)
        {
            Dictionary <string, object> data = Json.Deserialize <Dictionary <string, object> >(json);

            DataChat chat = new DataChat();

            chat.SetData(data);
            int contactID = -1;

            for (int i = 0; i < chat.Users.Length; i++)
            {
                if (!chat.Users[i].IsMe)
                {
                    contactID         = chat.Users[i].ID;
                    mChats[contactID] = chat;
                }
            }

            if (OnGetChat != null)
            {
                OnGetChat(contactID);
            }
        }