IOS-417 consolidating creation of swipe actions
authorDirk Zimmermann <dirk@pep-project.org>
Tue, 24 Jan 2017 10:47:08 +0100
changeset 15077270fb02923c
parent 1506 11d3b3dd5865
child 1509 a6013fbb2454
IOS-417 consolidating creation of swipe actions
pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
     1.1 --- a/pEpForiOS/UI/EmailDisplay/EmailListViewController.swift	Tue Jan 24 10:01:42 2017 +0100
     1.2 +++ b/pEpForiOS/UI/EmailDisplay/EmailListViewController.swift	Tue Jan 24 10:47:08 2017 +0100
     1.3 @@ -119,7 +119,7 @@
     1.4      // MARK: - UITableViewDelegate
     1.5  
     1.6      override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
     1.7 -        let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
     1.8 +        let _ = tableView.cellForRow(at: indexPath) as! EmailListViewCell
     1.9  
    1.10          if let fol = config?.folder {
    1.11              if fol.folderType == .drafts {
    1.12 @@ -134,9 +134,8 @@
    1.13          indexPath: IndexPath)-> [UITableViewRowAction]? {
    1.14          let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
    1.15          if let email = cell.messageAt(indexPath: indexPath, config: config) {
    1.16 -            let isFlagAction = createIsFlagAction(message: email, cell: cell)
    1.17 -            let deleteAction = createDeleteAction(cell)
    1.18 -            //let isReadAction = createIsReadAction(message: email, cell: cell)
    1.19 +            let isFlagAction = createFlagAction(message: email, cell: cell)
    1.20 +            let deleteAction = createDeleteAction(message: email, cell: cell)
    1.21              let moreAction = createMoreAction(message: email, cell: cell)
    1.22              return [deleteAction,isFlagAction,moreAction]
    1.23          }
    1.24 @@ -145,70 +144,84 @@
    1.25  
    1.26      // MARK: - Misc
    1.27  
    1.28 -    func createIsFlagAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
    1.29 -        // preparing action to trigger when user swipe
    1.30 -        let isFlagCompletionHandler: (UITableViewRowAction, IndexPath) -> Void =
    1.31 -            { (action, indexPath) in
    1.32 -                if (cell.isImportant(message: message)) {
    1.33 -                    message.imapFlags?.flagged = false
    1.34 -                } else {
    1.35 -                    message.imapFlags?.flagged = true
    1.36 -                }
    1.37 -                self.tableView.reloadRows(at: [indexPath], with: .none)
    1.38 +    func createRowAction(cell: EmailListViewCell,
    1.39 +        image: UIImage?, action: @escaping (UITableViewRowAction, IndexPath) -> Void,
    1.40 +        titleBlock: () -> String) -> UITableViewRowAction {
    1.41 +        let rowAction = UITableViewRowAction(style: .normal, title: titleBlock(),
    1.42 +                                             handler: action)
    1.43 +
    1.44 +        if let theImage = image {
    1.45 +            let iconColor = UIColor(patternImage: theImage)
    1.46 +            rowAction.backgroundColor = iconColor
    1.47          }
    1.48 -        // creating the action
    1.49 -        let isFlagAction = UITableViewRowAction(style: .normal, title: "          ",
    1.50 -                                                handler: isFlagCompletionHandler)
    1.51 -        // changing default action color
    1.52 -        let swipeFlagImage = UIImage(named: "swipe-flag")
    1.53 -        let flagIconColor = UIColor(patternImage: swipeFlagImage!)
    1.54 -        isFlagAction.backgroundColor = flagIconColor
    1.55  
    1.56 -        return isFlagAction
    1.57 +        return rowAction
    1.58      }
    1.59  
    1.60 -    func createDeleteAction (_ cell: EmailListViewCell) -> UITableViewRowAction {
    1.61 -        // preparing the title action to show when user swipe
    1.62 -        let deleteCompletionHandler: (UITableViewRowAction, IndexPath) -> Void =
    1.63 -            { (action, indexPath) in
    1.64 -                let message = cell.messageAt(indexPath: indexPath, config: self.config)
    1.65 -                message?.imapFlags?.deleted = true
    1.66 +    func createFlagAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
    1.67 +        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
    1.68 +            if (cell.isImportant(message: message)) {
    1.69 +                message.imapFlags?.flagged = false
    1.70 +            } else {
    1.71 +                message.imapFlags?.flagged = true
    1.72 +            }
    1.73 +            message.save()
    1.74 +            self.tableView.reloadRows(at: [indexPath], with: .none)
    1.75 +        }
    1.76 +        func title() -> String {
    1.77 +            var title = "Flag".localized
    1.78 +            if (message.imapFlags?.flagged ?? true) {
    1.79 +                title = "UnFlag".localized
    1.80 +            }
    1.81 +            return title
    1.82          }
    1.83  
    1.84 -        // creating the action
    1.85 -        let deleteAction = UITableViewRowAction(style: .normal, title: "          ",
    1.86 -                                                handler: deleteCompletionHandler)
    1.87 -        let swipeTrashImage = UIImage(named: "swipe-trash")
    1.88 -        let trashIconColor = UIColor(patternImage: swipeTrashImage!)
    1.89 -        deleteAction.backgroundColor = trashIconColor
    1.90 -        return deleteAction
    1.91 +        let image = UIImage(named: "swipe-flag")
    1.92 +
    1.93 +        return createRowAction(cell: cell, image: image, action: action, titleBlock: title)
    1.94      }
    1.95  
    1.96 -    func createIsReadAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
    1.97 -        // preparing the title action to show when user swipe
    1.98 -        var localizedisReadTitle = " "
    1.99 -        if (cell.isRead(message: message)) {
   1.100 -            localizedisReadTitle = NSLocalizedString(
   1.101 -                "Unread",
   1.102 -                comment: "Unread button title in swipe action on EmailListViewController")
   1.103 -        } else {
   1.104 -            localizedisReadTitle = NSLocalizedString(
   1.105 -                "Read",
   1.106 -                comment: "Read button title in swipe action on EmailListViewController")
   1.107 +    func createDeleteAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
   1.108 +        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
   1.109 +            guard let message = cell.messageAt(indexPath: indexPath, config: self.config) else {
   1.110 +                return
   1.111 +            }
   1.112 +            message.imapFlags?.deleted = true
   1.113 +            message.save()
   1.114 +            self.tableView.reloadRows(at: [indexPath], with: .none)
   1.115 +        }
   1.116 +        func title() -> String {
   1.117 +            return "Delete".localized
   1.118          }
   1.119  
   1.120 -        // creating the action
   1.121 -        let isReadCompletionHandler: (UITableViewRowAction, IndexPath) -> Void =
   1.122 -            { (action, indexPath) in
   1.123 -                if (cell.isRead(message: message)) {
   1.124 -                    message.imapFlags?.seen = false
   1.125 -                } else {
   1.126 -                    message.imapFlags?.seen = true
   1.127 -                }
   1.128 -                self.tableView.reloadRows(at: [indexPath], with: .none)
   1.129 +        let image = UIImage(named: "swipe-trash")
   1.130 +
   1.131 +        return createRowAction(cell: cell, image: image, action: action, titleBlock: title)
   1.132 +    }
   1.133 +
   1.134 +    func createMarkAsReadAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
   1.135 +        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
   1.136 +            if (cell.isRead(message: message)) {
   1.137 +                message.imapFlags?.seen = false
   1.138 +            } else {
   1.139 +                message.imapFlags?.seen = true
   1.140 +            }
   1.141 +            self.tableView.reloadRows(at: [indexPath], with: .none)
   1.142          }
   1.143 -        let isReadAction = UITableViewRowAction(style: .default, title: localizedisReadTitle,
   1.144 -                                                handler: isReadCompletionHandler)
   1.145 +        func title() -> String {
   1.146 +            if (cell.isRead(message: message)) {
   1.147 +                return NSLocalizedString(
   1.148 +                    "Unread",
   1.149 +                    comment: "Unread button title in swipe action on EmailListViewController")
   1.150 +            } else {
   1.151 +                return NSLocalizedString(
   1.152 +                    "Read",
   1.153 +                    comment: "Read button title in swipe action on EmailListViewController")
   1.154 +            }
   1.155 +        }
   1.156 +
   1.157 +        let isReadAction = createRowAction(cell: cell, image: nil, action: action,
   1.158 +                                           titleBlock: title)
   1.159          isReadAction.backgroundColor = UIColor.blue
   1.160  
   1.161          return isReadAction