IOS-445 handling mail deletion more graceful
authorDirk Zimmermann <dirk@pep-project.org>
Mon, 20 Feb 2017 14:15:41 +0100
changeset 17378d234ddb4824
parent 1736 db335dbbcf95
child 1738 3c4cbafb00af
IOS-445 handling mail deletion more graceful
pEpForiOS/UI/EmailDisplay/EmailListViewCell.swift
pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
     1.1 --- a/pEpForiOS/UI/EmailDisplay/EmailListViewCell.swift	Mon Feb 20 12:06:48 2017 +0100
     1.2 +++ b/pEpForiOS/UI/EmailDisplay/EmailListViewCell.swift	Mon Feb 20 14:15:41 2017 +0100
     1.3 @@ -57,7 +57,7 @@
     1.4          ratingImage.backgroundColor = nil
     1.5      }
     1.6  
     1.7 -    func configureCell(config: EmailListConfig?, indexPath: IndexPath) -> MessageID? {
     1.8 +    func configureCell(config: EmailListConfig?, indexPath: IndexPath) -> Message? {
     1.9          if let message = messageAt(indexPath: indexPath, config: config) {
    1.10              UIHelper.putString(message.from?.userName, toLabel: self.senderLabel)
    1.11              UIHelper.putString(message.shortMessage, toLabel: self.subjectLabel)
    1.12 @@ -85,7 +85,7 @@
    1.13              updateFlags(message: message)
    1.14              updatePepRating(message: message)
    1.15  
    1.16 -            return message.messageID
    1.17 +            return message
    1.18          }
    1.19          return nil
    1.20      }
     2.1 --- a/pEpForiOS/UI/EmailDisplay/EmailListViewController.swift	Mon Feb 20 12:06:48 2017 +0100
     2.2 +++ b/pEpForiOS/UI/EmailDisplay/EmailListViewController.swift	Mon Feb 20 14:15:41 2017 +0100
     2.3 @@ -26,7 +26,7 @@
     2.4      var config: EmailListConfig?
     2.5      var state = UIState()
     2.6      let searchController = UISearchController(searchResultsController: nil)
     2.7 -    let cellsByMessageID = NSCache<NSString, EmailListViewCell>()
     2.8 +    let cellsInUse = NSCache<NSString, EmailListViewCell>()
     2.9  
    2.10      /**
    2.11       After trustwords have been invoked, this will be the partner identity that
    2.12 @@ -118,8 +118,8 @@
    2.13                              cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    2.14          let cell = tableView.dequeueReusableCell(
    2.15              withIdentifier: "EmailListViewCell", for: indexPath) as! EmailListViewCell
    2.16 -        if let messageID = cell.configureCell(config: config, indexPath: indexPath) {
    2.17 -            cellsByMessageID.setObject(cell, forKey: messageID as NSString)
    2.18 +        if let message = cell.configureCell(config: config, indexPath: indexPath) {
    2.19 +            associate(message: message, toCell: cell)
    2.20          }
    2.21          return cell
    2.22      }
    2.23 @@ -360,25 +360,48 @@
    2.24                      // new message has arrived
    2.25                      if let index = folder.indexOf(message: msg) {
    2.26                          let ip = IndexPath(row: index, section: 0)
    2.27 +                        Log.info(
    2.28 +                            component: #function,
    2.29 +                            content: "insert message at \(index), \(folder.messageCount()) messages")
    2.30                          tableView.insertRows(at: [ip], with: .automatic)
    2.31                      } else {
    2.32                          tableView.reloadData()
    2.33                      }
    2.34                  } else if msg.isGhost {
    2.35 -                    if let cell = cellsByMessageID.object(forKey: msg.uuid as NSString) {
    2.36 -                        if let ip = tableView.indexPath(for: cell) {
    2.37 -                            tableView.deleteRows(at: [ip], with: .automatic)
    2.38 -                        }
    2.39 +                    if let cell = cellFor(message: msg), let ip = tableView.indexPath(for: cell) {
    2.40 +                        Log.info(
    2.41 +                            component: #function,
    2.42 +                            content: "delete message at \(index), \(folder.messageCount()) messages")
    2.43 +                        tableView.deleteRows(at: [ip], with: .automatic)
    2.44 +                    } else {
    2.45 +                        tableView.reloadData()
    2.46                      }
    2.47                  } else {
    2.48                      // other flags than delete must have been changed
    2.49 -                    if let cell = cellsByMessageID.object(forKey: msg.uuid as NSString) {
    2.50 +                    if let cell = cellFor(message: msg) {
    2.51                          cell.updateFlags(message: message)
    2.52 +                    } else {
    2.53 +                        tableView.reloadData()
    2.54                      }
    2.55                  }
    2.56              }
    2.57          }
    2.58      }
    2.59 +
    2.60 +    // MARK: - Message -> Cell association
    2.61 +
    2.62 +    func keyFor(message: Message) -> NSString {
    2.63 +        let parentName = message.parent?.name ?? "unknown"
    2.64 +        return "\(message.uuid) \(parentName) \(message.uuid)" as NSString
    2.65 +    }
    2.66 +
    2.67 +    func associate(message: Message, toCell: EmailListViewCell) {
    2.68 +        cellsInUse.setObject(toCell, forKey: keyFor(message: message))
    2.69 +    }
    2.70 +
    2.71 +    func cellFor(message: Message) -> EmailListViewCell? {
    2.72 +        return cellsInUse.object(forKey: keyFor(message: message))
    2.73 +    }
    2.74  }
    2.75  
    2.76  // MARK: - MessageFolderDelegate