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