diff --git a/enzevalos_iphone/MailHandler.swift b/enzevalos_iphone/MailHandler.swift index f3e06fee3e624ad0fc8a9062bd9e69c781b770f5..76747de0bbed4e46b0c848a8d4b0b72c6f6b1e77 100644 --- a/enzevalos_iphone/MailHandler.swift +++ b/enzevalos_iphone/MailHandler.swift @@ -395,7 +395,7 @@ } private func loadMessagesFromServer(_ uids: MCOIndexSet, folderPath: String, maxLoad: Int = MailHandler.MAXMAILS, completionCallback: @escaping ((_ error: MailServerConnectionError?) -> ())) { - guard IMAPSession != nil else { + guard let session = IMAPSession else { completionCallback(MailServerConnectionError.NoData) return } @@ -405,7 +405,7 @@ } // First fetch -> get flags let requestKind = MCOIMAPMessagesRequestKind(rawValue: MCOIMAPMessagesRequestKind.headers.rawValue | MCOIMAPMessagesRequestKind.flags.rawValue) - let fetchOperation: MCOIMAPFetchMessagesOperation = self.IMAPSession!.fetchMessagesOperation(withFolder: folderPath, requestKind: requestKind, uids: uids) + let fetchOperation: MCOIMAPFetchMessagesOperation = session.fetchMessagesOperation(withFolder: folderPath, requestKind: requestKind, uids: uids) fetchOperation.extraHeaders = MailHandler.extraHeaders fetchOperation.start{[unowned self] (err, msg, vanished) -> Void in @@ -566,27 +566,44 @@ } private func initFolder(folderPath: String, completionCallback: @escaping ((MailServerConnectionError?) -> ())) { - let requestKind = MCOIMAPMessagesRequestKind(rawValue: MCOIMAPMessagesRequestKind.headers.rawValue) - let uids = MCOIndexSet(range: MCORangeMake(1, UINT64_MAX)) - let toFetchIDs = MCOIndexSet() - - let fetchOperation: MCOIMAPFetchMessagesOperation = self.IMAPSession!.fetchMessagesOperation(withFolder: folderPath, requestKind: requestKind, uids: uids) - fetchOperation.start {[unowned self] (err, msg, vanished) -> Void in - guard err == nil else { - let conerror = MailServerConnectionError.findErrorCode(error: err!) + guard let session = IMAPSession else { + self.errorhandling(error: .ConnectionError, originalCall: {self.initFolder(folderPath: folderPath, completionCallback: completionCallback)}, completionCallback: completionCallback) + return + } + session.folderStatusOperation(folderPath)?.start({error, status in + if let error = error { + let conerror = MailServerConnectionError.findErrorCode(error: error) self.errorhandling(error: conerror, originalCall: {self.initFolder(folderPath: folderPath, completionCallback: completionCallback)}, completionCallback: completionCallback) return } - if let msgs = msg { - FolderRecord.lastDate(folder: folderPath, date: Date()) - for message in msgs { - toFetchIDs.add(UInt64(message.uid)) - } - self.loadMessagesFromServer(toFetchIDs, folderPath: folderPath, maxLoad: 50, completionCallback: completionCallback) - } else { + guard let status = status else { completionCallback(nil) + return } - } + + let requestKind = MCOIMAPMessagesRequestKind(rawValue: MCOIMAPMessagesRequestKind.headers.rawValue) + let min = max(status.uidNext - 1000, 1) + let uids = MCOIndexSet(range: MCORangeMake(UInt64(min), UInt64(status.uidNext))) + let toFetchIDs = MCOIndexSet() + + let fetchOperation: MCOIMAPFetchMessagesOperation = session.fetchMessagesOperation(withFolder: folderPath, requestKind: requestKind, uids: uids) + fetchOperation.start {[unowned self] (err, msg, vanished) -> Void in + guard err == nil else { + let conerror = MailServerConnectionError.findErrorCode(error: err!) + self.errorhandling(error: conerror, originalCall: {self.initFolder(folderPath: folderPath, completionCallback: completionCallback)}, completionCallback: completionCallback) + return + } + if let msgs = msg { + FolderRecord.lastDate(folder: folderPath, date: Date()) + for message in msgs { + toFetchIDs.add(UInt64(message.uid)) + } + self.loadMessagesFromServer(toFetchIDs, folderPath: folderPath, maxLoad: 50, completionCallback: completionCallback) + } else { + completionCallback(nil) + } + } + }) } private func initInbox(completionCallback: @escaping ((MailServerConnectionError?) -> ())) {