pEpForiOS/UI/EmailDisplay/EmailViewController.swift
author Dirk Zimmermann <dirk@pep-project.org>
Mon, 19 Dec 2016 17:00:48 +0100
changeset 1348 47fb980d20d4
parent 1272 a1649560e4d1
child 1359 88c59c8053c6
child 1361 a9a21279c74e
permissions -rw-r--r--
IOS-358 SendLayerDelegate -> MessageFolderDelegate
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@362
    11
import WebKit
dirk@273
    12
dirk@841
    13
import MessageModel
dirk@841
    14
dirk@361
    15
class EmailViewController: UIViewController {
dirk@516
    16
    /** Segue name for replying to the sender (from) */
dirk@516
    17
    let segueReplyFrom = "segueReplyFrom"
dirk@516
    18
dirk@663
    19
    /** Segue name for forwarding email */
dirk@663
    20
    let segueForward = "segueForward"
dirk@663
    21
dirk@516
    22
    /** Segue for invoking the trustwords controller */
ana@453
    23
    let segueTrustWordsContactList = "segueTrustWordsContactList"
ana@417
    24
dirk@362
    25
    let headerGapToContentY: CGFloat = 25
dirk@362
    26
dirk@279
    27
    struct UIState {
dirk@360
    28
        var loadingMail = false
dirk@360
    29
    }
dirk@360
    30
dirk@673
    31
    var state = UIState()
dirk@273
    32
    var appConfig: AppConfig!
dirk@361
    33
    let headerView = EmailHeaderView.init()
dirk@362
    34
    var webView: WKWebView!
dirk@361
    35
dirk@841
    36
    var message: Message!
dirk@361
    37
dirk@279
    38
    override func viewDidLoad() {
dirk@279
    39
        super.viewDidLoad()
dirk@362
    40
        let config = WKWebViewConfiguration.init()
dirk@362
    41
        webView = WKWebView.init(frame: view.frame, configuration: config)
dirk@362
    42
        view.addSubview(webView)
dirk@362
    43
        webView.scrollView.addSubview(headerView)
dirk@361
    44
    }
dirk@361
    45
dirk@784
    46
    override func viewWillAppear(_ animated: Bool) {
dirk@361
    47
        super.viewWillAppear(animated)
dirk@362
    48
        updateContents()
dirk@362
    49
    }
dirk@362
    50
dirk@362
    51
    func updateContents() {
dirk@362
    52
        // If the contentInset.top is already set, this means the view never
dirk@362
    53
        // really disappeared. So there is nothing to update in that case.
ana@557
    54
        headerView.message = message
ana@557
    55
        headerView.update(view.bounds.size.width)
ana@557
    56
dirk@673
    57
        // Mark as read. Duh!
dirk@965
    58
        message.imapFlags?.seen = true
dirk@673
    59
dirk@362
    60
        if webView.scrollView.contentInset.top == 0 {
dirk@362
    61
            loadWebViewContent()
dirk@362
    62
dirk@362
    63
            let headerViewSize = headerView.preferredSize
dirk@362
    64
dirk@362
    65
            let calculatedInsetTop = headerViewSize.height + headerGapToContentY
dirk@362
    66
            webView.scrollView.contentInset.top += calculatedInsetTop
dirk@362
    67
dirk@362
    68
            headerView.frame = CGRect.init(origin: CGPoint.init(x: 0, y: -calculatedInsetTop),
dirk@362
    69
                                           size: headerViewSize)
dirk@362
    70
            webView.frame = CGRect.init(origin: CGPoint.init(x: 0, y: 0),
dirk@362
    71
                                        size: CGSize.init(width: view.bounds.size.width,
dirk@362
    72
                                            height: view.bounds.size.height))
dirk@362
    73
        }
dirk@362
    74
    }
dirk@362
    75
dirk@362
    76
    func loadWebViewContent() {
dirk@784
    77
        let font = UIFont.preferredFont(forTextStyle: UIFontTextStyle.body)
dirk@687
    78
        let fontSize = font.pointSize
dirk@551
    79
        let fontFamily = font.familyName
dirk@551
    80
dirk@784
    81
        if let url = URL.init(string: "file:///") {
dirk@551
    82
            if let s = message.longMessage {
dirk@784
    83
                let s2 = s.replacingOccurrences(of: "\r\n", with: "<br>")
dirk@784
    84
                let s3 = s2.replacingOccurrences(of: "\n", with: "<br>")
dirk@551
    85
                let html: String = "<!DOCTYPE html>"
dirk@551
    86
                    + "<html>"
dirk@551
    87
                    + "<head>"
dirk@551
    88
                    + "<meta name=\"viewport\" content=\"initial-scale=1.0\" />"
dirk@551
    89
                    + "<style>"
dirk@551
    90
                    + "body {font-size: \(fontSize); font-family: '\(fontFamily)'}"
dirk@551
    91
                    + "</style>"
dirk@551
    92
                    + "</head>"
dirk@551
    93
                    + "<body>"
dirk@555
    94
                    + s3
dirk@551
    95
                    + "</body>"
dirk@551
    96
                    + "</html>"
dirk@551
    97
dirk@551
    98
                webView.loadHTMLString(html, baseURL: url)
dirk@362
    99
            }
dirk@362
   100
        }
dirk@279
   101
    }
dirk@279
   102
dirk@784
   103
    @IBAction func pressReply(_ sender: UIBarButtonItem) {
dirk@663
   104
        let alertViewWithoutTitle = UIAlertController()
dirk@663
   105
dirk@663
   106
        let alertActionReply = UIAlertAction (
dirk@663
   107
            title: NSLocalizedString("Reply",
dirk@784
   108
                comment: "Reply email button"), style: .default) { (action) in
dirk@784
   109
                    self.performSegue(withIdentifier: self.segueReplyFrom , sender: self)
dirk@279
   110
        }
dirk@663
   111
        alertViewWithoutTitle.addAction(alertActionReply)
ana@334
   112
dirk@663
   113
        let alertActionForward = UIAlertAction (
dirk@342
   114
            title: NSLocalizedString("Forward",
dirk@784
   115
                comment: "Forward email button"), style: .default) { (action) in
dirk@784
   116
                    self.performSegue(withIdentifier: self.segueForward , sender: self)
dirk@663
   117
        }
dirk@663
   118
        alertViewWithoutTitle.addAction(alertActionForward)
ana@334
   119
ana@334
   120
        let cancelAction = UIAlertAction(
dirk@342
   121
            title: NSLocalizedString("Cancel",
dirk@663
   122
                comment: "Cancel button text for email actions menu (reply, forward etc.)"),
dirk@784
   123
            style: .cancel) { (action) in }
ana@334
   124
dirk@663
   125
        alertViewWithoutTitle.addAction(cancelAction)
ana@334
   126
dirk@784
   127
        present(alertViewWithoutTitle, animated: true, completion: nil)
dirk@279
   128
    }
dirk@279
   129
dirk@784
   130
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
dirk@516
   131
        if (segue.identifier == segueReplyFrom) {
dirk@784
   132
            let destination = segue.destination
yves@1272
   133
                as? ComposeTableViewController
yves@1272
   134
            destination?.composeMode = .from
dirk@419
   135
            destination?.appConfig = appConfig
dirk@516
   136
            destination?.originalMessage = message
dirk@663
   137
        } else if (segue.identifier == segueForward) {
dirk@784
   138
            let destination = segue.destination
yves@1272
   139
                as? ComposeTableViewController
dirk@784
   140
            destination?.composeMode = .forward
dirk@663
   141
            destination?.appConfig = appConfig
dirk@663
   142
            destination?.originalMessage = message
dirk@663
   143
        } else if (segue.identifier == segueTrustWordsContactList) {
dirk@784
   144
            let destination = segue.destination as? TrustWordsViewController
ana@423
   145
            destination?.message = self.message
ana@482
   146
            destination?.appConfig = appConfig
ana@417
   147
        }
dirk@360
   148
    }
igor@929
   149
    
igor@929
   150
    override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) {
igor@929
   151
        print("Transition size: \(size)")
igor@929
   152
        updateViews(with: size)
igor@929
   153
    }
igor@929
   154
    
igor@929
   155
    func updateViews(with size: CGSize) {
igor@929
   156
        webView.frame.size = size
igor@929
   157
    }
dirk@784
   158
}