pEpForiOS/UI/EmailDisplayList/EmailListViewCell.swift
author Xavier Algarra <xavier@pep-project.org>
Mon, 07 May 2018 11:11:24 +0200
branchIOS-1064
changeset 4550 c04fe6d5c76b
parent 4549 fa6fb82b2eaa
permissions -rw-r--r--
IOS-1064 some changes
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 UIKit
xavier@3368
    10
import SwipeCellKit
dirk@31
    11
xavier@3368
    12
class EmailListViewCell: SwipeTableViewCell {
andreas@3151
    13
    static let storyboardId = "EmailListViewCell"
andreas@3151
    14
    static var flaggedImage: UIImage? = nil
andreas@3151
    15
    static var emptyContactImage = UIImage.init(named: "empty-avatar")
dirk@3112
    16
andreas@3522
    17
    @IBOutlet weak var addressLabel: UILabel!
dirk@31
    18
    @IBOutlet weak var subjectLabel: UILabel!
dirk@31
    19
    @IBOutlet weak var summaryLabel: UILabel!
dirk@32
    20
    @IBOutlet weak var dateLabel: UILabel!
dirk@1605
    21
andreas@4470
    22
    /** Used for \Flagged, contrary to the name. */
dirk@1931
    23
    @IBOutlet weak var flaggedImageView: UIImageView!
igor@1300
    24
    @IBOutlet weak var ratingImage: UIImageView!
yves@1364
    25
    @IBOutlet weak var attachmentIcon: UIImageView!
dirk@1917
    26
    @IBOutlet weak var contactImageView: UIImageView!
dirk@1917
    27
andreas@3151
    28
    var isFlagged:Bool = false {
andreas@3151
    29
        didSet {
andreas@3151
    30
            if isFlagged {
andreas@3151
    31
                setFlagged()
andreas@3151
    32
            } else {
andreas@3151
    33
                unsetFlagged()
andreas@3151
    34
            }
andreas@3151
    35
        }
andreas@3151
    36
    }
andreas@3151
    37
andreas@3151
    38
    var isSeen:Bool = false {
andreas@3151
    39
        didSet {
andreas@3151
    40
            if isSeen {
andreas@3151
    41
                setSeen()
andreas@3151
    42
            } else {
andreas@3151
    43
                unsetSeen()
andreas@3151
    44
            }
andreas@3151
    45
        }
andreas@3151
    46
    }
andreas@3151
    47
andreas@3151
    48
    var hasAttachment:Bool = false {
andreas@3151
    49
        didSet {
andreas@3151
    50
            if hasAttachment {
andreas@3151
    51
                attachmentIcon.isHidden = false
andreas@3151
    52
            } else {
andreas@3151
    53
                attachmentIcon.isHidden = true
andreas@3151
    54
            }
andreas@3151
    55
        }
andreas@3151
    56
    }
andreas@3151
    57
andreas@3151
    58
    func setPepRatingImage(image: UIImage?) {
andreas@3151
    59
        guard image != nil else {
andreas@3151
    60
            return
andreas@3151
    61
        }
andreas@3151
    62
        self.ratingImage.image = image
andreas@3151
    63
        self.ratingImage.isHidden = false
andreas@3151
    64
    }
andreas@3151
    65
andreas@3151
    66
    func setContactImage(image: UIImage?) {
andreas@3151
    67
        guard image != nil else {
andreas@3151
    68
            return
andreas@3151
    69
        }
andreas@3151
    70
        self.contactImageView.image = image
andreas@3151
    71
    }
dirk@1906
    72
ana@614
    73
    override func awakeFromNib() {
ana@614
    74
        super.awakeFromNib()
dirk@2015
    75
        self.contactImageView.layer.cornerRadius = round(contactImageView.bounds.size.width / 2)
dirk@1920
    76
        self.contactImageView.layer.masksToBounds = true
andreas@3151
    77
        resetToDefault()
ana@614
    78
    }
dirk@1542
    79
andreas@3151
    80
    override func prepareForReuse() {
andreas@3151
    81
        resetToDefault()
andreas@3151
    82
    }
andreas@3120
    83
andreas@3151
    84
    private func resetToDefault() {
andreas@3522
    85
        addressLabel.text = nil
andreas@3151
    86
        subjectLabel.text = nil
andreas@3151
    87
        summaryLabel.text = nil
andreas@3151
    88
        dateLabel.text = nil
andreas@3151
    89
        unsetFlagged()
andreas@3151
    90
        unsetSeen()
andreas@3151
    91
        ratingImage.isHidden = true
andreas@3151
    92
        hasAttachment = false
andreas@3151
    93
        contactImageView.image = EmailListViewCell.emptyContactImage
andreas@3151
    94
    }
andreas@3120
    95
andreas@3151
    96
    private func setFlagged() {
andreas@3151
    97
        if EmailListViewCell.flaggedImage == nil {
andreas@3151
    98
            EmailListViewCell.flaggedImage =
andreas@3151
    99
                FlagImages.create(imageSize: flaggedImageView.frame.size).flaggedImage
andreas@3151
   100
        }
andreas@3151
   101
        guard let saveImg = EmailListViewCell.flaggedImage else {
andreas@3151
   102
            return
andreas@3151
   103
        }
andreas@3151
   104
        self.flaggedImageView.isHidden = false
andreas@3151
   105
        self.flaggedImageView.image = saveImg
andreas@3151
   106
    }
dirk@1605
   107
andreas@3151
   108
    private func unsetFlagged() {
andreas@3151
   109
        self.flaggedImageView.isHidden = true
andreas@3151
   110
        self.flaggedImageView.image = nil
andreas@3151
   111
    }
andreas@3151
   112
andreas@3151
   113
    private func setSeen() {
andreas@3522
   114
        if let font = addressLabel.font {
andreas@3151
   115
            let font = UIFont.systemFont(ofSize: font.pointSize)
andreas@3168
   116
            setupLabels(font: font)
igor@1568
   117
        }
andreas@3151
   118
    }
dirk@1931
   119
andreas@3151
   120
    private func unsetSeen() {
andreas@3522
   121
        if let font = addressLabel.font {
andreas@3151
   122
            let font = UIFont.boldSystemFont(ofSize: font.pointSize)
andreas@3168
   123
            setupLabels(font: font)
dirk@1931
   124
        }
dirk@1931
   125
    }
dirk@1931
   126
andreas@3168
   127
    func setupLabels(font: UIFont) {
andreas@3522
   128
        addressLabel.font = font
dirk@1931
   129
        subjectLabel.font = font
dirk@1931
   130
        summaryLabel.font = font
dirk@1933
   131
        dateLabel.font = font
dirk@1542
   132
    }
dirk@784
   133
}