pEpForiOS/UI/EmailDisplay/EmailListViewController.swift
author Dirk Zimmermann <dirk@pep-project.org>
Thu, 16 Feb 2017 12:01:12 +0100
changeset 1718 b3c04048bce9
parent 1696 a95b55a243a3
child 1723 ef459fa4ae98
permissions -rw-r--r--
IOS-83 act on trustwords choice
dirk@30
     1
//
dirk@30
     2
//  EmailListViewController.swift
dirk@30
     3
//  pEpForiOS
dirk@30
     4
//
dirk@30
     5
//  Created by Dirk Zimmermann on 16/04/16.
dirk@30
     6
//  Copyright © 2016 p≡p Security S.A. All rights reserved.
dirk@30
     7
//
dirk@30
     8
dirk@30
     9
import Foundation
dirk@30
    10
import UIKit
dirk@31
    11
import CoreData
dirk@810
    12
import MessageModel
dirk@810
    13
igor@1260
    14
struct EmailListConfig {
igor@1338
    15
    var appConfig: AppConfig?
dirk@1333
    16
igor@1260
    17
    /** The folder to display, if it exists */
igor@1260
    18
    var folder: Folder?
igor@1260
    19
}
dirk@30
    20
dirk@854
    21
class EmailListViewController: UITableViewController {
dirk@583
    22
    struct UIState {
dirk@583
    23
        var isSynching: Bool = false
dirk@583
    24
    }
dirk@30
    25
dirk@1344
    26
    var config: EmailListConfig?
dirk@275
    27
    var state = UIState()
igor@1301
    28
    let searchController = UISearchController(searchResultsController: nil)
dirk@1542
    29
    let cellsByMessageID = NSCache<NSString, EmailListViewCell>()
dirk@719
    30
dirk@275
    31
    override func viewDidLoad() {
ylandert@935
    32
        super.viewDidLoad()
xavier@1623
    33
igor@1408
    34
        title = "EmailList.title".localized
igor@1284
    35
        UIHelper.emailListTableHeight(self.tableView)
igor@1301
    36
        addSearchBar()
dirk@275
    37
    }
dirk@31
    38
dirk@784
    39
    override func viewWillAppear(_ animated: Bool) {
igor@1301
    40
        super.viewWillAppear(animated)
xavier@1623
    41
dirk@1344
    42
        if MiscUtil.isUnitTest() {
dirk@1344
    43
            return
dirk@1344
    44
        }
dirk@1344
    45
dirk@1344
    46
        initialConfig()
dirk@854
    47
        updateModel()
dirk@1348
    48
dirk@1348
    49
        MessageModelConfig.messageFolderDelegate = self
dirk@1348
    50
    }
dirk@1348
    51
dirk@1348
    52
    override func viewWillDisappear(_ animated: Bool) {
dirk@1348
    53
        super.viewWillDisappear(animated)
dirk@1348
    54
        MessageModelConfig.messageFolderDelegate = nil
igor@1301
    55
    }
xavier@1623
    56
igor@1338
    57
    func initialConfig() {
dirk@1344
    58
        guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
igor@1338
    59
            return
igor@1338
    60
        }
dirk@1344
    61
dirk@1344
    62
        if config == nil {
igor@1395
    63
            config = EmailListConfig(appConfig: appDelegate.appConfig, folder: Folder.unifiedInbox())
dirk@1344
    64
        }
dirk@1348
    65
        if Account.all().isEmpty {
igor@1338
    66
            performSegue(withIdentifier:.segueAddNewAccount, sender: self)
igor@1338
    67
        }
igor@1338
    68
    }
xavier@1623
    69
igor@1301
    70
    func addSearchBar() {
igor@1301
    71
        searchController.searchResultsUpdater = self
igor@1301
    72
        searchController.dimsBackgroundDuringPresentation = false
igor@1301
    73
        searchController.delegate = self
igor@1301
    74
        definesPresentationContext = true
igor@1301
    75
        tableView.tableHeaderView = searchController.searchBar
igor@1301
    76
        tableView.setContentOffset(CGPoint(x: 0.0, y: 40.0), animated: false)
igor@1301
    77
    }
dirk@705
    78
xavier@1623
    79
dirk@1330
    80
    @IBAction func showUnreadButtonTapped(_ sender: UIBarButtonItem) {}
xavier@1623
    81
dirk@854
    82
    func updateModel() {
igor@1318
    83
        tableView.reloadData()
dirk@31
    84
    }
dirk@31
    85
dirk@58
    86
    // MARK: - UI State
dirk@58
    87
dirk@58
    88
    func updateUI() {
dirk@784
    89
        UIApplication.shared.isNetworkActivityIndicatorVisible = state.isSynching
dirk@748
    90
        if !state.isSynching {
igor@1260
    91
            refreshControl?.endRefreshing()
dirk@58
    92
        }
dirk@58
    93
    }
dirk@58
    94
dirk@31
    95
    // MARK: - UITableViewDataSource
dirk@31
    96
dirk@784
    97
    override func numberOfSections(in tableView: UITableView) -> Int {
dirk@1344
    98
        if let _ = config?.folder {
dirk@854
    99
            return 1
dirk@209
   100
        }
dirk@854
   101
        return 0
dirk@31
   102
    }
dirk@31
   103
dirk@784
   104
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
dirk@1344
   105
        if let fol = config?.folder  {
dirk@854
   106
            return fol.messageCount()
dirk@31
   107
        }
dirk@31
   108
        return 0
dirk@31
   109
    }
dirk@31
   110
dirk@784
   111
    override func tableView(_ tableView: UITableView,
dirk@784
   112
                            cellForRowAt indexPath: IndexPath) -> UITableViewCell {
dirk@784
   113
        let cell = tableView.dequeueReusableCell(
dirk@784
   114
            withIdentifier: "EmailListViewCell", for: indexPath) as! EmailListViewCell
dirk@1692
   115
        if let messageID = cell.configureCell(config: config, indexPath: indexPath) {
dirk@1542
   116
            cellsByMessageID.setObject(cell, forKey: messageID as NSString)
dirk@1542
   117
        }
dirk@31
   118
        return cell
dirk@31
   119
    }
dirk@31
   120
dirk@719
   121
    // MARK: - UITableViewDelegate
dirk@719
   122
dirk@784
   123
    override func tableView(_ tableView: UITableView, editActionsForRowAt
dirk@784
   124
        indexPath: IndexPath)-> [UITableViewRowAction]? {
xavier@1623
   125
dirk@784
   126
        let cell = tableView.cellForRow(at: indexPath) as! EmailListViewCell
igor@1260
   127
        if let email = cell.messageAt(indexPath: indexPath, config: config) {
igor@1576
   128
            let flagAction = createFlagAction(message: email, cell: cell)
dirk@1507
   129
            let deleteAction = createDeleteAction(message: email, cell: cell)
igor@1242
   130
            let moreAction = createMoreAction(message: email, cell: cell)
igor@1576
   131
            return [deleteAction, flagAction, moreAction]
dirk@855
   132
        }
dirk@855
   133
        return nil
dirk@744
   134
    }
dirk@744
   135
dirk@719
   136
    // MARK: - Misc
dirk@719
   137
dirk@1507
   138
    func createRowAction(cell: EmailListViewCell,
xavier@1623
   139
                         image: UIImage?, action: @escaping (UITableViewRowAction, IndexPath) -> Void,
xavier@1623
   140
                         title: String) -> UITableViewRowAction {
dirk@1510
   141
        let rowAction = UITableViewRowAction(
dirk@1510
   142
            style: .normal, title: title, handler: action)
dirk@1507
   143
dirk@1507
   144
        if let theImage = image {
dirk@1507
   145
            let iconColor = UIColor(patternImage: theImage)
dirk@1507
   146
            rowAction.backgroundColor = iconColor
dirk@744
   147
        }
dirk@744
   148
dirk@1507
   149
        return rowAction
dirk@744
   150
    }
dirk@744
   151
dirk@1507
   152
    func createFlagAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   153
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1522
   154
            if message.imapFlags == nil {
dirk@1522
   155
                Log.warn(component: #function, content: "message.imapFlags == nil")
dirk@1522
   156
            }
dirk@1605
   157
            if cell.isFlagged(message: message) {
dirk@1507
   158
                message.imapFlags?.flagged = false
dirk@1507
   159
            } else {
dirk@1507
   160
                message.imapFlags?.flagged = true
dirk@1507
   161
            }
dirk@1507
   162
            message.save()
dirk@1507
   163
            self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@1507
   164
        }
dirk@1510
   165
igor@1523
   166
        var title = "\n\nFlag".localized
dirk@1605
   167
        if message.imapFlags?.flagged ?? true {
igor@1530
   168
            title = "\n\nUnFlag".localized
dirk@744
   169
        }
dirk@744
   170
dirk@1510
   171
        return createRowAction(
dirk@1510
   172
            cell: cell, image: UIImage(named: "swipe-flag"), action: action, title: title)
dirk@744
   173
    }
dirk@744
   174
dirk@1507
   175
    func createDeleteAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   176
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1507
   177
            guard let message = cell.messageAt(indexPath: indexPath, config: self.config) else {
dirk@1507
   178
                return
dirk@1507
   179
            }
xavier@1623
   180
dirk@1650
   181
            message.delete() // mark for deletion/trash
dirk@1507
   182
            message.save()
igor@1576
   183
            self.tableView.reloadData()
dirk@1507
   184
        }
dirk@744
   185
dirk@1510
   186
        return createRowAction(
dirk@1510
   187
            cell: cell, image: UIImage(named: "swipe-trash"), action: action,
igor@1523
   188
            title: "\n\nDelete".localized)
dirk@1507
   189
    }
dirk@1507
   190
dirk@1507
   191
    func createMarkAsReadAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1507
   192
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
dirk@1605
   193
            if cell.haveSeen(message: message) {
dirk@1507
   194
                message.imapFlags?.seen = false
dirk@1507
   195
            } else {
dirk@1507
   196
                message.imapFlags?.seen = true
dirk@1507
   197
            }
dirk@1507
   198
            self.tableView.reloadRows(at: [indexPath], with: .none)
dirk@744
   199
        }
dirk@1510
   200
dirk@1510
   201
        var title = NSLocalizedString(
dirk@1510
   202
            "Unread", comment: "Unread button title in swipe action on EmailListViewController")
dirk@1605
   203
        if !cell.haveSeen(message: message) {
dirk@1510
   204
            title = NSLocalizedString(
dirk@1510
   205
                "Read", comment: "Read button title in swipe action on EmailListViewController")
dirk@1507
   206
        }
dirk@1507
   207
dirk@1507
   208
        let isReadAction = createRowAction(cell: cell, image: nil, action: action,
dirk@1510
   209
                                           title: title)
dirk@784
   210
        isReadAction.backgroundColor = UIColor.blue
dirk@744
   211
dirk@744
   212
        return isReadAction
dirk@744
   213
    }
xavier@1623
   214
igor@1242
   215
    func createMoreAction(message: Message, cell: EmailListViewCell) -> UITableViewRowAction {
dirk@1510
   216
        func action(action: UITableViewRowAction, indexPath: IndexPath) -> Void {
igor@1242
   217
            self.showMoreActionSheet(cell: cell)
igor@1242
   218
        }
dirk@1510
   219
dirk@1510
   220
        return createRowAction(
dirk@1510
   221
            cell: cell, image: UIImage(named: "swipe-more"), action: action,
igor@1523
   222
            title: "\n\nMore".localized)
igor@1242
   223
    }
xavier@1623
   224
igor@1242
   225
    // MARK: - Action Sheet
xavier@1623
   226
igor@1242
   227
    func showMoreActionSheet(cell: EmailListViewCell) {
igor@1242
   228
        let alertControler = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
igor@1438
   229
        alertControler.view.tintColor = .pEpGreen
igor@1242
   230
        let cancelAction = createCancelAction()
igor@1242
   231
        let replyAction = createReplyAction(cell: cell)
xavier@1623
   232
        let replyAllAction = createReplyAllAction(cell: cell)
igor@1242
   233
        let forwardAction = createForwardAction(cell: cell)
igor@1242
   234
        let markAction = createMarkAction()
igor@1242
   235
        alertControler.addAction(cancelAction)
igor@1242
   236
        alertControler.addAction(replyAction)
xavier@1620
   237
        alertControler.addAction(replyAllAction)
igor@1242
   238
        alertControler.addAction(forwardAction)
igor@1242
   239
        alertControler.addAction(markAction)
igor@1414
   240
        if let popoverPresentationController = alertControler.popoverPresentationController {
igor@1414
   241
            popoverPresentationController.sourceView = cell
igor@1414
   242
        }
igor@1242
   243
        present(alertControler, animated: true, completion: nil)
igor@1242
   244
    }
xavier@1623
   245
igor@1242
   246
    // MARK: - Action Sheet Actions
igor@1242
   247
igor@1242
   248
    func createCancelAction() -> UIAlertAction {
xavier@1623
   249
        return  UIAlertAction(title: "Cancel", style: .cancel) { (action) in}
igor@1242
   250
    }
xavier@1623
   251
igor@1242
   252
    func createReplyAction(cell: EmailListViewCell) ->  UIAlertAction {
igor@1242
   253
        return UIAlertAction(title: "Reply", style: .default) { (action) in
xavier@1623
   254
            // self.performSegue(withIdentifier: self.segueCompose, sender: cell)
igor@1338
   255
            self.performSegue(withIdentifier: .segueCompose, sender: cell)
igor@1242
   256
        }
igor@1242
   257
    }
xavier@1620
   258
xavier@1620
   259
    func createReplyAllAction(cell: EmailListViewCell) ->  UIAlertAction {
xavier@1623
   260
        return UIAlertAction(title: "Reply All", style: .default) { (action) in
xavier@1623
   261
            self.performSegue(withIdentifier: .segueReplyAll, sender: cell)
xavier@1620
   262
        }
xavier@1620
   263
    }
xavier@1620
   264
igor@1242
   265
    func createForwardAction(cell: EmailListViewCell) -> UIAlertAction {
igor@1242
   266
        return UIAlertAction(title: "Forward", style: .default) { (action) in
igor@1338
   267
            //self.performSegue(withIdentifier: self.segueCompose, sender: cell)
xavier@1664
   268
            self.performSegue(withIdentifier: .segueForward, sender: cell)
igor@1242
   269
        }
igor@1242
   270
    }
xavier@1623
   271
igor@1242
   272
    func createMarkAction() -> UIAlertAction {
igor@1242
   273
        return UIAlertAction(title: "Mark", style: .default) { (action) in
igor@1242
   274
        }
igor@1242
   275
    }
xavier@1623
   276
igor@1301
   277
    // MARK: - Content Search
xavier@1623
   278
igor@1301
   279
    func filterContentForSearchText(searchText: String) {
xavier@1623
   280
igor@1301
   281
    }
xavier@1623
   282
dirk@784
   283
}
igor@1301
   284
igor@1301
   285
extension EmailListViewController: UISearchResultsUpdating, UISearchControllerDelegate {
igor@1301
   286
    public func updateSearchResults(for searchController: UISearchController) {
igor@1301
   287
        filterContentForSearchText(searchText: searchController.searchBar.text!)
igor@1301
   288
    }
xavier@1623
   289
igor@1301
   290
    func didDismissSearchController(_ searchController: UISearchController) {
igor@1301
   291
    }
igor@1301
   292
}
igor@1338
   293
igor@1338
   294
// MARK: - Navigation
igor@1338
   295
igor@1338
   296
extension EmailListViewController: SegueHandlerType {
xavier@1623
   297
igor@1338
   298
    // MARK: - SegueHandlerType
xavier@1623
   299
igor@1338
   300
    enum SegueIdentifier: String {
igor@1338
   301
        case segueAddNewAccount
igor@1338
   302
        case segueEditAccounts
igor@1338
   303
        case segueShowEmail
igor@1338
   304
        case segueCompose
xavier@1623
   305
        case segueReplyAll
xavier@1664
   306
        case segueForward
igor@1338
   307
        case noSegue
igor@1338
   308
    }
xavier@1623
   309
igor@1338
   310
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
igor@1338
   311
        switch segueIdentifier(for: segue) {
xavier@1623
   312
        case .segueReplyAll:
xavier@1623
   313
            if let nav = segue.destination as? UINavigationController,
xavier@1623
   314
                let destination = nav.topViewController as? ComposeTableViewController,
xavier@1623
   315
                let cell = sender as? EmailListViewCell,
xavier@1623
   316
                let indexPath = self.tableView.indexPath(for: cell),
xavier@1623
   317
                let email = cell.messageAt(indexPath: indexPath, config: config) {
xavier@1623
   318
                destination.composeMode = .replyAll
xavier@1623
   319
                destination.appConfig = config?.appConfig
xavier@1623
   320
                destination.originalMessage = email
xavier@1623
   321
            }
igor@1338
   322
            break
igor@1338
   323
        case .segueShowEmail:
dirk@1594
   324
            if let vc = segue.destination as? EmailViewController,
igor@1338
   325
                let cell = sender as? EmailListViewCell,
igor@1338
   326
                let indexPath = self.tableView.indexPath(for: cell),
dirk@1594
   327
                let email = cell.messageAt(indexPath: indexPath, config: config) {
xavier@1623
   328
                vc.appConfig = config?.appConfig
xavier@1623
   329
                vc.message = email
igor@1338
   330
            }
igor@1338
   331
            break
xavier@1664
   332
        case .segueForward:
xavier@1664
   333
            if let nav = segue.destination as? UINavigationController,
xavier@1664
   334
                let destination = nav.topViewController as? ComposeTableViewController,
xavier@1664
   335
                let cell = sender as? EmailListViewCell,
xavier@1664
   336
                let indexPath = self.tableView.indexPath(for: cell),
xavier@1664
   337
                let email = cell.messageAt(indexPath: indexPath, config: config) {
xavier@1664
   338
                destination.composeMode = .forward
xavier@1664
   339
                destination.appConfig = config?.appConfig
xavier@1664
   340
                destination.originalMessage = email
xavier@1664
   341
            }
xavier@1664
   342
            break
dirk@1694
   343
        case .segueAddNewAccount, .segueEditAccounts, .segueCompose, .noSegue:
dirk@1694
   344
            break
igor@1338
   345
        }
igor@1338
   346
    }
dirk@1350
   347
dirk@1350
   348
    func didChangeInternal(messageFolder: MessageFolder) {
dirk@1350
   349
        if let folder = config?.folder,
dirk@1350
   350
            let message = messageFolder as? Message,
dirk@1558
   351
            folder.contains(message: message, deletedMessagesAreContained: true) {
dirk@1350
   352
            if let msg = messageFolder as? Message {
dirk@1350
   353
                if msg.isOriginal {
dirk@1535
   354
                    // new message has arrived
dirk@1675
   355
                    if let index = folder.indexOf(message: msg) {
dirk@1675
   356
                        let ip = IndexPath(row: index, section: 0)
dirk@1675
   357
                        tableView.insertRows(at: [ip], with: .automatic)
dirk@1675
   358
                    } else {
dirk@1675
   359
                        tableView.reloadData()
dirk@1675
   360
                    }
dirk@1558
   361
                } else if msg.isGhost {
dirk@1558
   362
                    if let cell = cellsByMessageID.object(forKey: msg.uuid as NSString) {
dirk@1692
   363
                        if let ip = tableView.indexPath(for: cell) {
dirk@1558
   364
                            tableView.deleteRows(at: [ip], with: .automatic)
dirk@1558
   365
                        }
dirk@1558
   366
                    }
dirk@1535
   367
                } else {
dirk@1558
   368
                    // other flags than delete must have been changed
dirk@1542
   369
                    if let cell = cellsByMessageID.object(forKey: msg.uuid as NSString) {
dirk@1542
   370
                        cell.updateFlags(message: message)
dirk@1542
   371
                    }
dirk@1350
   372
                }
dirk@1350
   373
            }
dirk@1350
   374
        }
dirk@1350
   375
    }
dirk@1718
   376
dirk@1718
   377
    /**
dirk@1718
   378
     For the unwind segue from the trustwords controller, when the user chose "trust" or
dirk@1718
   379
     "no trust".
dirk@1718
   380
     */
dirk@1718
   381
    @IBAction func unwindTrustedUntrusted(segue: UIStoryboardSegue) {
dirk@1718
   382
    }
igor@1338
   383
}
dirk@1348
   384
dirk@1348
   385
// MARK: - MessageFolderDelegate
dirk@1348
   386
dirk@1348
   387
extension EmailListViewController: MessageFolderDelegate {
dirk@1348
   388
    func didChange(messageFolder: MessageFolder) {
dirk@1675
   389
        GCD.onMainWait {
dirk@1350
   390
            self.didChangeInternal(messageFolder: messageFolder)
dirk@1348
   391
        }
dirk@1348
   392
    }
dirk@1348
   393
}