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