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