pEpForiOS/UI/EmailDisplay/EmailListViewCell.swift
author Dirk Zimmermann <dirk@pep-project.org>
Mon, 19 Dec 2016 17:00:48 +0100
changeset 1348 47fb980d20d4
parent 1344 6d21d35ace40
child 1359 88c59c8053c6
child 1364 21cf9229fe9f
permissions -rw-r--r--
IOS-358 SendLayerDelegate -> MessageFolderDelegate
     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     
    23     /**
    24      Indicates whether `defaultCellBackgroundColor` has been determined or not.
    25      */
    26     var determinedCellBackgroundColor: Bool = false
    27     
    28     /**
    29      The default background color for an email cell, as determined the first time a cell is
    30      created.
    31      */
    32     var defaultCellBackgroundColor: UIColor?
    33 
    34     let dateFormatter = UIHelper.dateFormatterEmailList()
    35 
    36     override func awakeFromNib() {
    37         super.awakeFromNib()
    38         self.selectionStyle = UITableViewCellSelectionStyle.none
    39 
    40         // generated an circle image
    41         isReadMessageImage.layer.cornerRadius = isReadMessageImage.frame.size.width / 2
    42         isReadMessageImage.clipsToBounds = true
    43 
    44         isReadMessageImage.isHidden = false
    45         isReadMessageImage.backgroundColor = .pEpBlue
    46     }
    47     
    48     func configureCell(indexPath: IndexPath, config: EmailListConfig?) {
    49         
    50         if !determinedCellBackgroundColor {
    51             defaultCellBackgroundColor = self.backgroundColor
    52             determinedCellBackgroundColor = true
    53         }
    54         
    55         if let email = messageAt(indexPath: indexPath, config: config) {
    56             if let pEpRating = PEPUtil.pEpRatingFromInt(email.pEpRatingInt) {
    57                 let privacyColor = PEPUtil.pEpColor(pEpRating: pEpRating)
    58                 if let uiColor = UIHelper.textBackgroundUIColorFromPrivacyColor(privacyColor) {
    59                     self.backgroundColor = uiColor
    60                 } else {
    61                     if determinedCellBackgroundColor {
    62                         self.backgroundColor = defaultCellBackgroundColor
    63                     }
    64                 }
    65             }
    66             UIHelper.putString(email.from?.displayString, toLabel: self.senderLabel)
    67             UIHelper.putString(email.shortMessage, toLabel: self.subjectLabel)
    68             
    69             // Snippet
    70             if let text = email.longMessage {
    71                 let theText = text.replaceNewLinesWith(" ").trimmedWhiteSpace()
    72                 UIHelper.putString(UIHelper.cleanHtml(theText), toLabel: self.summaryLabel)
    73             } else if let html = email.longMessageFormatted {
    74                 var text = html.extractTextFromHTML()
    75                 text = text.replaceNewLinesWith(" ").trimmedWhiteSpace()
    76                 UIHelper.putString(text, toLabel: self.summaryLabel)
    77             } else {
    78                 UIHelper.putString(nil, toLabel: self.summaryLabel)
    79             }
    80             
    81             if let receivedDate = email.received {
    82                 UIHelper.putString(dateFormatter.string(from: receivedDate as Date),
    83                                    toLabel: self.dateLabel)
    84             } else {
    85                 UIHelper.putString(nil, toLabel: self.dateLabel)
    86             }
    87             
    88             if (isRead(message: email)) {
    89                 self.isReadMessageImage.isHidden = true
    90             }
    91             else if (!isRead(message: email)) {
    92                 self.isReadMessageImage.isHidden = false
    93                 self.isReadMessageImage.backgroundColor = .pEpBlue
    94             }
    95         }
    96     }
    97     
    98     /**
    99      The message at the given position.
   100      */
   101     func isRead(message: Message)-> Bool {
   102         return message.imapFlags?.seen ?? false
   103     }
   104     
   105     func isImportant(message: Message)-> Bool {
   106         return message.imapFlags?.flagged ?? false
   107     }
   108     
   109     func messageAt(indexPath: IndexPath, config: EmailListConfig?) -> Message? {
   110         if let fol = config?.folder {
   111             return fol.messageAt(index: indexPath.row)
   112         }
   113         return nil
   114     }
   115 }