pEpForiOS/UI/EmailDisplay/EmailListViewCell.swift
author Dirk Zimmermann <dirk@pep-project.org>
Tue, 24 Jan 2017 10:47:08 +0100
changeset 1507 7270fb02923c
parent 1472 0c0cd852f3f3
child 1529 fe0cf4167c2f
permissions -rw-r--r--
IOS-417 consolidating creation of swipe actions
     1 //
     2 //  EmailListViewCell.swift
     3 //  pEpForiOS
     4 //
     5 //  Created by Dirk Zimmermann on 16/04/16.
     6 //  Copyright © 2016 p≡p Security S.A. All rights reserved.
     7 //
     8 
     9 import Foundation
    10 import UIKit
    11 import MessageModel
    12 
    13 class EmailListViewCell: UITableViewCell {
    14 
    15     @IBOutlet weak var senderLabel: UILabel!
    16     @IBOutlet weak var subjectLabel: UILabel!
    17     @IBOutlet weak var summaryLabel: UILabel!
    18     @IBOutlet weak var dateLabel: UILabel!
    19     @IBOutlet weak var isReadMessageImage: UIImageView!
    20     @IBOutlet weak var disclousureImage: UIImageView!
    21     @IBOutlet weak var ratingImage: UIImageView!
    22     @IBOutlet weak var attachmentIcon: UIImageView!
    23     
    24     /**
    25      Indicates whether `defaultCellBackgroundColor` has been determined or not.
    26      */
    27     var determinedCellBackgroundColor: Bool = false
    28     
    29     /**
    30      The default background color for an email cell, as determined the first time a cell is
    31      created.
    32      */
    33     var defaultCellBackgroundColor: UIColor?
    34 
    35     let dateFormatter = UIHelper.dateFormatterEmailList()
    36 
    37     override func awakeFromNib() {
    38         super.awakeFromNib()
    39         self.selectionStyle = UITableViewCellSelectionStyle.none
    40 
    41         // generated an circle image
    42         isReadMessageImage.layer.cornerRadius = isReadMessageImage.frame.size.width / 2
    43         isReadMessageImage.clipsToBounds = true
    44 
    45         isReadMessageImage.isHidden = false
    46         isReadMessageImage.backgroundColor = .pEpBlue
    47     }
    48     
    49     func configureCell(indexPath: IndexPath, config: EmailListConfig?) {
    50         
    51         if !determinedCellBackgroundColor {
    52             defaultCellBackgroundColor = self.backgroundColor
    53             determinedCellBackgroundColor = true
    54         }
    55         
    56         if let email = messageAt(indexPath: indexPath, config: config) {
    57             if let pEpRating = PEPUtil.pEpRatingFromInt(email.pEpRatingInt) {
    58                 let privacyColor = PEPUtil.pEpColor(pEpRating: pEpRating)
    59                 if let uiColor = UIHelper.textBackgroundUIColorFromPrivacyColor(privacyColor) {
    60                     self.backgroundColor = uiColor
    61                 } else {
    62                     if determinedCellBackgroundColor {
    63                         self.backgroundColor = defaultCellBackgroundColor
    64                     }
    65                 }
    66             }
    67             UIHelper.putString(email.from?.userName, toLabel: self.senderLabel)
    68             UIHelper.putString(email.shortMessage, toLabel: self.subjectLabel)
    69             
    70             // Snippet
    71             if let text = email.longMessage {
    72                 let theText = text.replaceNewLinesWith(" ").trimmedWhiteSpace()
    73                 UIHelper.putString(UIHelper.cleanHtml(theText), toLabel: self.summaryLabel)
    74             } else if let html = email.longMessageFormatted {
    75                 var text = html.extractTextFromHTML()
    76                 text = text.replaceNewLinesWith(" ").trimmedWhiteSpace()
    77                 UIHelper.putString(text, toLabel: self.summaryLabel)
    78             } else {
    79                 UIHelper.putString(nil, toLabel: self.summaryLabel)
    80             }
    81             
    82             if let originationDate = email.received {
    83                 UIHelper.putString(dateFormatter.string(from: originationDate as Date),
    84                                    toLabel: self.dateLabel)
    85             } else {
    86                 UIHelper.putString(nil, toLabel: self.dateLabel)
    87             }
    88             
    89             if (isRead(message: email)) {
    90                 self.isReadMessageImage.isHidden = true
    91             }
    92             else if (!isRead(message: email)) {
    93                 self.isReadMessageImage.isHidden = false
    94                 self.isReadMessageImage.backgroundColor = .pEpBlue
    95             }
    96             
    97             attachmentIcon.isHidden = email.attachments.count > 0 ? false : true
    98         }
    99     }
   100     
   101     /**
   102      The message at the given position.
   103      */
   104     func isRead(message: Message)-> Bool {
   105         return message.imapFlags?.seen ?? false
   106     }
   107     
   108     func isImportant(message: Message)-> Bool {
   109         return message.imapFlags?.flagged ?? false
   110     }
   111     
   112     func messageAt(indexPath: IndexPath, config: EmailListConfig?) -> Message? {
   113         if let fol = config?.folder {
   114             return fol.messageAt(index: indexPath.row)
   115         }
   116         return nil
   117     }
   118 }