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