private void RemoveAction(UIChatCellModel model) { if (model.Type == UIChatCellModel.UIChatCellModelType.ChatAction) { if (_data.Id.Equals(model.Action.To)) { UIChatCellModel existingModel = GetActionFromLookup(model.Action); if (existingModel != null) { if (!string.IsNullOrEmpty(existingModel.Action.AlternateId.ToString()) && _actionsLookUpDict.ContainsKey(existingModel.Action.AlternateId.ToString())) { _actionsLookUpDict.Remove(existingModel.Action.AlternateId.ToString()); } else if (!string.IsNullOrEmpty(existingModel.Action.Id.ToString()) && _actionsLookUpDict.ContainsKey(existingModel.Action.Id.ToString())) { _actionsLookUpDict.Remove(existingModel.Action.AlternateId.ToString()); } _chatCellModelList.Remove(existingModel); _isDirty = true; } } } }
private void SendChatMessage(string messageStr) { if (_data == null) { return; } long now = FizzUtils.Now(); Dictionary <string, string> data = new Dictionary <string, string>(); data.Add(FizzUIMessage.KEY_CLIENT_ID, now + ""); FizzChannelMessage message = new FizzChannelMessage( now, FizzService.Instance.UserId, FizzService.Instance.UserName, _data.Id, messageStr, data, null, now); UIChatCellModel model = GetChatCellModelFromAction(message); AddNewAction(model, false, true); FizzService.Instance.PublishMessage(message.To, message.Nick, message.Body, message.Data, FizzService.Instance.IsTranslationEnabled, true, ex => { if (ex == null) { model.Action.PublishState = UIChannelMessageState.Sent; AddNewAction(model); } }); }
private void CheckForDateHeader(FizzUIMessage 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) { UIChatCellModel model = new UIChatCellModel(); model.Action = action; model.Type = UIChatCellModel.UIChatCellModelType.DateHeader; _chatCellModelList.Add(model); } }
void OnChannelMessageDeleted(string channelId, FizzChannelMessage action) { if (_data != null && _data.Id.Equals(channelId)) { UIChatCellModel model = GetChatCellModelFromAction(action); RemoveAction(model); } }
public int GetItemType(int index) { UIChatActionType actionType; UIChatCellModel model = _chatCellModelList[index]; if (model.Type == UIChatCellModel.UIChatCellModelType.ChatAction) { FizzUIMessage lastAction = null; FizzUIMessage nextAction = null; FizzUIMessage chatAction = _chatCellModelList[index].Action; int lastIndex = index - 1; while (lastIndex > -1) { if (_chatCellModelList[lastIndex].Type == UIChatCellModel.UIChatCellModelType.ChatAction) { lastAction = _chatCellModelList[lastIndex].Action; break; } lastIndex--; } int nextIndex = index + 1; while (nextIndex < _chatCellModelList.Count) { if (_chatCellModelList[nextIndex].Type == UIChatCellModel.UIChatCellModelType.ChatAction) { nextAction = _chatCellModelList[nextIndex].Action; break; } nextIndex++; } string senderId = chatAction.From; bool ownMessage = _userId.Equals(senderId); UIChatActionType cellType = UIChatActionType.TheirsMessageAction; cellType = ownMessage ? UIChatActionType.YoursMessageAction : UIChatActionType.TheirsMessageAction; if (!ownMessage && lastAction != null && chatAction.From.Equals(lastAction.From)) { cellType = UIChatActionType.TheirsRepeatMessageAction; } // else if (ownMessage && nextAction != null && chatAction.From.Equals (nextAction.From)) { // cellType = UIChatActionType.YoursRepeatMessageAction; // } actionType = cellType; } else { actionType = UIChatActionType.DateHeader; } return((int)actionType); }
void OnChannelMessage(string channelId, FizzChannelMessage action) { if (_data != null && _data.Id.Equals(channelId)) { UIChatCellModel model = GetChatCellModelFromAction(action); model.Action.PublishState = UIChannelMessageState.Published; AddNewAction(model); } }
private UIChatCellModel GetChatCellModelFromAction(FizzChannelMessage action) { var model = new UIChatCellModel { Action = new FizzUIMessage(action.Id, action.From, action.Nick, action.To, action.Body, action.Data, action.Translations, action.Created), Type = UIChatCellModel.UIChatCellModelType.ChatAction }; return(model); }
private UIChatCellModel GetActionFromLookup(FizzUIMessage action) { UIChatCellModel 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(UIChatCellModel model) { if (model.Type == UIChatCellModel.UIChatCellModelType.ChatAction) { FizzUIMessage action = model.Action; if (!string.IsNullOrEmpty(action.Id.ToString()) && !_actionsLookUpDict.ContainsKey(action.Id.ToString())) { _actionsLookUpDict.Add(action.Id.ToString(), model); } else if (!string.IsNullOrEmpty(action.AlternateId.ToString()) && !_actionsLookUpDict.ContainsKey(action.AlternateId.ToString())) { _actionsLookUpDict.Add(action.AlternateId.ToString(), model); } } }
private void LoadMessages() { _chatCellModelList.Clear(); _actionsLookUpDict.Clear(); IList <FizzChannelMessage> actionsList = _data.Messages; for (int index = 0; index < actionsList.Count; index++) { FizzChannelMessage action = actionsList[index]; UIChatCellModel model = GetChatCellModelFromAction(action); model.Action.PublishState = UIChannelMessageState.Published; CheckForDateHeader(model.Action); _chatCellModelList.Add(model); AddActionInLookup(model); } }
private void AddNewAction(UIChatCellModel model, bool groupRefresh = false, bool hardRefresh = false, bool updateOnly = false) { if (model.Type == UIChatCellModel.UIChatCellModelType.ChatAction) { if (_data.Id.Equals(model.Action.To)) { UIChatCellModel existingModel = GetActionFromLookup(model.Action); if (existingModel != null) { existingModel.Action = model.Action; if (!groupRefresh) { _isDirty = true; } } else { if (!updateOnly) { CheckForDateHeader(model.Action); _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); } } } } } }
public GameObject GetListItem(int index, int itemType, GameObject obj) { if (obj == null) { switch (itemType) { case (int)UIChatActionType.YoursMessageAction: obj = Instantiate(rightCellView.gameObject); break; case (int)UIChatActionType.YoursRepeatMessageAction: obj = Instantiate(rightRepeatCellView.gameObject); break; case (int)UIChatActionType.TheirsMessageAction: obj = Instantiate(leftCellView.gameObject); break; case (int)UIChatActionType.TheirsRepeatMessageAction: obj = Instantiate(leftRepeatCellView.gameObject); break; case (int)UIChatActionType.DateHeader: obj = Instantiate(dateHeaderCellView.gameObject); break; } } UIChatCellModel model = _chatCellModelList[index]; if (model.Type == UIChatCellModel.UIChatCellModelType.ChatAction) { FizzUIMessage action = model.Action; UIChatCellView chatCellView = obj.GetComponent <UIChatCellView> (); chatCellView.rowNumber = index; chatCellView.SetData(action, _isAppTranslationEnabled); if (itemType == (int)UIChatActionType.TheirsMessageAction) { var leftCell = chatCellView as UILeftChatCellView; leftCell.onTranslateTogglePressed = OnTranslateToggleClicked; } else if (itemType == (int)UIChatActionType.TheirsRepeatMessageAction) { var leftRepeatCell = chatCellView as UILeftRepeatChatCellView; leftRepeatCell.onTranslateTogglePressed = OnTranslateToggleClicked; } if (action.Data != null) { RectTransform customView = null; if (_chatDataSource != null) { customView = _chatDataSource.GetCustomMessageDrawable(action); if (customView != null) { chatCellView.SetCustomData(customView); } } } } else if (model.Type == UIChatCellModel.UIChatCellModelType.DateHeader) { UIChatDateHeaderCellView dateHeader = obj.GetComponent <UIChatDateHeaderCellView> (); dateHeader.SetData(model.Action, _isAppTranslationEnabled); } return(obj); }