pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
changeset 1737 8d234ddb4824
parent 1725 2ef8f9937e40
child 1826 f8fe61819ba6
     1.1 --- a/pEpForiOS/UI/EmailDisplay/EmailListViewController.swift	Mon Feb 20 12:06:48 2017 +0100
     1.2 +++ b/pEpForiOS/UI/EmailDisplay/EmailListViewController.swift	Mon Feb 20 14:15:41 2017 +0100
     1.3 @@ -26,7 +26,7 @@
     1.4      var config: EmailListConfig?
     1.5      var state = UIState()
     1.6      let searchController = UISearchController(searchResultsController: nil)
     1.7 -    let cellsByMessageID = NSCache<NSString, EmailListViewCell>()
     1.8 +    let cellsInUse = NSCache<NSString, EmailListViewCell>()
     1.9  
    1.10      /**
    1.11       After trustwords have been invoked, this will be the partner identity that
    1.12 @@ -118,8 +118,8 @@
    1.13                              cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    1.14          let cell = tableView.dequeueReusableCell(
    1.15              withIdentifier: "EmailListViewCell", for: indexPath) as! EmailListViewCell
    1.16 -        if let messageID = cell.configureCell(config: config, indexPath: indexPath) {
    1.17 -            cellsByMessageID.setObject(cell, forKey: messageID as NSString)
    1.18 +        if let message = cell.configureCell(config: config, indexPath: indexPath) {
    1.19 +            associate(message: message, toCell: cell)
    1.20          }
    1.21          return cell
    1.22      }
    1.23 @@ -360,25 +360,48 @@
    1.24                      // new message has arrived
    1.25                      if let index = folder.indexOf(message: msg) {
    1.26                          let ip = IndexPath(row: index, section: 0)
    1.27 +                        Log.info(
    1.28 +                            component: #function,
    1.29 +                            content: "insert message at \(index), \(folder.messageCount()) messages")
    1.30                          tableView.insertRows(at: [ip], with: .automatic)
    1.31                      } else {
    1.32                          tableView.reloadData()
    1.33                      }
    1.34                  } else if msg.isGhost {
    1.35 -                    if let cell = cellsByMessageID.object(forKey: msg.uuid as NSString) {
    1.36 -                        if let ip = tableView.indexPath(for: cell) {
    1.37 -                            tableView.deleteRows(at: [ip], with: .automatic)
    1.38 -                        }
    1.39 +                    if let cell = cellFor(message: msg), let ip = tableView.indexPath(for: cell) {
    1.40 +                        Log.info(
    1.41 +                            component: #function,
    1.42 +                            content: "delete message at \(index), \(folder.messageCount()) messages")
    1.43 +                        tableView.deleteRows(at: [ip], with: .automatic)
    1.44 +                    } else {
    1.45 +                        tableView.reloadData()
    1.46                      }
    1.47                  } else {
    1.48                      // other flags than delete must have been changed
    1.49 -                    if let cell = cellsByMessageID.object(forKey: msg.uuid as NSString) {
    1.50 +                    if let cell = cellFor(message: msg) {
    1.51                          cell.updateFlags(message: message)
    1.52 +                    } else {
    1.53 +                        tableView.reloadData()
    1.54                      }
    1.55                  }
    1.56              }
    1.57          }
    1.58      }
    1.59 +
    1.60 +    // MARK: - Message -> Cell association
    1.61 +
    1.62 +    func keyFor(message: Message) -> NSString {
    1.63 +        let parentName = message.parent?.name ?? "unknown"
    1.64 +        return "\(message.uuid) \(parentName) \(message.uuid)" as NSString
    1.65 +    }
    1.66 +
    1.67 +    func associate(message: Message, toCell: EmailListViewCell) {
    1.68 +        cellsInUse.setObject(toCell, forKey: keyFor(message: message))
    1.69 +    }
    1.70 +
    1.71 +    func cellFor(message: Message) -> EmailListViewCell? {
    1.72 +        return cellsInUse.object(forKey: keyFor(message: message))
    1.73 +    }
    1.74  }
    1.75  
    1.76  // MARK: - MessageFolderDelegate