pEpForiOS/UI/MessageThreading/ThreadedFolder.swift
author Dirk Zimmermann <dirk@pep-project.org>
Thu, 02 Aug 2018 15:15:02 +0200
changeset 5670 f1417d86bf7c
parent 5562 b0af5e63258f
child 5676 532233f43a76
permissions -rw-r--r--
IOS-1226 referencedTopMessages() only considering top messages
     1 //
     2 //  ThreadedFolder.swift
     3 //  pEp
     4 //
     5 //  Created by Dirk Zimmermann on 05.06.18.
     6 //  Copyright © 2018 p≡p Security S.A. All rights reserved.
     7 //
     8 
     9 import Foundation
    10 
    11 import MessageModel
    12 
    13 class ThreadedFolder: ThreadedMessageFolderProtocol {
    14     let underlyingFolder: Folder
    15 
    16     init(folder: Folder) {
    17         underlyingFolder = folder
    18     }
    19 
    20     /**
    21      - Returns: The top messages of a folder, that is all messages fulfilling the
    22      underlying folder's filter that are at the top of threads.
    23      - Note: For performance reasons, the basic check is if a message is a child of
    24      the previous one, so the first message (and in most cases newest) is always a top message.
    25      */
    26     func allMessages() -> [Message] {
    27         var topMessages = [Message]()
    28 
    29         var messageIdSet = Set<MessageID>()
    30         let originalMessages = underlyingFolder.allMessagesNonThreaded()
    31 
    32         MessageModel.performAndWait {
    33             for msg in originalMessages {
    34                 if !messageIdSet.contains(msg.uuid) {
    35                     let threadMessageIds = msg.threadMessageIdSet()
    36                     if messageIdSet.intersection(threadMessageIds).isEmpty {
    37                         topMessages.append(msg)
    38                     }
    39                     messageIdSet.formUnion(threadMessageIds)
    40                 }
    41             }
    42         }
    43 
    44         return topMessages
    45     }
    46 
    47     func numberOfMessagesInThread(message: Message) -> Int {
    48         let theCount = messagesInThread(message: message).count
    49         if theCount > 0 {
    50             return theCount - 1
    51         }
    52         return theCount
    53     }
    54 
    55     func messagesInThread(message: Message) -> [Message] {
    56         let thread = message.threadMessages()
    57         if thread.count == 1 {
    58             return []
    59         } else {
    60             return thread
    61         }
    62     }
    63 
    64     func deleteSingle(message: Message) {
    65         message.imapDelete()
    66     }
    67 
    68     func deleteThread(message: Message) {
    69         let children = messagesInThread(message: message)
    70         for msgChild in children {
    71             msgChild.imapDelete()
    72         }
    73         message.imapDelete()
    74     }
    75 
    76     func referencedTopMessages(message: Message) -> [Message] {
    77         let topMessages = allMessages()
    78         var result = [Message]()
    79 
    80         MessageModel.performAndWait {
    81             let referenceIdSet = message.threadMessageIdSet()
    82 
    83             for aTopMsg in topMessages {
    84                 if aTopMsg == message {
    85                     continue
    86                 }
    87 
    88                 if !aTopMsg.threadMessageIdSet().intersection(referenceIdSet).isEmpty {
    89                     result.append(aTopMsg)
    90                 }
    91             }
    92         }
    93 
    94         return result
    95     }
    96 }