pEpForiOS/UI/EmailDisplay/EmailViewController.swift
author Dirk Zimmermann <dirk@pep-project.org>
Tue, 24 Jan 2017 10:47:08 +0100
changeset 1507 7270fb02923c
parent 1444 2f02ffec0b52
child 1529 fe0cf4167c2f
permissions -rw-r--r--
IOS-417 consolidating creation of swipe actions
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@1438
    45
        alertViewWithoutTitle.view.tintColor = .pEpGreen
igor@1414
    46
        
igor@1414
    47
        if let popoverPresentationController = alertViewWithoutTitle.popoverPresentationController {
igor@1414
    48
            popoverPresentationController.sourceView = view
igor@1414
    49
        }
dirk@663
    50
yves@1361
    51
        let alertActionReply = UIAlertAction (title: "Reply".localized, style: .default) { (action) in
yves@1361
    52
            self.performSegue(withIdentifier: .segueReplyFrom , sender: self)
dirk@279
    53
        }
dirk@663
    54
        alertViewWithoutTitle.addAction(alertActionReply)
ana@334
    55
yves@1361
    56
        let alertActionForward = UIAlertAction (title: "Forward".localized, style: .default) { (action) in
yves@1361
    57
            self.performSegue(withIdentifier: .segueForward , sender: self)
dirk@663
    58
        }
dirk@663
    59
        alertViewWithoutTitle.addAction(alertActionForward)
ana@334
    60
yves@1361
    61
        let cancelAction = UIAlertAction(title: "Cancel".localized, style: .cancel) { (action) in }
dirk@663
    62
        alertViewWithoutTitle.addAction(cancelAction)
ana@334
    63
dirk@784
    64
        present(alertViewWithoutTitle, animated: true, completion: nil)
dirk@279
    65
    }
igor@1444
    66
    
igor@1444
    67
    @IBAction func showRatingPressed(_ sender: UIBarButtonItem) {
igor@1444
    68
        let allIdentities = message.allIdentities
igor@1444
    69
        if allIdentities.count == 1 {
igor@1444
    70
            performSegue(withIdentifier: .segueTrustwords, sender: self)
igor@1444
    71
        }
igor@1444
    72
        else if allIdentities.count > 1 {
igor@1444
    73
            performSegue(withIdentifier: .seguePrivacyStatus, sender: self)
igor@1444
    74
        }
igor@1444
    75
    }
igor@1444
    76
    
yves@1361
    77
}
dirk@279
    78
yves@1361
    79
// MARK: TableView Delegate & Datasource
yves@1361
    80
yves@1361
    81
extension EmailViewController {
yves@1361
    82
    
yves@1361
    83
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
yves@1361
    84
        guard let row = tableData?.getRow(at: indexPath.row) else { return UITableViewAutomaticDimension }
yves@1361
    85
        
yves@1361
    86
        if row.display == .conditional {
yves@1361
    87
            return 0
yves@1361
    88
        }
yves@1361
    89
        
yves@1361
    90
        otherCellsHeight += row.height
yves@1361
    91
        if indexPath.row == (tableData?.numberOfRows())! - 1 {
yves@1361
    92
            let availableSpace = tableView.bounds.size.height - otherCellsHeight + 94.0
yves@1361
    93
            if computedHeight > 0 {
yves@1361
    94
                if availableSpace < computedHeight {
yves@1361
    95
                    return computedHeight
yves@1361
    96
                }
yves@1361
    97
            }
yves@1361
    98
            return (availableSpace > row.height) ? availableSpace : row.height
yves@1361
    99
        }
yves@1361
   100
        
yves@1361
   101
        return UITableViewAutomaticDimension
yves@1361
   102
    }
yves@1361
   103
    
yves@1361
   104
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
yves@1361
   105
        return tableData?.numberOfRows() ?? 0
yves@1361
   106
    }
yves@1361
   107
    
yves@1361
   108
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
yves@1361
   109
        guard let row = tableData?.getRow(at: indexPath.row) else { return UITableViewCell() }
yves@1361
   110
        
yves@1361
   111
        let cell = tableView.dequeueReusableCell(withIdentifier: row.identifier, for: indexPath) as! MessageCell
yves@1361
   112
        cell.updateCell(row, message)
yves@1361
   113
        cell.delegate = self
yves@1361
   114
        
yves@1361
   115
        return cell
yves@1361
   116
    }
yves@1361
   117
}
yves@1361
   118
yves@1361
   119
// MARK: - MessageContentCellDelegate
yves@1361
   120
yves@1361
   121
extension EmailViewController: MessageContentCellDelegate {
yves@1361
   122
    
yves@1361
   123
    func cellDidUpdateHeight(_ with: CGFloat) {
yves@1361
   124
        computedHeight = with
igor@1395
   125
        tableView.updateSize(true)
yves@1361
   126
    }
yves@1361
   127
}
yves@1361
   128
yves@1361
   129
yves@1361
   130
// MARK: - SegueHandlerType
yves@1361
   131
yves@1361
   132
extension EmailViewController: SegueHandlerType {
yves@1361
   133
    
yves@1361
   134
    enum SegueIdentifier: String {
yves@1361
   135
        case segueReplyFrom
yves@1361
   136
        case segueForward
igor@1444
   137
        case segueTrustWords1
yves@1361
   138
        case seguePrevious
yves@1361
   139
        case segueNext
igor@1444
   140
        case segueTrustwords
igor@1444
   141
        case seguePrivacyStatus
igor@1434
   142
        case noSegue
yves@1361
   143
    }
yves@1361
   144
    
dirk@784
   145
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
igor@1395
   146
        
yves@1361
   147
        switch segueIdentifier(for: segue) {
yves@1361
   148
        case .segueReplyFrom:
yves@1361
   149
            let destination = segue.destination as? ComposeTableViewController
yves@1272
   150
            destination?.composeMode = .from
dirk@419
   151
            destination?.appConfig = appConfig
dirk@516
   152
            destination?.originalMessage = message
yves@1361
   153
            break
yves@1361
   154
        case .segueForward:
yves@1361
   155
            let destination = segue.destination as? ComposeTableViewController
dirk@784
   156
            destination?.composeMode = .forward
dirk@663
   157
            destination?.appConfig = appConfig
dirk@663
   158
            destination?.originalMessage = message
yves@1361
   159
            break
igor@1444
   160
        case .segueTrustWords1:
dirk@784
   161
            let destination = segue.destination as? TrustWordsViewController
yves@1361
   162
            destination?.message = message
ana@482
   163
            destination?.appConfig = appConfig
yves@1361
   164
            break
yves@1361
   165
        case .seguePrevious:
yves@1361
   166
            let destination = segue.destination as! EmailViewController
yves@1361
   167
            if page > 0 { page -= 1 }
yves@1361
   168
            destination.message = message
yves@1361
   169
            destination.appConfig = appConfig
yves@1361
   170
            break
yves@1361
   171
        case .segueNext:
yves@1361
   172
            let destination = segue.destination as! EmailViewController
yves@1361
   173
            if page < datasource.count  { page += 1 }
yves@1361
   174
            destination.message = message
yves@1361
   175
            destination.appConfig = appConfig
yves@1361
   176
            destination.page = page
yves@1361
   177
            break
igor@1444
   178
        case .seguePrivacyStatus:
igor@1444
   179
            let destination = segue.destination as? PrivacyStatusTableViewController
igor@1444
   180
            destination?.message = message
igor@1444
   181
            destination?.appConfig = appConfig
igor@1444
   182
            break
igor@1444
   183
        case .segueTrustwords:
igor@1444
   184
            let destination = segue.destination as? TrustwordsTableViewController
igor@1444
   185
            destination?.message = message
igor@1444
   186
            destination?.appConfig = appConfig
igor@1444
   187
            break
igor@1434
   188
        case .noSegue:
igor@1434
   189
            break
ana@417
   190
        }
dirk@360
   191
    }
dirk@784
   192
}
igor@1378
   193