Beispiel #1
0
        public ChatViewModel(ConversatinInfoViewModel conversationInfo, BusinessCoreService business)
        {
            _conversationInfo = conversationInfo;
            ConversationId = _conversationInfo.Info.TargetId;
            _businessCoreService = business;

            _businessCoreService.Subscribe(CommandTypeEnum.HistoryMessage, HistoryMessageCallBack);
            _businessCoreService.Subscribe(CommandTypeEnum.Receive, ReceiveCallBack);
            _businessCoreService.Subscribe(CommandTypeEnum.GetDiscussionInfo, GetDiscussionInfoCallBack);
            _businessCoreService.GetHistoryMessage(conversationInfo.Info.TargetId, _conversationInfo.Info.ConversationType);
            Members = new ObservableCollection<UserInfo>();
            if (_conversationInfo.Info.ConversationType == ConversationType.PRIVATE)
            {
                MembersTemp.Add(_businessCoreService.GetContacts().SingleOrDefault(x => x.Id == _conversationInfo.Info.TargetId));
            }
            else if (_conversationInfo.Info.ConversationType == ConversationType.DISCUSSION)
            {
                _businessCoreService.GetDiscussionInfo(conversationInfo.Info.TargetId);
            }

            _imageCallBack = new ImageListenerEventHandler(this.UploadedCallBack);
            StartTimer();

            _isExpandMember = false;

            MyWebClient.UploadFileCompleted += myWebClient_UploadFileCompleted;
            MyWebClient.UploadProgressChanged += myWebClient_UploadProgressChanged;

            _name = conversationInfo.Info.TargertName;
        }
        private void StartDiscussionCallBack(JObject jobj)
        {
            if (jobj["result"].ToString() == "success")
            {
                string discussionId = (string)jobj["discussionId"];
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    var conversatinInfoViewModel = new ConversatinInfoViewModel(new ConversationMessageInfo()
                    {
                        AvatarIcon = SystemConfig.AvatarUrl + discussionId,
                        Content = new ContentInfo()
                        {
                            MessageType = MessageTypeEnum.TxtMsg,
                            content = "讨论组"
                        },
                        ConversationId = discussionId,
                        ConversationType = ConversationType.DISCUSSION,
                        SendTime = DateTime.Now,
                        SenderId = UserContext.Current.Id,
                        TargertName = "讨论组",
                        TargetId = discussionId,
                        Type = MessageTypeEnum.TxtMsg,
                        UnreadCount = 0
                    }, _businessCoreService);

                    _conversations.Insert(0, conversatinInfoViewModel);
                    SelectedItem = conversatinInfoViewModel;
                }));
            }
            else
            {
                //创建讨论组失败
            }
        }
        private void ConversationCallBack(JObject jobj)
        {
            if (jobj["result"].ToString() == "success" && jobj["data"] != null)
            {
                try
                {
                    var result = JsonConvert.DeserializeObject<IList<ConversationInfo>>(jobj["data"].ToString());

                    var conversationList = MessageParser.ParserConversation(result);

                    if (_conversations.Count != 0)
                    {
                        _conversations.Clear();
                    }

                    foreach (var item in conversationList.Where((x, i) => conversationList.ToList().FindIndex(z => z.TargetId == x.TargetId) == i))
                    {
                        var conversatinInfoViewModel = new ConversatinInfoViewModel(item, _businessCoreService);
                        _conversations.Add(conversatinInfoViewModel);
                    }
                }
                catch
                {

                }
            }

            OnPropertyChanged("NewNotifyFlag");
        }
        private void ReceiveCallBack(JObject jobj)
        {
            var result = JsonConvert.DeserializeObject<RcReceiveMsg>(jobj.ToString());
            if (result.result == "success")
            {
                var flag = true;
                var msgInfo = MessageParser.ParserReceiveToConversation(result);
                if (msgInfo.Type == MessageTypeEnum.TxtMsg && string.IsNullOrEmpty(msgInfo.Content.content)) return;
                
                foreach (var item in _conversations)
                {
                    if (item.Info.TargetId == msgInfo.TargetId)
                    {
                        flag = false;
                        item.Count += SelectedItem != null && SelectedItem.Info.TargetId != msgInfo.TargetId ? 1 : 0;
                        item.Content = msgInfo.Content;
                        Application.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            _conversations.Move(_conversations.IndexOf(item), 0);
                        }));

                        msgInfo.TargetName = item.TargetName;

                        break;
                    }
                }

                if (flag)
                {
                    var newConversation = MessageParser.ParserConversation(result);
                    var conversatinInfoViewModel = new ConversatinInfoViewModel(newConversation,
                        _businessCoreService);

                    Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        Application.Current.Dispatcher.Invoke(new Action(() =>
                        {
                            _conversations.Insert(0, conversatinInfoViewModel);
                            _businessCoreService.GetDiscussionInfo(conversatinInfoViewModel.Info.TargetId);
                        }));
                    }));

                    msgInfo.TargetName = conversatinInfoViewModel.TargetName;
                }

                Subscription.DoNotify(msgInfo);
            }

            OnPropertyChanged("NewNotifyFlag");
        }
        private void NewConversationCallBack(JObject jobj)
        {
            var result = JsonConvert.DeserializeObject<IList<ConversationInfo>>(jobj["data"].ToString());
            var conversationList = MessageParser.ParserConversation(result);

            if (_conversations.All(x => x.Info.TargetId != conversationList[0].TargetId))
            {
                Application.Current.Dispatcher.Invoke(new Action(() =>
                {
                    var conversatinInfoViewModel = new ConversatinInfoViewModel(conversationList[0],
                        _businessCoreService);
                    _conversations.Insert(0, conversatinInfoViewModel);
                    SelectedItem = _conversations.SingleOrDefault(x => x.Info.TargetId == conversationList[0].TargetId);
                }));
            }
            else
            {
                //选中
                SelectedItem = _conversations.SingleOrDefault(x => x.Info.TargetId == conversationList[0].TargetId);
            }
        }