private void HandleSendData(Dictionary <string, string> data)
        {
            if (data == null)
            {
                return;
            }

            if (ChannelsView.CurrentSelectedChannel == null)
            {
                return;
            }

            FizzChannel channel = ChannelsView.CurrentSelectedChannel;

            try
            {
                long now = FizzUtils.Now();
                data.Add(FizzMessageCellModel.KEY_CLIENT_ID, now + "");

                FizzMessageCellModel model = new FizzMessageCellModel(
                    now,
                    FizzService.Instance.UserId,
                    FizzService.Instance.UserName,
                    channel.Id,
                    string.Empty,
                    data,
                    null,
                    now)
                {
                    DeliveryState = FizzChatCellDeliveryState.Pending
                };

                MessagesView.AddMessage(model);

                FizzService.Instance.Client.Chat.PublishMessage(
                    channel.Id,
                    FizzService.Instance.UserName,
                    string.Empty,
                    data,
                    FizzService.Instance.IsTranslationEnabled,
                    channel.Meta.FilterContent,
                    channel.Meta.PersistMessages,
                    exception =>
                {
                    if (exception == null)
                    {
                        model.DeliveryState = FizzChatCellDeliveryState.Sent;
                        MessagesView.AddMessage(model);


                        string dataStr = Utils.GetDictionaryToString(data, FizzMessageCellModel.KEY_CLIENT_ID);
                        FizzService.Instance.Client.Ingestion.TextMessageSent(channel.Id, dataStr, FizzService.Instance.UserName);
                    }
                });
            }
            catch
            {
                FizzLogger.E("Something went wrong while calling PublishMessage of FizzService.");
            }
        }
        public int GetItemType(int index)
        {
            ChatCellViewType     actionType;
            FizzMessageCellModel model = _chatCellModelList[index];

            if (model.Type == FizzChatCellType.ChatCell)
            {
                FizzMessageCellModel lastAction = null;
                FizzMessageCellModel chatAction = _chatCellModelList[index];

                int lastIndex = index - 1;
                while (lastIndex > -1)
                {
                    if (_chatCellModelList[lastIndex].Type == FizzChatCellType.ChatCell)
                    {
                        lastAction = _chatCellModelList[lastIndex];
                        break;
                    }
                    lastIndex--;
                }

                string senderId = chatAction.From;

                bool ownMessage = _userId.Equals(senderId);

                actionType = ownMessage ? ChatCellViewType.YoursMessageAction : ChatCellViewType.TheirsMessageAction;
            }
            else
            {
                actionType = ChatCellViewType.DateHeader;
            }

            return((int)actionType);
        }
        private void CheckForDateHeader(FizzChannelMessage action)
        {
            bool shouldAddHeaderModel = false;

            if (_lastAction == null)
            {
                shouldAddHeaderModel = true;
                _lastAction          = action;
            }
            else
            {
                DateTime last = Utils.GetDateTimeToUnixTime(_lastAction.Created);
                last = last.Date;
                DateTime current = Utils.GetDateTimeToUnixTime(action.Created);
                TimeSpan ts      = current.Subtract(last);
                if (ts.Days > 0)
                {
                    shouldAddHeaderModel = true;
                }
                _lastAction = action;
            }

            if (shouldAddHeaderModel)
            {
                FizzMessageCellModel model = GetChatCellModelFromAction(action);
                model.Type = FizzChatCellType.DateCell;

                _chatCellModelList.Add(model);
            }
        }
 void OnChannelMessage(string channelId, FizzChannelMessage action)
 {
     if (_channel != null && _channel.Id.Equals(channelId))
     {
         FizzMessageCellModel model = GetChatCellModelFromAction(action);
         model.DeliveryState = FizzChatCellDeliveryState.Published;
         AddAction(model);
     }
 }
        private FizzMessageCellModel GetChatCellModelFromAction(FizzChannelMessage action)
        {
            var model = new FizzMessageCellModel(action.Id, action.From, action.Nick, action.To, action.Body, action.Data, action.Translations, action.Created)
            {
                Type = FizzChatCellType.ChatCell
            };

            return(model);
        }
        public void AddMessage(FizzMessageCellModel cellModel)
        {
            if (cellModel == null)
            {
                return;
            }

            AddAction(cellModel);
        }
Beispiel #7
0
        public override void SetData(FizzMessageCellModel model, bool appTranslationEnabled)
        {
            base.SetData(model, appTranslationEnabled);

            NickLabel.text = _model.Nick;
            //NickLabel.color = Utils.GetUserNickColor(_model.From);

            LoadChatMessageAction();
        }
        private FizzMessageCellModel GetActionFromLookup(FizzMessageCellModel action)
        {
            FizzMessageCellModel model = null;

            if (!string.IsNullOrEmpty(action.AlternateId.ToString()) && _actionsLookUpDict.ContainsKey(action.AlternateId.ToString()))
            {
                _actionsLookUpDict.TryGetValue(action.AlternateId.ToString(), out model);
            }
            else if (!string.IsNullOrEmpty(action.Id.ToString()) && _actionsLookUpDict.ContainsKey(action.Id.ToString()))
            {
                _actionsLookUpDict.TryGetValue(action.Id.ToString(), out model);
            }
            return(model);
        }
 private void AddActionInLookup(FizzMessageCellModel model)
 {
     if (model.Type == FizzChatCellType.ChatCell)
     {
         if (!string.IsNullOrEmpty(model.Id.ToString()) && !_actionsLookUpDict.ContainsKey(model.Id.ToString()))
         {
             _actionsLookUpDict.Add(model.Id.ToString(), model);
         }
         else if (!string.IsNullOrEmpty(model.AlternateId.ToString()) && !_actionsLookUpDict.ContainsKey(model.AlternateId.ToString()))
         {
             _actionsLookUpDict.Add(model.AlternateId.ToString(), model);
         }
     }
 }
        private void LoadMessages()
        {
            _chatCellModelList.Clear();
            _actionsLookUpDict.Clear();

            IList <FizzChannelMessage> actionsList = _channel.Messages;

            for (int index = 0; index < actionsList.Count; index++)
            {
                FizzChannelMessage action = actionsList[index];

                CheckForDateHeader(action);

                FizzMessageCellModel model = GetChatCellModelFromAction(action);
                model.DeliveryState = FizzChatCellDeliveryState.Published;

                _chatCellModelList.Add(model);
                AddActionInLookup(model);
            }
        }
        public virtual void SetData(FizzMessageCellModel model, bool appTranslationEnabled)
        {
            _model = model;
            _appTranslationEnabled = appTranslationEnabled;

            if (TimeLabel != null)
            {
                DateTime dt         = Utils.GetDateTimeToUnixTime(_model.Created);
                string   timeFormat = string.Format("{0:h:mm tt}", dt);
                TimeLabel.text = timeFormat;
            }

            if (CustomNode != null)
            {
                CustomNode.DestroyChildren();
                CustomNode.gameObject.SetActive(false);
            }

            MessageLabel.gameObject.SetActive(true);
        }
        private void AddAction(FizzMessageCellModel model, bool groupRefresh = false, bool hardRefresh = false, bool updateOnly = false)
        {
            if (model.Type == FizzChatCellType.ChatCell &&
                _channel.Id.Equals(model.To))
            {
                FizzMessageCellModel existingModel = GetActionFromLookup(model);
                if (existingModel != null)
                {
                    existingModel.Update(model);

                    if (!groupRefresh)
                    {
                        _isDirty = true;
                    }
                }
                else
                {
                    if (!updateOnly)
                    {
                        CheckForDateHeader(model);
                        _chatCellModelList.Add(model);
                        AddActionInLookup(model);
                        if (hardRefresh)
                        {
                            ScrollRect.RefreshContent();
                            ScrollRect.GoToScrollItem(_chatCellModelList.Count - 1);
                        }
                        else
                        {
                            // Should refresh content
                            _isDirty = true;
                            // Should reset scroll
                            _resetScroll = true;
                            // show scroll indicator
                            ScrollIndicator.gameObject.SetActive(_userScroll);
                        }
                    }
                }
            }
        }
        private void RemoveAction(FizzMessageCellModel model)
        {
            if (model.Type == FizzChatCellType.ChatCell &&
                _channel.Id.Equals(model.To))
            {
                FizzMessageCellModel existingModel = GetActionFromLookup(model);
                if (existingModel != null)
                {
                    if (!string.IsNullOrEmpty(existingModel.AlternateId.ToString()) &&
                        _actionsLookUpDict.ContainsKey(existingModel.AlternateId.ToString()))
                    {
                        _actionsLookUpDict.Remove(existingModel.AlternateId.ToString());
                    }
                    else if (!string.IsNullOrEmpty(existingModel.Id.ToString()) &&
                             _actionsLookUpDict.ContainsKey(existingModel.Id.ToString()))
                    {
                        _actionsLookUpDict.Remove(existingModel.AlternateId.ToString());
                    }
                    _chatCellModelList.Remove(existingModel);

                    _isDirty = true;
                }
            }
        }
        public GameObject GetListItem(int index, int itemType, GameObject obj)
        {
            if (obj == null)
            {
                switch (itemType)
                {
                case (int)ChatCellViewType.YoursMessageAction:
                    obj = Instantiate(userCellView.gameObject);
                    break;

                case (int)ChatCellViewType.TheirsMessageAction:
                    obj = Instantiate(otherCellView.gameObject);
                    break;

                case (int)ChatCellViewType.DateHeader:
                    obj = Instantiate(dateHeaderCellView.gameObject);
                    break;

                default:
                    obj = Instantiate(userCellView.gameObject);
                    break;
                }
            }

            FizzMessageCellModel model = _chatCellModelList[index];

            if (model.Type == FizzChatCellType.ChatCell)
            {
                FizzMessageCellView chatCellView = obj.GetComponent <FizzMessageCellView>();
                chatCellView.rowNumber = index;
                chatCellView.SetData(model, ShowMessageTranslation);

                if (itemType == (int)ChatCellViewType.TheirsMessageAction)
                {
                    var leftCell = chatCellView as FizzMessageOtherCellView;
                    leftCell.OnTranslateToggle = OnTranslateToggleClicked;
                }
                else if (itemType == (int)ChatCellViewType.TheirsRepeatMessageAction)
                {
                    var leftRepeatCell = chatCellView as FizzMessageOtherRepeatCellView;
                    leftRepeatCell.OnTranslateToggle = OnTranslateToggleClicked;
                }

                if (model.Data != null)
                {
                    RectTransform customView = null;
                    if (_chatDataSource != null)
                    {
                        customView = _chatDataSource.GetCustomMessageCellViewNode(model);
                        chatCellView.SetCustomData(customView);
                    }
                    else if (_chatDataSource == null && string.IsNullOrEmpty(model.Body))
                    {
                        chatCellView.SetCustomData(customView);
                    }
                }
            }
            else if (model.Type == FizzChatCellType.DateCell)
            {
                FizzMessageDateHeaderCellView dateHeader = obj.GetComponent <FizzMessageDateHeaderCellView>();
                dateHeader.SetData(model, ShowMessageTranslation);
            }

            return(obj);
        }
        public override void SetData(FizzMessageCellModel model, bool appTranslationEnabled)
        {
            base.SetData(model, appTranslationEnabled);

            MessageLabel.text = Utils.GetFormattedTimeForUnixTimeStamp(_model.Created);
        }
        public override void SetData(FizzMessageCellModel model, bool appTranslationEnabled)
        {
            base.SetData(model, appTranslationEnabled);

            LoadChatMessageAction();
        }
Beispiel #17
0
        private void HandleSendMessage(string message)
        {
            if (string.IsNullOrEmpty(message))
            {
                return;
            }

            FizzChannelModel channel = ChannelsView.CurrentSelectedChannel;

            if (channel == null)
            {
                return;
            }

            try
            {
                long now = FizzUtils.Now();
                Dictionary <string, string> data = new Dictionary <string, string>
                {
                    { FizzMessageCellModel.KEY_CLIENT_ID, now + "" }
                };

                FizzMessageCellModel model = new FizzMessageCellModel(
                    now,
                    FizzService.Instance.UserId,
                    FizzService.Instance.UserName,
                    channel.Id,
                    message,
                    data,
                    null,
                    now)
                {
                    DeliveryState = FizzChatCellDeliveryState.Pending
                };

                MessagesView.AddMessage(model);

                channel.PublishMessage(
                    FizzService.Instance.UserName,
                    message,
                    GetLanguageCode(),
                    data,
                    FizzService.Instance.IsTranslationEnabled,
                    exception =>
                {
                    if (exception == null)
                    {
                        if (model.DeliveryState != FizzChatCellDeliveryState.Published)
                        {
                            model.DeliveryState = FizzChatCellDeliveryState.Sent;
                        }
                        MessagesView.AddMessage(model);

                        FizzService.Instance.Client.Ingestion.TextMessageSent(channel.Id, message, FizzService.Instance.UserName);
                    }
                });
            }
            catch
            {
                FizzLogger.E("Something went wrong while calling PublishMessage of FizzService.");
            }
        }