Example #1
0
        public void Delete(bool deleteConversations)
        {
            // Contains the same references as in SelectedMessages,
            // these references can change when un-doing so keep a snapshot around
            var previousSelection = deleteConversations ?
                                    SelectedMessages.SelectMany(m => m.Conversation.Messages)
                                    .Distinct()             // Select all messages from selected conversations
                                    .ToList()
                                : new List <Message>(SelectedMessages);

            // Contains instance copies of messages, this will be the old data
            // before the do is applied.
            var messagesCopy = previousSelection
                               .Select(m => m.DuckCopy <Message>())
                               .ToList();

            #region Do action

            Action doAction = delegate
            {
                foreach (var message in previousSelection)
                {
                    message.MarkDeleted();
                }

                viewFilter.UpdateCurrentViewAsync();

                flipper.Delay();
            };

            #endregion

            #region Undo action

            Action undoAction = delegate
            {
                foreach (var message in previousSelection)
                {
                    // Get old message from copied data
                    Message message1 = message;

                    var oldMessage = messagesCopy.Single(m => m.MessageId == message1.MessageId);

                    message.IsRead             = oldMessage.IsRead;
                    message.TargetMessageState = oldMessage.TargetMessageState;
                    message.MessageFolder      = oldMessage.MessageFolder;

                    AsyncUpdateQueue.Enqueue(message);
                }

                // We cannot use the IEditableObject appraoch here because the conversation in question
                // probably might not be in view anymore. So instead we will refresh the whole view.
                viewFilter.RebuildCurrentViewAsync();
            };

            #endregion

            ClientState.Current.UndoManager.Execute(new HistoryAction(doAction, undoAction));
        }