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