Exemplo n.º 1
0
        private string GetConversationMessagesViewUri(ConversationModel selectedItemData)
        {
            YapperChatViewModel viewModel = (YapperChatViewModel)this.DataContext;
            viewModel.AllConversations.SelectedConversation = selectedItemData;

            UserModel recipient = null;
            foreach (UserModel user in selectedItemData.ConversationParticipants)
            {
                if (!user.Equals(UserSettingsModel.Instance.Me))
                {
                    recipient = user;
                }
            }

            string uriString =
                string.Format(
                    "/Views/ConversationMessagesView.xaml?conversationId={0}&recipientName={1}&recipientId={2}&isGroup={3}",
                    selectedItemData.ConversationId, recipient.Name, recipient.Id, selectedItemData.IsGroupConversation);
            return uriString;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get the list of conversations in the local database
        /// </summary>
        /// <returns></returns>
        public List<ConversationModel> GetConversations()
        {
            lock (this)
            {
                List<ConversationModel> conversations = new List<ConversationModel>();
                var conversationList = from c in this.dataContext.Table<MessageModel>()
                       where c.MessageType == MessageType.Conversation
                       orderby c.LastUpdateTimeUtcTicks descending
                       select c;

                foreach (MessageModel cm in conversationList)
                {
                    if (cm.MessageFlags != 0)
                    {
                        continue;
                    }

                    ConversationModel c = new ConversationModel();
                    c.ConversationId = cm.ConversationId;
                    c.LastPostUtcTime = cm.LastUpdateTime;
                    c.LastPostPreview = cm.TextMessage;
                    c.UnreadCount = cm.UnreadCount.HasValue? cm.UnreadCount.Value : 0;
                    c.ConversationParticipants = new Collection<UserModel>();

                    UserModel senderUser = this.GetUser(cm.SenderId);
                    if (senderUser == null)
                    {
                        senderUser = new UserModel() { Id = cm.SenderId, Name = Strings.UnknownUser };
                    }

                    UserModel recipientUser = this.GetUser(cm.RecipientId);
                    if (senderUser == null)
                    {
                        senderUser = new UserModel() { Id = cm.RecipientId, Name = Strings.UnknownUser };
                    }

                    c.ConversationParticipants.Add(senderUser);
                    c.ConversationParticipants.Add(recipientUser);

                    conversations.Add(c);
                }

                return conversations;
            }
        }
Exemplo n.º 3
0
        public void ServiceAddNewConversationTest()
        {
            List<List<ConversationModel>> conversations = new List<List<ConversationModel>>();
            List<UserModel> owners = new List<UserModel>();
            this.LoadConversations(conversations, owners);

            for (int i = 0; i < conversations.Count; i++)
            {
                if (conversations[i].Count == 0)
                {
                    return;
                }

                int newConversations = 3;
                MockUserSettings userSettings = new MockUserSettings();
                MockDataContextWrapper dataContextWrapper = new MockDataContextWrapper(new MockDatabase() { Conversations = conversations[i] });
                userSettings.Save(owners[i]);

                Random random = new Random((int)DateTime.Now.Ticks);
                List<ConversationModel> serviceConversation = new List<ConversationModel>();
                serviceConversation.AddRange(conversations[i]);
                long conversationIdGenerator = long.MaxValue;
                for (int j = 0; j < newConversations; j++)
                {
                    ConversationModel newConversation = new ConversationModel();
                    newConversation.ConversationId = conversationIdGenerator--;
                    newConversation.ConversationParticipants = new List<UserModel>();
                    newConversation.ConversationParticipants.Add(userSettings.Me);
                    newConversation.ConversationParticipants.Add(new UserModel() { Id = random.Next(100, 2000), Name = "ServiceAddNewConversationTestUser" + j.ToString(), PhoneNumber = "425 111 1111" });
                    newConversation.LastPostUtcTime = DateTime.Now;
                    newConversation.LastPostPreview = "New message for conversation" + j.ToString();
                    serviceConversation.Add(newConversation);
                }

                MockServiceProxy serviceProxy = new MockServiceProxy() { Conversations = serviceConversation };

                using (AllConversationsViewModel allConversations = new AllConversationsViewModel(serviceProxy, userSettings, dataContextWrapper))
                {
                    allConversations.LoadInitialConversations();

                    NotifyCollectionChangedTester<ConversationModel> collectionChangedTester = new NotifyCollectionChangedTester<ConversationModel>(allConversations.Conversations);

                    while (!allConversations.IsLoaded)
                    {
                        System.Threading.Thread.Sleep(1000);
                    }

                    Assert.AreEqual(newConversations, collectionChangedTester.Count, "Service proxy changes weren't generated");

                    for (int j = 0; j < allConversations.Conversations.Count; j++)
                    {
                        Assert.AreEqual(allConversations.Conversations[j].LastPostUtcTime, serviceConversation[j].LastPostUtcTime, "Date didn't match with the service proxy update");
                        Assert.AreEqual(allConversations.Conversations[j].LastPostPreview, serviceConversation[j].LastPostPreview, "preview didn't match with the service proxy update");
                    }

                    Assert.AreEqual(((MockTable<ConversationModel>)dataContextWrapper.Table<ConversationModel>()).Count, serviceConversation.Count, "New conversation was not inserted in to the database");
                }
            }
        }
Exemplo n.º 4
0
        public void ServiceUpdateExistingConversationTest()
        {
            List<List<ConversationModel>> conversations = new List<List<ConversationModel>>();
            List<UserModel> owners = new List<UserModel>();
            this.LoadConversations(conversations, owners);

            for (int i = 0; i < conversations.Count; i++)
            {
                if (conversations[i].Count == 0)
                {
                    return;
                }

                List<ConversationModel> serviceConversation = new List<ConversationModel>();
                for (int j = 0; j < conversations[i].Count; j++)
                {
                    ConversationModel newConversation = new ConversationModel();
                    newConversation.ConversationId = conversations[i][j].ConversationId;
                    newConversation.ConversationParticipants = conversations[i][j].ConversationParticipants;
                    newConversation.LastPostUtcTime = DateTime.Now;
                    newConversation.LastPostPreview = "New message for conversation" + j.ToString();
                    serviceConversation.Add(newConversation);
                }

                MockServiceProxy serviceProxy = new MockServiceProxy() { Conversations = serviceConversation };
                MockUserSettings userSettings = new MockUserSettings();
                MockDataContextWrapper dataContextWrapper = new MockDataContextWrapper(new MockDatabase() { Conversations = conversations[i] });
                userSettings.Save(owners[i]);

                using (AllConversationsViewModel allConversations = new AllConversationsViewModel(serviceProxy, userSettings, dataContextWrapper))
                {
                    allConversations.LoadInitialConversations();

                    NotifyCollectionChangedTester<ConversationModel> collectionChangedTester = new NotifyCollectionChangedTester<ConversationModel>(allConversations.Conversations);

                    while (!allConversations.IsLoaded)
                    {
                        System.Threading.Thread.Sleep(1000);
                    }

                    Assert.AreEqual(collectionChangedTester.Count, serviceConversation.Count, "Service proxy changes weren't generated");

                    for (int j = 0; j < allConversations.Conversations.Count; j++)
                    {
                        Assert.AreEqual(allConversations.Conversations[j].LastPostUtcTime, serviceConversation[j].LastPostUtcTime, "Date didn't match with the service proxy update");
                        Assert.AreEqual(allConversations.Conversations[j].LastPostPreview, serviceConversation[j].LastPostPreview, "preview didn't match with the service proxy update");
                    }
                }
            }
        }
Exemplo n.º 5
0
 void SetTaskTextForDisplay(ConversationModel conversation, MessageModel message)
 {
     if (message.IsTaskDeleted.HasValue && message.IsTaskDeleted.Value)
     {
         conversation.LastPostPreview = message.SenderId == UserSettingsModel.Instance.Me.Id ? string.Format(YapperChat.Resources.Strings.YouDeletedTask, message.TaskName, message.Sender.Name) : string.Format(YapperChat.Resources.Strings.HasDeletedTask, message.Sender.Name, message.TaskName, message.TaskName);
     }
     else
     {
         conversation.LastPostPreview = message.SenderId == UserSettingsModel.Instance.Me.Id ? string.Format(YapperChat.Resources.Strings.YouUpdatedTask, message.TaskName, message.Sender.Name) : string.Format(YapperChat.Resources.Strings.HasUpdatedTask, message.Sender.Name, message.TaskName);
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// Handles a push notification
        /// </summary>
        /// <param name="pushEvent"></param>
        private void HandleNewMessage(MessageModel message, bool isPush)
        {
            if (message == null)
            {
                return;
            }

            DispatcherHelper.InvokeOnUiThread(
                () =>
                {
                    lock (this)
                    {
                        if (message == null ||
                            message.MessageType != MessageType.Message ||
                            (message.MessageFlags & MessageFlags.TaskItem) == MessageFlags.TaskItem ||
                            (message.MessageFlags & MessageFlags.TaskInfo) == MessageFlags.TaskInfo ||
                            (message.MessageFlags & MessageFlags.Task) == MessageFlags.Task)
                        {
                            return;
                        }

                        ConversationModel conversation = conversation = this.GetConversationFromConversationId(message.ConversationId);

                        // if we can't find the conversation, find by sender
                        if (conversation == null)
                        {
                            int otherParticipant = message.SenderId == this.userSettings.UserId ? message.RecipientId : message.SenderId;
                            if (this.recipientConversationMap.ContainsKey(otherParticipant))
                            {
                                conversation = this.recipientConversationMap[otherParticipant];
                                if (conversation != null && conversation.ConversationId == Guid.Empty)
                                {
                                    conversation.ConversationId = message.ConversationId;
                                }
                            }
                        }

                        // if it's still null, add the new conversation
                        if (conversation != null && conversation.LastPostUtcTime < message.PostDateTime)
                        {
                            conversation.LastPostUtcTime = message.PostDateTime;
                            conversation.LastPostPreview = message.TextMessageForDisplay;
                            conversation.UnreadCount = DataSync.Instance.GetUnreadCount(conversation.ConversationId);
                            this.Conversations.Remove(conversation);

                            this.Conversations.Add(conversation);
                        }

                        if (conversation == null)
                        {
                            conversation = new ConversationModel();
                            conversation.ConversationId = message.ConversationId;
                            conversation.ConversationParticipants = new Collection<UserModel>();
                            conversation.ConversationParticipants.Add(message.Sender);
                            conversation.ConversationParticipants.Add(message.Recipient);
                            conversation.LastPostPreview = message.TextMessageForDisplay;
                            conversation.LastPostUtcTime = message.LastUpdateTime;
                            conversation.UnreadCount = 1;

                            this.AddNewConversation(conversation);
                        }
                    }
                });
        }
Exemplo n.º 7
0
        /// <summary>
        /// Gets the participant id for a given conversation
        /// </summary>
        /// <param name="conversationModel"></param>
        /// <returns></returns>
        private int GetParticipantId(ConversationModel conversationModel)
        {
            if (conversationModel.ConversationParticipants != null)
            {
                foreach (UserModel participant in conversationModel.ConversationParticipants)
                {
                    if (participant != null && (participant.Id != this.userSettings.UserId))
                    {
                        return participant.Id;
                    }
                }
            }

            return -1;
        }
Exemplo n.º 8
0
 /// <summary>
 /// Adds a new conversation to the viewmodel
 /// </summary>
 /// <param name="conversation"></param>
 public void AddNewConversation(ConversationModel conversation)
 {
     this.Conversations.Insert(0, conversation);
     int participantId = this.GetParticipantId(conversation);
     if (!this.recipientConversationMap.ContainsKey(participantId))
     {
         this.recipientConversationMap.Add(participantId, conversation);
         this.SelectedConversation = conversation;
     }
 }
Exemplo n.º 9
0
        public ConversationModel CreateConversation(UserModel currentUser)
        {
            ConversationModel conversation = new ConversationModel();
            conversation.ConversationId = this.ConversationId;
            conversation.LastPostUtcTime = this.MessagePostDate;
            conversation.LastPostPreview = this.Message;
            conversation.ConversationParticipants = new EntitySet<UserModel>();
            conversation.ConversationParticipants.Add(this.GetSender());
            conversation.ConversationParticipants.Add(currentUser);

            return conversation;
        }