merge default IOS-1740
authorAlejandro Gelos <alejandro@pep-project.org>
Tue, 20 Aug 2019 13:55:09 +0200
branchIOS-1740
changeset 972628adceb916e6
parent 9725 efdbdfc23000
parent 9720 cc56f761fb79
child 9729 5d894b1a599f
child 9737 fae6b3abb830
merge default
     1.1 --- a/pEpForiOS/UI/EmailDisplayList/EmailListViewModel.swift	Tue Aug 20 13:52:25 2019 +0200
     1.2 +++ b/pEpForiOS/UI/EmailDisplayList/EmailListViewModel.swift	Tue Aug 20 13:55:09 2019 +0200
     1.3 @@ -43,13 +43,6 @@
     1.4  
     1.5      var indexPathShown: IndexPath?
     1.6  
     1.7 -    private let queueForHeavyStuff: OperationQueue = {
     1.8 -        let createe = OperationQueue()
     1.9 -        createe.qualityOfService = .userInitiated
    1.10 -        createe.name = "security.pep.EmailListViewModel.queueForHeavyStuff"
    1.11 -        return createe
    1.12 -    }()
    1.13 -
    1.14      var lastSearchTerm = ""
    1.15      var updatesEnabled = true
    1.16  
    1.17 @@ -165,8 +158,7 @@
    1.18      // MARK: - Public Data Access & Manipulation
    1.19  
    1.20      func viewModel(for index: Int) -> MessageViewModel? {
    1.21 -        let messageViewModel = MessageViewModel(with: messageQueryResults[index],
    1.22 -                                                queue: queueForHeavyStuff)
    1.23 +        let messageViewModel = MessageViewModel(with: messageQueryResults[index])
    1.24          return messageViewModel
    1.25      }
    1.26  
     2.1 --- a/pEpForiOS/UI/EmailDisplayList/MessageViewModel.swift	Tue Aug 20 13:52:25 2019 +0200
     2.2 +++ b/pEpForiOS/UI/EmailDisplayList/MessageViewModel.swift	Tue Aug 20 13:55:09 2019 +0200
     2.3 @@ -15,8 +15,14 @@
     2.4  class MessageViewModel: CustomDebugStringConvertible {
     2.5      static fileprivate var maxBodyPreviewCharacters = 120
     2.6  
     2.7 -    private var queue: OperationQueue
     2.8 -    private var runningOperations = [Operation]()
     2.9 +    private let queueForHeavyStuff: OperationQueue = {
    2.10 +        let createe = OperationQueue()
    2.11 +        createe.qualityOfService = .userInitiated
    2.12 +        createe.name = "security.pep.MessageViewModel.queueForHeavyStuff"
    2.13 +        return createe
    2.14 +    }()
    2.15 +
    2.16 +    let message: Message
    2.17  
    2.18      let uid: Int
    2.19      private let uuid: MessageID
    2.20 @@ -55,8 +61,8 @@
    2.21          }
    2.22      }
    2.23  
    2.24 -    required init(with message: Message, queue: OperationQueue) {
    2.25 -        self.queue = queue
    2.26 +    required init(with message: Message) {
    2.27 +        self.message = message
    2.28  
    2.29          uid = message.uid
    2.30          uuid = message.uuid
    2.31 @@ -102,14 +108,10 @@
    2.32      }
    2.33  
    2.34      func unsubscribeForUpdates() {
    2.35 -        cancelAllBackgroundOperations()
    2.36 +        queueForHeavyStuff.cancelAllOperations()
    2.37      }
    2.38  
    2.39 -    private func cancelAllBackgroundOperations() {
    2.40 -        runningOperations.forEach { $0.cancel() }
    2.41 -    }
    2.42 -
    2.43 -    private func setBodyPeek(for message:Message) {
    2.44 +    private func setBodyPeek(for message: Message) {
    2.45          if let bodyPeek = internalBoddyPeek {
    2.46              self.bodyPeek = bodyPeek
    2.47          } else {
    2.48 @@ -117,7 +119,7 @@
    2.49                  // It's valid to loose self here. The view can dissappear @ any time.
    2.50                  self?.bodyPeek = bodyPeek
    2.51              }
    2.52 -            addToRunningOperations(operation)
    2.53 +            queueForHeavyStuff.addOperation(operation)
    2.54          }
    2.55      }
    2.56  
    2.57 @@ -195,9 +197,6 @@
    2.58  
    2.59      func appendInlinedAttachmentsPlainText(to text: String) -> String {
    2.60          var result = text
    2.61 -        guard let message = message() else {
    2.62 -            return result
    2.63 -        }
    2.64          let inlinedText = message.inlinedTextAttachments()
    2.65          for inlinedTextAttachment in inlinedText {
    2.66              guard
    2.67 @@ -221,33 +220,17 @@
    2.68          return result
    2.69      }
    2.70  
    2.71 -    public func message() -> Message? {
    2.72 -        guard let msg = Message.by(uid: uid,
    2.73 -                                   uuid: uuid,
    2.74 -                                   folderName: parentFolderName,
    2.75 -                                   accountAddress: accountAddress)
    2.76 -            else {
    2.77 -                // The model has changed.
    2.78 -                return nil
    2.79 -        }
    2.80 -        return msg
    2.81 -    }
    2.82 -
    2.83      func getProfilePicture(completion: @escaping (UIImage?) -> ()) {
    2.84          let operation = getProfilePictureOperation(completion: completion)
    2.85 -        addToRunningOperations(operation)
    2.86 +        queueForHeavyStuff.addOperation(operation)
    2.87      }
    2.88  
    2.89      func getSecurityBadge(completion: @escaping (UIImage?) ->()) {
    2.90          let operation = getSecurityBadgeOperation(completion: completion)
    2.91 -        addToRunningOperations(operation)
    2.92 +        queueForHeavyStuff.addOperation(operation)
    2.93      }
    2.94  
    2.95      func getBodyMessage() -> NSMutableAttributedString {
    2.96 -        guard let message = message() else {
    2.97 -            //crash and return nonsense
    2.98 -            return NSMutableAttributedString()
    2.99 -        }
   2.100          let finalText = NSMutableAttributedString()
   2.101          if message.underAttack {
   2.102              let status = String.pEpRatingTranslation(pEpRating: .underAttack)
   2.103 @@ -282,7 +265,7 @@
   2.104              NSAttributedString.Key.foregroundColor: UIColor.lightGray
   2.105          ]
   2.106          var temp: [String] = []
   2.107 -        message()?.allRecipients.forEach { (recepient) in
   2.108 +        message.allRecipients.forEach { (recepient) in
   2.109              let recepient = recepient.address
   2.110              temp.append(recepient)
   2.111          }
   2.112 @@ -313,11 +296,6 @@
   2.113  
   2.114  extension MessageViewModel {
   2.115  
   2.116 -    private func addToRunningOperations(_ op: Operation) {
   2.117 -        runningOperations.append(op)
   2.118 -        queue.addOperation(op)
   2.119 -    }
   2.120 -
   2.121      private func getBodyPeekOperation(for message: Message, completion: @escaping (String)->()) -> Operation {
   2.122          let session = Session()
   2.123          let safeMsg = message.safeForSession(session)
   2.124 @@ -351,12 +329,8 @@
   2.125      }
   2.126  
   2.127      private func getSecurityBadgeOperation(completion: @escaping (UIImage?) -> ()) -> Operation {
   2.128 -        let msg = message()
   2.129          let session = Session()
   2.130 -        guard let safeMsg = msg?.safeForSession(session) else {
   2.131 -            // return empty OP
   2.132 -            return Operation()
   2.133 -        }
   2.134 +        let safeMsg = message.safeForSession(session)
   2.135          
   2.136          let getSecurityBadgeOperation = SelfReferencingOperation { [weak self] operation in
   2.137              guard let me = self else {