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