pEpForiOS/UI/EmailDisplay/EmailViewController.swift
author igor <igor@pep-project.org>
Tue, 17 Jan 2017 13:22:16 +0100
changeset 1438 a95b5815208d
parent 1434 36fe24c22fab
child 1444 2f02ffec0b52
permissions -rw-r--r--
IOS-399 Finished
     1 //
     2 //  EmailViewController.swift
     3 //  pEpForiOS
     4 //
     5 //  Created by Dirk Zimmermann on 31/05/16.
     6 //  Copyright © 2016 p≡p Security S.A. All rights reserved.
     7 //
     8 
     9 import Foundation
    10 import UIKit
    11 import MessageModel
    12 
    13 class EmailViewController: UITableViewController {
    14 
    15     var appConfig: AppConfig!
    16     var message: Message!
    17     var tableData: ComposeDataSource?
    18     var datasource = [Message]()
    19     var page = 0
    20     var otherCellsHeight: CGFloat = 0.0
    21     var computedHeight: CGFloat = 0.0
    22     
    23     override func viewDidLoad() {
    24         super.viewDidLoad()
    25         loadDatasource("MessageData")
    26         
    27         tableView.estimatedRowHeight = 72.0
    28         tableView.rowHeight = UITableViewAutomaticDimension
    29         tableView.setNeedsLayout()
    30         tableView.layoutIfNeeded()
    31     }
    32     
    33     fileprivate final func loadDatasource(_ file: String) {
    34         if let path = Bundle.main.path(forResource: file, ofType: "plist") {
    35             if let dict = NSDictionary(contentsOfFile: path) as? [String: Any] {
    36                 tableData = ComposeDataSource(with: dict["Rows"] as! [[String: Any]])
    37             }
    38         }
    39     }
    40     
    41     // MARK: - IBActions
    42 
    43     @IBAction func pressReply(_ sender: UIBarButtonItem) {
    44         let alertViewWithoutTitle = UIAlertController()
    45         alertViewWithoutTitle.view.tintColor = .pEpGreen
    46         
    47         if let popoverPresentationController = alertViewWithoutTitle.popoverPresentationController {
    48             popoverPresentationController.sourceView = view
    49         }
    50 
    51         let alertActionReply = UIAlertAction (title: "Reply".localized, style: .default) { (action) in
    52             self.performSegue(withIdentifier: .segueReplyFrom , sender: self)
    53         }
    54         alertViewWithoutTitle.addAction(alertActionReply)
    55 
    56         let alertActionForward = UIAlertAction (title: "Forward".localized, style: .default) { (action) in
    57             self.performSegue(withIdentifier: .segueForward , sender: self)
    58         }
    59         alertViewWithoutTitle.addAction(alertActionForward)
    60 
    61         let cancelAction = UIAlertAction(title: "Cancel".localized, style: .cancel) { (action) in }
    62         alertViewWithoutTitle.addAction(cancelAction)
    63 
    64         present(alertViewWithoutTitle, animated: true, completion: nil)
    65     }
    66 }
    67 
    68 // MARK: TableView Delegate & Datasource
    69 
    70 extension EmailViewController {
    71     
    72     override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    73         guard let row = tableData?.getRow(at: indexPath.row) else { return UITableViewAutomaticDimension }
    74         
    75         if row.display == .conditional {
    76             return 0
    77         }
    78         
    79         otherCellsHeight += row.height
    80         if indexPath.row == (tableData?.numberOfRows())! - 1 {
    81             let availableSpace = tableView.bounds.size.height - otherCellsHeight + 94.0
    82             if computedHeight > 0 {
    83                 if availableSpace < computedHeight {
    84                     return computedHeight
    85                 }
    86             }
    87             return (availableSpace > row.height) ? availableSpace : row.height
    88         }
    89         
    90         return UITableViewAutomaticDimension
    91     }
    92     
    93     override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    94         return tableData?.numberOfRows() ?? 0
    95     }
    96     
    97     override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    98         guard let row = tableData?.getRow(at: indexPath.row) else { return UITableViewCell() }
    99         
   100         let cell = tableView.dequeueReusableCell(withIdentifier: row.identifier, for: indexPath) as! MessageCell
   101         cell.updateCell(row, message)
   102         cell.delegate = self
   103         
   104         return cell
   105     }
   106 }
   107 
   108 // MARK: - MessageContentCellDelegate
   109 
   110 extension EmailViewController: MessageContentCellDelegate {
   111     
   112     func cellDidUpdateHeight(_ with: CGFloat) {
   113         computedHeight = with
   114         tableView.updateSize(true)
   115     }
   116 }
   117 
   118 
   119 // MARK: - SegueHandlerType
   120 
   121 extension EmailViewController: SegueHandlerType {
   122     
   123     enum SegueIdentifier: String {
   124         case segueReplyFrom
   125         case segueForward
   126         case segueTrustWords
   127         case seguePrevious
   128         case segueNext
   129         case noSegue
   130     }
   131     
   132     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
   133         
   134         switch segueIdentifier(for: segue) {
   135         case .segueReplyFrom:
   136             let destination = segue.destination as? ComposeTableViewController
   137             destination?.composeMode = .from
   138             destination?.appConfig = appConfig
   139             destination?.originalMessage = message
   140             break
   141         case .segueForward:
   142             let destination = segue.destination as? ComposeTableViewController
   143             destination?.composeMode = .forward
   144             destination?.appConfig = appConfig
   145             destination?.originalMessage = message
   146             break
   147         case .segueTrustWords:
   148             let destination = segue.destination as? TrustWordsViewController
   149             destination?.message = message
   150             destination?.appConfig = appConfig
   151             break
   152         case .seguePrevious:
   153             let destination = segue.destination as! EmailViewController
   154             if page > 0 { page -= 1 }
   155             destination.message = message
   156             destination.appConfig = appConfig
   157             break
   158         case .segueNext:
   159             let destination = segue.destination as! EmailViewController
   160             if page < datasource.count  { page += 1 }
   161             destination.message = message
   162             destination.appConfig = appConfig
   163             destination.page = page
   164             break
   165         case .noSegue:
   166             break
   167         }
   168     }
   169 }
   170